/[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.82 by wakaba, Wed Mar 5 02:55:08 2008 UTC revision 1.145 by wakaba, Sat May 24 11:57:47 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    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributetype => 'attributeType',
215      basefrequency => 'baseFrequency',
216      baseprofile => 'baseProfile',
217      calcmode => 'calcMode',
218      clippathunits => 'clipPathUnits',
219      contentscripttype => 'contentScriptType',
220      contentstyletype => 'contentStyleType',
221      diffuseconstant => 'diffuseConstant',
222      edgemode => 'edgeMode',
223      externalresourcesrequired => 'externalResourcesRequired',
224      fecolormatrix => 'feColorMatrix',
225      fecomposite => 'feComposite',
226      fegaussianblur => 'feGaussianBlur',
227      femorphology => 'feMorphology',
228      fetile => 'feTile',
229      filterres => 'filterRes',
230      filterunits => 'filterUnits',
231      glyphref => 'glyphRef',
232      gradienttransform => 'gradientTransform',
233      gradientunits => 'gradientUnits',
234      kernelmatrix => 'kernelMatrix',
235      kernelunitlength => 'kernelUnitLength',
236      keypoints => 'keyPoints',
237      keysplines => 'keySplines',
238      keytimes => 'keyTimes',
239      lengthadjust => 'lengthAdjust',
240      limitingconeangle => 'limitingConeAngle',
241      markerheight => 'markerHeight',
242      markerunits => 'markerUnits',
243      markerwidth => 'markerWidth',
244      maskcontentunits => 'maskContentUnits',
245      maskunits => 'maskUnits',
246      numoctaves => 'numOctaves',
247      pathlength => 'pathLength',
248      patterncontentunits => 'patternContentUnits',
249      patterntransform => 'patternTransform',
250      patternunits => 'patternUnits',
251      pointsatx => 'pointsAtX',
252      pointsaty => 'pointsAtY',
253      pointsatz => 'pointsAtZ',
254      preservealpha => 'preserveAlpha',
255      preserveaspectratio => 'preserveAspectRatio',
256      primitiveunits => 'primitiveUnits',
257      refx => 'refX',
258      refy => 'refY',
259      repeatcount => 'repeatCount',
260      repeatdur => 'repeatDur',
261      requiredextensions => 'requiredExtensions',
262      specularconstant => 'specularConstant',
263      specularexponent => 'specularExponent',
264      spreadmethod => 'spreadMethod',
265      startoffset => 'startOffset',
266      stddeviation => 'stdDeviation',
267      stitchtiles => 'stitchTiles',
268      surfacescale => 'surfaceScale',
269      systemlanguage => 'systemLanguage',
270      tablevalues => 'tableValues',
271      targetx => 'targetX',
272      targety => 'targetY',
273      textlength => 'textLength',
274      viewbox => 'viewBox',
275      viewtarget => 'viewTarget',
276      xchannelselector => 'xChannelSelector',
277      ychannelselector => 'yChannelSelector',
278      zoomandpan => 'zoomAndPan',
279  };  };
280    
281    my $foreign_attr_xname = {
282      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
283      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
284      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
285      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
286      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
287      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
288      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
289      'xml:base' => [$XML_NS, ['xml', 'base']],
290      'xml:lang' => [$XML_NS, ['xml', 'lang']],
291      'xml:space' => [$XML_NS, ['xml', 'space']],
292      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
293      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
294    };
295    
296    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
297    
298  my $c1_entity_char = {  my $c1_entity_char = {
299    0x80 => 0x20AC,    0x80 => 0x20AC,
300    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 330  my $c1_entity_char = {
330    0x9F => 0x0178,    0x9F => 0x0178,
331  }; # $c1_entity_char  }; # $c1_entity_char
332    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
333  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
334      my $self = shift;
335      my $charset_name = shift;
336      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
337      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
338    } # parse_byte_string
339    
340    sub parse_byte_stream ($$$$;$) {
341    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
342    my $charset = shift;    my $charset_name = shift;
343    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;  
   }  
344    
345    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
346      my $self = shift;      my (%opt) = @_;
347      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
348      ## TODO: if $charset is supported    };
349      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
350    
351      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
352      require Message::Charset::Info;
353      ## Step 1        my $charset;
354      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
355        $charset = 'utf-8';    my ($char_stream, $e_status);
356    
357      SNIFFING: {
358    
359        ## Step 1
360        if (defined $charset_name) {
361          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
362    
363          ## ISSUE: Unsupported encoding is not ignored according to the spec.
364          ($char_stream, $e_status) = $charset->get_decode_handle
365              ($byte_stream, allow_error_reporting => 1,
366               allow_fallback => 1);
367          if ($char_stream) {
368            $self->{confident} = 1;
369            last SNIFFING;
370          } else {
371            ## TODO: unsupported error
372          }
373      }      }
374    
375      ## Step 2      ## Step 2
376      if (defined $self->{input_encoding} and      my $byte_buffer = '';
377          $self->{input_encoding} eq $charset) {      for (1..1024) {
378          my $char = $byte_stream->getc;
379          last unless defined $char;
380          $byte_buffer .= $char;
381        } ## TODO: timeout
382    
383        ## Step 3
384        if ($byte_buffer =~ /^\xFE\xFF/) {
385          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
386          ($char_stream, $e_status) = $charset->get_decode_handle
387              ($byte_stream, allow_error_reporting => 1,
388               allow_fallback => 1, byte_buffer => \$byte_buffer);
389        $self->{confident} = 1;        $self->{confident} = 1;
390        return;        last SNIFFING;
391        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
392          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
393          ($char_stream, $e_status) = $charset->get_decode_handle
394              ($byte_stream, allow_error_reporting => 1,
395               allow_fallback => 1, byte_buffer => \$byte_buffer);
396          $self->{confident} = 1;
397          last SNIFFING;
398        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
399          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
400          ($char_stream, $e_status) = $charset->get_decode_handle
401              ($byte_stream, allow_error_reporting => 1,
402               allow_fallback => 1, byte_buffer => \$byte_buffer);
403          $self->{confident} = 1;
404          last SNIFFING;
405      }      }
406    
407      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
408          ':'.$charset, level => 'w');      ## TODO: <meta charset>
409    
410      ## Step 3      ## Step 5
411      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
412    
413      ## Step 4      ## Step 6
414      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
415        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
416            ($byte_buffer);
417        if (defined $charset_name) {
418          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
419    
420          ## ISSUE: Unsupported encoding is not ignored according to the spec.
421          require Whatpm::Charset::DecodeHandle;
422          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
423              ($byte_stream);
424          ($char_stream, $e_status) = $charset->get_decode_handle
425              ($buffer, allow_error_reporting => 1,
426               allow_fallback => 1, byte_buffer => \$byte_buffer);
427          if ($char_stream) {
428            $buffer->{buffer} = $byte_buffer;
429            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
430                            value => $charset_name,
431                            level => $self->{info_level},
432                            line => 1, column => 1);
433            $self->{confident} = 0;
434            last SNIFFING;
435          }
436        }
437    
438        ## Step 7: default
439        ## TODO: Make this configurable.
440        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
441            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
442            ## detectable in the step 6.
443        require Whatpm::Charset::DecodeHandle;
444        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
445            ($byte_stream);
446        ($char_stream, $e_status)
447            = $charset->get_decode_handle ($buffer,
448                                           allow_error_reporting => 1,
449                                           allow_fallback => 1,
450                                           byte_buffer => \$byte_buffer);
451        $buffer->{buffer} = $byte_buffer;
452        !!!parse-error (type => 'sniffing:default', ## TODO: type name
453                        value => 'windows-1252',
454                        level => $self->{info_level},
455                        line => 1, column => 1);
456        $self->{confident} = 0;
457      } # SNIFFING
458    
459      $self->{input_encoding} = $charset->get_iana_name;
460      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
461        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      } elsif (not ($e_status &
466                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
467        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
468                        value => $self->{input_encoding},
469                        level => $self->{unsupported_level},
470                        line => 1, column => 1);
471      }
472    
473      $self->{change_encoding} = sub {
474        my $self = shift;
475        $charset_name = shift;
476        my $token = shift;
477    
478        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
479        ($char_stream, $e_status) = $charset->get_decode_handle
480            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
481             byte_buffer => \ $buffer->{buffer});
482        
483        if ($char_stream) { # if supported
484          ## "Change the encoding" algorithm:
485    
486          ## Step 1    
487          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
488            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
489            ($char_stream, $e_status) = $charset->get_decode_handle
490                ($byte_stream,
491                 byte_buffer => \ $buffer->{buffer});
492          }
493          $charset_name = $charset->get_iana_name;
494          
495          ## Step 2
496          if (defined $self->{input_encoding} and
497              $self->{input_encoding} eq $charset_name) {
498            !!!parse-error (type => 'charset label:matching', ## TODO: type
499                            value => $charset_name,
500                            level => $self->{info_level});
501            $self->{confident} = 1;
502            return;
503          }
504    
505          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
506              ':'.$charset_name, level => 'w', token => $token);
507          
508          ## Step 3
509          # if (can) {
510            ## change the encoding on the fly.
511            #$self->{confident} = 1;
512            #return;
513          # }
514          
515          ## Step 4
516          throw Whatpm::HTML::RestartParser ();
517        }
518    }; # $self->{change_encoding}    }; # $self->{change_encoding}
519    
520      my $char_onerror = sub {
521        my (undef, $type, %opt) = @_;
522        !!!parse-error (%opt, type => $type,
523                        line => $self->{line}, column => $self->{column} + 1);
524        if ($opt{octets}) {
525          ${$opt{octets}} = "\x{FFFD}"; # relacement character
526        }
527      };
528      $char_stream->onerror ($char_onerror);
529    
530    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
531    my $return;    my $return;
532    try {    try {
533      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
534    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
535      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
536      $s = \ (Encode::decode ($charset, $$bytes_s));      
537      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
538        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
539          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        } elsif (not ($e_status &
544                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
545          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
546                          value => $self->{input_encoding},
547                          level => $self->{unsupported_level},
548                          line => 1, column => 1);
549        }
550      $self->{confident} = 1;      $self->{confident} = 1;
551      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
552        $return = $self->parse_char_stream ($char_stream, @args);
553    };    };
554    return $return;    return $return;
555  } # parse_byte_string  } # parse_byte_stream
556    
557  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
558  ## 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 563  sub parse_byte_string ($$$$;$) {
563  ## 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
564  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
565    
566  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
567      my $self = shift;
568      require utf8;
569      my $s = ref $_[0] ? $_[0] : \($_[0]);
570      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
571      return $self->parse_char_stream ($input, @_[1..$#_]);
572    } # parse_char_string
573    *parse_string = \&parse_char_string;
574    
575  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
576    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
577    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
578    $self->{document} = $_[1];    $self->{document} = $_[1];
579    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
580    
# Line 177  sub parse_string ($$$;$) { Line 585  sub parse_string ($$$;$) {
585        if defined $self->{input_encoding};        if defined $self->{input_encoding};
586    
587    my $i = 0;    my $i = 0;
588    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
589    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
590    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
591      my $self = shift;      my $self = shift;
592    
593      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
594      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
595    
596      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
597      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
598      $column++;        $char = $self->{next_next_char};
599          delete $self->{next_next_char};
600        } else {
601          $char = $input->getc;
602        }
603        $self->{next_char} = -1 and return unless defined $char;
604        $self->{next_char} = ord $char;
605    
606        ($self->{line_prev}, $self->{column_prev})
607            = ($self->{line}, $self->{column});
608        $self->{column}++;
609            
610      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
611        $line++;        !!!cp ('j1');
612        $column = 0;        $self->{line}++;
613          $self->{column} = 0;
614      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
615        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
616          my $next = $input->getc;
617          if (defined $next and $next ne "\x0A") {
618            $self->{next_next_char} = $next;
619          }
620        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
621        $line++;        $self->{line}++;
622        $column = 0;        $self->{column} = 0;
623      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
624          !!!cp ('j3');
625        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
626      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
627          !!!cp ('j4');
628        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
629        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
630        } elsif ($self->{next_char} <= 0x0008 or
631                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
632                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
633                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
634                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
635                 {
636                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
637                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
638                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
639                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
640                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
641                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
642                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
643                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
644                  0x10FFFE => 1, 0x10FFFF => 1,
645                 }->{$self->{next_char}}) {
646          !!!cp ('j5');
647          !!!parse-error (type => 'control char', level => $self->{must_level});
648    ## TODO: error type documentation
649      }      }
650    };    };
651    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 653  sub parse_string ($$$;$) {
653    
654    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
655      my (%opt) = @_;      my (%opt) = @_;
656      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
657        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
658        warn "Parse error ($opt{type}) at line $line column $column\n";
659    };    };
660    $self->{parse_error} = sub {    $self->{parse_error} = sub {
661      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
662    };    };
663    
664    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 666  sub parse_string ($$$;$) {
666    $self->_construct_tree;    $self->_construct_tree;
667    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
668    
669      delete $self->{parse_error}; # remove loop
670    
671    return $self->{document};    return $self->{document};
672  } # parse_string  } # parse_char_stream
673    
674  sub new ($) {  sub new ($) {
675    my $class = shift;    my $class = shift;
676    my $self = bless {}, $class;    my $self = bless {
677        must_level => 'm',
678        should_level => 's',
679        good_level => 'w',
680        warn_level => 'w',
681        info_level => 'i',
682        unsupported_level => 'u',
683      }, $class;
684    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
685      $self->{next_char} = -1;      $self->{next_char} = -1;
686    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 742  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
742  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
743  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
744  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
745    sub SELF_CLOSING_START_TAG_STATE () { 34 }
746    sub CDATA_BLOCK_STATE () { 35 }
747    
748  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
749  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 760  sub TABLE_IMS ()      { 0b1000000 }
760  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
761  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
762  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
763    sub SELECT_IMS ()     { 0b10000000000 }
764    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
765        ## NOTE: "in foreign content" insertion mode is special; it is combined
766        ## with the secondary insertion mode.  In this parser, they are stored
767        ## together in the bit-or'ed form.
768    
769    ## NOTE: "initial" and "before html" insertion modes have no constants.
770    
771    ## NOTE: "after after body" insertion mode.
772  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
773    
774    ## NOTE: "after after frameset" insertion mode.
775  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
776    
777  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
778  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
779  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 787  sub IN_TABLE_IM () { TABLE_IMS }
787  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
788  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
789  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
790  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
791    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
792  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
793    
794  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 332  sub _initialize_tokenizer ($) { Line 801  sub _initialize_tokenizer ($) {
801    undef $self->{current_attribute};    undef $self->{current_attribute};
802    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
803    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
804      delete $self->{self_closing};
805    $self->{char} = [];    $self->{char} = [];
806    # $self->{next_char}    # $self->{next_char}
807    !!!next-input-character;    !!!next-input-character;
# Line 352  sub _initialize_tokenizer ($) { Line 822  sub _initialize_tokenizer ($) {
822  ##        ->{value}  ##        ->{value}
823  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
824  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
825    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
826    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
827    ##     while the token is pushed back to the stack.
828    
829    ## ISSUE: "When a DOCTYPE token is created, its
830    ## <i>self-closing flag</i> must be unset (its other state is that it
831    ## be set), and its attributes list must be empty.": Wrong subject?
832    
833  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
834    
# Line 378  sub _initialize_tokenizer ($) { Line 855  sub _initialize_tokenizer ($) {
855    
856  sub _get_next_token ($) {  sub _get_next_token ($) {
857    my $self = shift;    my $self = shift;
858    
859      if ($self->{self_closing}) {
860        !!!parse-error (type => 'nestc', token => $self->{current_token});
861        ## NOTE: The |self_closing| flag is only set by start tag token.
862        ## In addition, when a start tag token is emitted, it is always set to
863        ## |current_token|.
864        delete $self->{self_closing};
865      }
866    
867    if (@{$self->{token}}) {    if (@{$self->{token}}) {
868        $self->{self_closing} = $self->{token}->[0]->{self_closing};
869      return shift @{$self->{token}};      return shift @{$self->{token}};
870    }    }
871    
# Line 441  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928          #          #
929        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
930          !!!cp (11);          !!!cp (11);
931          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
932                      line => $self->{line}, column => $self->{column}});
933          last A; ## TODO: ok?          last A; ## TODO: ok?
934        } else {        } else {
935          !!!cp (12);          !!!cp (12);
936        }        }
937        # Anything else        # Anything else
938        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
939                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
940                       line => $self->{line}, column => $self->{column},
941                      };
942        ## Stay in the data state        ## Stay in the data state
943        !!!next-input-character;        !!!next-input-character;
944    
# Line 457  sub _get_next_token ($) { Line 947  sub _get_next_token ($) {
947        redo A;        redo A;
948      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
949        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
950    
951          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
952                
953        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
954    
# Line 465  sub _get_next_token ($) { Line 957  sub _get_next_token ($) {
957    
958        unless (defined $token) {        unless (defined $token) {
959          !!!cp (13);          !!!cp (13);
960          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
961                      line => $l, column => $c,
962                     });
963        } else {        } else {
964          !!!cp (14);          !!!cp (14);
965          !!!emit ($token);          !!!emit ($token);
# Line 484  sub _get_next_token ($) { Line 978  sub _get_next_token ($) {
978            ## reconsume            ## reconsume
979            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
980    
981            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
982                        line => $self->{line_prev},
983                        column => $self->{column_prev},
984                       });
985    
986            redo A;            redo A;
987          }          }
# Line 504  sub _get_next_token ($) { Line 1001  sub _get_next_token ($) {
1001            !!!cp (19);            !!!cp (19);
1002            $self->{current_token}            $self->{current_token}
1003              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1004                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1005                   line => $self->{line_prev},
1006                   column => $self->{column_prev}};
1007            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1008            !!!next-input-character;            !!!next-input-character;
1009            redo A;            redo A;
# Line 512  sub _get_next_token ($) { Line 1011  sub _get_next_token ($) {
1011                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1012            !!!cp (20);            !!!cp (20);
1013            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1014                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1015                                        line => $self->{line_prev},
1016                                        column => $self->{column_prev}};
1017            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1018            !!!next-input-character;            !!!next-input-character;
1019            redo A;            redo A;
1020          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1021            !!!cp (21);            !!!cp (21);
1022            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1023                              line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1026            !!!next-input-character;            !!!next-input-character;
1027    
1028            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1029                        line => $self->{line_prev},
1030                        column => $self->{column_prev},
1031                       });
1032    
1033            redo A;            redo A;
1034          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1035            !!!cp (22);            !!!cp (22);
1036            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1037                              line => $self->{line_prev},
1038                              column => $self->{column_prev});
1039            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1040              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1041                                        line => $self->{line_prev},
1042                                        column => $self->{column_prev},
1043                                       };
1044            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1045            redo A;            redo A;
1046          } else {          } else {
1047            !!!cp (23);            !!!cp (23);
1048            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1049                              line => $self->{line_prev},
1050                              column => $self->{column_prev});
1051            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1055                        line => $self->{line_prev},
1056                        column => $self->{column_prev},
1057                       });
1058    
1059            redo A;            redo A;
1060          }          }
# Line 545  sub _get_next_token ($) { Line 1062  sub _get_next_token ($) {
1062          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1063        }        }
1064      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1065          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1066        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1067          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1068    
1069            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1070            my @next_char;            my @next_char;
1071            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 563  sub _get_next_token ($) { Line 1082  sub _get_next_token ($) {
1082                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1083                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1084    
1085                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1086                            line => $l, column => $c,
1087                           });
1088        
1089                redo A;                redo A;
1090              }              }
# Line 582  sub _get_next_token ($) { Line 1103  sub _get_next_token ($) {
1103              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1104              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1105              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1106              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                          line => $l, column => $c,
1108                         });
1109              redo A;              redo A;
1110            } else {            } else {
1111              !!!cp (27);              !!!cp (27);
# Line 595  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118            !!!cp (28);            !!!cp (28);
1119            # next-input-character is already done            # next-input-character is already done
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1122                        line => $l, column => $c,
1123                       });
1124            redo A;            redo A;
1125          }          }
1126        }        }
# Line 603  sub _get_next_token ($) { Line 1128  sub _get_next_token ($) {
1128        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1129            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1130          !!!cp (29);          !!!cp (29);
1131          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1132                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1133                   tag_name => chr ($self->{next_char} + 0x0020),
1134                   line => $l, column => $c};
1135          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1136          !!!next-input-character;          !!!next-input-character;
1137          redo A;          redo A;
# Line 612  sub _get_next_token ($) { Line 1139  sub _get_next_token ($) {
1139                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1140          !!!cp (30);          !!!cp (30);
1141          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1142                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
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;
1147        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1148          !!!cp (31);          !!!cp (31);
1149          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1150                            line => $self->{line_prev}, ## "<" in "</>"
1151                            column => $self->{column_prev} - 1);
1152          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1153          !!!next-input-character;          !!!next-input-character;
1154          redo A;          redo A;
# Line 628  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1159          # reconsume          # reconsume
1160    
1161          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1162                      line => $l, column => $c,
1163                     });
1164    
1165          redo A;          redo A;
1166        } else {        } else {
1167          !!!cp (33);          !!!cp (33);
1168          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1169          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1170            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1171                                      line => $self->{line_prev}, # "<" of "</"
1172                                      column => $self->{column_prev} - 1,
1173                                     };
1174          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1175          redo A;          redo A;
1176        }        }
# Line 651  sub _get_next_token ($) { Line 1187  sub _get_next_token ($) {
1187        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1188          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1190            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1191          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1192            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 684  sub _get_next_token ($) { Line 1218  sub _get_next_token ($) {
1218          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1219          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1220            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1221            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1222          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1223            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 706  sub _get_next_token ($) { Line 1238  sub _get_next_token ($) {
1238    
1239          redo A;          redo A;
1240        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (42);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!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  
1244          redo A;          redo A;
1245        } else {        } else {
1246          !!!cp (44);          !!!cp (44);
# Line 741  sub _get_next_token ($) { Line 1263  sub _get_next_token ($) {
1263        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1264          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1265            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1266            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1267          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1268            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 764  sub _get_next_token ($) { Line 1284  sub _get_next_token ($) {
1284        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1285                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1286          !!!cp (49);          !!!cp (49);
1287          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1288                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1289                   value => '',
1290                   line => $self->{line}, column => $self->{column}};
1291          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (50);
1296            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1297          !!!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  
1298          redo A;          redo A;
1299        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1300          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1301          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1302            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1303            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1304          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1305            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 819  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          } else {          } else {
1330            !!!cp (56);            !!!cp (56);
1331          }          }
1332          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1333                                value => ''};              = {name => chr ($self->{next_char}),
1334                   value => '',
1335                   line => $self->{line}, column => $self->{column}};
1336          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
# Line 830  sub _get_next_token ($) { Line 1342  sub _get_next_token ($) {
1342          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1343              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1344            !!!cp (57);            !!!cp (57);
1345            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1346            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1347          } else {          } else {
1348            !!!cp (58);            !!!cp (58);
# Line 859  sub _get_next_token ($) { Line 1371  sub _get_next_token ($) {
1371          $before_leave->();          $before_leave->();
1372          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1373            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1374            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1375          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1376            !!!cp (62);            !!!cp (62);
# Line 885  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1398            !!!cp (64);
1399          $before_leave->();          $before_leave->();
1400            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1401          !!!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  
1402          redo A;          redo A;
1403        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1404          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1405          $before_leave->();          $before_leave->();
1406          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1407            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1408            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1409          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1410            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 957  sub _get_next_token ($) { Line 1455  sub _get_next_token ($) {
1455        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1456          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1457            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1458            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1459          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1460            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 981  sub _get_next_token ($) { Line 1477  sub _get_next_token ($) {
1477        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1478                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1479          !!!cp (76);          !!!cp (76);
1480          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1481                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1488            !!!cp (77);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1495            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1496            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1497          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1029  sub _get_next_token ($) { Line 1514  sub _get_next_token ($) {
1514          redo A;          redo A;
1515        } else {        } else {
1516          !!!cp (82);          !!!cp (82);
1517          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1518                                value => ''};              = {name => chr ($self->{next_char}),
1519                   value => '',
1520                   line => $self->{line}, column => $self->{column}};
1521          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;                  redo A;        
# Line 1063  sub _get_next_token ($) { Line 1550  sub _get_next_token ($) {
1550        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1551          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1552            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1553            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1554          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1555            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1088  sub _get_next_token ($) { Line 1573  sub _get_next_token ($) {
1573          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1574          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1575            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1576            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1577          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1578            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1620  sub _get_next_token ($) {
1620          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1621          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1622            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1623            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1624          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1625            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1662  sub _get_next_token ($) {
1662          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1663          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1664            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1665            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1666          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1667            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1228  sub _get_next_token ($) { Line 1707  sub _get_next_token ($) {
1707        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1708          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1709            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1711          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1253  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1731          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1732            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1733            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1734          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1735            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1325  sub _get_next_token ($) { Line 1800  sub _get_next_token ($) {
1800        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1801          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1802            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1804          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1805            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1347  sub _get_next_token ($) { Line 1820  sub _get_next_token ($) {
1820    
1821          redo A;          redo A;
1822        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1823            !!!cp (122);
1824            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1825          !!!next-input-character;          !!!next-input-character;
1826          if ($self->{next_char} == 0x003E and # >          redo A;
1827              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1828              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1829            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1830            !!!cp (122);            !!!cp (122.3);
1831            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1832            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (122.1);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (122.2);
1839              }
1840          } else {          } else {
1841            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1842          }          }
1843          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1844          # next-input-character is already done          ## Reconsume.
1845            !!!emit ($self->{current_token}); # start tag or end tag
1846          redo A;          redo A;
1847        } else {        } else {
1848          !!!cp (124);          !!!cp ('124.1');
1849          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1850          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1851          ## reconsume          ## reconsume
1852          redo A;          redo A;
1853        }        }
1854        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1855          if ($self->{next_char} == 0x003E) { # >
1856            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1857              !!!cp ('124.2');
1858              !!!parse-error (type => 'nestc', token => $self->{current_token});
1859              ## TODO: Different type than slash in start tag
1860              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1861              if ($self->{current_token}->{attributes}) {
1862                !!!cp ('124.4');
1863                !!!parse-error (type => 'end tag attribute');
1864              } else {
1865                !!!cp ('124.5');
1866              }
1867              ## TODO: Test |<title></title/>|
1868            } else {
1869              !!!cp ('124.3');
1870              $self->{self_closing} = 1;
1871            }
1872    
1873            $self->{state} = DATA_STATE;
1874            !!!next-input-character;
1875    
1876            !!!emit ($self->{current_token}); # start tag or end tag
1877    
1878            redo A;
1879          } elsif ($self->{next_char} == -1) {
1880            !!!parse-error (type => 'unclosed tag');
1881            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1882              !!!cp (124.7);
1883              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1884            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1885              if ($self->{current_token}->{attributes}) {
1886                !!!cp (124.5);
1887                !!!parse-error (type => 'end tag attribute');
1888              } else {
1889                ## NOTE: This state should never be reached.
1890                !!!cp (124.6);
1891              }
1892            } else {
1893              die "$0: $self->{current_token}->{type}: Unknown token type";
1894            }
1895            $self->{state} = DATA_STATE;
1896            ## Reconsume.
1897            !!!emit ($self->{current_token}); # start tag or end tag
1898            redo A;
1899          } else {
1900            !!!cp ('124.4');
1901            !!!parse-error (type => 'nestc');
1902            ## TODO: This error type is wrong.
1903            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1904            ## Reconsume.
1905            redo A;
1906          }
1907      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1908        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1909                
1910        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1911          #my $token = {type => COMMENT_TOKEN, data => ''};
1912    
1913        BC: {        BC: {
1914          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1379  sub _get_next_token ($) { Line 1916  sub _get_next_token ($) {
1916            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1917            !!!next-input-character;            !!!next-input-character;
1918    
1919            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1920    
1921            redo A;            redo A;
1922          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1387  sub _get_next_token ($) { Line 1924  sub _get_next_token ($) {
1924            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1925            ## reconsume            ## reconsume
1926    
1927            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1928    
1929            redo A;            redo A;
1930          } else {          } else {
1931            !!!cp (126);            !!!cp (126);
1932            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1933            !!!next-input-character;            !!!next-input-character;
1934            redo BC;            redo BC;
1935          }          }
# Line 1402  sub _get_next_token ($) { Line 1939  sub _get_next_token ($) {
1939      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1940        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1941    
1942          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1943    
1944        my @next_char;        my @next_char;
1945        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1946                
# Line 1410  sub _get_next_token ($) { Line 1949  sub _get_next_token ($) {
1949          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1950          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1951            !!!cp (127);            !!!cp (127);
1952            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1953                                        line => $l, column => $c,
1954                                       };
1955            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1956            !!!next-input-character;            !!!next-input-character;
1957            redo A;            redo A;
# Line 1446  sub _get_next_token ($) { Line 1987  sub _get_next_token ($) {
1987                      !!!cp (129);                      !!!cp (129);
1988                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1989                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1990                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1991                                                  quirks => 1,
1992                                                  line => $l, column => $c,
1993                                                 };
1994                      !!!next-input-character;                      !!!next-input-character;
1995                      redo A;                      redo A;
1996                    } else {                    } else {
# Line 1466  sub _get_next_token ($) { Line 2011  sub _get_next_token ($) {
2011          } else {          } else {
2012            !!!cp (135);            !!!cp (135);
2013          }          }
2014          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2015                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2016                   $self->{next_char} == 0x005B) { # [
2017            !!!next-input-character;
2018            push @next_char, $self->{next_char};
2019            if ($self->{next_char} == 0x0043) { # C
2020              !!!next-input-character;
2021              push @next_char, $self->{next_char};
2022              if ($self->{next_char} == 0x0044) { # D
2023                !!!next-input-character;
2024                push @next_char, $self->{next_char};
2025                if ($self->{next_char} == 0x0041) { # A
2026                  !!!next-input-character;
2027                  push @next_char, $self->{next_char};
2028                  if ($self->{next_char} == 0x0054) { # T
2029                    !!!next-input-character;
2030                    push @next_char, $self->{next_char};
2031                    if ($self->{next_char} == 0x0041) { # A
2032                      !!!next-input-character;
2033                      push @next_char, $self->{next_char};
2034                      if ($self->{next_char} == 0x005B) { # [
2035                        !!!cp (135.1);
2036                        $self->{state} = CDATA_BLOCK_STATE;
2037                        !!!next-input-character;
2038                        redo A;
2039                      } else {
2040                        !!!cp (135.2);
2041                      }
2042                    } else {
2043                      !!!cp (135.3);
2044                    }
2045                  } else {
2046                    !!!cp (135.4);                
2047                  }
2048                } else {
2049                  !!!cp (135.5);
2050                }
2051              } else {
2052                !!!cp (135.6);
2053              }
2054            } else {
2055              !!!cp (135.7);
2056            }
2057        } else {        } else {
2058          !!!cp (136);          !!!cp (136);
2059        }        }
# Line 1474  sub _get_next_token ($) { Line 2062  sub _get_next_token ($) {
2062        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2063        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2064        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2065          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2066                                    line => $l, column => $c,
2067                                   };
2068        redo A;        redo A;
2069                
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1597  sub _get_next_token ($) { Line 2188  sub _get_next_token ($) {
2188          redo A;          redo A;
2189        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2190          !!!cp (152);          !!!cp (152);
2191          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2192                            line => $self->{line_prev},
2193                            column => $self->{column_prev});
2194          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2195          ## Stay in the state          ## Stay in the state
2196          !!!next-input-character;          !!!next-input-character;
# Line 1613  sub _get_next_token ($) { Line 2206  sub _get_next_token ($) {
2206          redo A;          redo A;
2207        } else {        } else {
2208          !!!cp (154);          !!!cp (154);
2209          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2210                            line => $self->{line_prev},
2211                            column => $self->{column_prev});
2212          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2213          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2214          !!!next-input-character;          !!!next-input-character;
# Line 1652  sub _get_next_token ($) { Line 2247  sub _get_next_token ($) {
2247          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249    
2250          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2251    
2252          redo A;          redo A;
2253        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1661  sub _get_next_token ($) { Line 2256  sub _get_next_token ($) {
2256          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2257          ## reconsume          ## reconsume
2258    
2259          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2260    
2261          redo A;          redo A;
2262        } else {        } else {
2263          !!!cp (160);          !!!cp (160);
2264          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2265              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2266  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2267          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2268          !!!next-input-character;          !!!next-input-character;
# Line 2144  sub _get_next_token ($) { Line 2736  sub _get_next_token ($) {
2736          redo A;          redo A;
2737        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2738          !!!cp (217);          !!!cp (217);
         !!!parse-error (type => 'unclosed DOCTYPE');  
2739    
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          ## reconsume          ## reconsume
# Line 2186  sub _get_next_token ($) { Line 2777  sub _get_next_token ($) {
2777          !!!next-input-character;          !!!next-input-character;
2778          redo A;          redo A;
2779        }        }
2780        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2781          my $s = '';
2782          
2783          my ($l, $c) = ($self->{line}, $self->{column});
2784    
2785          CS: while ($self->{next_char} != -1) {
2786            if ($self->{next_char} == 0x005D) { # ]
2787              !!!next-input-character;
2788              if ($self->{next_char} == 0x005D) { # ]
2789                !!!next-input-character;
2790                MDC: {
2791                  if ($self->{next_char} == 0x003E) { # >
2792                    !!!cp (221.1);
2793                    !!!next-input-character;
2794                    last CS;
2795                  } elsif ($self->{next_char} == 0x005D) { # ]
2796                    !!!cp (221.2);
2797                    $s .= ']';
2798                    !!!next-input-character;
2799                    redo MDC;
2800                  } else {
2801                    !!!cp (221.3);
2802                    $s .= ']]';
2803                    #
2804                  }
2805                } # MDC
2806              } else {
2807                !!!cp (221.4);
2808                $s .= ']';
2809                #
2810              }
2811            } else {
2812              !!!cp (221.5);
2813              #
2814            }
2815            $s .= chr $self->{next_char};
2816            !!!next-input-character;
2817          } # CS
2818    
2819          $self->{state} = DATA_STATE;
2820          ## next-input-character done or EOF, which is reconsumed.
2821    
2822          if (length $s) {
2823            !!!cp (221.6);
2824            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2825                      line => $l, column => $c});
2826          } else {
2827            !!!cp (221.7);
2828          }
2829    
2830          redo A;
2831    
2832          ## ISSUE: "text tokens" in spec.
2833          ## TODO: Streaming support
2834      } else {      } else {
2835        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2836      }      }
# Line 2197  sub _get_next_token ($) { Line 2842  sub _get_next_token ($) {
2842  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2843    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2844    
2845      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2846    
2847    if ({    if ({
2848         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2849         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2237  sub _tokenize_attempt_to_consume_an_enti Line 2884  sub _tokenize_attempt_to_consume_an_enti
2884            redo X;            redo X;
2885          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2886            !!!cp (1005);            !!!cp (1005);
2887            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2888            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2889            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2890            return undef;            return undef;
# Line 2246  sub _tokenize_attempt_to_consume_an_enti Line 2893  sub _tokenize_attempt_to_consume_an_enti
2893            !!!next-input-character;            !!!next-input-character;
2894          } else {          } else {
2895            !!!cp (1007);            !!!cp (1007);
2896            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2897          }          }
2898    
2899          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2900            !!!cp (1008);            !!!cp (1008);
2901            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2902            $code = 0xFFFD;            $code = 0xFFFD;
2903          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2904            !!!cp (1009);            !!!cp (1009);
2905            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2906            $code = 0xFFFD;            $code = 0xFFFD;
2907          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2908            !!!cp (1010);            !!!cp (1010);
2909            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2910            $code = 0x000A;            $code = 0x000A;
2911          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2912            !!!cp (1011);            !!!cp (1011);
2913            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2914            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2915          }          }
2916    
2917          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2918                  has_reference => 1};                  has_reference => 1,
2919                    line => $l, column => $c,
2920                   };
2921        } # X        } # X
2922      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2923               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2289  sub _tokenize_attempt_to_consume_an_enti Line 2938  sub _tokenize_attempt_to_consume_an_enti
2938          !!!next-input-character;          !!!next-input-character;
2939        } else {        } else {
2940          !!!cp (1014);          !!!cp (1014);
2941          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2942        }        }
2943    
2944        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2945          !!!cp (1015);          !!!cp (1015);
2946          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2947          $code = 0xFFFD;          $code = 0xFFFD;
2948        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2949          !!!cp (1016);          !!!cp (1016);
2950          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2951          $code = 0xFFFD;          $code = 0xFFFD;
2952        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2953          !!!cp (1017);          !!!cp (1017);
2954          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2955          $code = 0x000A;          $code = 0x000A;
2956        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2957          !!!cp (1018);          !!!cp (1018);
2958          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2959          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2960        }        }
2961                
2962        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2963                  line => $l, column => $c,
2964                 };
2965      } else {      } else {
2966        !!!cp (1019);        !!!cp (1019);
2967        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2968        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2969        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2970        return undef;        return undef;
# Line 2330  sub _tokenize_attempt_to_consume_an_enti Line 2981  sub _tokenize_attempt_to_consume_an_enti
2981      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2982      our $EntityChar;      our $EntityChar;
2983    
2984      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2985             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2986             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2987               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2363  sub _tokenize_attempt_to_consume_an_enti Line 3014  sub _tokenize_attempt_to_consume_an_enti
3014            
3015      if ($match > 0) {      if ($match > 0) {
3016        !!!cp (1023);        !!!cp (1023);
3017        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3018                  line => $l, column => $c,
3019                 };
3020      } elsif ($match < 0) {      } elsif ($match < 0) {
3021        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3022        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3023          !!!cp (1024);          !!!cp (1024);
3024          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3025                    line => $l, column => $c,
3026                   };
3027        } else {        } else {
3028          !!!cp (1025);          !!!cp (1025);
3029          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3030                    line => $l, column => $c,
3031                   };
3032        }        }
3033      } else {      } else {
3034        !!!cp (1026);        !!!cp (1026);
3035        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3036        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3037        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3038                  line => $l, column => $c,
3039                 };
3040      }      }
3041    } else {    } else {
3042      !!!cp (1027);      !!!cp (1027);
3043      ## no characters are consumed      ## no characters are consumed
3044      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3045      return undef;      return undef;
3046    }    }
3047  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2420  sub _construct_tree ($) { Line 3079  sub _construct_tree ($) {
3079        
3080    !!!next-token;    !!!next-token;
3081    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3082    undef $self->{form_element};    undef $self->{form_element};
3083    undef $self->{head_element};    undef $self->{head_element};
3084    $self->{open_elements} = [];    $self->{open_elements} = [];
3085    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3086    
3087      ## NOTE: The "initial" insertion mode.
3088    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3089    
3090      ## NOTE: The "before html" insertion mode.
3091    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3092      $self->{insertion_mode} = BEFORE_HEAD_IM;
3093    
3094      ## NOTE: The "before head" insertion mode and so on.
3095    $self->_tree_construction_main;    $self->_tree_construction_main;
3096  } # _construct_tree  } # _construct_tree
3097    
3098  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3099    my $self = shift;    my $self = shift;
3100    
3101      ## NOTE: "initial" insertion mode
3102    
3103    INITIAL: {    INITIAL: {
3104      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3105        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2445  sub _tree_construction_initial ($) { Line 3112  sub _tree_construction_initial ($) {
3112            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3113            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3114          !!!cp ('t1');          !!!cp ('t1');
3115          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3116        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3117          !!!cp ('t2');          !!!cp ('t2');
3118          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3119          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3120        } else {        } else {
3121          !!!cp ('t3');          !!!cp ('t3');
3122        }        }
3123                
3124        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3125          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3126          ## NOTE: Default value for both |public_id| and |system_id| attributes
3127          ## are empty strings, so that we don't set any value in missing cases.
3128        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3129            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3130        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2470  sub _tree_construction_initial ($) { Line 3139  sub _tree_construction_initial ($) {
3139        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3140          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3141          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3142          if ({          my $prefix = [
3143            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3144            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3145            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3146            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3147            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3148            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3149            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3150            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3151            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3152            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3153            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3154            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3155            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3156            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3157            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3158            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3159            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3160            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3161            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3162            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3165            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3168            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3171            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3175            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3176            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3177            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3178            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3179            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3180            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3181            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3182            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3183            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3187            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3188            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3189            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3190            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3191            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3192            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3193            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3194            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3195            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3196            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3197            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3198            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3199            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3200            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3201            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3202            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3203            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3204            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3205            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3206            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3207            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3208            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3209            "-//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}) {  
3210            !!!cp ('t5');            !!!cp ('t5');
3211            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3212          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3213                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3214            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3215              !!!cp ('t6');              !!!cp ('t6');
3216              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2555  sub _tree_construction_initial ($) { Line 3218  sub _tree_construction_initial ($) {
3218              !!!cp ('t7');              !!!cp ('t7');
3219              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3220            }            }
3221          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3222                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3223            !!!cp ('t8');            !!!cp ('t8');
3224            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3225          } else {          } else {
# Line 2569  sub _tree_construction_initial ($) { Line 3232  sub _tree_construction_initial ($) {
3232          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3233          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3234          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") {
3235            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3236              ## marked as quirks.
3237            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3238            !!!cp ('t11');            !!!cp ('t11');
3239          } else {          } else {
# Line 2579  sub _tree_construction_initial ($) { Line 3243  sub _tree_construction_initial ($) {
3243          !!!cp ('t13');          !!!cp ('t13');
3244        }        }
3245                
3246        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3247        !!!next-token;        !!!next-token;
3248        return;        return;
3249      } elsif ({      } elsif ({
# Line 2588  sub _tree_construction_initial ($) { Line 3252  sub _tree_construction_initial ($) {
3252                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3253               }->{$token->{type}}) {               }->{$token->{type}}) {
3254        !!!cp ('t14');        !!!cp ('t14');
3255        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3256        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3257        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3258        ## reprocess        ## reprocess
3259          !!!ack-later;
3260        return;        return;
3261      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3262        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2599  sub _tree_construction_initial ($) { Line 3264  sub _tree_construction_initial ($) {
3264    
3265          unless (length $token->{data}) {          unless (length $token->{data}) {
3266            !!!cp ('t15');            !!!cp ('t15');
3267            ## Stay in the phase            ## Stay in the insertion mode.
3268            !!!next-token;            !!!next-token;
3269            redo INITIAL;            redo INITIAL;
3270          } else {          } else {
# Line 2609  sub _tree_construction_initial ($) { Line 3274  sub _tree_construction_initial ($) {
3274          !!!cp ('t17');          !!!cp ('t17');
3275        }        }
3276    
3277        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3278        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3279        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3280        ## reprocess        ## reprocess
3281        return;        return;
3282      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
# Line 2619  sub _tree_construction_initial ($) { Line 3284  sub _tree_construction_initial ($) {
3284        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3285        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3286                
3287        ## Stay in the phase.        ## Stay in the insertion mode.
3288        !!!next-token;        !!!next-token;
3289        redo INITIAL;        redo INITIAL;
3290      } else {      } else {
# Line 2632  sub _tree_construction_initial ($) { Line 3297  sub _tree_construction_initial ($) {
3297    
3298  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3299    my $self = shift;    my $self = shift;
3300    
3301      ## NOTE: "before html" insertion mode.
3302        
3303    B: {    B: {
3304        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3305          !!!cp ('t19');          !!!cp ('t19');
3306          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3307          ## Ignore the token          ## Ignore the token
3308          ## Stay in the phase          ## Stay in the insertion mode.
3309          !!!next-token;          !!!next-token;
3310          redo B;          redo B;
3311        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3312          !!!cp ('t20');          !!!cp ('t20');
3313          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3314          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3315          ## Stay in the phase          ## Stay in the insertion mode.
3316          !!!next-token;          !!!next-token;
3317          redo B;          redo B;
3318        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2654  sub _tree_construction_root_element ($) Line 3321  sub _tree_construction_root_element ($)
3321    
3322            unless (length $token->{data}) {            unless (length $token->{data}) {
3323              !!!cp ('t21');              !!!cp ('t21');
3324              ## Stay in the phase              ## Stay in the insertion mode.
3325              !!!next-token;              !!!next-token;
3326              redo B;              redo B;
3327            } else {            } else {
# Line 2668  sub _tree_construction_root_element ($) Line 3335  sub _tree_construction_root_element ($)
3335    
3336          #          #
3337        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3338          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3339              $token->{attributes}->{manifest}) {            my $root_element;
3340            !!!cp ('t24');            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3341            $self->{application_cache_selection}            $self->{document}->append_child ($root_element);
3342                 ->($token->{attributes}->{manifest}->{value});            push @{$self->{open_elements}},
3343            ## ISSUE: No relative reference resolution?                [$root_element, $el_category->{html}];
3344    
3345              if ($token->{attributes}->{manifest}) {
3346                !!!cp ('t24');
3347                $self->{application_cache_selection}
3348                    ->($token->{attributes}->{manifest}->{value});
3349                ## ISSUE: Spec is unclear on relative references.
3350                ## According to Hixie (#whatwg 2008-03-19), it should be
3351                ## resolved against the base URI of the document in HTML
3352                ## or xml:base of the element in XHTML.
3353              } else {
3354                !!!cp ('t25');
3355                $self->{application_cache_selection}->(undef);
3356              }
3357    
3358              !!!nack ('t25c');
3359    
3360              !!!next-token;
3361              return; ## Go to the "before head" insertion mode.
3362          } else {          } else {
3363            !!!cp ('t25');            !!!cp ('t25.1');
3364            $self->{application_cache_selection}->(undef);            #
3365          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3366        } elsif ({        } elsif ({
3367                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3368                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3369                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3370          !!!cp ('t26');          !!!cp ('t26');
         $self->{application_cache_selection}->(undef);  
   
         ## ISSUE: There is an issue in the spec  
3371          #          #
3372        } else {        } else {
3373          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3374        }        }
3375    
3376        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3377        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3378        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3379        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3380        #redo B;  
3381        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3382    
3383        ## NOTE: Reprocess the token.
3384        !!!ack-later;
3385        return; ## Go to the "before head" insertion mode.
3386    
3387        ## ISSUE: There is an issue in the spec
3388    } # B    } # B
3389    
3390    die "$0: _tree_construction_root_element: This should never be reached";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2717  sub _reset_insertion_mode ($) { Line 3402  sub _reset_insertion_mode ($) {
3402            
3403      ## Step 3      ## Step 3
3404      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3405        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3406          $last = 1;          $last = 1;
3407          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3408            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3409                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3410              !!!cp ('t27');          } else {
3411              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3412          }          }
3413        }        }
3414              
3415        ## Step 4..13        ## Step 4..14
3416        my $new_mode = {        my $new_mode;
3417          if ($node->[1] & FOREIGN_EL) {
3418            !!!cp ('t28.1');
3419            ## NOTE: Strictly spaking, the line below only applies to MathML and
3420            ## SVG elements.  Currently the HTML syntax supports only MathML and
3421            ## SVG elements as foreigners.
3422            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3423            ## ISSUE: What is set as the secondary insertion mode?
3424          } elsif ($node->[1] & TABLE_CELL_EL) {
3425            if ($last) {
3426              !!!cp ('t28.2');
3427              #
3428            } else {
3429              !!!cp ('t28.3');
3430              $new_mode = IN_CELL_IM;
3431            }
3432          } else {
3433            !!!cp ('t28.4');
3434            $new_mode = {
3435                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3436                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3437                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3438                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3439                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3440                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2751  sub _reset_insertion_mode ($) { Line 3445  sub _reset_insertion_mode ($) {
3445                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3446                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3447                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3448                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3449          }
3450        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3451                
3452        ## Step 14        ## Step 15
3453        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3454          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3455            !!!cp ('t29');            !!!cp ('t29');
3456            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2769  sub _reset_insertion_mode ($) { Line 3464  sub _reset_insertion_mode ($) {
3464          !!!cp ('t31');          !!!cp ('t31');
3465        }        }
3466                
3467        ## Step 15        ## Step 16
3468        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3469                
3470        ## Step 16        ## Step 17
3471        $i--;        $i--;
3472        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3473                
3474        ## Step 17        ## Step 18
3475        redo S3;        redo S3;
3476      } # S3      } # S3
3477    
# Line 2880  sub _tree_construction_main ($) { Line 3575  sub _tree_construction_main ($) {
3575      !!!cp ('t39');      !!!cp ('t39');
3576    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3577    
3578    my $parse_rcdata = sub ($$) {    my $insert;
3579      my ($content_model_flag, $insert) = @_;  
3580      my $parse_rcdata = sub ($) {
3581        my ($content_model_flag) = @_;
3582    
3583      ## Step 1      ## Step 1
3584      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3585      my $el;      my $el;
3586      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3587    
3588      ## Step 2      ## Step 2
3589      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3590    
3591      ## Step 3      ## Step 3
3592      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2897  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595      ## Step 4      ## Step 4
3596      my $text = '';      my $text = '';
3597        !!!nack ('t40.1');
3598      !!!next-token;      !!!next-token;
3599      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3600        !!!cp ('t40');        !!!cp ('t40');
# Line 2919  sub _tree_construction_main ($) { Line 3617  sub _tree_construction_main ($) {
3617          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3618        !!!cp ('t42');        !!!cp ('t42');
3619        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3620      } else {      } else {
3621        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3622          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3623            !!!cp ('t43');
3624            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3625          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3626            !!!cp ('t44');
3627            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3628          } else {
3629            die "$0: $content_model_flag in parse_rcdata";
3630          }
3631      }      }
3632      !!!next-token;      !!!next-token;
3633    }; # $parse_rcdata    }; # $parse_rcdata
3634    
3635    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3636      my $script_el;      my $script_el;
3637      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3638      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3639    
3640      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3641      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3642            
3643      my $text = '';      my $text = '';
3644        !!!nack ('t45.1');
3645      !!!next-token;      !!!next-token;
3646      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3647        !!!cp ('t45');        !!!cp ('t45');
# Line 2960  sub _tree_construction_main ($) { Line 3661  sub _tree_construction_main ($) {
3661        ## Ignore the token        ## Ignore the token
3662      } else {      } else {
3663        !!!cp ('t48');        !!!cp ('t48');
3664        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3665        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3666        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3667      }      }
# Line 2983  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684      !!!next-token;      !!!next-token;
3685    }; # $script_start_tag    }; # $script_start_tag
3686    
3687      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3688      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3689      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3690    
3691    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3692      my $tag_name = shift;      my $end_tag_token = shift;
3693        my $tag_name = $end_tag_token->{tag_name};
3694    
3695        ## NOTE: The adoption agency algorithm (AAA).
3696    
3697      FET: {      FET: {
3698        ## Step 1        ## Step 1
3699        my $formatting_element;        my $formatting_element;
3700        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3701        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3702          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3703              !!!cp ('t52');
3704              last AFE;
3705            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3706                         eq $tag_name) {
3707            !!!cp ('t51');            !!!cp ('t51');
3708            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3709            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3710            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3711          }          }
3712        } # AFE        } # AFE
3713        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3714          !!!cp ('t53');          !!!cp ('t53');
3715          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3716          ## Ignore the token          ## Ignore the token
3717          !!!next-token;          !!!next-token;
3718          return;          return;
# Line 3020  sub _tree_construction_main ($) { Line 3729  sub _tree_construction_main ($) {
3729              last INSCOPE;              last INSCOPE;
3730            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3731              !!!cp ('t55');              !!!cp ('t55');
3732              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3733                                token => $end_tag_token);
3734              ## Ignore the token              ## Ignore the token
3735              !!!next-token;              !!!next-token;
3736              return;              return;
3737            }            }
3738          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3739            !!!cp ('t56');            !!!cp ('t56');
3740            $in_scope = 0;            $in_scope = 0;
3741          }          }
3742        } # INSCOPE        } # INSCOPE
3743        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3744          !!!cp ('t57');          !!!cp ('t57');
3745          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3746                            token => $end_tag_token);
3747          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3748          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3749          return;          return;
3750        }        }
3751        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3752          !!!cp ('t58');          !!!cp ('t58');
3753          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3754                            value => $self->{open_elements}->[-1]->[0]
3755                                ->manakai_local_name,
3756                            token => $end_tag_token);
3757        }        }
3758                
3759        ## Step 2        ## Step 2
# Line 3050  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3762        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3763          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3764          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3765              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3766              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3767               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3768            !!!cp ('t59');            !!!cp ('t59');
3769            $furthest_block = $node;            $furthest_block = $node;
3770            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3139  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850        } # S7          } # S7  
3851                
3852        ## Step 8        ## Step 8
3853        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3854            my $foster_parent_element;
3855            my $next_sibling;
3856            OE: for (reverse 0..$#{$self->{open_elements}}) {
3857              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3858                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3859                                 if (defined $parent and $parent->node_type == 1) {
3860                                   !!!cp ('t65.1');
3861                                   $foster_parent_element = $parent;
3862                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3863                                 } else {
3864                                   !!!cp ('t65.2');
3865                                   $foster_parent_element
3866                                     = $self->{open_elements}->[$_ - 1]->[0];
3867                                 }
3868                                 last OE;
3869                               }
3870                             } # OE
3871                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3872                               unless defined $foster_parent_element;
3873            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3874            $open_tables->[-1]->[1] = 1; # tainted
3875          } else {
3876            !!!cp ('t65.3');
3877            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3878          }
3879                
3880        ## Step 9        ## Step 9
3881        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3185  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921      } # FET      } # FET
3922    }; # $formatting_end_tag    }; # $formatting_end_tag
3923    
3924    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3925      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3926    }; # $insert_to_current    }; # $insert_to_current
3927    
3928    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3929                         my $child = shift;      my $child = shift;
3930                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3931                              table => 1, tbody => 1, tfoot => 1,        # MUST
3932                              thead => 1, tr => 1,        my $foster_parent_element;
3933                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3934                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3935                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3936                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3937                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3938                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3217  sub _tree_construction_main ($) { Line 3950  sub _tree_construction_main ($) {
3950                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3951                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3952                             ($child, $next_sibling);                             ($child, $next_sibling);
3953                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3954                           !!!cp ('t72');      } else {
3955                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3956                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3957        }
3958    }; # $insert_to_foster    }; # $insert_to_foster
3959    
3960    my $insert;    B: while (1) {
   
   B: {  
3961      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3962        !!!cp ('t73');        !!!cp ('t73');
3963        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3964        ## Ignore the token        ## Ignore the token
3965        ## Stay in the phase        ## Stay in the phase
3966        !!!next-token;        !!!next-token;
3967        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3968      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3969               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3970        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3971          !!!cp ('t79');          !!!cp ('t79');
3972          ## Turn into the main phase          !!!parse-error (type => 'after html:html', token => $token);
         !!!parse-error (type => 'after html:html');  
3973          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3974        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3975          !!!cp ('t80');          !!!cp ('t80');
3976          ## Turn into the main phase          !!!parse-error (type => 'after html:html', token => $token);
         !!!parse-error (type => 'after html:html');  
3977          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3978        } else {        } else {
3979          !!!cp ('t81');          !!!cp ('t81');
3980        }        }
3981    
3982  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3983  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!cp ('t82');  
         !!!parse-error (type => 'not first start tag');  
       } else {  
         !!!cp ('t83');  
       }  
3984        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3985        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3986          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3301  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3991          }          }
3992        }        }
3993          !!!nack ('t84.1');
3994        !!!next-token;        !!!next-token;
3995        redo B;        next B;
3996      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3997        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3998        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3316  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4007        }        }
4008        !!!next-token;        !!!next-token;
4009        redo B;        next B;
4010      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4011          if ($token->{type} == CHARACTER_TOKEN) {
4012            !!!cp ('t87.1');
4013            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4014            !!!next-token;
4015            next B;
4016          } elsif ($token->{type} == START_TAG_TOKEN) {
4017            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4018                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4019                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4020                ($token->{tag_name} eq 'svg' and
4021                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4022              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4023              !!!cp ('t87.2');
4024              #
4025            } elsif ({
4026                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4027                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4028                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4029                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4030                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4031                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4032                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4033                      var => 1,
4034                     }->{$token->{tag_name}}) {
4035              !!!cp ('t87.2');
4036              !!!parse-error (type => 'not closed',
4037                              value => $self->{open_elements}->[-1]->[0]
4038                                  ->manakai_local_name,
4039                              token => $token);
4040    
4041              pop @{$self->{open_elements}}
4042                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4043    
4044              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4045              ## Reprocess.
4046              next B;
4047            } else {
4048              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4049              my $tag_name = $token->{tag_name};
4050              if ($nsuri eq $SVG_NS) {
4051                $tag_name = {
4052                   altglyph => 'altGlyph',
4053                   altglyphdef => 'altGlyphDef',
4054                   altglyphitem => 'altGlyphItem',
4055                   animatecolor => 'animateColor',
4056                   animatemotion => 'animateMotion',
4057                   animatetransform => 'animateTransform',
4058                   clippath => 'clipPath',
4059                   feblend => 'feBlend',
4060                   fecolormatrix => 'feColorMatrix',
4061                   fecomponenttransfer => 'feComponentTransfer',
4062                   fecomposite => 'feComposite',
4063                   feconvolvematrix => 'feConvolveMatrix',
4064                   fediffuselighting => 'feDiffuseLighting',
4065                   fedisplacementmap => 'feDisplacementMap',
4066                   fedistantlight => 'feDistantLight',
4067                   feflood => 'feFlood',
4068                   fefunca => 'feFuncA',
4069                   fefuncb => 'feFuncB',
4070                   fefuncg => 'feFuncG',
4071                   fefuncr => 'feFuncR',
4072                   fegaussianblur => 'feGaussianBlur',
4073                   feimage => 'feImage',
4074                   femerge => 'feMerge',
4075                   femergenode => 'feMergeNode',
4076                   femorphology => 'feMorphology',
4077                   feoffset => 'feOffset',
4078                   fepointlight => 'fePointLight',
4079                   fespecularlighting => 'feSpecularLighting',
4080                   fespotlight => 'feSpotLight',
4081                   fetile => 'feTile',
4082                   feturbulence => 'feTurbulence',
4083                   foreignobject => 'foreignObject',
4084                   glyphref => 'glyphRef',
4085                   lineargradient => 'linearGradient',
4086                   radialgradient => 'radialGradient',
4087                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4088                   textpath => 'textPath',  
4089                }->{$tag_name} || $tag_name;
4090              }
4091    
4092              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4093    
4094              ## "adjust foreign attributes" - done in insert-element-f
4095    
4096              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4097    
4098              if ($self->{self_closing}) {
4099                pop @{$self->{open_elements}};
4100                !!!ack ('t87.3');
4101              } else {
4102                !!!cp ('t87.4');
4103              }
4104    
4105              !!!next-token;
4106              next B;
4107            }
4108          } elsif ($token->{type} == END_TAG_TOKEN) {
4109            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4110            !!!cp ('t87.5');
4111            #
4112          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4113            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4114            !!!cp ('t87.6');
4115            #
4116            ## TODO: ...
4117          } else {
4118            die "$0: $token->{type}: Unknown token type";        
4119          }
4120        }
4121    
4122        if ($self->{insertion_mode} & HEAD_IMS) {
4123        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4124          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4125            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4126                !!!cp ('t88.2');
4127                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4128              } else {
4129                !!!cp ('t88.1');
4130                ## Ignore the token.
4131                !!!next-token;
4132                next B;
4133              }
4134            unless (length $token->{data}) {            unless (length $token->{data}) {
4135              !!!cp ('t88');              !!!cp ('t88');
4136              !!!next-token;              !!!next-token;
4137              redo B;              next B;
4138            }            }
4139          }          }
4140    
4141          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4142            !!!cp ('t89');            !!!cp ('t89');
4143            ## As if <head>            ## As if <head>
4144            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4145            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4146            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4147                  [$self->{head_element}, $el_category->{head}];
4148    
4149            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4150            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3343  sub _tree_construction_main ($) { Line 4154  sub _tree_construction_main ($) {
4154            !!!cp ('t90');            !!!cp ('t90');
4155            ## As if </noscript>            ## As if </noscript>
4156            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4157            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4158                        
4159            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4160            ## As if </head>            ## As if </head>
# Line 3359  sub _tree_construction_main ($) { Line 4170  sub _tree_construction_main ($) {
4170            !!!cp ('t92');            !!!cp ('t92');
4171          }          }
4172    
4173              ## "after head" insertion mode          ## "after head" insertion mode
4174              ## As if <body>          ## As if <body>
4175              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4176              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4177              ## reprocess          ## reprocess
4178              redo B;          next B;
4179            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4180              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4181                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182                  !!!cp ('t93');              !!!cp ('t93');
4183                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4184                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4185                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4186                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4187                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4188                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4189                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4190                  !!!cp ('t94');              !!!next-token;
4191                  #              next B;
4192                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4193                  !!!cp ('t95');              !!!cp ('t93.2');
4194                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4195                  ## Ignore the token              ## Ignore the token
4196                  !!!next-token;              !!!nack ('t93.3');
4197                  redo B;              !!!next-token;
4198                }              next B;
4199              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4200                !!!cp ('t96');              !!!cp ('t95');
4201                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4202                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4203                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4204                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4205                next B;
4206              }
4207            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4208              !!!cp ('t96');
4209              ## As if <head>
4210              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4211              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4212              push @{$self->{open_elements}},
4213                  [$self->{head_element}, $el_category->{head}];
4214    
4215                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4216                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4217              } else {          } else {
4218                !!!cp ('t97');            !!!cp ('t97');
4219              }          }
4220    
4221              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4222                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4223                  !!!cp ('t98');                  !!!cp ('t98');
4224                  ## As if </noscript>                  ## As if </noscript>
4225                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4226                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4227                                
4228                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4229                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3414  sub _tree_construction_main ($) { Line 4234  sub _tree_construction_main ($) {
4234                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4235                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4236                  !!!cp ('t100');                  !!!cp ('t100');
4237                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4238                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4239                        [$self->{head_element}, $el_category->{head}];
4240                } else {                } else {
4241                  !!!cp ('t101');                  !!!cp ('t101');
4242                }                }
4243                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4244                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4245                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4246                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4247                  !!!nack ('t101.1');
4248                !!!next-token;                !!!next-token;
4249                redo B;                next B;
4250              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4251                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4252                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4253                  !!!cp ('t102');                  !!!cp ('t102');
4254                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4255                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4256                        [$self->{head_element}, $el_category->{head}];
4257                } else {                } else {
4258                  !!!cp ('t103');                  !!!cp ('t103');
4259                }                }
4260                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4261                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4262                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4263                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4264                  !!!ack ('t103.1');
4265                !!!next-token;                !!!next-token;
4266                redo B;                next B;
4267              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4268                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4269                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4270                  !!!cp ('t104');                  !!!cp ('t104');
4271                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4272                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4273                        [$self->{head_element}, $el_category->{head}];
4274                } else {                } else {
4275                  !!!cp ('t105');                  !!!cp ('t105');
4276                }                }
4277                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4278                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.
4279    
4280                unless ($self->{confident}) {                unless ($self->{confident}) {
4281                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4282                    !!!cp ('t106');                    !!!cp ('t106');
4283                      ## NOTE: Whether the encoding is supported or not is handled
4284                      ## in the {change_encoding} callback.
4285                    $self->{change_encoding}                    $self->{change_encoding}
4286                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4287                             $token);
4288                                        
4289                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4290                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4291                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4292                                                 ->{has_reference});                                                 ->{has_reference});
4293                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4294                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4295                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4296                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4297                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4298                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4299                      !!!cp ('t107');                      !!!cp ('t107');
4300                        ## NOTE: Whether the encoding is supported or not is handled
4301                        ## in the {change_encoding} callback.
4302                      $self->{change_encoding}                      $self->{change_encoding}
4303                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4304                               $token);
4305                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4306                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4307                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3497  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                  }                  }
4328                }                }
4329    
4330                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4331                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4332                  !!!ack ('t110.1');
4333                !!!next-token;                !!!next-token;
4334                redo B;                next B;
4335              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4336                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4337                  !!!cp ('t111');                  !!!cp ('t111');
4338                  ## As if </noscript>                  ## As if </noscript>
4339                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4340                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4341                                
4342                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4343                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4344                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4345                  !!!cp ('t112');                  !!!cp ('t112');
4346                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4347                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4348                        [$self->{head_element}, $el_category->{head}];
4349                } else {                } else {
4350                  !!!cp ('t113');                  !!!cp ('t113');
4351                }                }
# Line 3521  sub _tree_construction_main ($) { Line 4353  sub _tree_construction_main ($) {
4353                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4354                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4355                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4356                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4357                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4358                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4359                redo B;                next B;
4360              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4361                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4362                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4363                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4364                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4365                  !!!cp ('t114');                  !!!cp ('t114');
4366                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4367                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4368                        [$self->{head_element}, $el_category->{head}];
4369                } else {                } else {
4370                  !!!cp ('t115');                  !!!cp ('t115');
4371                }                }
4372                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4373                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4374                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4375                redo B;                next B;
4376              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4377                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4378                  !!!cp ('t116');                  !!!cp ('t116');
4379                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4380                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4381                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4382                    !!!nack ('t116.1');
4383                  !!!next-token;                  !!!next-token;
4384                  redo B;                  next B;
4385                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                  !!!cp ('t117');                  !!!cp ('t117');
4387                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4388                  ## Ignore the token                  ## Ignore the token
4389                    !!!nack ('t117.1');
4390                  !!!next-token;                  !!!next-token;
4391                  redo B;                  next B;
4392                } else {                } else {
4393                  !!!cp ('t118');                  !!!cp ('t118');
4394                  #                  #
# Line 3564  sub _tree_construction_main ($) { Line 4398  sub _tree_construction_main ($) {
4398                  !!!cp ('t119');                  !!!cp ('t119');
4399                  ## As if </noscript>                  ## As if </noscript>
4400                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4401                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4402                                
4403                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4404                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4405                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4406                  !!!cp ('t120');                  !!!cp ('t120');
4407                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4408                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4409                        [$self->{head_element}, $el_category->{head}];
4410                } else {                } else {
4411                  !!!cp ('t121');                  !!!cp ('t121');
4412                }                }
4413    
4414                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4415                $script_start_tag->($insert_to_current);                $script_start_tag->();
4416                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4417                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4418                redo B;                next B;
4419              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4420                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4421                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4422                  !!!cp ('t122');                  !!!cp ('t122');
4423                  ## As if </noscript>                  ## As if </noscript>
4424                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4425                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4426                                    
4427                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4428                  ## As if </head>                  ## As if </head>
# Line 3604  sub _tree_construction_main ($) { Line 4439  sub _tree_construction_main ($) {
4439                }                }
4440    
4441                ## "after head" insertion mode                ## "after head" insertion mode
4442                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4443                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4444                  !!!cp ('t126');                  !!!cp ('t126');
4445                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3614  sub _tree_construction_main ($) { Line 4449  sub _tree_construction_main ($) {
4449                } else {                } else {
4450                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4451                }                }
4452                  !!!nack ('t127.1');
4453                !!!next-token;                !!!next-token;
4454                redo B;                next B;
4455              } else {              } else {
4456                !!!cp ('t128');                !!!cp ('t128');
4457                #                #
# Line 3625  sub _tree_construction_main ($) { Line 4461  sub _tree_construction_main ($) {
4461                !!!cp ('t129');                !!!cp ('t129');
4462                ## As if </noscript>                ## As if </noscript>
4463                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4464                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4465                                
4466                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4467                ## As if </head>                ## As if </head>
# Line 3644  sub _tree_construction_main ($) { Line 4480  sub _tree_construction_main ($) {
4480    
4481              ## "after head" insertion mode              ## "after head" insertion mode
4482              ## As if <body>              ## As if <body>
4483              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4484              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4485              ## reprocess              ## reprocess
4486              redo B;              !!!ack-later;
4487                next B;
4488            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4489              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4490                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4491                  !!!cp ('t132');                  !!!cp ('t132');
4492                  ## As if <head>                  ## As if <head>
4493                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4494                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4495                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4496                        [$self->{head_element}, $el_category->{head}];
4497    
4498                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4499                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4500                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                  !!!cp ('t133');                  !!!cp ('t133');
4505                  ## As if </noscript>                  ## As if </noscript>
4506                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4507                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4508                                    
4509                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4510                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4511                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4512                  !!!next-token;                  !!!next-token;
4513                  redo B;                  next B;
4514                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4515                  !!!cp ('t134');                  !!!cp ('t134');
4516                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4517                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4518                  !!!next-token;                  !!!next-token;
4519                  redo B;                  next B;
4520                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4521                    !!!cp ('t134.1');
4522                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4523                    ## Ignore the token
4524                    !!!next-token;
4525                    next B;
4526                } else {                } else {
4527                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4528                }                }
4529              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4530                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3689  sub _tree_construction_main ($) { Line 4532  sub _tree_construction_main ($) {
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4537                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4538                  !!!cp ('t137');                  !!!cp ('t137');
4539                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4540                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                } else {                } else {
4544                  !!!cp ('t138');                  !!!cp ('t138');
4545                  #                  #
# Line 3703  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547              } elsif ({              } elsif ({
4548                        body => 1, html => 1,                        body => 1, html => 1,
4549                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4550                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4551                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4552                  ## 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) {  
4553                  !!!cp ('t140');                  !!!cp ('t140');
4554                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4555                    ## Ignore the token
4556                    !!!next-token;
4557                    next B;
4558                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4559                    !!!cp ('t140.1');
4560                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4561                  ## Ignore the token                  ## Ignore the token
4562                  !!!next-token;                  !!!next-token;
4563                  redo B;                  next B;
4564                } else {                } else {
4565                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4566                }                }
4567                              } elsif ($token->{tag_name} eq 'p') {
4568                #                !!!cp ('t142');
4569              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4570                        p => 1, br => 1,                ## Ignore the token
4571                       }->{$token->{tag_name}}) {                !!!next-token;
4572                  next B;
4573                } elsif ($token->{tag_name} eq 'br') {
4574                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575                  !!!cp ('t142');                  !!!cp ('t142.2');
4576                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4577                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4579                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4580      
4581                    ## Reprocess in the "after head" insertion mode...
4582                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4583                    !!!cp ('t143.2');
4584                    ## As if </head>
4585                    pop @{$self->{open_elements}};
4586                    $self->{insertion_mode} = AFTER_HEAD_IM;
4587      
4588                    ## Reprocess in the "after head" insertion mode...
4589                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4590                    !!!cp ('t143.3');
4591                    ## ISSUE: Two parse errors for <head><noscript></br>
4592                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4593                    ## As if </noscript>
4594                    pop @{$self->{open_elements}};
4595                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4596    
4597                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4598                } else {                  ## As if </head>
4599                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4600                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4601    
4602                #                  ## Reprocess in the "after head" insertion mode...
4603              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4604                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4605                  #                  #
4606                } else {                } else {
4607                  !!!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;  
4608                }                }
4609    
4610                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4611                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4612                  ## Ignore the token
4613                  !!!next-token;
4614                  next B;
4615                } else {
4616                  !!!cp ('t145');
4617                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4618                  ## Ignore the token
4619                  !!!next-token;
4620                  next B;
4621              }              }
4622    
4623              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4624                !!!cp ('t146');                !!!cp ('t146');
4625                ## As if </noscript>                ## As if </noscript>
4626                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4627                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4628                                
4629                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4630                ## As if </head>                ## As if </head>
# Line 3773  sub _tree_construction_main ($) { Line 4640  sub _tree_construction_main ($) {
4640              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4641  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4642                !!!cp ('t148');                !!!cp ('t148');
4643                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4644                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4645                !!!next-token;                !!!next-token;
4646                redo B;                next B;
4647              } else {              } else {
4648                !!!cp ('t149');                !!!cp ('t149');
4649              }              }
4650    
4651              ## "after head" insertion mode              ## "after head" insertion mode
4652              ## As if <body>              ## As if <body>
4653              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4654              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4655              ## reprocess              ## reprocess
4656              redo B;              next B;
4657            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4658              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4659            }            !!!cp ('t149.1');
4660    
4661              ## NOTE: As if <head>
4662              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663              $self->{open_elements}->[-1]->[0]->append_child
4664                  ($self->{head_element});
4665              #push @{$self->{open_elements}},
4666              #    [$self->{head_element}, $el_category->{head}];
4667              #$self->{insertion_mode} = IN_HEAD_IM;
4668              ## NOTE: Reprocess.
4669    
4670              ## NOTE: As if </head>
4671              #pop @{$self->{open_elements}};
4672              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4673              ## NOTE: Reprocess.
4674              
4675              #
4676            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4677              !!!cp ('t149.2');
4678    
4679              ## NOTE: As if </head>
4680              pop @{$self->{open_elements}};
4681              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4682              ## NOTE: Reprocess.
4683    
4684              #
4685            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4686              !!!cp ('t149.3');
4687    
4688              !!!parse-error (type => 'in noscript:#eof', token => $token);
4689    
4690              ## As if </noscript>
4691              pop @{$self->{open_elements}};
4692              #$self->{insertion_mode} = IN_HEAD_IM;
4693              ## NOTE: Reprocess.
4694    
4695              ## NOTE: As if </head>
4696              pop @{$self->{open_elements}};
4697              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4698              ## NOTE: Reprocess.
4699    
4700              #
4701            } else {
4702              !!!cp ('t149.4');
4703              #
4704            }
4705    
4706            ## NOTE: As if <body>
4707            !!!insert-element ('body',, $token);
4708            $self->{insertion_mode} = IN_BODY_IM;
4709            ## NOTE: Reprocess.
4710            next B;
4711          } else {
4712            die "$0: $token->{type}: Unknown token type";
4713          }
4714    
4715            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4716      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3801  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4723    
4724              !!!next-token;              !!!next-token;
4725              redo B;              next B;
4726            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4727              if ({              if ({
4728                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3809  sub _tree_construction_main ($) { Line 4730  sub _tree_construction_main ($) {
4730                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4731                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4732                  ## have an element in table scope                  ## have an element in table scope
4733                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4734                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4735                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4736                      !!!cp ('t151');                      !!!cp ('t151');
4737                      $tn = $node->[1];  
4738                      last INSCOPE;                      ## Close the cell
4739                    } elsif ({                      !!!back-token; # <x>
4740                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4741                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4742                                  line => $token->{line},
4743                                  column => $token->{column}};
4744                        next B;
4745                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4746                      !!!cp ('t152');                      !!!cp ('t152');
4747                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4748                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4749                    }                    }
4750                                    }
4751                  !!!cp ('t154');  
4752                  ## Close the cell                  !!!cp ('t153');
4753                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4754                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4755                  redo B;                  ## Ignore the token
4756                    !!!nack ('t153.1');
4757                    !!!next-token;
4758                    next B;
4759                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4760                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4761                                    
4762                  ## As if </caption>                  ## NOTE: As if </caption>.
4763                  ## have a table element in table scope                  ## have a table element in table scope
4764                  my $i;                  my $i;
4765                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4766                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4767                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4768                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4769                      $i = $_;                        !!!cp ('t155');
4770                      last INSCOPE;                        $i = $_;
4771                    } elsif ({                        last INSCOPE;
4772                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4773                             }->{$node->[1]}) {                        !!!cp ('t156');
4774                      !!!cp ('t156');                        last;
4775                      last INSCOPE;                      }
4776                    }                    }
4777    
4778                      !!!cp ('t157');
4779                      !!!parse-error (type => 'start tag not allowed',
4780                                      value => $token->{tag_name}, token => $token);
4781                      ## Ignore the token
4782                      !!!nack ('t157.1');
4783                      !!!next-token;
4784                      next B;
4785                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4786                                    
4787                  ## generate implied end tags                  ## generate implied end tags
4788                  if ({                  while ($self->{open_elements}->[-1]->[1]
4789                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4790                    !!!cp ('t158');                    !!!cp ('t158');
4791                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4792                  }                  }
4793    
4794                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4795                    !!!cp ('t159');                    !!!cp ('t159');
4796                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4797                                      value => $self->{open_elements}->[-1]->[0]
4798                                          ->manakai_local_name,
4799                                      token => $token);
4800                  } else {                  } else {
4801                    !!!cp ('t160');                    !!!cp ('t160');
4802                  }                  }
# Line 3893  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4809                                    
4810                  ## reprocess                  ## reprocess
4811                  redo B;                  !!!ack-later;
4812                    next B;
4813                } else {                } else {
4814                  !!!cp ('t161');                  !!!cp ('t161');
4815                  #                  #
# Line 3909  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825                  my $i;                  my $i;
4826                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4827                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4828                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                      !!!cp ('t163');                      !!!cp ('t163');
4830                      $i = $_;                      $i = $_;
4831                      last INSCOPE;                      last INSCOPE;
4832                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4833                      !!!cp ('t164');                      !!!cp ('t164');
4834                      last INSCOPE;                      last INSCOPE;
4835                    }                    }
4836                  } # INSCOPE                  } # INSCOPE
4837                    unless (defined $i) {                    unless (defined $i) {
4838                      !!!cp ('t165');                      !!!cp ('t165');
4839                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4840                      ## Ignore the token                      ## Ignore the token
4841                      !!!next-token;                      !!!next-token;
4842                      redo B;                      next B;
4843                    }                    }
4844                                    
4845                  ## generate implied end tags                  ## generate implied end tags
4846                  if ({                  while ($self->{open_elements}->[-1]->[1]
4847                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4848                    !!!cp ('t166');                    !!!cp ('t166');
4849                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4850                  }                  }
4851                    
4852                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4853                            ne $token->{tag_name}) {
4854                    !!!cp ('t167');                    !!!cp ('t167');
4855                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4856                                      value => $self->{open_elements}->[-1]->[0]
4857                                          ->manakai_local_name,
4858                                      token => $token);
4859                  } else {                  } else {
4860                    !!!cp ('t168');                    !!!cp ('t168');
4861                  }                  }
# Line 3957  sub _tree_construction_main ($) { Line 4867  sub _tree_construction_main ($) {
4867                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4868                                    
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4872                  !!!cp ('t169');                  !!!cp ('t169');
4873                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4874                  ## Ignore the token                  ## Ignore the token
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } else {                } else {
4878                  !!!cp ('t170');                  !!!cp ('t170');
4879                  #                  #
# Line 3972  sub _tree_construction_main ($) { Line 4882  sub _tree_construction_main ($) {
4882                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4883                  ## have a table element in table scope                  ## have a table element in table scope
4884                  my $i;                  my $i;
4885                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4886                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4887                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4888                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4889                      $i = $_;                        !!!cp ('t171');
4890                      last INSCOPE;                        $i = $_;
4891                    } elsif ({                        last INSCOPE;
4892                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4893                             }->{$node->[1]}) {                        !!!cp ('t172');
4894                      !!!cp ('t172');                        last;
4895                      last INSCOPE;                      }
4896                    }                    }
4897    
4898                      !!!cp ('t173');
4899                      !!!parse-error (type => 'unmatched end tag',
4900                                      value => $token->{tag_name}, token => $token);
4901                      ## Ignore the token
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4909                    !!!cp ('t174');                    !!!cp ('t174');
4910                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4911                  }                  }
4912                                    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!cp ('t175');                    !!!cp ('t175');
4915                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4916                                      value => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                  } else {                  } else {
4920                    !!!cp ('t176');                    !!!cp ('t176');
4921                  }                  }
# Line 4020  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  !!!next-token;                  !!!next-token;
4930                  redo B;                  next B;
4931                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4932                  !!!cp ('t177');                  !!!cp ('t177');
4933                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4934                  ## Ignore the token                  ## Ignore the token
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                  !!!cp ('t178');                  !!!cp ('t178');
4939                  #                  #
# Line 4039  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946                ## have an element in table scope                ## have an element in table scope
4947                my $i;                my $i;
4948                my $tn;                my $tn;
4949                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4950                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4951                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4952                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4953                    $i = $_;                      !!!cp ('t179');
4954                    last INSCOPE;                      $i = $_;
4955                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4956                    !!!cp ('t180');                      ## Close the cell
4957                    $tn = $node->[1];                      !!!back-token; # </x>
4958                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4959                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4960                  } elsif ({                                column => $token->{column}};
4961                            table => 1, html => 1,                      next B;
4962                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4963                    !!!cp ('t181');                      !!!cp ('t180');
4964                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4965                        ## NOTE: There is exactly one |td| or |th| element
4966                        ## in scope in the stack of open elements by definition.
4967                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4968                        ## ISSUE: Can this be reached?
4969                        !!!cp ('t181');
4970                        last;
4971                      }
4972                  }                  }
4973                } # INSCOPE  
               unless (defined $i) {  
4974                  !!!cp ('t182');                  !!!cp ('t182');
4975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4976                        value => $token->{tag_name}, token => $token);
4977                  ## Ignore the token                  ## Ignore the token
4978                  !!!next-token;                  !!!next-token;
4979                  redo B;                  next B;
4980                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4981              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4982                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4983                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4984    
4985                ## As if </caption>                ## As if </caption>
4986                ## have a table element in table scope                ## have a table element in table scope
4987                my $i;                my $i;
4988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4990                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4991                    !!!cp ('t184');                    !!!cp ('t184');
4992                    $i = $_;                    $i = $_;
4993                    last INSCOPE;                    last INSCOPE;
4994                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4995                    !!!cp ('t185');                    !!!cp ('t185');
4996                    last INSCOPE;                    last INSCOPE;
4997                  }                  }
4998                } # INSCOPE                } # INSCOPE
4999                unless (defined $i) {                unless (defined $i) {
5000                  !!!cp ('t186');                  !!!cp ('t186');
5001                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5002                  ## Ignore the token                  ## Ignore the token
5003                  !!!next-token;                  !!!next-token;
5004                  redo B;                  next B;
5005                }                }
5006                                
5007                ## generate implied end tags                ## generate implied end tags
5008                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5009                  !!!cp ('t187');                  !!!cp ('t187');
5010                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5011                }                }
5012    
5013                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5014                  !!!cp ('t188');                  !!!cp ('t188');
5015                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5016                                    value => $self->{open_elements}->[-1]->[0]
5017                                        ->manakai_local_name,
5018                                    token => $token);
5019                } else {                } else {
5020                  !!!cp ('t189');                  !!!cp ('t189');
5021                }                }
# Line 4128  sub _tree_construction_main ($) { Line 5027  sub _tree_construction_main ($) {
5027                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5028    
5029                ## reprocess                ## reprocess
5030                redo B;                next B;
5031              } elsif ({              } elsif ({
5032                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5033                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5034                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5035                  !!!cp ('t190');                  !!!cp ('t190');
5036                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5037                  ## Ignore the token                  ## Ignore the token
5038                  !!!next-token;                  !!!next-token;
5039                  redo B;                  next B;
5040                } else {                } else {
5041                  !!!cp ('t191');                  !!!cp ('t191');
5042                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 5047  sub _tree_construction_main ($) {
5047                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5048                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5049                !!!cp ('t192');                !!!cp ('t192');
5050                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5051                ## Ignore the token                ## Ignore the token
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054              } else {              } else {
5055                !!!cp ('t193');                !!!cp ('t193');
5056                #                #
5057              }              }
5058          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5059            for my $entry (@{$self->{open_elements}}) {
5060              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5061                !!!cp ('t75');
5062                !!!parse-error (type => 'in body:#eof', token => $token);
5063                last;
5064              }
5065            }
5066    
5067            ## Stop parsing.
5068            last B;
5069        } else {        } else {
5070          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5071        }        }
# Line 4164  sub _tree_construction_main ($) { Line 5074  sub _tree_construction_main ($) {
5074        #        #
5075      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5076        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5077              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5078                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5079              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5080                                
5081                unless (length $token->{data}) {            unless (length $token->{data}) {
5082                  !!!cp ('t194');              !!!cp ('t194');
5083                  !!!next-token;              !!!next-token;
5084                  redo B;              next B;
5085                } else {            } else {
5086                  !!!cp ('t195');              !!!cp ('t195');
5087                }            }
5088              }          }
5089    
5090              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5091    
5092              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5093              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4184  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095              ## result in a new Text node.              ## result in a new Text node.
5096              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5097                            
5098              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]}) {  
5099                # MUST                # MUST
5100                my $foster_parent_element;                my $foster_parent_element;
5101                my $next_sibling;                my $next_sibling;
5102                my $prev_sibling;                my $prev_sibling;
5103                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5104                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5105                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5106                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5107                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4221  sub _tree_construction_main ($) { Line 5129  sub _tree_construction_main ($) {
5129                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5130                     $next_sibling);                     $next_sibling);
5131                }                }
5132              } else {            $open_tables->[-1]->[1] = 1; # tainted
5133                !!!cp ('t200');          } else {
5134                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5135              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5136            }
5137                            
5138              !!!next-token;          !!!next-token;
5139              redo B;          next B;
5140        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5141              if ({              if ({
5142                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4235  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5145                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5146                  ## Clear back to table context                  ## Clear back to table context
5147                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5148                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5149                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5150                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5151                  }                  }
5152                                    
5153                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5154                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5155                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5156                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5158  sub _tree_construction_main ($) {
5158                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5159                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5160                    !!!cp ('t202');                    !!!cp ('t202');
5161                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5162                  }                  }
5163                                    
5164                  ## Clear back to table body context                  ## Clear back to table body context
5165                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5166                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5167                    !!!cp ('t203');                    !!!cp ('t203');
5168                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5169                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5170                  }                  }
5171                                    
5172                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5173                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5174                    !!!cp ('t204');                    !!!cp ('t204');
5175                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5176                      !!!nack ('t204');
5177                    !!!next-token;                    !!!next-token;
5178                    redo B;                    next B;
5179                  } else {                  } else {
5180                    !!!cp ('t205');                    !!!cp ('t205');
5181                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5182                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5183                  }                  }
5184                } else {                } else {
# Line 4278  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186                }                }
5187    
5188                ## Clear back to table row context                ## Clear back to table row context
5189                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5190                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5191                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5192                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5193                }                }
5194                                
5195                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5196                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5197    
5198                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5199                                
5200                  !!!nack ('t207.1');
5201                !!!next-token;                !!!next-token;
5202                redo B;                next B;
5203              } elsif ({              } elsif ({
5204                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5205                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                  my $i;                  my $i;
5212                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5214                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5215                      !!!cp ('t208');                      !!!cp ('t208');
5216                      $i = $_;                      $i = $_;
5217                      last INSCOPE;                      last INSCOPE;
5218                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5219                      !!!cp ('t209');                      !!!cp ('t209');
5220                      last INSCOPE;                      last INSCOPE;
5221                    }                    }
5222                  } # INSCOPE                  } # INSCOPE
5223                  unless (defined $i) {                  unless (defined $i) {
5224                   !!!cp ('t210');                    !!!cp ('t210');
5225                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  ## TODO: This type is wrong.
5226                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5227                    ## Ignore the token                    ## Ignore the token
5228                      !!!nack ('t210.1');
5229                    !!!next-token;                    !!!next-token;
5230                    redo B;                    next B;
5231                  }                  }
5232                                    
5233                  ## Clear back to table row context                  ## Clear back to table row context
5234                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5235                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5236                    !!!cp ('t211');                    !!!cp ('t211');
5237                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5238                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5239                  }                  }
5240                                    
# Line 4337  sub _tree_construction_main ($) { Line 5243  sub _tree_construction_main ($) {
5243                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5244                    !!!cp ('t212');                    !!!cp ('t212');
5245                    ## reprocess                    ## reprocess
5246                    redo B;                    !!!ack-later;
5247                      next B;
5248                  } else {                  } else {
5249                    !!!cp ('t213');                    !!!cp ('t213');
5250                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4349  sub _tree_construction_main ($) { Line 5256  sub _tree_construction_main ($) {
5256                  my $i;                  my $i;
5257                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5258                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5259                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5260                      !!!cp ('t214');                      !!!cp ('t214');
5261                      $i = $_;                      $i = $_;
5262                      last INSCOPE;                      last INSCOPE;
5263                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5264                      !!!cp ('t215');                      !!!cp ('t215');
5265                      last INSCOPE;                      last INSCOPE;
5266                    }                    }
# Line 4365  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268                  unless (defined $i) {                  unless (defined $i) {
5269                    !!!cp ('t216');                    !!!cp ('t216');
5270  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5271                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5272                    ## Ignore the token                    ## Ignore the token
5273                      !!!nack ('t216.1');
5274                    !!!next-token;                    !!!next-token;
5275                    redo B;                    next B;
5276                  }                  }
5277    
5278                  ## Clear back to table body context                  ## Clear back to table body context
5279                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5280                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5281                    !!!cp ('t217');                    !!!cp ('t217');
5282                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5283                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5284                  }                  }
5285                                    
# Line 4396  sub _tree_construction_main ($) { Line 5299  sub _tree_construction_main ($) {
5299    
5300                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5301                  ## Clear back to table context                  ## Clear back to table context
5302                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5303                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5304                    !!!cp ('t219');                    !!!cp ('t219');
5305                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5306                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5307                  }                  }
5308                                    
5309                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5310                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5311                  ## reprocess                  ## reprocess
5312                  redo B;                  !!!ack-later;
5313                    next B;
5314                } elsif ({                } elsif ({
5315                          caption => 1,                          caption => 1,
5316                          colgroup => 1,                          colgroup => 1,
5317                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5318                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5319                  ## Clear back to table context                  ## Clear back to table context
5320                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5321                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5322                    !!!cp ('t220');                    !!!cp ('t220');
5323                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5324                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5325                  }                  }
5326                                    
5327                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5328                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5329                                    
5330                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5331                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5332                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5333                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4432  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5337                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5338                  !!!next-token;                  !!!next-token;
5339                  redo B;                  !!!nack ('t220.1');
5340                    next B;
5341                } else {                } else {
5342                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5343                }                }
5344              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5345                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5346                                  value => $self->{open_elements}->[-1]->[0]
5347                                      ->manakai_local_name,
5348                                  token => $token);
5349    
5350                ## As if </table>                ## As if </table>
5351                ## have a table element in table scope                ## have a table element in table scope
5352                my $i;                my $i;
5353                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5354                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5355                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5356                    !!!cp ('t221');                    !!!cp ('t221');
5357                    $i = $_;                    $i = $_;
5358                    last INSCOPE;                    last INSCOPE;
5359                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5360                    !!!cp ('t222');                    !!!cp ('t222');
5361                    last INSCOPE;                    last INSCOPE;
5362                  }                  }
5363                } # INSCOPE                } # INSCOPE
5364                unless (defined $i) {                unless (defined $i) {
5365                  !!!cp ('t223');                  !!!cp ('t223');
5366                  !!!parse-error (type => 'unmatched end tag:table');  ## TODO: The following is wrong, maybe.
5367                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5368                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5369                    !!!nack ('t223.1');
5370                  !!!next-token;                  !!!next-token;
5371                  redo B;                  next B;
5372                }                }
5373                                
5374    ## TODO: Followings are removed from the latest spec.
5375                ## generate implied end tags                ## generate implied end tags
5376                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5377                  !!!cp ('t224');                  !!!cp ('t224');
5378                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5379                }                }
5380    
5381                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5382                  !!!cp ('t225');                  !!!cp ('t225');
5383                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## NOTE: |<table><tr><table>|
5384                    !!!parse-error (type => 'not closed',
5385                                    value => $self->{open_elements}->[-1]->[0]
5386                                        ->manakai_local_name,
5387                                    token => $token);
5388                } else {                } else {
5389                  !!!cp ('t226');                  !!!cp ('t226');
5390                }                }
5391    
5392                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5393                  pop @{$open_tables};
5394    
5395                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5396    
5397                ## reprocess            ## reprocess
5398                redo B;            !!!ack-later;
5399              next B;
5400            } elsif ($token->{tag_name} eq 'style') {
5401              if (not $open_tables->[-1]->[1]) { # tainted
5402                !!!cp ('t227.8');
5403                ## NOTE: This is a "as if in head" code clone.
5404                $parse_rcdata->(CDATA_CONTENT_MODEL);
5405                next B;
5406              } else {
5407                !!!cp ('t227.7');
5408                #
5409              }
5410            } elsif ($token->{tag_name} eq 'script') {
5411              if (not $open_tables->[-1]->[1]) { # tainted
5412                !!!cp ('t227.6');
5413                ## NOTE: This is a "as if in head" code clone.
5414                $script_start_tag->();
5415                next B;
5416              } else {
5417                !!!cp ('t227.5');
5418                #
5419              }
5420            } elsif ($token->{tag_name} eq 'input') {
5421              if (not $open_tables->[-1]->[1]) { # tainted
5422                if ($token->{attributes}->{type}) { ## TODO: case
5423                  my $type = lc $token->{attributes}->{type}->{value};
5424                  if ($type eq 'hidden') {
5425                    !!!cp ('t227.3');
5426                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5427    
5428                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5429    
5430                    ## TODO: form element pointer
5431    
5432                    pop @{$self->{open_elements}};
5433    
5434                    !!!next-token;
5435                    !!!ack ('t227.2.1');
5436                    next B;
5437                  } else {
5438                    !!!cp ('t227.2');
5439                    #
5440                  }
5441                } else {
5442                  !!!cp ('t227.1');
5443                  #
5444                }
5445              } else {
5446                !!!cp ('t227.4');
5447                #
5448              }
5449          } else {          } else {
5450            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5451            #            #
5452          }          }
5453    
5454            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5455    
5456            $insert = $insert_to_foster;
5457            #
5458        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5459              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5460                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4505  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                my $i;                my $i;
5463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5464                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5465                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5466                    !!!cp ('t228');                    !!!cp ('t228');
5467                    $i = $_;                    $i = $_;
5468                    last INSCOPE;                    last INSCOPE;
5469                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5470                    !!!cp ('t229');                    !!!cp ('t229');
5471                    last INSCOPE;                    last INSCOPE;
5472                  }                  }
5473                } # INSCOPE                } # INSCOPE
5474                unless (defined $i) {                unless (defined $i) {
5475                  !!!cp ('t230');                  !!!cp ('t230');
5476                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                  ## Ignore the token                  ## Ignore the token
5478                    !!!nack ('t230.1');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                } else {                } else {
5482                  !!!cp ('t232');                  !!!cp ('t232');
5483                }                }
5484    
5485                ## Clear back to table row context                ## Clear back to table row context
5486                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5487                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5488                  !!!cp ('t231');                  !!!cp ('t231');
5489                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5490                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5491                }                }
5492    
5493                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5494                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5495                !!!next-token;                !!!next-token;
5496                redo B;                !!!nack ('t231.1');
5497                  next B;
5498              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5499                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5500                  ## As if </tr>                  ## As if </tr>
# Line 4546  sub _tree_construction_main ($) { Line 5502  sub _tree_construction_main ($) {
5502                  my $i;                  my $i;
5503                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5504                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5505                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5506                      !!!cp ('t233');                      !!!cp ('t233');
5507                      $i = $_;                      $i = $_;
5508                      last INSCOPE;                      last INSCOPE;
5509                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5510                      !!!cp ('t234');                      !!!cp ('t234');
5511                      last INSCOPE;                      last INSCOPE;
5512                    }                    }
5513                  } # INSCOPE                  } # INSCOPE
5514                  unless (defined $i) {                  unless (defined $i) {
5515                    !!!cp ('t235');                    !!!cp ('t235');
5516                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});  ## TODO: The following is wrong.
5517                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5518                    ## Ignore the token                    ## Ignore the token
5519                      !!!nack ('t236.1');
5520                    !!!next-token;                    !!!next-token;
5521                    redo B;                    next B;
5522                  }                  }
5523                                    
5524                  ## Clear back to table row context                  ## Clear back to table row context
5525                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5526                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5527                    !!!cp ('t236');                    !!!cp ('t236');
5528                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5529                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5530                  }                  }
5531                                    
# Line 4584  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539                  my $i;                  my $i;
5540                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5541                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5542                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5543                      !!!cp ('t237');                      !!!cp ('t237');
5544                      $i = $_;                      $i = $_;
5545                      last INSCOPE;                      last INSCOPE;
5546                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5547                      !!!cp ('t238');                      !!!cp ('t238');
5548                      last INSCOPE;                      last INSCOPE;
5549                    }                    }
5550                  } # INSCOPE                  } # INSCOPE
5551                  unless (defined $i) {                  unless (defined $i) {
5552                    !!!cp ('t239');                    !!!cp ('t239');
5553                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5554                    ## Ignore the token                    ## Ignore the token
5555                      !!!nack ('t239.1');
5556                    !!!next-token;                    !!!next-token;
5557                    redo B;                    next B;
5558                  }                  }
5559                                    
5560                  ## Clear back to table body context                  ## Clear back to table body context
5561                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5562                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5563                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5564                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5565                  }                  }
5566                                    
# Line 4626  sub _tree_construction_main ($) { Line 5576  sub _tree_construction_main ($) {
5576                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5577                }                }
5578    
5579                  ## NOTE: </table> in the "in table" insertion mode.
5580                  ## When you edit the code fragment below, please ensure that
5581                  ## the code for <table> in the "in table" insertion mode
5582                  ## is synced with it.
5583    
5584                ## have a table element in table scope                ## have a table element in table scope
5585                my $i;                my $i;
5586                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5587                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5588                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5589                    !!!cp ('t241');                    !!!cp ('t241');
5590                    $i = $_;                    $i = $_;
5591                    last INSCOPE;                    last INSCOPE;
5592                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5593                    !!!cp ('t242');                    !!!cp ('t242');
5594                    last INSCOPE;                    last INSCOPE;
5595                  }                  }
5596                } # INSCOPE                } # INSCOPE
5597                unless (defined $i) {                unless (defined $i) {
5598                  !!!cp ('t243');                  !!!cp ('t243');
5599                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                    !!!nack ('t243.1');
5602                  !!!next-token;                  !!!next-token;
5603                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5604                }                }
5605                                    
5606                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5607                  pop @{$open_tables};
5608                                
5609                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5610                                
5611                !!!next-token;                !!!next-token;
5612                redo B;                next B;
5613              } elsif ({              } elsif ({
5614                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5615                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4684  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                  my $i;                  my $i;
5620                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5621                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5622                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5623                      !!!cp ('t247');                      !!!cp ('t247');
5624                      $i = $_;                      $i = $_;
5625                      last INSCOPE;                      last INSCOPE;
5626                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5627                      !!!cp ('t248');                      !!!cp ('t248');
5628                      last INSCOPE;                      last INSCOPE;
5629                    }                    }
5630                  } # INSCOPE                  } # INSCOPE
5631                    unless (defined $i) {                    unless (defined $i) {
5632                      !!!cp ('t249');                      !!!cp ('t249');
5633                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                      ## Ignore the token                      ## Ignore the token
5635                        !!!nack ('t249.1');
5636                      !!!next-token;                      !!!next-token;
5637                      redo B;                      next B;
5638                    }                    }
5639                                    
5640                  ## As if </tr>                  ## As if </tr>
# Line 4708  sub _tree_construction_main ($) { Line 5642  sub _tree_construction_main ($) {
5642                  my $i;                  my $i;
5643                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5644                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5645                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5646                      !!!cp ('t250');                      !!!cp ('t250');
5647                      $i = $_;                      $i = $_;
5648                      last INSCOPE;                      last INSCOPE;
5649                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5650                      !!!cp ('t251');                      !!!cp ('t251');
5651                      last INSCOPE;                      last INSCOPE;
5652                    }                    }
5653                  } # INSCOPE                  } # INSCOPE
5654                    unless (defined $i) {                    unless (defined $i) {
5655                      !!!cp ('t252');                      !!!cp ('t252');
5656                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5657                      ## Ignore the token                      ## Ignore the token
5658                        !!!nack ('t252.1');
5659                      !!!next-token;                      !!!next-token;
5660                      redo B;                      next B;
5661                    }                    }
5662                                    
5663                  ## Clear back to table row context                  ## Clear back to table row context
5664                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5665                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5666                    !!!cp ('t253');                    !!!cp ('t253');
5667                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5668                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5669                  }                  }
5670                                    
# Line 4745  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                my $i;                my $i;
5678                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5679                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5680                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5681                    !!!cp ('t254');                    !!!cp ('t254');
5682                    $i = $_;                    $i = $_;
5683                    last INSCOPE;                    last INSCOPE;
5684                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5685                    !!!cp ('t255');                    !!!cp ('t255');
5686                    last INSCOPE;                    last INSCOPE;
5687                  }                  }
5688                } # INSCOPE                } # INSCOPE
5689                unless (defined $i) {                unless (defined $i) {
5690                  !!!cp ('t256');                  !!!cp ('t256');
5691                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                    !!!nack ('t256.1');
5694                  !!!next-token;                  !!!next-token;
5695                  redo B;                  next B;
5696                }                }
5697    
5698                ## Clear back to table body context                ## Clear back to table body context
5699                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5700                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5701                  !!!cp ('t257');                  !!!cp ('t257');
5702                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5704                }                }
5705    
5706                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5707                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5708                  !!!nack ('t257.1');
5709                !!!next-token;                !!!next-token;
5710                redo B;                next B;
5711              } elsif ({              } elsif ({
5712                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5713                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5714                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5715                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5716                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5717                !!!cp ('t258');            !!!cp ('t258');
5718                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5719                ## Ignore the token            ## Ignore the token
5720                !!!next-token;            !!!nack ('t258.1');
5721                redo B;             !!!next-token;
5722              next B;
5723          } else {          } else {
5724            !!!cp ('t259');            !!!cp ('t259');
5725            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5726    
5727            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5728            #            #
5729          }          }
5730          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5731            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5732                    @{$self->{open_elements}} == 1) { # redundant, maybe
5733              !!!parse-error (type => 'in body:#eof', token => $token);
5734              !!!cp ('t259.1');
5735              #
5736            } else {
5737              !!!cp ('t259.2');
5738              #
5739            }
5740    
5741            ## Stop parsing
5742            last B;
5743        } else {        } else {
5744          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5745        }        }
# Line 4805  sub _tree_construction_main ($) { Line 5750  sub _tree_construction_main ($) {
5750                unless (length $token->{data}) {                unless (length $token->{data}) {
5751                  !!!cp ('t260');                  !!!cp ('t260');
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                  next B;
5754                }                }
5755              }              }
5756                            
# Line 4814  sub _tree_construction_main ($) { Line 5759  sub _tree_construction_main ($) {
5759            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5760              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5761                !!!cp ('t262');                !!!cp ('t262');
5762                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5764                  !!!ack ('t262.1');
5765                !!!next-token;                !!!next-token;
5766                redo B;                next B;
5767              } else {              } else {
5768                !!!cp ('t263');                !!!cp ('t263');
5769                #                #
5770              }              }
5771            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5772              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5773                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5774                  !!!cp ('t264');                  !!!cp ('t264');
5775                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5776                  ## Ignore the token                  ## Ignore the token
5777                  !!!next-token;                  !!!next-token;
5778                  redo B;                  next B;
5779                } else {                } else {
5780                  !!!cp ('t265');                  !!!cp ('t265');
5781                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5782                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5783                  !!!next-token;                  !!!next-token;
5784                  redo B;                              next B;            
5785                }                }
5786              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5787                !!!cp ('t266');                !!!cp ('t266');
5788                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5789                ## Ignore the token                ## Ignore the token
5790                !!!next-token;                !!!next-token;
5791                redo B;                next B;
5792              } else {              } else {
5793                !!!cp ('t267');                !!!cp ('t267');
5794                #                #
5795              }              }
5796            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5797              !!!cp ('t268');          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5798              #              @{$self->{open_elements}} == 1) { # redundant, maybe
5799            }            !!!cp ('t270.2');
5800              ## Stop parsing.
5801              last B;
5802            } else {
5803              ## NOTE: As if </colgroup>.
5804              !!!cp ('t270.1');
5805              pop @{$self->{open_elements}}; # colgroup
5806              $self->{insertion_mode} = IN_TABLE_IM;
5807              ## Reprocess.
5808              next B;
5809            }
5810          } else {
5811            die "$0: $token->{type}: Unknown token type";
5812          }
5813    
5814            ## As if </colgroup>            ## As if </colgroup>
5815            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5816              !!!cp ('t269');              !!!cp ('t269');
5817              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5818                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5819              ## Ignore the token              ## Ignore the token
5820                !!!nack ('t269.1');
5821              !!!next-token;              !!!next-token;
5822              redo B;              next B;
5823            } else {            } else {
5824              !!!cp ('t270');              !!!cp ('t270');
5825              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5826              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5827                !!!ack-later;
5828              ## reprocess              ## reprocess
5829              redo B;              next B;
5830            }            }
5831      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5832        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5833          !!!cp ('t271');          !!!cp ('t271');
5834          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5835          !!!next-token;          !!!next-token;
5836          redo B;          next B;
5837        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5838              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5839                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5840                  !!!cp ('t272');              !!!cp ('t272');
5841                  ## As if </option>              ## As if </option>
5842                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5843                } else {            } else {
5844                  !!!cp ('t273');              !!!cp ('t273');
5845                }            }
5846    
5847                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5848                !!!next-token;            !!!nack ('t273.1');
5849                redo B;            !!!next-token;
5850              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5851                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5852                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5853                  ## As if </option>              !!!cp ('t274');
5854                  pop @{$self->{open_elements}};              ## As if </option>
5855                } else {              pop @{$self->{open_elements}};
5856                  !!!cp ('t275');            } else {
5857                }              !!!cp ('t275');
5858              }
5859    
5860                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5861                  !!!cp ('t276');              !!!cp ('t276');
5862                  ## As if </optgroup>              ## As if </optgroup>
5863                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5864                } else {            } else {
5865                  !!!cp ('t277');              !!!cp ('t277');
5866                }            }
5867    
5868                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5869                !!!next-token;            !!!nack ('t277.1');
5870                redo B;            !!!next-token;
5871              } elsif ($token->{tag_name} eq 'select') {            next B;
5872                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5873                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5874                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5875                my $i;                    {
5876                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5877                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5878                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5879                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
5880                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
5881                    last INSCOPE;            !!!parse-error (type => 'not closed:select', token => $token);
5882                  } elsif ({            ## NOTE: As if the token were </select> (<select> case) or
5883                            table => 1, html => 1,            ## as if there were </select> (otherwise).
5884                           }->{$node->[1]}) {            ## have an element in table scope
5885                    !!!cp ('t279');            my $i;
5886                    last INSCOPE;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5887                  }              my $node = $self->{open_elements}->[$_];
5888                } # INSCOPE              if ($node->[1] & SELECT_EL) {
5889                unless (defined $i) {                !!!cp ('t278');
5890                  !!!cp ('t280');                $i = $_;
5891                  !!!parse-error (type => 'unmatched end tag:select');                last INSCOPE;
5892                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5893                  !!!next-token;                !!!cp ('t279');
5894                  redo B;                last INSCOPE;
5895                }              }
5896              } # INSCOPE
5897              unless (defined $i) {
5898                !!!cp ('t280');
5899                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5900                ## Ignore the token
5901                !!!nack ('t280.1');
5902                !!!next-token;
5903                next B;
5904              }
5905                                
5906                !!!cp ('t281');            !!!cp ('t281');
5907                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5908    
5909                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5910    
5911                !!!next-token;            if ($token->{tag_name} eq 'select') {
5912                redo B;              !!!nack ('t281.2');
5913                !!!next-token;
5914                next B;
5915              } else {
5916                !!!cp ('t281.1');
5917                !!!ack-later;
5918                ## Reprocess the token.
5919                next B;
5920              }
5921          } else {          } else {
5922            !!!cp ('t282');            !!!cp ('t282');
5923            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5924            ## Ignore the token            ## Ignore the token
5925              !!!nack ('t282.1');
5926            !!!next-token;            !!!next-token;
5927            redo B;            next B;
5928          }          }
5929        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5930              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5931                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5932                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5933                  !!!cp ('t283');              !!!cp ('t283');
5934                  ## As if </option>              ## As if </option>
5935                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5936                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5937                  !!!cp ('t284');              !!!cp ('t284');
5938                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5939                } else {            } else {
5940                  !!!cp ('t285');              !!!cp ('t285');
5941                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5942                  ## Ignore the token              ## Ignore the token
5943                }            }
5944                !!!next-token;            !!!nack ('t285.1');
5945                redo B;            !!!next-token;
5946              } elsif ($token->{tag_name} eq 'option') {            next B;
5947                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5948                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5949                  pop @{$self->{open_elements}};              !!!cp ('t286');
5950                } else {              pop @{$self->{open_elements}};
5951                  !!!cp ('t287');            } else {
5952                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5953                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5954                }              ## Ignore the token
5955                !!!next-token;            }
5956                redo B;            !!!nack ('t287.1');
5957              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5958                ## have an element in table scope            next B;
5959                my $i;          } elsif ($token->{tag_name} eq 'select') {
5960                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5961                  my $node = $self->{open_elements}->[$_];            my $i;
5962                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5963                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5964                    $i = $_;              if ($node->[1] & SELECT_EL) {
5965                    last INSCOPE;                !!!cp ('t288');
5966                  } elsif ({                $i = $_;
5967                            table => 1, html => 1,                last INSCOPE;
5968                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5969                    !!!cp ('t289');                !!!cp ('t289');
5970                    last INSCOPE;                last INSCOPE;
5971                  }              }
5972                } # INSCOPE            } # INSCOPE
5973                unless (defined $i) {            unless (defined $i) {
5974                  !!!cp ('t290');              !!!cp ('t290');
5975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5976                  ## Ignore the token              ## Ignore the token
5977                  !!!next-token;              !!!nack ('t290.1');
5978                  redo B;              !!!next-token;
5979                }              next B;
5980              }
5981                                
5982                !!!cp ('t291');            !!!cp ('t291');
5983                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5984    
5985                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5986    
5987                !!!next-token;            !!!nack ('t291.1');
5988                redo B;            !!!next-token;
5989              } elsif ({            next B;
5990                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5991                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5992                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5993                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5994                     }->{$token->{tag_name}}) {
5995    ## TODO: The following is wrong?
5996              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5997                                
5998                ## have an element in table scope            ## have an element in table scope
5999                my $i;            my $i;
6000                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6001                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6002                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6003                    !!!cp ('t292');                !!!cp ('t292');
6004                    $i = $_;                $i = $_;
6005                    last INSCOPE;                last INSCOPE;
6006                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6007                            table => 1, html => 1,                !!!cp ('t293');
6008                           }->{$node->[1]}) {                last INSCOPE;
6009                    !!!cp ('t293');              }
6010                    last INSCOPE;            } # INSCOPE
6011                  }            unless (defined $i) {
6012                } # INSCOPE              !!!cp ('t294');
6013                unless (defined $i) {              ## Ignore the token
6014                  !!!cp ('t294');              !!!nack ('t294.1');
6015                  ## Ignore the token              !!!next-token;
6016                  !!!next-token;              next B;
6017                  redo B;            }
               }  
6018                                
6019                ## As if </select>            ## As if </select>
6020                ## have an element in table scope            ## have an element in table scope
6021                undef $i;            undef $i;
6022                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6023                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6024                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6025                    !!!cp ('t295');                !!!cp ('t295');
6026                    $i = $_;                $i = $_;
6027                    last INSCOPE;                last INSCOPE;
6028                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6029                            table => 1, html => 1,  ## ISSUE: Can this state be reached?
6030                           }->{$node->[1]}) {                !!!cp ('t296');
6031                    !!!cp ('t296');                last INSCOPE;
6032                    last INSCOPE;              }
6033                  }            } # INSCOPE
6034                } # INSCOPE            unless (defined $i) {
6035                unless (defined $i) {              !!!cp ('t297');
6036                  !!!cp ('t297');  ## TODO: The following error type is correct?
6037                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6038                  ## Ignore the </select> token              ## Ignore the </select> token
6039                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6040                  redo B;              !!!next-token; ## TODO: ok?
6041                }              next B;
6042              }
6043                                
6044                !!!cp ('t298');            !!!cp ('t298');
6045                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6046    
6047                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6048    
6049                ## reprocess            !!!ack-later;
6050                redo B;            ## reprocess
6051              next B;
6052          } else {          } else {
6053            !!!cp ('t299');            !!!cp ('t299');
6054            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6055            ## Ignore the token            ## Ignore the token
6056              !!!nack ('t299.3');
6057            !!!next-token;            !!!next-token;
6058            redo B;            next B;
6059            }
6060          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6061            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6062                    @{$self->{open_elements}} == 1) { # redundant, maybe
6063              !!!cp ('t299.1');
6064              !!!parse-error (type => 'in body:#eof', token => $token);
6065            } else {
6066              !!!cp ('t299.2');
6067          }          }
6068    
6069            ## Stop parsing.
6070            last B;
6071        } else {        } else {
6072          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6073        }        }
# Line 5085  sub _tree_construction_main ($) { Line 6083  sub _tree_construction_main ($) {
6083            unless (length $token->{data}) {            unless (length $token->{data}) {
6084              !!!cp ('t300');              !!!cp ('t300');
6085              !!!next-token;              !!!next-token;
6086              redo B;              next B;
6087            }            }
6088          }          }
6089                    
6090          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6091            !!!cp ('t301');            !!!cp ('t301');
6092            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6093    
6094            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6095          } else {          } else {
6096            !!!cp ('t302');            !!!cp ('t302');
6097          }          }
6098                    
6099          ## "after body" insertion mode          ## "after body" insertion mode
6100          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6101    
6102          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6103          ## reprocess          ## reprocess
6104          redo B;          next B;
6105        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6106          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6107            !!!cp ('t303');            !!!cp ('t303');
6108            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6109                        
6110            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6111          } else {          } else {
6112            !!!cp ('t304');            !!!cp ('t304');
6113          }          }
6114    
6115          ## "after body" insertion mode          ## "after body" insertion mode
6116          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6117    
6118          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6119            !!!ack-later;
6120          ## reprocess          ## reprocess
6121          redo B;          next B;
6122        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6123          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6124            !!!cp ('t305');            !!!cp ('t305');
6125            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126                        
6127            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6128            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6129          } else {          } else {
6130            !!!cp ('t306');            !!!cp ('t306');
6131          }          }
# Line 5135  sub _tree_construction_main ($) { Line 6134  sub _tree_construction_main ($) {
6134          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6135            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6136              !!!cp ('t307');              !!!cp ('t307');
6137              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6138              ## Ignore the token              ## Ignore the token
6139              !!!next-token;              !!!next-token;
6140              redo B;              next B;
6141            } else {            } else {
6142              !!!cp ('t308');              !!!cp ('t308');
6143              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6144              !!!next-token;              !!!next-token;
6145              redo B;              next B;
6146            }            }
6147          } else {          } else {
6148            !!!cp ('t309');            !!!cp ('t309');
6149            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6150    
6151            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6152            ## reprocess            ## reprocess
6153            redo B;            next B;
6154          }          }
6155          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156            !!!cp ('t309.2');
6157            ## Stop parsing
6158            last B;
6159        } else {        } else {
6160          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6161        }        }
# Line 5164  sub _tree_construction_main ($) { Line 6167  sub _tree_construction_main ($) {
6167            unless (length $token->{data}) {            unless (length $token->{data}) {
6168              !!!cp ('t310');              !!!cp ('t310');
6169              !!!next-token;              !!!next-token;
6170              redo B;              next B;
6171            }            }
6172          }          }
6173                    
6174          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6175            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6176              !!!cp ('t311');              !!!cp ('t311');
6177              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6178            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6179              !!!cp ('t312');              !!!cp ('t312');
6180              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6181            } else { # "after html frameset"            } else { # "after html frameset"
6182              !!!cp ('t313');              !!!cp ('t313');
6183              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6184    
6185              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6186              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6187              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6188            }            }
6189                        
6190            ## Ignore the token.            ## Ignore the token.
# Line 5192  sub _tree_construction_main ($) { Line 6195  sub _tree_construction_main ($) {
6195              !!!cp ('t315');              !!!cp ('t315');
6196              !!!next-token;              !!!next-token;
6197            }            }
6198            redo B;            next B;
6199          }          }
6200                    
6201          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6202        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6203          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6204            !!!cp ('t316');            !!!cp ('t316');
6205            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6206    
6207            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6208            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6209          } else {          } else {
6210            !!!cp ('t317');            !!!cp ('t317');
6211          }          }
# Line 5210  sub _tree_construction_main ($) { Line 6213  sub _tree_construction_main ($) {
6213          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6214              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6215            !!!cp ('t318');            !!!cp ('t318');
6216            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217              !!!nack ('t318.1');
6218            !!!next-token;            !!!next-token;
6219            redo B;            next B;
6220          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6221                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6222            !!!cp ('t319');            !!!cp ('t319');
6223            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6224            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6225              !!!ack ('t319.1');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6229            !!!cp ('t320');            !!!cp ('t320');
6230            ## NOTE: As if in body.            ## NOTE: As if in body.
6231            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6232            redo B;            next B;
6233          } else {          } else {
6234            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6235              !!!cp ('t321');              !!!cp ('t321');
6236              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6237            } else {            } else {
6238              !!!cp ('t322');              !!!cp ('t322');
6239              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6240            }            }
6241            ## Ignore the token            ## Ignore the token
6242              !!!nack ('t322.1');
6243            !!!next-token;            !!!next-token;
6244            redo B;            next B;
6245          }          }
6246        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6247          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6248            !!!cp ('t323');            !!!cp ('t323');
6249            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6250    
6251            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6252            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6253          } else {          } else {
6254            !!!cp ('t324');            !!!cp ('t324');
6255          }          }
6256    
6257          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6258              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6259            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6260                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6261              !!!cp ('t325');              !!!cp ('t325');
6262              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6263              ## Ignore the token              ## Ignore the token
6264              !!!next-token;              !!!next-token;
6265            } else {            } else {
# Line 5263  sub _tree_construction_main ($) { Line 6269  sub _tree_construction_main ($) {
6269            }            }
6270    
6271            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6272                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6273              !!!cp ('t327');              !!!cp ('t327');
6274              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275            } else {            } else {
6276              !!!cp ('t328');              !!!cp ('t328');
6277            }            }
6278            redo B;            next B;
6279          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6280                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6281            !!!cp ('t329');            !!!cp ('t329');
6282            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6283            !!!next-token;            !!!next-token;
6284            redo B;            next B;
6285          } else {          } else {
6286            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6287              !!!cp ('t330');              !!!cp ('t330');
6288              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6289            } else {            } else {
6290              !!!cp ('t331');              !!!cp ('t331');
6291              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6292            }            }
6293            ## Ignore the token            ## Ignore the token
6294            !!!next-token;            !!!next-token;
6295            redo B;            next B;
6296          }          }
6297          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6298            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6299                    @{$self->{open_elements}} == 1) { # redundant, maybe
6300              !!!cp ('t331.1');
6301              !!!parse-error (type => 'in body:#eof', token => $token);
6302            } else {
6303              !!!cp ('t331.2');
6304            }
6305            
6306            ## Stop parsing
6307            last B;
6308        } else {        } else {
6309          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6310        }        }
# Line 5302  sub _tree_construction_main ($) { Line 6319  sub _tree_construction_main ($) {
6319        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6320          !!!cp ('t332');          !!!cp ('t332');
6321          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6322          $script_start_tag->($insert);          $script_start_tag->();
6323          redo B;          next B;
6324        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6325          !!!cp ('t333');          !!!cp ('t333');
6326          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6327          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6328          redo B;          next B;
6329        } elsif ({        } elsif ({
6330                  base => 1, link => 1,                  base => 1, link => 1,
6331                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6332          !!!cp ('t334');          !!!cp ('t334');
6333          ## 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
6334          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6336            !!!ack ('t334.1');
6337          !!!next-token;          !!!next-token;
6338          redo B;          next B;
6339        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6340          ## 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
6341          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6342          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.
6343    
6344          unless ($self->{confident}) {          unless ($self->{confident}) {
6345            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6346              !!!cp ('t335');              !!!cp ('t335');
6347                ## NOTE: Whether the encoding is supported or not is handled
6348                ## in the {change_encoding} callback.
6349              $self->{change_encoding}              $self->{change_encoding}
6350                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6351                            
6352              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6353                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6354                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6355                                           ->{has_reference});                                           ->{has_reference});
6356            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6357              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6358                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6359                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6360                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6361                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6362                !!!cp ('t336');                !!!cp ('t336');
6363                  ## NOTE: Whether the encoding is supported or not is handled
6364                  ## in the {change_encoding} callback.
6365                $self->{change_encoding}                $self->{change_encoding}
6366                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6367                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6368                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6369                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5366  sub _tree_construction_main ($) { Line 6387  sub _tree_construction_main ($) {
6387            }            }
6388          }          }
6389    
6390            !!!ack ('t338.1');
6391          !!!next-token;          !!!next-token;
6392          redo B;          next B;
6393        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6394          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6395          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6396          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6397            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6398        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6399          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6400                                
6401          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6402              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6403            !!!cp ('t342');            !!!cp ('t342');
6404            ## Ignore the token            ## Ignore the token
6405          } else {          } else {
# Line 5400  sub _tree_construction_main ($) { Line 6413  sub _tree_construction_main ($) {
6413              }              }
6414            }            }
6415          }          }
6416            !!!nack ('t343.1');
6417          !!!next-token;          !!!next-token;
6418          redo B;          next B;
6419        } elsif ({        } elsif ({
6420                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6421                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6422                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6423                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6424                  pre => 1,                  pre => 1, listing => 1,
6425                    form => 1,
6426                    table => 1,
6427                    hr => 1,
6428                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6429            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6430              !!!cp ('t350');
6431              !!!parse-error (type => 'in form:form', token => $token);
6432              ## Ignore the token
6433              !!!nack ('t350.1');
6434              !!!next-token;
6435              next B;
6436            }
6437    
6438          ## has a p element in scope          ## has a p element in scope
6439          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6440            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6441              !!!cp ('t344');              !!!cp ('t344');
6442              !!!back-token;              !!!back-token; # <form>
6443              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6444              redo B;                        line => $token->{line}, column => $token->{column}};
6445            } elsif ({              next B;
6446                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6447              !!!cp ('t345');              !!!cp ('t345');
6448              last INSCOPE;              last INSCOPE;
6449            }            }
6450          } # INSCOPE          } # INSCOPE
6451                        
6452          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6453          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6454              !!!nack ('t346.1');
6455            !!!next-token;            !!!next-token;
6456            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6457              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5438  sub _tree_construction_main ($) { Line 6464  sub _tree_construction_main ($) {
6464            } else {            } else {
6465              !!!cp ('t348');              !!!cp ('t348');
6466            }            }
6467          } else {          } elsif ($token->{tag_name} eq 'form') {
6468            !!!cp ('t347');            !!!cp ('t347.1');
6469              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6470    
6471              !!!nack ('t347.2');
6472            !!!next-token;            !!!next-token;
6473          }          } elsif ($token->{tag_name} eq 'table') {
6474          redo B;            !!!cp ('t382');
6475        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6476          if (defined $self->{form_element}) {            
6477            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6478            !!!parse-error (type => 'in form:form');  
6479            ## Ignore the token            !!!nack ('t382.1');
6480              !!!next-token;
6481            } elsif ($token->{tag_name} eq 'hr') {
6482              !!!cp ('t386');
6483              pop @{$self->{open_elements}};
6484            
6485              !!!nack ('t386.1');
6486            !!!next-token;            !!!next-token;
           redo B;  
6487          } else {          } else {
6488            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6489            !!!next-token;            !!!next-token;
           redo B;  
6490          }          }
6491        } elsif ($token->{tag_name} eq 'li') {          next B;
6492          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6493          ## has a p element in scope          ## has a p element in scope
6494          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6495            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6496              !!!cp ('t353');              !!!cp ('t353');
6497              !!!back-token;              !!!back-token; # <x>
6498              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6499              redo B;                        line => $token->{line}, column => $token->{column}};
6500            } elsif ({              next B;
6501                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6502              !!!cp ('t354');              !!!cp ('t354');
6503              last INSCOPE;              last INSCOPE;
6504            }            }
# Line 5492  sub _tree_construction_main ($) { Line 6507  sub _tree_construction_main ($) {
6507          ## Step 1          ## Step 1
6508          my $i = -1;          my $i = -1;
6509          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6510            my $li_or_dtdd = {li => {li => 1},
6511                              dt => {dt => 1, dd => 1},
6512                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6513          LI: {          LI: {
6514            ## Step 2            ## Step 2
6515            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6516              if ($i != -1) {              if ($i != -1) {
6517                !!!cp ('t355');                !!!cp ('t355');
6518                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6519                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6520                                      ->manakai_local_name,
6521                                  token => $token);
6522              } else {              } else {
6523                !!!cp ('t356');                !!!cp ('t356');
6524              }              }
# Line 5509  sub _tree_construction_main ($) { Line 6529  sub _tree_construction_main ($) {
6529            }            }
6530                        
6531            ## Step 3            ## Step 3
6532            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6533                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6534                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6535                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6536                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6537                  not ($node->[1] & DIV_EL)) {
6538              !!!cp ('t358');              !!!cp ('t358');
6539              last LI;              last LI;
6540            }            }
# Line 5525  sub _tree_construction_main ($) { Line 6546  sub _tree_construction_main ($) {
6546            redo LI;            redo LI;
6547          } # LI          } # LI
6548                        
6549          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6550          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6551          !!!next-token;          !!!next-token;
6552          redo B;          next B;
6553        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6554          ## has a p element in scope          ## has a p element in scope
6555          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6556            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6557              !!!cp ('t367');              !!!cp ('t367');
6558              !!!back-token;              !!!back-token; # <plaintext>
6559              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6560              redo B;                        line => $token->{line}, column => $token->{column}};
6561            } elsif ({              next B;
6562                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6563              !!!cp ('t368');              !!!cp ('t368');
6564              last INSCOPE;              last INSCOPE;
6565            }            }
6566          } # INSCOPE          } # INSCOPE
6567                        
6568          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6569                        
6570          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6571                        
6572            !!!nack ('t368.1');
6573          !!!next-token;          !!!next-token;
6574          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6575        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6576          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6577            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6578            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6579              !!!cp ('t371');              !!!cp ('t371');
6580              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6581                            
6582              !!!back-token;              !!!back-token; # <a>
6583              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6584              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6585                $formatting_end_tag->($token);
6586                            
6587              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6588                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5688  sub _tree_construction_main ($) { Line 6607  sub _tree_construction_main ($) {
6607                        
6608          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6609    
6610          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6611          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6612    
6613            !!!nack ('t374.1');
6614          !!!next-token;          !!!next-token;
6615          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;  
6616        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6617          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6618    
6619          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6620          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6621            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6622            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6623              !!!cp ('t376');              !!!cp ('t376');
6624              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6625              !!!back-token;              !!!back-token; # <nobr>
6626              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6627              redo B;                        line => $token->{line}, column => $token->{column}};
6628            } elsif ({              next B;
6629                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6630              !!!cp ('t377');              !!!cp ('t377');
6631              last INSCOPE;              last INSCOPE;
6632            }            }
6633          } # INSCOPE          } # INSCOPE
6634                    
6635          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6636          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6637                    
6638            !!!nack ('t377.1');
6639          !!!next-token;          !!!next-token;
6640          redo B;          next B;
6641        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6642          ## has a button element in scope          ## has a button element in scope
6643          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6645            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6646              !!!cp ('t378');              !!!cp ('t378');
6647              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6648              !!!back-token;              !!!back-token; # <button>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6650              redo B;                        line => $token->{line}, column => $token->{column}};
6651            } elsif ({              next B;
6652                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6653              !!!cp ('t379');              !!!cp ('t379');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
# Line 5753  sub _tree_construction_main ($) { Line 6657  sub _tree_construction_main ($) {
6657                        
6658          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6659                        
6660          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661          push @$active_formatting_elements, ['#marker', ''];  
6662            ## TODO: associate with $self->{form_element} if defined
6663    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6664          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6665            
6666          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6667          !!!next-token;          !!!next-token;
6668          redo B;          next B;
6669        } elsif ({        } elsif ({
6670                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6671                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6672                  image => 1,                  noembed => 1,
6673                    noframes => 1,
6674                    noscript => 0, ## TODO: 1 if scripting is enabled
6675                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6676          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6677            !!!cp ('t384');            !!!cp ('t381');
6678            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6679          } else {          } else {
6680            !!!cp ('t385');            !!!cp ('t399');
6681          }          }
6682            ## NOTE: There is an "as if in body" code clone.
6683          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6684          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6685        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6686          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6687                    
6688          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6689            !!!cp ('t389');            !!!cp ('t389');
6690            ## Ignore the token            ## Ignore the token
6691              !!!nack ('t389'); ## NOTE: Not acknowledged.
6692            !!!next-token;            !!!next-token;
6693            redo B;            next B;
6694          } else {          } else {
6695            my $at = $token->{attributes};            my $at = $token->{attributes};
6696            my $form_attrs;            my $form_attrs;
# Line 5867  sub _tree_construction_main ($) { Line 6701  sub _tree_construction_main ($) {
6701            delete $at->{prompt};            delete $at->{prompt};
6702            my @tokens = (            my @tokens = (
6703                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6704                           attributes => $form_attrs},                           attributes => $form_attrs,
6705                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6706                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6707                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6708                            {type => START_TAG_TOKEN, tag_name => 'p',
6709                             line => $token->{line}, column => $token->{column}},
6710                            {type => START_TAG_TOKEN, tag_name => 'label',
6711                             line => $token->{line}, column => $token->{column}},
6712                         );                         );
6713            if ($prompt_attr) {            if ($prompt_attr) {
6714              !!!cp ('t390');              !!!cp ('t390');
6715              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6716                               #line => $token->{line}, column => $token->{column},
6717                              };
6718            } else {            } else {
6719              !!!cp ('t391');              !!!cp ('t391');
6720              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6721                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6722                               #line => $token->{line}, column => $token->{column},
6723                              }; # SHOULD
6724              ## TODO: make this configurable              ## TODO: make this configurable
6725            }            }
6726            push @tokens,            push @tokens,
6727                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6728                             line => $token->{line}, column => $token->{column}},
6729                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6730                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6731                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6732                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6733                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6734            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6735                             line => $token->{line}, column => $token->{column}},
6736                            {type => END_TAG_TOKEN, tag_name => 'form',
6737                             line => $token->{line}, column => $token->{column}};
6738              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6739            !!!back-token (@tokens);            !!!back-token (@tokens);
6740            redo B;            !!!next-token;
6741              next B;
6742          }          }
6743        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6744          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6745          my $el;          my $el;
6746          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6747                    
6748          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6749          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5904  sub _tree_construction_main ($) { Line 6752  sub _tree_construction_main ($) {
6752          $insert->($el);          $insert->($el);
6753                    
6754          my $text = '';          my $text = '';
6755            !!!nack ('t392.1');
6756          !!!next-token;          !!!next-token;
6757          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6758            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5934  sub _tree_construction_main ($) { Line 6783  sub _tree_construction_main ($) {
6783            ## Ignore the token            ## Ignore the token
6784          } else {          } else {
6785            !!!cp ('t398');            !!!cp ('t398');
6786            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6787          }          }
6788          !!!next-token;          !!!next-token;
6789          redo B;          next B;
6790        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6791                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6792          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6793    
6794            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6795    
6796            ## "adjust foreign attributes" - done in insert-element-f
6797                    
6798          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6799                    
6800          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6801              pop @{$self->{open_elements}};
6802              !!!ack ('t398.1');
6803            } else {
6804              !!!cp ('t398.2');
6805              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6806              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6807              ## mode, "in body" (not "in foreign content") secondary insertion
6808              ## mode, maybe.
6809            }
6810    
6811          !!!next-token;          !!!next-token;
6812          redo B;          next B;
6813        } elsif ({        } elsif ({
6814                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6815                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5964  sub _tree_construction_main ($) { Line 6817  sub _tree_construction_main ($) {
6817                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6818                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6819          !!!cp ('t401');          !!!cp ('t401');
6820          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6821          ## Ignore the token          ## Ignore the token
6822            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6823          !!!next-token;          !!!next-token;
6824          redo B;          next B;
6825                    
6826          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6827        } else {        } else {
6828          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6829              !!!cp ('t384');
6830              !!!parse-error (type => 'image', token => $token);
6831              $token->{tag_name} = 'img';
6832            } else {
6833              !!!cp ('t385');
6834            }
6835    
6836            ## NOTE: There is an "as if <br>" code clone.
6837          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6838                    
6839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6840    
6841            if ({
6842                 applet => 1, marquee => 1, object => 1,
6843                }->{$token->{tag_name}}) {
6844              !!!cp ('t380');
6845              push @$active_formatting_elements, ['#marker', ''];
6846              !!!nack ('t380.1');
6847            } elsif ({
6848                      b => 1, big => 1, em => 1, font => 1, i => 1,
6849                      s => 1, small => 1, strile => 1,
6850                      strong => 1, tt => 1, u => 1,
6851                     }->{$token->{tag_name}}) {
6852              !!!cp ('t375');
6853              push @$active_formatting_elements, $self->{open_elements}->[-1];
6854              !!!nack ('t375.1');
6855            } elsif ($token->{tag_name} eq 'input') {
6856              !!!cp ('t388');
6857              ## TODO: associate with $self->{form_element} if defined
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t388.2');
6860            } elsif ({
6861                      area => 1, basefont => 1, bgsound => 1, br => 1,
6862                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6863                      #image => 1,
6864                     }->{$token->{tag_name}}) {
6865              !!!cp ('t388.1');
6866              pop @{$self->{open_elements}};
6867              !!!ack ('t388.3');
6868            } elsif ($token->{tag_name} eq 'select') {
6869              ## TODO: associate with $self->{form_element} if defined
6870            
6871              if ($self->{insertion_mode} & TABLE_IMS or
6872                  $self->{insertion_mode} & BODY_TABLE_IMS or
6873                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6874                !!!cp ('t400.1');
6875                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6876              } else {
6877                !!!cp ('t400.2');
6878                $self->{insertion_mode} = IN_SELECT_IM;
6879              }
6880              !!!nack ('t400.3');
6881            } else {
6882              !!!nack ('t402');
6883            }
6884                    
6885          !!!next-token;          !!!next-token;
6886          redo B;          next B;
6887        }        }
6888      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6889        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6890          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6891              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6892            for (@{$self->{open_elements}}) {          INSCOPE: {
6893              unless ({            for (reverse @{$self->{open_elements}}) {
6894                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6895                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6896                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6897                      }->{$_->[1]}) {                last INSCOPE;
6898                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6899                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6900              } else {                last;
               !!!cp ('t404');  
6901              }              }
6902            }            }
6903    
6904            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6905            !!!next-token;                            value => $token->{tag_name}, token => $token);
6906            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6907            !!!next-token;            !!!next-token;
6908            redo B;            next B;
6909            } # INSCOPE
6910    
6911            for (@{$self->{open_elements}}) {
6912              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6913                !!!cp ('t403');
6914                !!!parse-error (type => 'not closed',
6915                                value => $_->[0]->manakai_local_name,
6916                                token => $token);
6917                last;
6918              } else {
6919                !!!cp ('t404');
6920              }
6921          }          }
6922    
6923            $self->{insertion_mode} = AFTER_BODY_IM;
6924            !!!next-token;
6925            next B;
6926        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6927          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
6928            ## up-to-date, though it has same effect as speced.
6929            if (@{$self->{open_elements}} > 1 and
6930                $self->{open_elements}->[1]->[1] & BODY_EL) {
6931            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6932            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6933              !!!cp ('t406');              !!!cp ('t406');
6934              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6935                                value => $self->{open_elements}->[1]->[0]
6936                                    ->manakai_local_name,
6937                                token => $token);
6938            } else {            } else {
6939              !!!cp ('t407');              !!!cp ('t407');
6940            }            }
6941            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6942            ## reprocess            ## reprocess
6943            redo B;            next B;
6944          } else {          } else {
6945            !!!cp ('t408');            !!!cp ('t408');
6946            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6947            ## Ignore the token            ## Ignore the token
6948            !!!next-token;            !!!next-token;
6949            redo B;            next B;
6950          }          }
6951        } elsif ({        } elsif ({
6952                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6953                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6954                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6955                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6956                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6957                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6958          ## has an element in scope          ## has an element in scope
6959          my $i;          my $i;
6960          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6962            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6963              !!!cp ('t410');              !!!cp ('t410');
6964              $i = $_;              $i = $_;
6965              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6966            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6967              !!!cp ('t411');              !!!cp ('t411');
6968              last INSCOPE;              last INSCOPE;
6969            }            }
6970          } # INSCOPE          } # INSCOPE
6971            
6972          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6973            if (defined $i) {            !!!cp ('t413');
6974              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6975            } else {
6976              ## Step 1. generate implied end tags
6977              while ({
6978                      dd => ($token->{tag_name} ne 'dd'),
6979                      dt => ($token->{tag_name} ne 'dt'),
6980                      li => ($token->{tag_name} ne 'li'),
6981                      p => 1,
6982                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6983                !!!cp ('t409');
6984                pop @{$self->{open_elements}};
6985              }
6986    
6987              ## Step 2.
6988              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6989                      ne $token->{tag_name}) {
6990              !!!cp ('t412');              !!!cp ('t412');
6991              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[-1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995            } else {            } else {
6996              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6997            }            }
6998          }  
6999                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7000            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7001          } elsif ($token->{tag_name} eq 'p') {  
7002            !!!cp ('t415');            ## Step 4.
7003            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7004            my $el;                if {
7005            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7006            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7007          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7011            undef $self->{form_element};
7012    
7013          ## has an element in scope          ## has an element in scope
7014            my $i;
7015          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7017            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7018              !!!cp ('t418');              !!!cp ('t418');
7019                $i = $_;
7020              last INSCOPE;              last INSCOPE;
7021            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7022              !!!cp ('t419');              !!!cp ('t419');
7023              last INSCOPE;              last INSCOPE;
7024            }            }
7025          } # INSCOPE          } # INSCOPE
7026            
7027          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
7028            !!!cp ('t421');            !!!cp ('t421');
7029            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7030            } else {
7031              ## Step 1. generate implied end tags
7032              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7033                !!!cp ('t417');
7034                pop @{$self->{open_elements}};
7035              }
7036              
7037              ## Step 2.
7038              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7039                      ne $token->{tag_name}) {
7040                !!!cp ('t417.1');
7041                !!!parse-error (type => 'not closed',
7042                                value => $self->{open_elements}->[-1]->[0]
7043                                    ->manakai_local_name,
7044                                token => $token);
7045              } else {
7046                !!!cp ('t420');
7047              }  
7048              
7049              ## Step 3.
7050              splice @{$self->{open_elements}}, $i;
7051          }          }
7052    
         undef $self->{form_element};  
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055        } elsif ({        } elsif ({
7056                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7057                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6141  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059          my $i;          my $i;
7060          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7062            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
7063              !!!cp ('t423');              !!!cp ('t423');
7064              $i = $_;              $i = $_;
7065              last INSCOPE;              last INSCOPE;
7066            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7067              !!!cp ('t424');              !!!cp ('t424');
7068              last INSCOPE;              last INSCOPE;
7069            }            }
7070          } # INSCOPE          } # INSCOPE
7071    
7072            unless (defined $i) { # has an element in scope
7073              !!!cp ('t425.1');
7074              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7075            } else {
7076              ## Step 1. generate implied end tags
7077              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7078                !!!cp ('t422');
7079                pop @{$self->{open_elements}};
7080              }
7081              
7082              ## Step 2.
7083              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7084                      ne $token->{tag_name}) {
7085                !!!cp ('t425');
7086                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7087              } else {
7088                !!!cp ('t426');
7089              }
7090    
7091              ## Step 3.
7092              splice @{$self->{open_elements}}, $i;
7093            }
7094                    
7095          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7096            !!!cp ('t425');          next B;
7097            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7098            ## has an element in scope
7099            my $i;
7100            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7101              my $node = $self->{open_elements}->[$_];
7102              if ($node->[1] & P_EL) {
7103                !!!cp ('t410.1');
7104                $i = $_;
7105                last INSCOPE;
7106              } elsif ($node->[1] & SCOPING_EL) {
7107                !!!cp ('t411.1');
7108                last INSCOPE;
7109              }
7110            } # INSCOPE
7111    
7112            if (defined $i) {
7113              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7114                      ne $token->{tag_name}) {
7115                !!!cp ('t412.1');
7116                !!!parse-error (type => 'not closed',
7117                                value => $self->{open_elements}->[-1]->[0]
7118                                    ->manakai_local_name,
7119                                token => $token);
7120              } else {
7121                !!!cp ('t414.1');
7122              }
7123    
7124              splice @{$self->{open_elements}}, $i;
7125          } else {          } else {
7126            !!!cp ('t426');            !!!cp ('t413.1');
7127              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7128    
7129              !!!cp ('t415.1');
7130              ## As if <p>, then reprocess the current token
7131              my $el;
7132              !!!create-element ($el, $HTML_NS, 'p',, $token);
7133              $insert->($el);
7134              ## NOTE: Not inserted into |$self->{open_elements}|.
7135          }          }
7136            
         splice @{$self->{open_elements}}, $i if defined $i;  
7137          !!!next-token;          !!!next-token;
7138          redo B;          next B;
7139        } elsif ({        } elsif ({
7140                  a => 1,                  a => 1,
7141                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6186  sub _tree_construction_main ($) { Line 7143  sub _tree_construction_main ($) {
7143                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7145          !!!cp ('t427');          !!!cp ('t427');
7146          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7147          redo B;          next B;
7148        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7149          !!!cp ('t428');          !!!cp ('t428');
7150          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7151    
7152          ## As if <br>          ## As if <br>
7153          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7154                    
7155          my $el;          my $el;
7156          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7157          $insert->($el);          $insert->($el);
7158                    
7159          ## Ignore the token.          ## Ignore the token.
7160          !!!next-token;          !!!next-token;
7161          redo B;          next B;
7162        } elsif ({        } elsif ({
7163                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7164                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6215  sub _tree_construction_main ($) { Line 7172  sub _tree_construction_main ($) {
7172                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7173                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7174          !!!cp ('t429');          !!!cp ('t429');
7175          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7176          ## Ignore the token          ## Ignore the token
7177          !!!next-token;          !!!next-token;
7178          redo B;          next B;
7179                    
7180          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7181                    
# Line 6229  sub _tree_construction_main ($) { Line 7186  sub _tree_construction_main ($) {
7186    
7187          ## Step 2          ## Step 2
7188          S2: {          S2: {
7189            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7190              ## Step 1              ## Step 1
7191              ## generate implied end tags              ## generate implied end tags
7192              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7193                !!!cp ('t430');                !!!cp ('t430');
7194                !!!back-token;                ## ISSUE: Can this case be reached?
7195                $token = {type => END_TAG_TOKEN,                pop @{$self->{open_elements}};
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7196              }              }
7197                    
7198              ## Step 2              ## Step 2
7199              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7200                        ne $token->{tag_name}) {
7201                !!!cp ('t431');                !!!cp ('t431');
7202                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7203                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7204                                  value => $self->{open_elements}->[-1]->[0]
7205                                      ->manakai_local_name,
7206                                  token => $token);
7207              } else {              } else {
7208                !!!cp ('t432');                !!!cp ('t432');
7209              }              }
# Line 6260  sub _tree_construction_main ($) { Line 7215  sub _tree_construction_main ($) {
7215              last S2;              last S2;
7216            } else {            } else {
7217              ## Step 3              ## Step 3
7218              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7219                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7220                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7221                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7222                !!!cp ('t433');                !!!cp ('t433');
7223                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7224                ## Ignore the token                ## Ignore the token
7225                !!!next-token;                !!!next-token;
7226                last S2;                last S2;
# Line 6281  sub _tree_construction_main ($) { Line 7236  sub _tree_construction_main ($) {
7236            ## Step 5;            ## Step 5;
7237            redo S2;            redo S2;
7238          } # S2          } # S2
7239          redo B;          next B;
7240        }        }
7241      }      }
7242      redo B;      next B;
7243      } continue { # B
7244        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7245          ## NOTE: The code below is executed in cases where it does not have
7246          ## to be, but it it is harmless even in those cases.
7247          ## has an element in scope
7248          INSCOPE: {
7249            for (reverse 0..$#{$self->{open_elements}}) {
7250              my $node = $self->{open_elements}->[$_];
7251              if ($node->[1] & FOREIGN_EL) {
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                last;
7255              }
7256            }
7257            
7258            ## NOTE: No foreign element in scope.
7259            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7260          } # INSCOPE
7261        }
7262    } # B    } # B
7263    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7264    ## Stop parsing # MUST    ## Stop parsing # MUST
7265        
7266    ## TODO: script stuffs    ## TODO: script stuffs
# Line 6333  sub set_inner_html ($$$) { Line 7302  sub set_inner_html ($$$) {
7302      my $p = $class->new;      my $p = $class->new;
7303      $p->{document} = $doc;      $p->{document} = $doc;
7304    
7305      ## Step 9 # MUST      ## Step 8 # MUST
7306      my $i = 0;      my $i = 0;
7307      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7308      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7309      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7310        my $self = shift;        my $self = shift;
7311    
# Line 6345  sub set_inner_html ($$$) { Line 7314  sub set_inner_html ($$$) {
7314    
7315        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7316        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7317        $column++;  
7318          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7319          $p->{column}++;
7320    
7321        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7322          $line++;          $p->{line}++;
7323          $column = 0;          $p->{column} = 0;
7324          !!!cp ('i1');          !!!cp ('i1');
7325        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7326          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7327          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7328          $line++;          $p->{line}++;
7329          $column = 0;          $p->{column} = 0;
7330          !!!cp ('i2');          !!!cp ('i2');
7331        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7332          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6364  sub set_inner_html ($$$) { Line 7335  sub set_inner_html ($$$) {
7335          !!!cp ('i4');          !!!cp ('i4');
7336          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7337          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7338          } elsif ($self->{next_char} <= 0x0008 or
7339                   (0x000E <= $self->{next_char} and
7340                    $self->{next_char} <= 0x001F) or
7341                   (0x007F <= $self->{next_char} and
7342                    $self->{next_char} <= 0x009F) or
7343                   (0xD800 <= $self->{next_char} and
7344                    $self->{next_char} <= 0xDFFF) or
7345                   (0xFDD0 <= $self->{next_char} and
7346                    $self->{next_char} <= 0xFDDF) or
7347                   {
7348                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7349                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7350                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7351                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7352                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7353                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7354                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7355                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7356                    0x10FFFE => 1, 0x10FFFF => 1,
7357                   }->{$self->{next_char}}) {
7358            !!!cp ('i4.1');
7359            !!!parse-error (type => 'control char', level => $self->{must_level});
7360    ## TODO: error type documentation
7361        }        }
7362      };      };
7363      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6371  sub set_inner_html ($$$) { Line 7365  sub set_inner_html ($$$) {
7365            
7366      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7367        my (%opt) = @_;        my (%opt) = @_;
7368        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7369          my $column = $opt{column};
7370          if (defined $opt{token} and defined $opt{token}->{line}) {
7371            $line = $opt{token}->{line};
7372            $column = $opt{token}->{column};
7373          }
7374          warn "Parse error ($opt{type}) at line $line column $column\n";
7375      };      };
7376      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7377        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7378      };      };
7379            
7380      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6398  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          unless defined $p->{content_model};          unless defined $p->{content_model};
7399          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7400    
7401      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7402          ## TODO: Foreign element OK?
7403    
7404      ## Step 4      ## Step 3
7405      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7406        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7407    
7408      ## Step 5 # MUST      ## Step 4 # MUST
7409      $doc->append_child ($root);      $doc->append_child ($root);
7410    
7411      ## Step 6 # MUST      ## Step 5 # MUST
7412      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7413    
7414      undef $p->{head_element};      undef $p->{head_element};
7415    
7416      ## Step 7 # MUST      ## Step 6 # MUST
7417      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7418    
7419      ## Step 8 # MUST      ## Step 7 # MUST
7420      my $anode = $node;      my $anode = $node;
7421      AN: while (defined $anode) {      AN: while (defined $anode) {
7422        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
# Line 6431  sub set_inner_html ($$$) { Line 7432  sub set_inner_html ($$$) {
7432        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7433      } # AN      } # AN
7434            
7435      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7436      {      {
7437        my $self = $p;        my $self = $p;
7438        !!!next-token;        !!!next-token;
7439      }      }
7440      $p->_tree_construction_main;      $p->_tree_construction_main;
7441    
7442      ## Step 11 # MUST      ## Step 10 # MUST
7443      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7444      for (@cn) {      for (@cn) {
7445        $node->remove_child ($_);        $node->remove_child ($_);
7446      }      }
7447      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7448    
7449      ## Step 12 # MUST      ## Step 11 # MUST
7450      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7451      for (@cn) {      for (@cn) {
7452        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 6455  sub set_inner_html ($$$) { Line 7455  sub set_inner_html ($$$) {
7455      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7456    
7457      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7458    
7459        delete $p->{parse_error}; # delete loop
7460    } else {    } else {
7461      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";
7462    }    }

Legend:
Removed from v.1.82  
changed lines
  Added in v.1.145

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24