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

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

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

revision 1.86 by wakaba, Thu Mar 6 15:56:52 2008 UTC revision 1.150 by wakaba, Sun Jun 1 06:47:08 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      attributename => 'attributeName',
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      filterres => 'filterRes',
226      filterunits => 'filterUnits',
227      glyphref => 'glyphRef',
228      gradienttransform => 'gradientTransform',
229      gradientunits => 'gradientUnits',
230      kernelmatrix => 'kernelMatrix',
231      kernelunitlength => 'kernelUnitLength',
232      keypoints => 'keyPoints',
233      keysplines => 'keySplines',
234      keytimes => 'keyTimes',
235      lengthadjust => 'lengthAdjust',
236      limitingconeangle => 'limitingConeAngle',
237      markerheight => 'markerHeight',
238      markerunits => 'markerUnits',
239      markerwidth => 'markerWidth',
240      maskcontentunits => 'maskContentUnits',
241      maskunits => 'maskUnits',
242      numoctaves => 'numOctaves',
243      pathlength => 'pathLength',
244      patterncontentunits => 'patternContentUnits',
245      patterntransform => 'patternTransform',
246      patternunits => 'patternUnits',
247      pointsatx => 'pointsAtX',
248      pointsaty => 'pointsAtY',
249      pointsatz => 'pointsAtZ',
250      preservealpha => 'preserveAlpha',
251      preserveaspectratio => 'preserveAspectRatio',
252      primitiveunits => 'primitiveUnits',
253      refx => 'refX',
254      refy => 'refY',
255      repeatcount => 'repeatCount',
256      repeatdur => 'repeatDur',
257      requiredextensions => 'requiredExtensions',
258      requiredfeatures => 'requiredFeatures',
259      specularconstant => 'specularConstant',
260      specularexponent => 'specularExponent',
261      spreadmethod => 'spreadMethod',
262      startoffset => 'startOffset',
263      stddeviation => 'stdDeviation',
264      stitchtiles => 'stitchTiles',
265      surfacescale => 'surfaceScale',
266      systemlanguage => 'systemLanguage',
267      tablevalues => 'tableValues',
268      targetx => 'targetX',
269      targety => 'targetY',
270      textlength => 'textLength',
271      viewbox => 'viewBox',
272      viewtarget => 'viewTarget',
273      xchannelselector => 'xChannelSelector',
274      ychannelselector => 'yChannelSelector',
275      zoomandpan => 'zoomAndPan',
276    };
277    
278    my $foreign_attr_xname = {
279      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
280      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
281      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
282      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
283      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
284      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
285      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
286      'xml:base' => [$XML_NS, ['xml', 'base']],
287      'xml:lang' => [$XML_NS, ['xml', 'lang']],
288      'xml:space' => [$XML_NS, ['xml', 'space']],
289      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
290      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
291  };  };
292    
293    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
294    
295  my $c1_entity_char = {  my $c1_entity_char = {
296    0x80 => 0x20AC,    0x80 => 0x20AC,
297    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 327  my $c1_entity_char = {
327    0x9F => 0x0178,    0x9F => 0x0178,
328  }; # $c1_entity_char  }; # $c1_entity_char
329    
 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  
   
330  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
331      my $self = shift;
332      my $charset_name = shift;
333      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
334      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
335    } # parse_byte_string
336    
337    sub parse_byte_stream ($$$$;$) {
338    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
339    my $charset = shift;    my $charset_name = shift;
340    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;  
   }  
341    
342    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
343      my $self = shift;      my (%opt) = @_;
344      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
345      ## TODO: if $charset is supported    };
346      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
347    
348      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
349      require Message::Charset::Info;
350      ## Step 1        my $charset;
351      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
352        $charset = 'utf-8';    my ($char_stream, $e_status);
353    
354      SNIFFING: {
355    
356        ## Step 1
357        if (defined $charset_name) {
358          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
359    
360          ## ISSUE: Unsupported encoding is not ignored according to the spec.
361          ($char_stream, $e_status) = $charset->get_decode_handle
362              ($byte_stream, allow_error_reporting => 1,
363               allow_fallback => 1);
364          if ($char_stream) {
365            $self->{confident} = 1;
366            last SNIFFING;
367          } else {
368            ## TODO: unsupported error
369          }
370      }      }
371    
372      ## Step 2      ## Step 2
373      if (defined $self->{input_encoding} and      my $byte_buffer = '';
374          $self->{input_encoding} eq $charset) {      for (1..1024) {
375          my $char = $byte_stream->getc;
376          last unless defined $char;
377          $byte_buffer .= $char;
378        } ## TODO: timeout
379    
380        ## Step 3
381        if ($byte_buffer =~ /^\xFE\xFF/) {
382          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
383          ($char_stream, $e_status) = $charset->get_decode_handle
384              ($byte_stream, allow_error_reporting => 1,
385               allow_fallback => 1, byte_buffer => \$byte_buffer);
386        $self->{confident} = 1;        $self->{confident} = 1;
387        return;        last SNIFFING;
388        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
389          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
390          ($char_stream, $e_status) = $charset->get_decode_handle
391              ($byte_stream, allow_error_reporting => 1,
392               allow_fallback => 1, byte_buffer => \$byte_buffer);
393          $self->{confident} = 1;
394          last SNIFFING;
395        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
396          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
397          ($char_stream, $e_status) = $charset->get_decode_handle
398              ($byte_stream, allow_error_reporting => 1,
399               allow_fallback => 1, byte_buffer => \$byte_buffer);
400          $self->{confident} = 1;
401          last SNIFFING;
402      }      }
403    
404      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
405          ':'.$charset, level => 'w');      ## TODO: <meta charset>
406    
407      ## Step 3      ## Step 5
408      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
409    
410      ## Step 4      ## Step 6
411      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
412        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
413            ($byte_buffer);
414        if (defined $charset_name) {
415          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
416    
417          ## ISSUE: Unsupported encoding is not ignored according to the spec.
418          require Whatpm::Charset::DecodeHandle;
419          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
420              ($byte_stream);
421          ($char_stream, $e_status) = $charset->get_decode_handle
422              ($buffer, allow_error_reporting => 1,
423               allow_fallback => 1, byte_buffer => \$byte_buffer);
424          if ($char_stream) {
425            $buffer->{buffer} = $byte_buffer;
426            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
427                            value => $charset_name,
428                            level => $self->{info_level},
429                            line => 1, column => 1);
430            $self->{confident} = 0;
431            last SNIFFING;
432          }
433        }
434    
435        ## Step 7: default
436        ## TODO: Make this configurable.
437        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
438            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
439            ## detectable in the step 6.
440        require Whatpm::Charset::DecodeHandle;
441        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
442            ($byte_stream);
443        ($char_stream, $e_status)
444            = $charset->get_decode_handle ($buffer,
445                                           allow_error_reporting => 1,
446                                           allow_fallback => 1,
447                                           byte_buffer => \$byte_buffer);
448        $buffer->{buffer} = $byte_buffer;
449        !!!parse-error (type => 'sniffing:default', ## TODO: type name
450                        value => 'windows-1252',
451                        level => $self->{info_level},
452                        line => 1, column => 1);
453        $self->{confident} = 0;
454      } # SNIFFING
455    
456      $self->{input_encoding} = $charset->get_iana_name;
457      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
458        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
459                        value => $self->{input_encoding},
460                        level => $self->{unsupported_level},
461                        line => 1, column => 1);
462      } elsif (not ($e_status &
463                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
464        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      }
469    
470      $self->{change_encoding} = sub {
471        my $self = shift;
472        $charset_name = shift;
473        my $token = shift;
474    
475        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
476        ($char_stream, $e_status) = $charset->get_decode_handle
477            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
478             byte_buffer => \ $buffer->{buffer});
479        
480        if ($char_stream) { # if supported
481          ## "Change the encoding" algorithm:
482    
483          ## Step 1    
484          if ($charset->{category} &
485              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
486            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
487            ($char_stream, $e_status) = $charset->get_decode_handle
488                ($byte_stream,
489                 byte_buffer => \ $buffer->{buffer});
490          }
491          $charset_name = $charset->get_iana_name;
492          
493          ## Step 2
494          if (defined $self->{input_encoding} and
495              $self->{input_encoding} eq $charset_name) {
496            !!!parse-error (type => 'charset label:matching', ## TODO: type
497                            value => $charset_name,
498                            level => $self->{info_level});
499            $self->{confident} = 1;
500            return;
501          }
502    
503          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
504              ':'.$charset_name, level => 'w', token => $token);
505          
506          ## Step 3
507          # if (can) {
508            ## change the encoding on the fly.
509            #$self->{confident} = 1;
510            #return;
511          # }
512          
513          ## Step 4
514          throw Whatpm::HTML::RestartParser ();
515        }
516    }; # $self->{change_encoding}    }; # $self->{change_encoding}
517    
518      my $char_onerror = sub {
519        my (undef, $type, %opt) = @_;
520        !!!parse-error (%opt, type => $type,
521                        line => $self->{line}, column => $self->{column} + 1);
522        if ($opt{octets}) {
523          ${$opt{octets}} = "\x{FFFD}"; # relacement character
524        }
525      };
526      $char_stream->onerror ($char_onerror);
527    
528    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
529    my $return;    my $return;
530    try {    try {
531      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
532    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
533      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
534      $s = \ (Encode::decode ($charset, $$bytes_s));      
535      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
536        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
537          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
538                          value => $self->{input_encoding},
539                          level => $self->{unsupported_level},
540                          line => 1, column => 1);
541        } elsif (not ($e_status &
542                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
543          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
544                          value => $self->{input_encoding},
545                          level => $self->{unsupported_level},
546                          line => 1, column => 1);
547        }
548      $self->{confident} = 1;      $self->{confident} = 1;
549      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
550        $return = $self->parse_char_stream ($char_stream, @args);
551    };    };
552    return $return;    return $return;
553  } # parse_byte_string  } # parse_byte_stream
554    
555  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
556  ## 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 561  sub parse_byte_string ($$$$;$) {
561  ## 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
562  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
563    
564  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
565      my $self = shift;
566      require utf8;
567      my $s = ref $_[0] ? $_[0] : \($_[0]);
568      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
569      return $self->parse_char_stream ($input, @_[1..$#_]);
570    } # parse_char_string
571    *parse_string = \&parse_char_string;
572    
573  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
574    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
575    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
576    $self->{document} = $_[1];    $self->{document} = $_[1];
577    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
578    
# Line 177  sub parse_string ($$$;$) { Line 583  sub parse_string ($$$;$) {
583        if defined $self->{input_encoding};        if defined $self->{input_encoding};
584    
585    my $i = 0;    my $i = 0;
586    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
587    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
588    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
589      my $self = shift;      my $self = shift;
590    
591      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
592      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
593    
594      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
595      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
596      $column++;        $char = $self->{next_next_char};
597          delete $self->{next_next_char};
598        } else {
599          $char = $input->getc;
600        }
601        $self->{next_char} = -1 and return unless defined $char;
602        $self->{next_char} = ord $char;
603    
604        ($self->{line_prev}, $self->{column_prev})
605            = ($self->{line}, $self->{column});
606        $self->{column}++;
607            
608      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
609        $line++;        !!!cp ('j1');
610        $column = 0;        $self->{line}++;
611          $self->{column} = 0;
612      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
613        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
614          my $next = $input->getc;
615          if (defined $next and $next ne "\x0A") {
616            $self->{next_next_char} = $next;
617          }
618        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
619        $line++;        $self->{line}++;
620        $column = 0;        $self->{column} = 0;
621      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
622          !!!cp ('j3');
623        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
624      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
625          !!!cp ('j4');
626        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
627        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
628        } elsif ($self->{next_char} <= 0x0008 or
629                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
630                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
631                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
632                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
633                 {
634                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
635                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
636                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
637                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
638                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
639                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
640                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
641                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
642                  0x10FFFE => 1, 0x10FFFF => 1,
643                 }->{$self->{next_char}}) {
644          !!!cp ('j5');
645          !!!parse-error (type => 'control char', level => $self->{must_level});
646    ## TODO: error type documentation
647      }      }
648    };    };
649    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 651  sub parse_string ($$$;$) {
651    
652    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
653      my (%opt) = @_;      my (%opt) = @_;
654      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
655        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
656        warn "Parse error ($opt{type}) at line $line column $column\n";
657    };    };
658    $self->{parse_error} = sub {    $self->{parse_error} = sub {
659      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
660    };    };
661    
662    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 664  sub parse_string ($$$;$) {
664    $self->_construct_tree;    $self->_construct_tree;
665    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
666    
667      delete $self->{parse_error}; # remove loop
668    
669    return $self->{document};    return $self->{document};
670  } # parse_string  } # parse_char_stream
671    
672  sub new ($) {  sub new ($) {
673    my $class = shift;    my $class = shift;
674    my $self = bless {}, $class;    my $self = bless {
675        must_level => 'm',
676        should_level => 's',
677        good_level => 'w',
678        warn_level => 'w',
679        info_level => 'i',
680        unsupported_level => 'u',
681      }, $class;
682    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
683      $self->{next_char} = -1;      $self->{next_char} = -1;
684    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 740  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
740  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
741  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
742  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
743    sub SELF_CLOSING_START_TAG_STATE () { 34 }
744    sub CDATA_BLOCK_STATE () { 35 }
745    
746  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
747  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 758  sub TABLE_IMS ()      { 0b1000000 }
758  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
759  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
760  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
761    sub SELECT_IMS ()     { 0b10000000000 }
762    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
763        ## NOTE: "in foreign content" insertion mode is special; it is combined
764        ## with the secondary insertion mode.  In this parser, they are stored
765        ## together in the bit-or'ed form.
766    
767  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
768    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 785  sub IN_TABLE_IM () { TABLE_IMS }
785  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
786  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
787  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
788  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
789    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
790  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
791    
792  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 799  sub _initialize_tokenizer ($) {
799    undef $self->{current_attribute};    undef $self->{current_attribute};
800    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
801    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
802      delete $self->{self_closing};
803    $self->{char} = [];    $self->{char} = [];
804    # $self->{next_char}    # $self->{next_char}
805    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 820  sub _initialize_tokenizer ($) {
820  ##        ->{value}  ##        ->{value}
821  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
822  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
823    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
824    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
825    ##     while the token is pushed back to the stack.
826    
827  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
828    
# Line 384  sub _initialize_tokenizer ($) { Line 849  sub _initialize_tokenizer ($) {
849    
850  sub _get_next_token ($) {  sub _get_next_token ($) {
851    my $self = shift;    my $self = shift;
852    
853      if ($self->{self_closing}) {
854        !!!parse-error (type => 'nestc', token => $self->{current_token});
855        ## NOTE: The |self_closing| flag is only set by start tag token.
856        ## In addition, when a start tag token is emitted, it is always set to
857        ## |current_token|.
858        delete $self->{self_closing};
859      }
860    
861    if (@{$self->{token}}) {    if (@{$self->{token}}) {
862        $self->{self_closing} = $self->{token}->[0]->{self_closing};
863      return shift @{$self->{token}};      return shift @{$self->{token}};
864    }    }
865    
# Line 447  sub _get_next_token ($) { Line 922  sub _get_next_token ($) {
922          #          #
923        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
924          !!!cp (11);          !!!cp (11);
925          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
926                      line => $self->{line}, column => $self->{column}});
927          last A; ## TODO: ok?          last A; ## TODO: ok?
928        } else {        } else {
929          !!!cp (12);          !!!cp (12);
930        }        }
931        # Anything else        # Anything else
932        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
933                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
934                       line => $self->{line}, column => $self->{column},
935                      };
936        ## Stay in the data state        ## Stay in the data state
937        !!!next-input-character;        !!!next-input-character;
938    
# Line 463  sub _get_next_token ($) { Line 941  sub _get_next_token ($) {
941        redo A;        redo A;
942      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
943        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
944    
945          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
946                
947        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
948    
# Line 471  sub _get_next_token ($) { Line 951  sub _get_next_token ($) {
951    
952        unless (defined $token) {        unless (defined $token) {
953          !!!cp (13);          !!!cp (13);
954          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
955                      line => $l, column => $c,
956                     });
957        } else {        } else {
958          !!!cp (14);          !!!cp (14);
959          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 972  sub _get_next_token ($) {
972            ## reconsume            ## reconsume
973            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
974    
975            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
976                        line => $self->{line_prev},
977                        column => $self->{column_prev},
978                       });
979    
980            redo A;            redo A;
981          }          }
# Line 510  sub _get_next_token ($) { Line 995  sub _get_next_token ($) {
995            !!!cp (19);            !!!cp (19);
996            $self->{current_token}            $self->{current_token}
997              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
998                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
999                   line => $self->{line_prev},
1000                   column => $self->{column_prev}};
1001            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1002            !!!next-input-character;            !!!next-input-character;
1003            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1005  sub _get_next_token ($) {
1005                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1006            !!!cp (20);            !!!cp (20);
1007            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1008                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1009                                        line => $self->{line_prev},
1010                                        column => $self->{column_prev}};
1011            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1012            !!!next-input-character;            !!!next-input-character;
1013            redo A;            redo A;
1014          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1015            !!!cp (21);            !!!cp (21);
1016            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1017                              line => $self->{line_prev},
1018                              column => $self->{column_prev});
1019            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1020            !!!next-input-character;            !!!next-input-character;
1021    
1022            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1023                        line => $self->{line_prev},
1024                        column => $self->{column_prev},
1025                       });
1026    
1027            redo A;            redo A;
1028          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1029            !!!cp (22);            !!!cp (22);
1030            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1031                              line => $self->{line_prev},
1032                              column => $self->{column_prev});
1033            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1034              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1035                                        line => $self->{line_prev},
1036                                        column => $self->{column_prev},
1037                                       };
1038            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1039            redo A;            redo A;
1040          } else {          } else {
1041            !!!cp (23);            !!!cp (23);
1042            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1043                              line => $self->{line_prev},
1044                              column => $self->{column_prev});
1045            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1046            ## reconsume            ## reconsume
1047    
1048            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1049                        line => $self->{line_prev},
1050                        column => $self->{column_prev},
1051                       });
1052    
1053            redo A;            redo A;
1054          }          }
# Line 551  sub _get_next_token ($) { Line 1056  sub _get_next_token ($) {
1056          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1057        }        }
1058      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1059          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1060        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1061          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1062    
1063            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1064            my @next_char;            my @next_char;
1065            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 569  sub _get_next_token ($) { Line 1076  sub _get_next_token ($) {
1076                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1077                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1078    
1079                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1080                            line => $l, column => $c,
1081                           });
1082        
1083                redo A;                redo A;
1084              }              }
# Line 588  sub _get_next_token ($) { Line 1097  sub _get_next_token ($) {
1097              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1098              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1099              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1100              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1101                          line => $l, column => $c,
1102                         });
1103              redo A;              redo A;
1104            } else {            } else {
1105              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112            !!!cp (28);            !!!cp (28);
1113            # next-input-character is already done            # next-input-character is already done
1114            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1115            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1116                        line => $l, column => $c,
1117                       });
1118            redo A;            redo A;
1119          }          }
1120        }        }
# Line 609  sub _get_next_token ($) { Line 1122  sub _get_next_token ($) {
1122        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1123            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1124          !!!cp (29);          !!!cp (29);
1125          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1126                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1127                   tag_name => chr ($self->{next_char} + 0x0020),
1128                   line => $l, column => $c};
1129          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1130          !!!next-input-character;          !!!next-input-character;
1131          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1133  sub _get_next_token ($) {
1133                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1134          !!!cp (30);          !!!cp (30);
1135          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1136                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1137                                      line => $l, column => $c};
1138          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
1141        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1142          !!!cp (31);          !!!cp (31);
1143          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1144                            line => $self->{line_prev}, ## "<" in "</>"
1145                            column => $self->{column_prev} - 1);
1146          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1147          !!!next-input-character;          !!!next-input-character;
1148          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1152  sub _get_next_token ($) {
1152          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1153          # reconsume          # reconsume
1154    
1155          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1156                      line => $l, column => $c,
1157                     });
1158    
1159          redo A;          redo A;
1160        } else {        } else {
1161          !!!cp (33);          !!!cp (33);
1162          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1163          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1164            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1165                                      line => $self->{line_prev}, # "<" of "</"
1166                                      column => $self->{column_prev} - 1,
1167                                     };
1168          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1169          redo A;          redo A;
1170        }        }
# Line 657  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1182          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1183            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1184            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1185          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1186            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1212  sub _get_next_token ($) {
1212          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1213          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1214            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1215            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1216          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1217            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232    
1233          redo A;          redo A;
1234        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1235            !!!cp (42);
1236            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1237          !!!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  
1238          redo A;          redo A;
1239        } else {        } else {
1240          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1258          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1259            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1260            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1261          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1262            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1278  sub _get_next_token ($) {
1278        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1279                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1280          !!!cp (49);          !!!cp (49);
1281          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1282                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1283                   value => '',
1284                   line => $self->{line}, column => $self->{column}};
1285          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1286          !!!next-input-character;          !!!next-input-character;
1287          redo A;          redo A;
1288        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1289            !!!cp (50);
1290            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1291          !!!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  
1292          redo A;          redo A;
1293        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1294          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1295          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1296            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1297            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1298          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1299            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323          } else {          } else {
1324            !!!cp (56);            !!!cp (56);
1325          }          }
1326          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1327                                value => ''};              = {name => chr ($self->{next_char}),
1328                   value => '',
1329                   line => $self->{line}, column => $self->{column}};
1330          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1336  sub _get_next_token ($) {
1336          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1337              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1338            !!!cp (57);            !!!cp (57);
1339            !!!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});
1340            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1341          } else {          } else {
1342            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1365  sub _get_next_token ($) {
1365          $before_leave->();          $before_leave->();
1366          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1367            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1368            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1369          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1370            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1389  sub _get_next_token ($) {
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1392            !!!cp (64);
1393          $before_leave->();          $before_leave->();
1394            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1395          !!!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  
1396          redo A;          redo A;
1397        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1398          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1399          $before_leave->();          $before_leave->();
1400          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1401            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1402            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1403          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1404            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1449  sub _get_next_token ($) {
1449        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1450          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1451            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1452            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1453          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1454            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1471  sub _get_next_token ($) {
1471        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1472                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1473          !!!cp (76);          !!!cp (76);
1474          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1475                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1476                   value => '',
1477                   line => $self->{line}, column => $self->{column}};
1478          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1482            !!!cp (77);
1483            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1484          !!!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  
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1487          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1488          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1489            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1490            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1491          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1492            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1508  sub _get_next_token ($) {
1508          redo A;          redo A;
1509        } else {        } else {
1510          !!!cp (82);          !!!cp (82);
1511          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1512                                value => ''};              = {name => chr ($self->{next_char}),
1513                   value => '',
1514                   line => $self->{line}, column => $self->{column}};
1515          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1567  sub _get_next_token ($) {
1567          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1568          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1569            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1570            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1571          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1572            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1615          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1616            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1618          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1619            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1656  sub _get_next_token ($) {
1656          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1657          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1658            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1659            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1660          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1661            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1702          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1703            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1705          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1706            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1724  sub _get_next_token ($) {
1724          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1725          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1726            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1727            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1728          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1729            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1794  sub _get_next_token ($) {
1794        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1795          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1796            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1797            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1798          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1799            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1814  sub _get_next_token ($) {
1814    
1815          redo A;          redo A;
1816        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1817            !!!cp (122);
1818            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1819          !!!next-input-character;          !!!next-input-character;
1820          if ($self->{next_char} == 0x003E and # >          redo A;
1821              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1822              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1823            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1824            !!!cp (122);            !!!cp (122.3);
1825            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1826            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1827              if ($self->{current_token}->{attributes}) {
1828                !!!cp (122.1);
1829                !!!parse-error (type => 'end tag attribute');
1830              } else {
1831                ## NOTE: This state should never be reached.
1832                !!!cp (122.2);
1833              }
1834          } else {          } else {
1835            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1836          }          }
1837          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1838          # next-input-character is already done          ## Reconsume.
1839            !!!emit ($self->{current_token}); # start tag or end tag
1840          redo A;          redo A;
1841        } else {        } else {
1842          !!!cp (124);          !!!cp ('124.1');
1843          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1844          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1845          ## reconsume          ## reconsume
1846          redo A;          redo A;
1847        }        }
1848        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1849          if ($self->{next_char} == 0x003E) { # >
1850            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1851              !!!cp ('124.2');
1852              !!!parse-error (type => 'nestc', token => $self->{current_token});
1853              ## TODO: Different type than slash in start tag
1854              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1855              if ($self->{current_token}->{attributes}) {
1856                !!!cp ('124.4');
1857                !!!parse-error (type => 'end tag attribute');
1858              } else {
1859                !!!cp ('124.5');
1860              }
1861              ## TODO: Test |<title></title/>|
1862            } else {
1863              !!!cp ('124.3');
1864              $self->{self_closing} = 1;
1865            }
1866    
1867            $self->{state} = DATA_STATE;
1868            !!!next-input-character;
1869    
1870            !!!emit ($self->{current_token}); # start tag or end tag
1871    
1872            redo A;
1873          } elsif ($self->{next_char} == -1) {
1874            !!!parse-error (type => 'unclosed tag');
1875            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1876              !!!cp (124.7);
1877              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1878            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1879              if ($self->{current_token}->{attributes}) {
1880                !!!cp (124.5);
1881                !!!parse-error (type => 'end tag attribute');
1882              } else {
1883                ## NOTE: This state should never be reached.
1884                !!!cp (124.6);
1885              }
1886            } else {
1887              die "$0: $self->{current_token}->{type}: Unknown token type";
1888            }
1889            $self->{state} = DATA_STATE;
1890            ## Reconsume.
1891            !!!emit ($self->{current_token}); # start tag or end tag
1892            redo A;
1893          } else {
1894            !!!cp ('124.4');
1895            !!!parse-error (type => 'nestc');
1896            ## TODO: This error type is wrong.
1897            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1898            ## Reconsume.
1899            redo A;
1900          }
1901      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1902        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1903                
1904        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1905          #my $token = {type => COMMENT_TOKEN, data => ''};
1906    
1907        BC: {        BC: {
1908          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1910  sub _get_next_token ($) {
1910            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1911            !!!next-input-character;            !!!next-input-character;
1912    
1913            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1914    
1915            redo A;            redo A;
1916          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1918  sub _get_next_token ($) {
1918            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1919            ## reconsume            ## reconsume
1920    
1921            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1922    
1923            redo A;            redo A;
1924          } else {          } else {
1925            !!!cp (126);            !!!cp (126);
1926            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1927            !!!next-input-character;            !!!next-input-character;
1928            redo BC;            redo BC;
1929          }          }
# Line 1408  sub _get_next_token ($) { Line 1933  sub _get_next_token ($) {
1933      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1934        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1935    
1936          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1937    
1938        my @next_char;        my @next_char;
1939        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1940                
# Line 1416  sub _get_next_token ($) { Line 1943  sub _get_next_token ($) {
1943          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1944          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1945            !!!cp (127);            !!!cp (127);
1946            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1947                                        line => $l, column => $c,
1948                                       };
1949            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1950            !!!next-input-character;            !!!next-input-character;
1951            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1981  sub _get_next_token ($) {
1981                      !!!cp (129);                      !!!cp (129);
1982                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1983                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1984                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1985                                                  quirks => 1,
1986                                                  line => $l, column => $c,
1987                                                 };
1988                      !!!next-input-character;                      !!!next-input-character;
1989                      redo A;                      redo A;
1990                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 2005  sub _get_next_token ($) {
2005          } else {          } else {
2006            !!!cp (135);            !!!cp (135);
2007          }          }
2008          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2009                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2010                   $self->{next_char} == 0x005B) { # [
2011            !!!next-input-character;
2012            push @next_char, $self->{next_char};
2013            if ($self->{next_char} == 0x0043) { # C
2014              !!!next-input-character;
2015              push @next_char, $self->{next_char};
2016              if ($self->{next_char} == 0x0044) { # D
2017                !!!next-input-character;
2018                push @next_char, $self->{next_char};
2019                if ($self->{next_char} == 0x0041) { # A
2020                  !!!next-input-character;
2021                  push @next_char, $self->{next_char};
2022                  if ($self->{next_char} == 0x0054) { # T
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} == 0x005B) { # [
2029                        !!!cp (135.1);
2030                        $self->{state} = CDATA_BLOCK_STATE;
2031                        !!!next-input-character;
2032                        redo A;
2033                      } else {
2034                        !!!cp (135.2);
2035                      }
2036                    } else {
2037                      !!!cp (135.3);
2038                    }
2039                  } else {
2040                    !!!cp (135.4);                
2041                  }
2042                } else {
2043                  !!!cp (135.5);
2044                }
2045              } else {
2046                !!!cp (135.6);
2047              }
2048            } else {
2049              !!!cp (135.7);
2050            }
2051        } else {        } else {
2052          !!!cp (136);          !!!cp (136);
2053        }        }
# Line 1480  sub _get_next_token ($) { Line 2056  sub _get_next_token ($) {
2056        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2057        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2058        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2059          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2060                                    line => $l, column => $c,
2061                                   };
2062        redo A;        redo A;
2063                
2064        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2182  sub _get_next_token ($) {
2182          redo A;          redo A;
2183        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2184          !!!cp (152);          !!!cp (152);
2185          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2186                            line => $self->{line_prev},
2187                            column => $self->{column_prev});
2188          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2189          ## Stay in the state          ## Stay in the state
2190          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2200  sub _get_next_token ($) {
2200          redo A;          redo A;
2201        } else {        } else {
2202          !!!cp (154);          !!!cp (154);
2203          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2204                            line => $self->{line_prev},
2205                            column => $self->{column_prev});
2206          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2207          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2208          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2241  sub _get_next_token ($) {
2241          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2242          !!!next-input-character;          !!!next-input-character;
2243    
2244          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2245    
2246          redo A;          redo A;
2247        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2250  sub _get_next_token ($) {
2250          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2251          ## reconsume          ## reconsume
2252    
2253          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2254    
2255          redo A;          redo A;
2256        } else {        } else {
2257          !!!cp (160);          !!!cp (160);
2258          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2259              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2260  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2261          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2262          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2731  sub _get_next_token ($) {
2731        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2732          !!!cp (217);          !!!cp (217);
2733          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2734          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2735          ## reconsume          ## reconsume
2736    
# Line 2192  sub _get_next_token ($) { Line 2771  sub _get_next_token ($) {
2771          !!!next-input-character;          !!!next-input-character;
2772          redo A;          redo A;
2773        }        }
2774        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2775          my $s = '';
2776          
2777          my ($l, $c) = ($self->{line}, $self->{column});
2778    
2779          CS: while ($self->{next_char} != -1) {
2780            if ($self->{next_char} == 0x005D) { # ]
2781              !!!next-input-character;
2782              if ($self->{next_char} == 0x005D) { # ]
2783                !!!next-input-character;
2784                MDC: {
2785                  if ($self->{next_char} == 0x003E) { # >
2786                    !!!cp (221.1);
2787                    !!!next-input-character;
2788                    last CS;
2789                  } elsif ($self->{next_char} == 0x005D) { # ]
2790                    !!!cp (221.2);
2791                    $s .= ']';
2792                    !!!next-input-character;
2793                    redo MDC;
2794                  } else {
2795                    !!!cp (221.3);
2796                    $s .= ']]';
2797                    #
2798                  }
2799                } # MDC
2800              } else {
2801                !!!cp (221.4);
2802                $s .= ']';
2803                #
2804              }
2805            } else {
2806              !!!cp (221.5);
2807              #
2808            }
2809            $s .= chr $self->{next_char};
2810            !!!next-input-character;
2811          } # CS
2812    
2813          $self->{state} = DATA_STATE;
2814          ## next-input-character done or EOF, which is reconsumed.
2815    
2816          if (length $s) {
2817            !!!cp (221.6);
2818            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2819                      line => $l, column => $c});
2820          } else {
2821            !!!cp (221.7);
2822          }
2823    
2824          redo A;
2825    
2826          ## ISSUE: "text tokens" in spec.
2827          ## TODO: Streaming support
2828      } else {      } else {
2829        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2830      }      }
# Line 2203  sub _get_next_token ($) { Line 2836  sub _get_next_token ($) {
2836  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2837    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2838    
2839      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2840    
2841    if ({    if ({
2842         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2843         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2878  sub _tokenize_attempt_to_consume_an_enti
2878            redo X;            redo X;
2879          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2880            !!!cp (1005);            !!!cp (1005);
2881            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2882            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2883            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2884            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2887  sub _tokenize_attempt_to_consume_an_enti
2887            !!!next-input-character;            !!!next-input-character;
2888          } else {          } else {
2889            !!!cp (1007);            !!!cp (1007);
2890            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2891          }          }
2892    
2893          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2894            !!!cp (1008);            !!!cp (1008);
2895            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2896            $code = 0xFFFD;            $code = 0xFFFD;
2897          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2898            !!!cp (1009);            !!!cp (1009);
2899            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2900            $code = 0xFFFD;            $code = 0xFFFD;
2901          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2902            !!!cp (1010);            !!!cp (1010);
2903            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2904            $code = 0x000A;            $code = 0x000A;
2905          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2906            !!!cp (1011);            !!!cp (1011);
2907            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2908            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2909          }          }
2910    
2911          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2912                  has_reference => 1};                  has_reference => 1,
2913                    line => $l, column => $c,
2914                   };
2915        } # X        } # X
2916      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2917               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2932  sub _tokenize_attempt_to_consume_an_enti
2932          !!!next-input-character;          !!!next-input-character;
2933        } else {        } else {
2934          !!!cp (1014);          !!!cp (1014);
2935          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2936        }        }
2937    
2938        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2939          !!!cp (1015);          !!!cp (1015);
2940          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2941          $code = 0xFFFD;          $code = 0xFFFD;
2942        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2943          !!!cp (1016);          !!!cp (1016);
2944          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2945          $code = 0xFFFD;          $code = 0xFFFD;
2946        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2947          !!!cp (1017);          !!!cp (1017);
2948          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2949          $code = 0x000A;          $code = 0x000A;
2950        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2951          !!!cp (1018);          !!!cp (1018);
2952          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2953          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2954        }        }
2955                
2956        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2957                  line => $l, column => $c,
2958                 };
2959      } else {      } else {
2960        !!!cp (1019);        !!!cp (1019);
2961        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2962        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2963        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2964        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2975  sub _tokenize_attempt_to_consume_an_enti
2975      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2976      our $EntityChar;      our $EntityChar;
2977    
2978      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2979             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2980             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2981               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 3008  sub _tokenize_attempt_to_consume_an_enti
3008            
3009      if ($match > 0) {      if ($match > 0) {
3010        !!!cp (1023);        !!!cp (1023);
3011        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3012                  line => $l, column => $c,
3013                 };
3014      } elsif ($match < 0) {      } elsif ($match < 0) {
3015        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3016        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3017          !!!cp (1024);          !!!cp (1024);
3018          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3019                    line => $l, column => $c,
3020                   };
3021        } else {        } else {
3022          !!!cp (1025);          !!!cp (1025);
3023          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3024                    line => $l, column => $c,
3025                   };
3026        }        }
3027      } else {      } else {
3028        !!!cp (1026);        !!!cp (1026);
3029        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3030        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3031        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3032                  line => $l, column => $c,
3033                 };
3034      }      }
3035    } else {    } else {
3036      !!!cp (1027);      !!!cp (1027);
3037      ## no characters are consumed      ## no characters are consumed
3038      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3039      return undef;      return undef;
3040    }    }
3041  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 3106  sub _tree_construction_initial ($) {
3106            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3107            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3108          !!!cp ('t1');          !!!cp ('t1');
3109          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3110        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3111          !!!cp ('t2');          !!!cp ('t2');
3112          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3113          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3114        } else {        } else {
3115          !!!cp ('t3');          !!!cp ('t3');
3116        }        }
3117                
3118        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3119          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3120          ## NOTE: Default value for both |public_id| and |system_id| attributes
3121          ## are empty strings, so that we don't set any value in missing cases.
3122        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3123            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3124        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3133  sub _tree_construction_initial ($) {
3133        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3134          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3135          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3136          if ({          my $prefix = [
3137            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3138            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3139            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3140            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3141            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3142            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3143            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3144            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3145            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3146            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3147            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3148            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3149            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3150            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3151            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3152            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3153            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3154            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3155            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3156            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3157            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3158            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3159            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3160            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3161            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3162            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3163            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3164            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3165            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3168            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3169            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3170            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3171            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3172            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3173            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3174            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3175            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3176            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3177            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3178            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3179            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3180            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3181            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3182            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3183            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3184            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3185            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3186            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3187            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3188            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3189            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3190            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3191            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3192            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3193            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3194            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3195            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3196            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3197            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3198            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3199            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3200            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3201            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3202            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3203            "-//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}) {  
3204            !!!cp ('t5');            !!!cp ('t5');
3205            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3206          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3207                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3208            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3209              !!!cp ('t6');              !!!cp ('t6');
3210              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3212  sub _tree_construction_initial ($) {
3212              !!!cp ('t7');              !!!cp ('t7');
3213              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3214            }            }
3215          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3216                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3217            !!!cp ('t8');            !!!cp ('t8');
3218            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3219          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3226  sub _tree_construction_initial ($) {
3226          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3227          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3228          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") {
3229            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3230              ## marked as quirks.
3231            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3232            !!!cp ('t11');            !!!cp ('t11');
3233          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3246  sub _tree_construction_initial ($) {
3246                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3247               }->{$token->{type}}) {               }->{$token->{type}}) {
3248        !!!cp ('t14');        !!!cp ('t14');
3249        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3250        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3251        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3252        ## reprocess        ## reprocess
3253          !!!ack-later;
3254        return;        return;
3255      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3256        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3268  sub _tree_construction_initial ($) {
3268          !!!cp ('t17');          !!!cp ('t17');
3269        }        }
3270    
3271        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3272        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3273        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3274        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3297  sub _tree_construction_root_element ($)
3297    B: {    B: {
3298        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3299          !!!cp ('t19');          !!!cp ('t19');
3300          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3301          ## Ignore the token          ## Ignore the token
3302          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3303          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3331  sub _tree_construction_root_element ($)
3331        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3332          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3333            my $root_element;            my $root_element;
3334            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3335            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3336            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3337                  [$root_element, $el_category->{html}];
3338    
3339            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3340              !!!cp ('t24');              !!!cp ('t24');
3341              $self->{application_cache_selection}              $self->{application_cache_selection}
3342                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3343              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3344                ## According to Hixie (#whatwg 2008-03-19), it should be
3345                ## resolved against the base URI of the document in HTML
3346                ## or xml:base of the element in XHTML.
3347            } else {            } else {
3348              !!!cp ('t25');              !!!cp ('t25');
3349              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3350            }            }
3351    
3352              !!!nack ('t25c');
3353    
3354            !!!next-token;            !!!next-token;
3355            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3356          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3367  sub _tree_construction_root_element ($)
3367          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3368        }        }
3369    
3370      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3371        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3372      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3373      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3374    
3375      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3376    
3377      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3378        !!!ack-later;
3379      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3380    
3381      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3396  sub _reset_insertion_mode ($) {
3396            
3397      ## Step 3      ## Step 3
3398      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"!?  
3399        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3400          $last = 1;          $last = 1;
3401          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3402            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3403                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3404              !!!cp ('t27');          } else {
3405              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3406          }          }
3407        }        }
3408              
3409        ## Step 4..13        ## Step 4..14
3410        my $new_mode = {        my $new_mode;
3411          if ($node->[1] & FOREIGN_EL) {
3412            !!!cp ('t28.1');
3413            ## NOTE: Strictly spaking, the line below only applies to MathML and
3414            ## SVG elements.  Currently the HTML syntax supports only MathML and
3415            ## SVG elements as foreigners.
3416            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3417          } elsif ($node->[1] & TABLE_CELL_EL) {
3418            if ($last) {
3419              !!!cp ('t28.2');
3420              #
3421            } else {
3422              !!!cp ('t28.3');
3423              $new_mode = IN_CELL_IM;
3424            }
3425          } else {
3426            !!!cp ('t28.4');
3427            $new_mode = {
3428                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3429                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3430                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3431                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3432                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3433                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3438  sub _reset_insertion_mode ($) {
3438                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3439                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3440                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3441                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3442          }
3443        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3444                
3445        ## Step 14        ## Step 15
3446        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3447          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3448            !!!cp ('t29');            !!!cp ('t29');
3449            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3457  sub _reset_insertion_mode ($) {
3457          !!!cp ('t31');          !!!cp ('t31');
3458        }        }
3459                
3460        ## Step 15        ## Step 16
3461        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3462                
3463        ## Step 16        ## Step 17
3464        $i--;        $i--;
3465        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3466                
3467        ## Step 17        ## Step 18
3468        redo S3;        redo S3;
3469      } # S3      } # S3
3470    
# Line 2908  sub _tree_construction_main ($) { Line 3568  sub _tree_construction_main ($) {
3568      !!!cp ('t39');      !!!cp ('t39');
3569    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3570    
3571    my $parse_rcdata = sub ($$) {    my $insert;
3572      my ($content_model_flag, $insert) = @_;  
3573      my $parse_rcdata = sub ($) {
3574        my ($content_model_flag) = @_;
3575    
3576      ## Step 1      ## Step 1
3577      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3578      my $el;      my $el;
3579      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3580    
3581      ## Step 2      ## Step 2
3582      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3583    
3584      ## Step 3      ## Step 3
3585      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3587  sub _tree_construction_main ($) {
3587    
3588      ## Step 4      ## Step 4
3589      my $text = '';      my $text = '';
3590        !!!nack ('t40.1');
3591      !!!next-token;      !!!next-token;
3592      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3593        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3610  sub _tree_construction_main ($) {
3610          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3611        !!!cp ('t42');        !!!cp ('t42');
3612        ## 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});  
3613      } else {      } else {
3614        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3615          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3616            !!!cp ('t43');
3617            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3618          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3619            !!!cp ('t44');
3620            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3621          } else {
3622            die "$0: $content_model_flag in parse_rcdata";
3623          }
3624      }      }
3625      !!!next-token;      !!!next-token;
3626    }; # $parse_rcdata    }; # $parse_rcdata
3627    
3628    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3629      my $script_el;      my $script_el;
3630      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3631      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3632    
3633      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3634      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3635            
3636      my $text = '';      my $text = '';
3637        !!!nack ('t45.1');
3638      !!!next-token;      !!!next-token;
3639      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3640        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3654  sub _tree_construction_main ($) {
3654        ## Ignore the token        ## Ignore the token
3655      } else {      } else {
3656        !!!cp ('t48');        !!!cp ('t48');
3657        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3658        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3659        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3660      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3677  sub _tree_construction_main ($) {
3677      !!!next-token;      !!!next-token;
3678    }; # $script_start_tag    }; # $script_start_tag
3679    
3680      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3681      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3682      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3683    
3684    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3685      my $tag_name = shift;      my $end_tag_token = shift;
3686        my $tag_name = $end_tag_token->{tag_name};
3687    
3688        ## NOTE: The adoption agency algorithm (AAA).
3689    
3690      FET: {      FET: {
3691        ## Step 1        ## Step 1
3692        my $formatting_element;        my $formatting_element;
3693        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3694        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3695          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3696              !!!cp ('t52');
3697              last AFE;
3698            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3699                         eq $tag_name) {
3700            !!!cp ('t51');            !!!cp ('t51');
3701            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3702            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3703            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3704          }          }
3705        } # AFE        } # AFE
3706        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3707          !!!cp ('t53');          !!!cp ('t53');
3708          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3709          ## Ignore the token          ## Ignore the token
3710          !!!next-token;          !!!next-token;
3711          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3722  sub _tree_construction_main ($) {
3722              last INSCOPE;              last INSCOPE;
3723            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3724              !!!cp ('t55');              !!!cp ('t55');
3725              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3726                                token => $end_tag_token);
3727              ## Ignore the token              ## Ignore the token
3728              !!!next-token;              !!!next-token;
3729              return;              return;
3730            }            }
3731          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3732            !!!cp ('t56');            !!!cp ('t56');
3733            $in_scope = 0;            $in_scope = 0;
3734          }          }
3735        } # INSCOPE        } # INSCOPE
3736        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3737          !!!cp ('t57');          !!!cp ('t57');
3738          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3739                            token => $end_tag_token);
3740          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3741          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3742          return;          return;
3743        }        }
3744        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3745          !!!cp ('t58');          !!!cp ('t58');
3746          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3747                            value => $self->{open_elements}->[-1]->[0]
3748                                ->manakai_local_name,
3749                            token => $end_tag_token);
3750        }        }
3751                
3752        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3754  sub _tree_construction_main ($) {
3754        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3755        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3756          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3757          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3758              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3759              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3760               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3761            !!!cp ('t59');            !!!cp ('t59');
3762            $furthest_block = $node;            $furthest_block = $node;
3763            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843        } # S7          } # S7  
3844                
3845        ## Step 8        ## Step 8
3846        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3847            my $foster_parent_element;
3848            my $next_sibling;
3849            OE: for (reverse 0..$#{$self->{open_elements}}) {
3850              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3851                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3852                                 if (defined $parent and $parent->node_type == 1) {
3853                                   !!!cp ('t65.1');
3854                                   $foster_parent_element = $parent;
3855                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3856                                 } else {
3857                                   !!!cp ('t65.2');
3858                                   $foster_parent_element
3859                                     = $self->{open_elements}->[$_ - 1]->[0];
3860                                 }
3861                                 last OE;
3862                               }
3863                             } # OE
3864                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3865                               unless defined $foster_parent_element;
3866            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3867            $open_tables->[-1]->[1] = 1; # tainted
3868          } else {
3869            !!!cp ('t65.3');
3870            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3871          }
3872                
3873        ## Step 9        ## Step 9
3874        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914      } # FET      } # FET
3915    }; # $formatting_end_tag    }; # $formatting_end_tag
3916    
3917    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3918      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3919    }; # $insert_to_current    }; # $insert_to_current
3920    
3921    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3922                         my $child = shift;      my $child = shift;
3923                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3924                              table => 1, tbody => 1, tfoot => 1,        # MUST
3925                              thead => 1, tr => 1,        my $foster_parent_element;
3926                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3927                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3928                           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') {  
3929                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3930                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3931                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3943  sub _tree_construction_main ($) {
3943                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3944                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3945                             ($child, $next_sibling);                             ($child, $next_sibling);
3946                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3947                           !!!cp ('t72');      } else {
3948                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3949                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3950        }
3951    }; # $insert_to_foster    }; # $insert_to_foster
3952    
3953    my $insert;    B: while (1) {
   
   B: {  
3954      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3955        !!!cp ('t73');        !!!cp ('t73');
3956        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3957        ## Ignore the token        ## Ignore the token
3958        ## Stay in the phase        ## Stay in the phase
3959        !!!next-token;        !!!next-token;
3960        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3961      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3962               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3963        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3964          !!!cp ('t79');          !!!cp ('t79');
3965          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3966          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3967        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3968          !!!cp ('t80');          !!!cp ('t80');
3969          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3970          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3971        } else {        } else {
3972          !!!cp ('t81');          !!!cp ('t81');
3973        }        }
3974    
3975        !!!cp ('t82');        !!!cp ('t82');
3976        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3977        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3978        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3979          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3984          }          }
3985        }        }
3986          !!!nack ('t84.1');
3987        !!!next-token;        !!!next-token;
3988        redo B;        next B;
3989      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3990        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3991        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 3999  sub _tree_construction_main ($) {
3999          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4000        }        }
4001        !!!next-token;        !!!next-token;
4002        redo B;        next B;
4003      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4004          if ($token->{type} == CHARACTER_TOKEN) {
4005            !!!cp ('t87.1');
4006            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4007            !!!next-token;
4008            next B;
4009          } elsif ($token->{type} == START_TAG_TOKEN) {
4010            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4011                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4012                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4013                ($token->{tag_name} eq 'svg' and
4014                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4015              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4016              !!!cp ('t87.2');
4017              #
4018            } elsif ({
4019                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4020                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4021                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4022                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4023                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4024                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4025                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4026                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4027                     }->{$token->{tag_name}}) {
4028              !!!cp ('t87.2');
4029              !!!parse-error (type => 'not closed',
4030                              value => $self->{open_elements}->[-1]->[0]
4031                                  ->manakai_local_name,
4032                              token => $token);
4033    
4034              pop @{$self->{open_elements}}
4035                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4036    
4037              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4038              ## Reprocess.
4039              next B;
4040            } else {
4041              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4042              my $tag_name = $token->{tag_name};
4043              if ($nsuri eq $SVG_NS) {
4044                $tag_name = {
4045                   altglyph => 'altGlyph',
4046                   altglyphdef => 'altGlyphDef',
4047                   altglyphitem => 'altGlyphItem',
4048                   animatecolor => 'animateColor',
4049                   animatemotion => 'animateMotion',
4050                   animatetransform => 'animateTransform',
4051                   clippath => 'clipPath',
4052                   feblend => 'feBlend',
4053                   fecolormatrix => 'feColorMatrix',
4054                   fecomponenttransfer => 'feComponentTransfer',
4055                   fecomposite => 'feComposite',
4056                   feconvolvematrix => 'feConvolveMatrix',
4057                   fediffuselighting => 'feDiffuseLighting',
4058                   fedisplacementmap => 'feDisplacementMap',
4059                   fedistantlight => 'feDistantLight',
4060                   feflood => 'feFlood',
4061                   fefunca => 'feFuncA',
4062                   fefuncb => 'feFuncB',
4063                   fefuncg => 'feFuncG',
4064                   fefuncr => 'feFuncR',
4065                   fegaussianblur => 'feGaussianBlur',
4066                   feimage => 'feImage',
4067                   femerge => 'feMerge',
4068                   femergenode => 'feMergeNode',
4069                   femorphology => 'feMorphology',
4070                   feoffset => 'feOffset',
4071                   fepointlight => 'fePointLight',
4072                   fespecularlighting => 'feSpecularLighting',
4073                   fespotlight => 'feSpotLight',
4074                   fetile => 'feTile',
4075                   feturbulence => 'feTurbulence',
4076                   foreignobject => 'foreignObject',
4077                   glyphref => 'glyphRef',
4078                   lineargradient => 'linearGradient',
4079                   radialgradient => 'radialGradient',
4080                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4081                   textpath => 'textPath',  
4082                }->{$tag_name} || $tag_name;
4083              }
4084    
4085              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4086    
4087              ## "adjust foreign attributes" - done in insert-element-f
4088    
4089              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4090    
4091              if ($self->{self_closing}) {
4092                pop @{$self->{open_elements}};
4093                !!!ack ('t87.3');
4094              } else {
4095                !!!cp ('t87.4');
4096              }
4097    
4098              !!!next-token;
4099              next B;
4100            }
4101          } elsif ($token->{type} == END_TAG_TOKEN) {
4102            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4103            !!!cp ('t87.5');
4104            #
4105          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4106            !!!cp ('t87.6');
4107            !!!parse-error (type => 'not closed',
4108                            value => $self->{open_elements}->[-1]->[0]
4109                                ->manakai_local_name,
4110                            token => $token);
4111    
4112            pop @{$self->{open_elements}}
4113                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4114    
4115            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4116            ## Reprocess.
4117            next B;
4118          } else {
4119            die "$0: $token->{type}: Unknown token type";        
4120          }
4121        }
4122    
4123        if ($self->{insertion_mode} & HEAD_IMS) {
4124        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4126            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4127                !!!cp ('t88.2');
4128                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4129              } else {
4130                !!!cp ('t88.1');
4131                ## Ignore the token.
4132                !!!next-token;
4133                next B;
4134              }
4135            unless (length $token->{data}) {            unless (length $token->{data}) {
4136              !!!cp ('t88');              !!!cp ('t88');
4137              !!!next-token;              !!!next-token;
4138              redo B;              next B;
4139            }            }
4140          }          }
4141    
4142          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4143            !!!cp ('t89');            !!!cp ('t89');
4144            ## As if <head>            ## As if <head>
4145            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4146            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4147            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4148                  [$self->{head_element}, $el_category->{head}];
4149    
4150            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4151            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 4155  sub _tree_construction_main ($) {
4155            !!!cp ('t90');            !!!cp ('t90');
4156            ## As if </noscript>            ## As if </noscript>
4157            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4158            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4159                        
4160            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4161            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 4171  sub _tree_construction_main ($) {
4171            !!!cp ('t92');            !!!cp ('t92');
4172          }          }
4173    
4174              ## "after head" insertion mode          ## "after head" insertion mode
4175              ## As if <body>          ## As if <body>
4176              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4177              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4178              ## reprocess          ## reprocess
4179              redo B;          next B;
4180            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4181              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4182                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4183                  !!!cp ('t93');              !!!cp ('t93');
4184                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4185                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4186                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4187                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4188                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4189                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4190                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4191                  !!!cp ('t94');              !!!next-token;
4192                  #              next B;
4193                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4194                  !!!cp ('t95');              !!!cp ('t93.2');
4195                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4196                  ## Ignore the token              ## Ignore the token
4197                  !!!next-token;              !!!nack ('t93.3');
4198                  redo B;              !!!next-token;
4199                }              next B;
4200              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4201                !!!cp ('t96');              !!!cp ('t95');
4202                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4203                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4204                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4205                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4206                next B;
4207              }
4208            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4209              !!!cp ('t96');
4210              ## As if <head>
4211              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4212              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4213              push @{$self->{open_elements}},
4214                  [$self->{head_element}, $el_category->{head}];
4215    
4216                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4217                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4218              } else {          } else {
4219                !!!cp ('t97');            !!!cp ('t97');
4220              }          }
4221    
4222              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4223                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4224                  !!!cp ('t98');                  !!!cp ('t98');
4225                  ## As if </noscript>                  ## As if </noscript>
4226                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4227                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4228                                
4229                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4230                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 4235  sub _tree_construction_main ($) {
4235                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4236                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4237                  !!!cp ('t100');                  !!!cp ('t100');
4238                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4239                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4240                        [$self->{head_element}, $el_category->{head}];
4241                } else {                } else {
4242                  !!!cp ('t101');                  !!!cp ('t101');
4243                }                }
4244                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4245                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4246                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4247                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4248                  !!!nack ('t101.1');
4249                !!!next-token;                !!!next-token;
4250                redo B;                next B;
4251              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4252                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4253                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4254                  !!!cp ('t102');                  !!!cp ('t102');
4255                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4256                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4257                        [$self->{head_element}, $el_category->{head}];
4258                } else {                } else {
4259                  !!!cp ('t103');                  !!!cp ('t103');
4260                }                }
4261                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4262                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4263                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4264                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4265                  !!!ack ('t103.1');
4266                !!!next-token;                !!!next-token;
4267                redo B;                next B;
4268              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4269                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4270                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4271                  !!!cp ('t104');                  !!!cp ('t104');
4272                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4273                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4274                        [$self->{head_element}, $el_category->{head}];
4275                } else {                } else {
4276                  !!!cp ('t105');                  !!!cp ('t105');
4277                }                }
4278                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4279                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.
4280    
4281                unless ($self->{confident}) {                unless ($self->{confident}) {
4282                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4283                    !!!cp ('t106');                    !!!cp ('t106');
4284                      ## NOTE: Whether the encoding is supported or not is handled
4285                      ## in the {change_encoding} callback.
4286                    $self->{change_encoding}                    $self->{change_encoding}
4287                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4288                             $token);
4289                                        
4290                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4291                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4292                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4293                                                 ->{has_reference});                                                 ->{has_reference});
4294                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4295                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4296                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4297                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4298                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4299                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4300                      !!!cp ('t107');                      !!!cp ('t107');
4301                        ## NOTE: Whether the encoding is supported or not is handled
4302                        ## in the {change_encoding} callback.
4303                      $self->{change_encoding}                      $self->{change_encoding}
4304                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4305                               $token);
4306                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4307                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4308                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4328  sub _tree_construction_main ($) {
4328                  }                  }
4329                }                }
4330    
4331                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4332                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4333                  !!!ack ('t110.1');
4334                !!!next-token;                !!!next-token;
4335                redo B;                next B;
4336              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4337                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4338                  !!!cp ('t111');                  !!!cp ('t111');
4339                  ## As if </noscript>                  ## As if </noscript>
4340                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4341                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4342                                
4343                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4344                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4345                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4346                  !!!cp ('t112');                  !!!cp ('t112');
4347                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4348                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4349                        [$self->{head_element}, $el_category->{head}];
4350                } else {                } else {
4351                  !!!cp ('t113');                  !!!cp ('t113');
4352                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4354  sub _tree_construction_main ($) {
4354                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4355                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4356                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4357                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4358                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4359                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4360                redo B;                next B;
4361              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4362                         $token->{tag_name} eq 'noframes') {
4363                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4364                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4365                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4366                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                  !!!cp ('t114');                  !!!cp ('t114');
4368                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4369                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4370                        [$self->{head_element}, $el_category->{head}];
4371                } else {                } else {
4372                  !!!cp ('t115');                  !!!cp ('t115');
4373                }                }
4374                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4375                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4376                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4377                redo B;                next B;
4378              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4379                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4380                  !!!cp ('t116');                  !!!cp ('t116');
4381                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4382                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4383                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4384                    !!!nack ('t116.1');
4385                  !!!next-token;                  !!!next-token;
4386                  redo B;                  next B;
4387                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4388                  !!!cp ('t117');                  !!!cp ('t117');
4389                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4390                  ## Ignore the token                  ## Ignore the token
4391                    !!!nack ('t117.1');
4392                  !!!next-token;                  !!!next-token;
4393                  redo B;                  next B;
4394                } else {                } else {
4395                  !!!cp ('t118');                  !!!cp ('t118');
4396                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4400  sub _tree_construction_main ($) {
4400                  !!!cp ('t119');                  !!!cp ('t119');
4401                  ## As if </noscript>                  ## As if </noscript>
4402                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4403                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4404                                
4405                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4406                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4407                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4408                  !!!cp ('t120');                  !!!cp ('t120');
4409                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4410                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4411                        [$self->{head_element}, $el_category->{head}];
4412                } else {                } else {
4413                  !!!cp ('t121');                  !!!cp ('t121');
4414                }                }
4415    
4416                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4417                $script_start_tag->($insert_to_current);                $script_start_tag->();
4418                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4419                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4420                redo B;                next B;
4421              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4422                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4423                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4424                  !!!cp ('t122');                  !!!cp ('t122');
4425                  ## As if </noscript>                  ## As if </noscript>
4426                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4427                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4428                                    
4429                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4430                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4441  sub _tree_construction_main ($) {
4441                }                }
4442    
4443                ## "after head" insertion mode                ## "after head" insertion mode
4444                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4446                  !!!cp ('t126');                  !!!cp ('t126');
4447                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4451  sub _tree_construction_main ($) {
4451                } else {                } else {
4452                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4453                }                }
4454                  !!!nack ('t127.1');
4455                !!!next-token;                !!!next-token;
4456                redo B;                next B;
4457              } else {              } else {
4458                !!!cp ('t128');                !!!cp ('t128');
4459                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4463  sub _tree_construction_main ($) {
4463                !!!cp ('t129');                !!!cp ('t129');
4464                ## As if </noscript>                ## As if </noscript>
4465                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4466                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4467                                
4468                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4469                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482    
4483              ## "after head" insertion mode              ## "after head" insertion mode
4484              ## As if <body>              ## As if <body>
4485              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4486              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4487              ## reprocess              ## reprocess
4488              redo B;              !!!ack-later;
4489                next B;
4490            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4491              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4492                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4493                  !!!cp ('t132');                  !!!cp ('t132');
4494                  ## As if <head>                  ## As if <head>
4495                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4496                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4497                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4498                        [$self->{head_element}, $el_category->{head}];
4499    
4500                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4506                  !!!cp ('t133');                  !!!cp ('t133');
4507                  ## As if </noscript>                  ## As if </noscript>
4508                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4509                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4510                                    
4511                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4513                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4514                  !!!next-token;                  !!!next-token;
4515                  redo B;                  next B;
4516                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4517                  !!!cp ('t134');                  !!!cp ('t134');
4518                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4519                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4520                  !!!next-token;                  !!!next-token;
4521                  redo B;                  next B;
4522                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4523                    !!!cp ('t134.1');
4524                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4525                    ## Ignore the token
4526                    !!!next-token;
4527                    next B;
4528                } else {                } else {
4529                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4530                }                }
4531              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4532                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3706  sub _tree_construction_main ($) { Line 4534  sub _tree_construction_main ($) {
4534                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4535                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4536                  !!!next-token;                  !!!next-token;
4537                  redo B;                  next B;
4538                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4539                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4540                  !!!cp ('t137');                  !!!cp ('t137');
4541                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4542                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4543                  !!!next-token;                  !!!next-token;
4544                  redo B;                  next B;
4545                } else {                } else {
4546                  !!!cp ('t138');                  !!!cp ('t138');
4547                  #                  #
# Line 3720  sub _tree_construction_main ($) { Line 4549  sub _tree_construction_main ($) {
4549              } elsif ({              } elsif ({
4550                        body => 1, html => 1,                        body => 1, html => 1,
4551                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4552                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4553                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4554                  ## 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) {  
4555                  !!!cp ('t140');                  !!!cp ('t140');
4556                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4557                    ## Ignore the token
4558                    !!!next-token;
4559                    next B;
4560                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4561                    !!!cp ('t140.1');
4562                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4563                  ## Ignore the token                  ## Ignore the token
4564                  !!!next-token;                  !!!next-token;
4565                  redo B;                  next B;
4566                } else {                } else {
4567                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4568                }                }
4569                              } elsif ($token->{tag_name} eq 'p') {
4570                #                !!!cp ('t142');
4571              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4572                        p => 1, br => 1,                ## Ignore the token
4573                       }->{$token->{tag_name}}) {                !!!next-token;
4574                  next B;
4575                } elsif ($token->{tag_name} eq 'br') {
4576                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4577                  !!!cp ('t142');                  !!!cp ('t142.2');
4578                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4579                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4580                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4581                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4582      
4583                    ## Reprocess in the "after head" insertion mode...
4584                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4585                    !!!cp ('t143.2');
4586                    ## As if </head>
4587                    pop @{$self->{open_elements}};
4588                    $self->{insertion_mode} = AFTER_HEAD_IM;
4589      
4590                    ## Reprocess in the "after head" insertion mode...
4591                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4592                    !!!cp ('t143.3');
4593                    ## ISSUE: Two parse errors for <head><noscript></br>
4594                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4595                    ## As if </noscript>
4596                    pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4598    
4599                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4600                } else {                  ## As if </head>
4601                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4602                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4603    
4604                #                  ## Reprocess in the "after head" insertion mode...
4605              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4606                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4607                  #                  #
4608                } else {                } else {
4609                  !!!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;  
4610                }                }
4611    
4612                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4613                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4614                  ## Ignore the token
4615                  !!!next-token;
4616                  next B;
4617                } else {
4618                  !!!cp ('t145');
4619                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4620                  ## Ignore the token
4621                  !!!next-token;
4622                  next B;
4623              }              }
4624    
4625              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4626                !!!cp ('t146');                !!!cp ('t146');
4627                ## As if </noscript>                ## As if </noscript>
4628                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4629                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4630                                
4631                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4632                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4642  sub _tree_construction_main ($) {
4642              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4643  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4644                !!!cp ('t148');                !!!cp ('t148');
4645                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4646                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4647                !!!next-token;                !!!next-token;
4648                redo B;                next B;
4649              } else {              } else {
4650                !!!cp ('t149');                !!!cp ('t149');
4651              }              }
4652    
4653              ## "after head" insertion mode              ## "after head" insertion mode
4654              ## As if <body>              ## As if <body>
4655              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4656              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4657              ## reprocess              ## reprocess
4658              redo B;              next B;
4659            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4660              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4661            }            !!!cp ('t149.1');
4662    
4663              ## NOTE: As if <head>
4664              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4665              $self->{open_elements}->[-1]->[0]->append_child
4666                  ($self->{head_element});
4667              #push @{$self->{open_elements}},
4668              #    [$self->{head_element}, $el_category->{head}];
4669              #$self->{insertion_mode} = IN_HEAD_IM;
4670              ## NOTE: Reprocess.
4671    
4672              ## NOTE: As if </head>
4673              #pop @{$self->{open_elements}};
4674              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4675              ## NOTE: Reprocess.
4676              
4677              #
4678            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4679              !!!cp ('t149.2');
4680    
4681              ## NOTE: As if </head>
4682              pop @{$self->{open_elements}};
4683              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4684              ## NOTE: Reprocess.
4685    
4686              #
4687            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4688              !!!cp ('t149.3');
4689    
4690              !!!parse-error (type => 'in noscript:#eof', token => $token);
4691    
4692              ## As if </noscript>
4693              pop @{$self->{open_elements}};
4694              #$self->{insertion_mode} = IN_HEAD_IM;
4695              ## NOTE: Reprocess.
4696    
4697              ## NOTE: As if </head>
4698              pop @{$self->{open_elements}};
4699              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4700              ## NOTE: Reprocess.
4701    
4702              #
4703            } else {
4704              !!!cp ('t149.4');
4705              #
4706            }
4707    
4708            ## NOTE: As if <body>
4709            !!!insert-element ('body',, $token);
4710            $self->{insertion_mode} = IN_BODY_IM;
4711            ## NOTE: Reprocess.
4712            next B;
4713          } else {
4714            die "$0: $token->{type}: Unknown token type";
4715          }
4716    
4717            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4718      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4725    
4726              !!!next-token;              !!!next-token;
4727              redo B;              next B;
4728            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4729              if ({              if ({
4730                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4732  sub _tree_construction_main ($) {
4732                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4733                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4734                  ## have an element in table scope                  ## have an element in table scope
4735                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4736                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4737                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4738                      !!!cp ('t151');                      !!!cp ('t151');
4739                      $tn = $node->[1];  
4740                      last INSCOPE;                      ## Close the cell
4741                    } elsif ({                      !!!back-token; # <x>
4742                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4743                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4744                                  line => $token->{line},
4745                                  column => $token->{column}};
4746                        next B;
4747                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4748                      !!!cp ('t152');                      !!!cp ('t152');
4749                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4750                        last;
4751                    }                    }
4752                  } # INSCOPE                  }
4753                    unless (defined $tn) {  
4754                      !!!cp ('t153');                  !!!cp ('t153');
4755  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4756                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4757                      ## Ignore the token                  ## Ignore the token
4758                      !!!next-token;                  !!!nack ('t153.1');
4759                      redo B;                  !!!next-token;
4760                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4761                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4762                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4763                                    
4764                  ## As if </caption>                  ## NOTE: As if </caption>.
4765                  ## have a table element in table scope                  ## have a table element in table scope
4766                  my $i;                  my $i;
4767                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4768                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4769                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4770                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4771                      $i = $_;                        !!!cp ('t155');
4772                      last INSCOPE;                        $i = $_;
4773                    } elsif ({                        last INSCOPE;
4774                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4775                             }->{$node->[1]}) {                        !!!cp ('t156');
4776                      !!!cp ('t156');                        last;
4777                      last INSCOPE;                      }
4778                    }                    }
4779    
4780                      !!!cp ('t157');
4781                      !!!parse-error (type => 'start tag not allowed',
4782                                      value => $token->{tag_name}, token => $token);
4783                      ## Ignore the token
4784                      !!!nack ('t157.1');
4785                      !!!next-token;
4786                      next B;
4787                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4788                                    
4789                  ## generate implied end tags                  ## generate implied end tags
4790                  while ({                  while ($self->{open_elements}->[-1]->[1]
4791                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4792                    !!!cp ('t158');                    !!!cp ('t158');
4793                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4794                  }                  }
4795    
4796                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4797                    !!!cp ('t159');                    !!!cp ('t159');
4798                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4799                                      value => $self->{open_elements}->[-1]->[0]
4800                                          ->manakai_local_name,
4801                                      token => $token);
4802                  } else {                  } else {
4803                    !!!cp ('t160');                    !!!cp ('t160');
4804                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4810  sub _tree_construction_main ($) {
4810                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4811                                    
4812                  ## reprocess                  ## reprocess
4813                  redo B;                  !!!ack-later;
4814                    next B;
4815                } else {                } else {
4816                  !!!cp ('t161');                  !!!cp ('t161');
4817                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 4827  sub _tree_construction_main ($) {
4827                  my $i;                  my $i;
4828                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4829                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4830                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4831                      !!!cp ('t163');                      !!!cp ('t163');
4832                      $i = $_;                      $i = $_;
4833                      last INSCOPE;                      last INSCOPE;
4834                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4835                      !!!cp ('t164');                      !!!cp ('t164');
4836                      last INSCOPE;                      last INSCOPE;
4837                    }                    }
4838                  } # INSCOPE                  } # INSCOPE
4839                    unless (defined $i) {                    unless (defined $i) {
4840                      !!!cp ('t165');                      !!!cp ('t165');
4841                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4842                      ## Ignore the token                      ## Ignore the token
4843                      !!!next-token;                      !!!next-token;
4844                      redo B;                      next B;
4845                    }                    }
4846                                    
4847                  ## generate implied end tags                  ## generate implied end tags
4848                  while ({                  while ($self->{open_elements}->[-1]->[1]
4849                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4850                    !!!cp ('t166');                    !!!cp ('t166');
4851                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4852                  }                  }
4853    
4854                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4855                            ne $token->{tag_name}) {
4856                    !!!cp ('t167');                    !!!cp ('t167');
4857                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4858                                      value => $self->{open_elements}->[-1]->[0]
4859                                          ->manakai_local_name,
4860                                      token => $token);
4861                  } else {                  } else {
4862                    !!!cp ('t168');                    !!!cp ('t168');
4863                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4870                                    
4871                  !!!next-token;                  !!!next-token;
4872                  redo B;                  next B;
4873                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4874                  !!!cp ('t169');                  !!!cp ('t169');
4875                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4876                  ## Ignore the token                  ## Ignore the token
4877                  !!!next-token;                  !!!next-token;
4878                  redo B;                  next B;
4879                } else {                } else {
4880                  !!!cp ('t170');                  !!!cp ('t170');
4881                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 4884  sub _tree_construction_main ($) {
4884                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4885                  ## have a table element in table scope                  ## have a table element in table scope
4886                  my $i;                  my $i;
4887                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4888                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4889                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4890                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4891                      $i = $_;                        !!!cp ('t171');
4892                      last INSCOPE;                        $i = $_;
4893                    } elsif ({                        last INSCOPE;
4894                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4895                             }->{$node->[1]}) {                        !!!cp ('t172');
4896                      !!!cp ('t172');                        last;
4897                      last INSCOPE;                      }
4898                    }                    }
4899    
4900                      !!!cp ('t173');
4901                      !!!parse-error (type => 'unmatched end tag',
4902                                      value => $token->{tag_name}, token => $token);
4903                      ## Ignore the token
4904                      !!!next-token;
4905                      next B;
4906                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4907                                    
4908                  ## generate implied end tags                  ## generate implied end tags
4909                  while ({                  while ($self->{open_elements}->[-1]->[1]
4910                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4911                    !!!cp ('t174');                    !!!cp ('t174');
4912                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4913                  }                  }
4914                                    
4915                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4916                    !!!cp ('t175');                    !!!cp ('t175');
4917                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4918                                      value => $self->{open_elements}->[-1]->[0]
4919                                          ->manakai_local_name,
4920                                      token => $token);
4921                  } else {                  } else {
4922                    !!!cp ('t176');                    !!!cp ('t176');
4923                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 4929  sub _tree_construction_main ($) {
4929                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4930                                    
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  next B;
4933                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4934                  !!!cp ('t177');                  !!!cp ('t177');
4935                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4936                  ## Ignore the token                  ## Ignore the token
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  next B;
4939                } else {                } else {
4940                  !!!cp ('t178');                  !!!cp ('t178');
4941                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 4948  sub _tree_construction_main ($) {
4948                ## have an element in table scope                ## have an element in table scope
4949                my $i;                my $i;
4950                my $tn;                my $tn;
4951                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4952                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4953                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4954                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4955                    $i = $_;                      !!!cp ('t179');
4956                    last INSCOPE;                      $i = $_;
4957                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4958                    !!!cp ('t180');                      ## Close the cell
4959                    $tn = $node->[1];                      !!!back-token; # </x>
4960                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4961                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4962                  } elsif ({                                column => $token->{column}};
4963                            table => 1, html => 1,                      next B;
4964                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4965                    !!!cp ('t181');                      !!!cp ('t180');
4966                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4967                        ## NOTE: There is exactly one |td| or |th| element
4968                        ## in scope in the stack of open elements by definition.
4969                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4970                        ## ISSUE: Can this be reached?
4971                        !!!cp ('t181');
4972                        last;
4973                      }
4974                  }                  }
4975                } # INSCOPE  
               unless (defined $i) {  
4976                  !!!cp ('t182');                  !!!cp ('t182');
4977                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4978                        value => $token->{tag_name}, token => $token);
4979                  ## Ignore the token                  ## Ignore the token
4980                  !!!next-token;                  !!!next-token;
4981                  redo B;                  next B;
4982                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4983              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4984                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4985                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4986    
4987                ## As if </caption>                ## As if </caption>
4988                ## have a table element in table scope                ## have a table element in table scope
4989                my $i;                my $i;
4990                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4991                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4992                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4993                    !!!cp ('t184');                    !!!cp ('t184');
4994                    $i = $_;                    $i = $_;
4995                    last INSCOPE;                    last INSCOPE;
4996                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4997                    !!!cp ('t185');                    !!!cp ('t185');
4998                    last INSCOPE;                    last INSCOPE;
4999                  }                  }
5000                } # INSCOPE                } # INSCOPE
5001                unless (defined $i) {                unless (defined $i) {
5002                  !!!cp ('t186');                  !!!cp ('t186');
5003                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5004                  ## Ignore the token                  ## Ignore the token
5005                  !!!next-token;                  !!!next-token;
5006                  redo B;                  next B;
5007                }                }
5008                                
5009                ## generate implied end tags                ## generate implied end tags
5010                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5011                  !!!cp ('t187');                  !!!cp ('t187');
5012                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5013                }                }
5014    
5015                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5016                  !!!cp ('t188');                  !!!cp ('t188');
5017                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5018                                    value => $self->{open_elements}->[-1]->[0]
5019                                        ->manakai_local_name,
5020                                    token => $token);
5021                } else {                } else {
5022                  !!!cp ('t189');                  !!!cp ('t189');
5023                }                }
# Line 4120  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5030    
5031                ## reprocess                ## reprocess
5032                redo B;                next B;
5033              } elsif ({              } elsif ({
5034                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5035                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5036                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5037                  !!!cp ('t190');                  !!!cp ('t190');
5038                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5039                  ## Ignore the token                  ## Ignore the token
5040                  !!!next-token;                  !!!next-token;
5041                  redo B;                  next B;
5042                } else {                } else {
5043                  !!!cp ('t191');                  !!!cp ('t191');
5044                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5050                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5051                !!!cp ('t192');                !!!cp ('t192');
5052                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5053                ## Ignore the token                ## Ignore the token
5054                !!!next-token;                !!!next-token;
5055                redo B;                next B;
5056              } else {              } else {
5057                !!!cp ('t193');                !!!cp ('t193');
5058                #                #
5059              }              }
5060          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5061            for my $entry (@{$self->{open_elements}}) {
5062              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5063                !!!cp ('t75');
5064                !!!parse-error (type => 'in body:#eof', token => $token);
5065                last;
5066              }
5067            }
5068    
5069            ## Stop parsing.
5070            last B;
5071        } else {        } else {
5072          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5073        }        }
# Line 4156  sub _tree_construction_main ($) { Line 5076  sub _tree_construction_main ($) {
5076        #        #
5077      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5078        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5079              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5080                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5081              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5082                                
5083                unless (length $token->{data}) {            unless (length $token->{data}) {
5084                  !!!cp ('t194');              !!!cp ('t194');
5085                  !!!next-token;              !!!next-token;
5086                  redo B;              next B;
5087                } else {            } else {
5088                  !!!cp ('t195');              !!!cp ('t195');
5089                }            }
5090              }          }
5091    
5092              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5093    
5094              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5095              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 5097  sub _tree_construction_main ($) {
5097              ## result in a new Text node.              ## result in a new Text node.
5098              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5099                            
5100              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]}) {  
5101                # MUST                # MUST
5102                my $foster_parent_element;                my $foster_parent_element;
5103                my $next_sibling;                my $next_sibling;
5104                my $prev_sibling;                my $prev_sibling;
5105                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5106                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5107                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5108                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5109                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 5131  sub _tree_construction_main ($) {
5131                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5132                     $next_sibling);                     $next_sibling);
5133                }                }
5134              } else {            $open_tables->[-1]->[1] = 1; # tainted
5135                !!!cp ('t200');          } else {
5136                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5137              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5138            }
5139                            
5140              !!!next-token;          !!!next-token;
5141              redo B;          next B;
5142        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5143              if ({              if ({
5144                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4227  sub _tree_construction_main ($) { Line 5146  sub _tree_construction_main ($) {
5146                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5147                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5148                  ## Clear back to table context                  ## Clear back to table context
5149                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5150                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5151                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5152                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5153                  }                  }
5154                                    
5155                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5156                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5157                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5158                }                }
# Line 4242  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5161                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5162                    !!!cp ('t202');                    !!!cp ('t202');
5163                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5164                  }                  }
5165                                    
5166                  ## Clear back to table body context                  ## Clear back to table body context
5167                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5168                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5169                    !!!cp ('t203');                    !!!cp ('t203');
5170                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5171                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5172                  }                  }
5173                                    
5174                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5175                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5176                    !!!cp ('t204');                    !!!cp ('t204');
5177                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5178                      !!!nack ('t204');
5179                    !!!next-token;                    !!!next-token;
5180                    redo B;                    next B;
5181                  } else {                  } else {
5182                    !!!cp ('t205');                    !!!cp ('t205');
5183                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5184                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5185                  }                  }
5186                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188                }                }
5189    
5190                ## Clear back to table row context                ## Clear back to table row context
5191                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5192                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5193                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5194                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5195                }                }
5196                                
5197                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5198                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5199    
5200                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5201                                
5202                  !!!nack ('t207.1');
5203                !!!next-token;                !!!next-token;
5204                redo B;                next B;
5205              } elsif ({              } elsif ({
5206                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5207                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 5213  sub _tree_construction_main ($) {
5213                  my $i;                  my $i;
5214                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5215                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5216                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5217                      !!!cp ('t208');                      !!!cp ('t208');
5218                      $i = $_;                      $i = $_;
5219                      last INSCOPE;                      last INSCOPE;
5220                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5221                      !!!cp ('t209');                      !!!cp ('t209');
5222                      last INSCOPE;                      last INSCOPE;
5223                    }                    }
5224                  } # INSCOPE                  } # INSCOPE
5225                  unless (defined $i) {                  unless (defined $i) {
5226                   !!!cp ('t210');                    !!!cp ('t210');
5227  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5228                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5229                    ## Ignore the token                    ## Ignore the token
5230                      !!!nack ('t210.1');
5231                    !!!next-token;                    !!!next-token;
5232                    redo B;                    next B;
5233                  }                  }
5234                                    
5235                  ## Clear back to table row context                  ## Clear back to table row context
5236                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5237                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5238                    !!!cp ('t211');                    !!!cp ('t211');
5239                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5240                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5241                  }                  }
5242                                    
# Line 4335  sub _tree_construction_main ($) { Line 5245  sub _tree_construction_main ($) {
5245                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5246                    !!!cp ('t212');                    !!!cp ('t212');
5247                    ## reprocess                    ## reprocess
5248                    redo B;                    !!!ack-later;
5249                      next B;
5250                  } else {                  } else {
5251                    !!!cp ('t213');                    !!!cp ('t213');
5252                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                  my $i;                  my $i;
5259                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5260                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5261                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5262                      !!!cp ('t214');                      !!!cp ('t214');
5263                      $i = $_;                      $i = $_;
5264                      last INSCOPE;                      last INSCOPE;
5265                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5266                      !!!cp ('t215');                      !!!cp ('t215');
5267                      last INSCOPE;                      last INSCOPE;
5268                    }                    }
# Line 4363  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270                  unless (defined $i) {                  unless (defined $i) {
5271                    !!!cp ('t216');                    !!!cp ('t216');
5272  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5273                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5274                    ## Ignore the token                    ## Ignore the token
5275                      !!!nack ('t216.1');
5276                    !!!next-token;                    !!!next-token;
5277                    redo B;                    next B;
5278                  }                  }
5279    
5280                  ## Clear back to table body context                  ## Clear back to table body context
5281                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5282                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5283                    !!!cp ('t217');                    !!!cp ('t217');
5284                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5285                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5286                  }                  }
5287                                    
# Line 4395  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301    
5302                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5303                  ## Clear back to table context                  ## Clear back to table context
5304                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5305                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5306                    !!!cp ('t219');                    !!!cp ('t219');
5307                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5308                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5309                  }                  }
5310                                    
5311                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5312                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5313                  ## reprocess                  ## reprocess
5314                  redo B;                  !!!ack-later;
5315                    next B;
5316                } elsif ({                } elsif ({
5317                          caption => 1,                          caption => 1,
5318                          colgroup => 1,                          colgroup => 1,
5319                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5320                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5321                  ## Clear back to table context                  ## Clear back to table context
5322                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5323                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5324                    !!!cp ('t220');                    !!!cp ('t220');
5325                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5326                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5327                  }                  }
5328                                    
5329                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5330                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5331                                    
5332                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5333                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5334                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5335                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5339                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5340                  !!!next-token;                  !!!next-token;
5341                  redo B;                  !!!nack ('t220.1');
5342                    next B;
5343                } else {                } else {
5344                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5345                }                }
5346              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5347                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5348                                  value => $self->{open_elements}->[-1]->[0]
5349                                      ->manakai_local_name,
5350                                  token => $token);
5351    
5352                ## As if </table>                ## As if </table>
5353                ## have a table element in table scope                ## have a table element in table scope
5354                my $i;                my $i;
5355                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5356                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5357                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5358                    !!!cp ('t221');                    !!!cp ('t221');
5359                    $i = $_;                    $i = $_;
5360                    last INSCOPE;                    last INSCOPE;
5361                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5362                    !!!cp ('t222');                    !!!cp ('t222');
5363                    last INSCOPE;                    last INSCOPE;
5364                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5366  sub _tree_construction_main ($) {
5366                unless (defined $i) {                unless (defined $i) {
5367                  !!!cp ('t223');                  !!!cp ('t223');
5368  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5369                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5370                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5371                    !!!nack ('t223.1');
5372                  !!!next-token;                  !!!next-token;
5373                  redo B;                  next B;
5374                }                }
5375                                
5376    ## TODO: Followings are removed from the latest spec.
5377                ## generate implied end tags                ## generate implied end tags
5378                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5379                  !!!cp ('t224');                  !!!cp ('t224');
5380                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5381                }                }
5382    
5383                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5384                  !!!cp ('t225');                  !!!cp ('t225');
5385  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5386                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5387                                    value => $self->{open_elements}->[-1]->[0]
5388                                        ->manakai_local_name,
5389                                    token => $token);
5390                } else {                } else {
5391                  !!!cp ('t226');                  !!!cp ('t226');
5392                }                }
5393    
5394                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5395                  pop @{$open_tables};
5396    
5397                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5398    
5399                ## reprocess            ## reprocess
5400                redo B;            !!!ack-later;
5401              next B;
5402            } elsif ($token->{tag_name} eq 'style') {
5403              if (not $open_tables->[-1]->[1]) { # tainted
5404                !!!cp ('t227.8');
5405                ## NOTE: This is a "as if in head" code clone.
5406                $parse_rcdata->(CDATA_CONTENT_MODEL);
5407                next B;
5408              } else {
5409                !!!cp ('t227.7');
5410                #
5411              }
5412            } elsif ($token->{tag_name} eq 'script') {
5413              if (not $open_tables->[-1]->[1]) { # tainted
5414                !!!cp ('t227.6');
5415                ## NOTE: This is a "as if in head" code clone.
5416                $script_start_tag->();
5417                next B;
5418              } else {
5419                !!!cp ('t227.5');
5420                #
5421              }
5422            } elsif ($token->{tag_name} eq 'input') {
5423              if (not $open_tables->[-1]->[1]) { # tainted
5424                if ($token->{attributes}->{type}) { ## TODO: case
5425                  my $type = lc $token->{attributes}->{type}->{value};
5426                  if ($type eq 'hidden') {
5427                    !!!cp ('t227.3');
5428                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5429    
5430                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5431    
5432                    ## TODO: form element pointer
5433    
5434                    pop @{$self->{open_elements}};
5435    
5436                    !!!next-token;
5437                    !!!ack ('t227.2.1');
5438                    next B;
5439                  } else {
5440                    !!!cp ('t227.2');
5441                    #
5442                  }
5443                } else {
5444                  !!!cp ('t227.1');
5445                  #
5446                }
5447              } else {
5448                !!!cp ('t227.4');
5449                #
5450              }
5451          } else {          } else {
5452            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5453            #            #
5454          }          }
5455    
5456            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5457    
5458            $insert = $insert_to_foster;
5459            #
5460        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5461              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5462                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5464  sub _tree_construction_main ($) {
5464                my $i;                my $i;
5465                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5466                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5467                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5468                    !!!cp ('t228');                    !!!cp ('t228');
5469                    $i = $_;                    $i = $_;
5470                    last INSCOPE;                    last INSCOPE;
5471                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5472                    !!!cp ('t229');                    !!!cp ('t229');
5473                    last INSCOPE;                    last INSCOPE;
5474                  }                  }
5475                } # INSCOPE                } # INSCOPE
5476                unless (defined $i) {                unless (defined $i) {
5477                  !!!cp ('t230');                  !!!cp ('t230');
5478                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5479                  ## Ignore the token                  ## Ignore the token
5480                    !!!nack ('t230.1');
5481                  !!!next-token;                  !!!next-token;
5482                  redo B;                  next B;
5483                } else {                } else {
5484                  !!!cp ('t232');                  !!!cp ('t232');
5485                }                }
5486    
5487                ## Clear back to table row context                ## Clear back to table row context
5488                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5489                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5490                  !!!cp ('t231');                  !!!cp ('t231');
5491  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5492                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5493                }                }
5494    
5495                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5496                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5497                !!!next-token;                !!!next-token;
5498                redo B;                !!!nack ('t231.1');
5499                  next B;
5500              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5501                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5502                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5504  sub _tree_construction_main ($) {
5504                  my $i;                  my $i;
5505                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5506                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5507                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5508                      !!!cp ('t233');                      !!!cp ('t233');
5509                      $i = $_;                      $i = $_;
5510                      last INSCOPE;                      last INSCOPE;
5511                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5512                      !!!cp ('t234');                      !!!cp ('t234');
5513                      last INSCOPE;                      last INSCOPE;
5514                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516                  unless (defined $i) {                  unless (defined $i) {
5517                    !!!cp ('t235');                    !!!cp ('t235');
5518  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5519                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5520                    ## Ignore the token                    ## Ignore the token
5521                      !!!nack ('t236.1');
5522                    !!!next-token;                    !!!next-token;
5523                    redo B;                    next B;
5524                  }                  }
5525                                    
5526                  ## Clear back to table row context                  ## Clear back to table row context
5527                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5528                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5529                    !!!cp ('t236');                    !!!cp ('t236');
5530  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5531                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5532                  }                  }
5533                                    
# Line 4584  sub _tree_construction_main ($) { Line 5541  sub _tree_construction_main ($) {
5541                  my $i;                  my $i;
5542                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5543                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5544                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5545                      !!!cp ('t237');                      !!!cp ('t237');
5546                      $i = $_;                      $i = $_;
5547                      last INSCOPE;                      last INSCOPE;
5548                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5549                      !!!cp ('t238');                      !!!cp ('t238');
5550                      last INSCOPE;                      last INSCOPE;
5551                    }                    }
5552                  } # INSCOPE                  } # INSCOPE
5553                  unless (defined $i) {                  unless (defined $i) {
5554                    !!!cp ('t239');                    !!!cp ('t239');
5555                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5556                    ## Ignore the token                    ## Ignore the token
5557                      !!!nack ('t239.1');
5558                    !!!next-token;                    !!!next-token;
5559                    redo B;                    next B;
5560                  }                  }
5561                                    
5562                  ## Clear back to table body context                  ## Clear back to table body context
5563                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5564                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5565                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5566                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5567                  }                  }
5568                                    
# Line 4626  sub _tree_construction_main ($) { Line 5578  sub _tree_construction_main ($) {
5578                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5579                }                }
5580    
5581                  ## NOTE: </table> in the "in table" insertion mode.
5582                  ## When you edit the code fragment below, please ensure that
5583                  ## the code for <table> in the "in table" insertion mode
5584                  ## is synced with it.
5585    
5586                ## have a table element in table scope                ## have a table element in table scope
5587                my $i;                my $i;
5588                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5589                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5590                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5591                    !!!cp ('t241');                    !!!cp ('t241');
5592                    $i = $_;                    $i = $_;
5593                    last INSCOPE;                    last INSCOPE;
5594                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5595                    !!!cp ('t242');                    !!!cp ('t242');
5596                    last INSCOPE;                    last INSCOPE;
5597                  }                  }
5598                } # INSCOPE                } # INSCOPE
5599                unless (defined $i) {                unless (defined $i) {
5600                  !!!cp ('t243');                  !!!cp ('t243');
5601                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5602                  ## Ignore the token                  ## Ignore the token
5603                    !!!nack ('t243.1');
5604                  !!!next-token;                  !!!next-token;
5605                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5606                }                }
5607                                    
5608                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5609                  pop @{$open_tables};
5610                                
5611                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5612                                
5613                !!!next-token;                !!!next-token;
5614                redo B;                next B;
5615              } elsif ({              } elsif ({
5616                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5617                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 5621  sub _tree_construction_main ($) {
5621                  my $i;                  my $i;
5622                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5623                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5624                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5625                      !!!cp ('t247');                      !!!cp ('t247');
5626                      $i = $_;                      $i = $_;
5627                      last INSCOPE;                      last INSCOPE;
5628                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5629                      !!!cp ('t248');                      !!!cp ('t248');
5630                      last INSCOPE;                      last INSCOPE;
5631                    }                    }
5632                  } # INSCOPE                  } # INSCOPE
5633                    unless (defined $i) {                    unless (defined $i) {
5634                      !!!cp ('t249');                      !!!cp ('t249');
5635                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5636                      ## Ignore the token                      ## Ignore the token
5637                        !!!nack ('t249.1');
5638                      !!!next-token;                      !!!next-token;
5639                      redo B;                      next B;
5640                    }                    }
5641                                    
5642                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 5644  sub _tree_construction_main ($) {
5644                  my $i;                  my $i;
5645                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5646                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5647                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5648                      !!!cp ('t250');                      !!!cp ('t250');
5649                      $i = $_;                      $i = $_;
5650                      last INSCOPE;                      last INSCOPE;
5651                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5652                      !!!cp ('t251');                      !!!cp ('t251');
5653                      last INSCOPE;                      last INSCOPE;
5654                    }                    }
5655                  } # INSCOPE                  } # INSCOPE
5656                    unless (defined $i) {                    unless (defined $i) {
5657                      !!!cp ('t252');                      !!!cp ('t252');
5658                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5659                      ## Ignore the token                      ## Ignore the token
5660                        !!!nack ('t252.1');
5661                      !!!next-token;                      !!!next-token;
5662                      redo B;                      next B;
5663                    }                    }
5664                                    
5665                  ## Clear back to table row context                  ## Clear back to table row context
5666                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5667                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5668                    !!!cp ('t253');                    !!!cp ('t253');
5669  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5670                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5671                  }                  }
5672                                    
# Line 4742  sub _tree_construction_main ($) { Line 5679  sub _tree_construction_main ($) {
5679                my $i;                my $i;
5680                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5681                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5682                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5683                    !!!cp ('t254');                    !!!cp ('t254');
5684                    $i = $_;                    $i = $_;
5685                    last INSCOPE;                    last INSCOPE;
5686                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5687                    !!!cp ('t255');                    !!!cp ('t255');
5688                    last INSCOPE;                    last INSCOPE;
5689                  }                  }
5690                } # INSCOPE                } # INSCOPE
5691                unless (defined $i) {                unless (defined $i) {
5692                  !!!cp ('t256');                  !!!cp ('t256');
5693                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5694                  ## Ignore the token                  ## Ignore the token
5695                    !!!nack ('t256.1');
5696                  !!!next-token;                  !!!next-token;
5697                  redo B;                  next B;
5698                }                }
5699    
5700                ## Clear back to table body context                ## Clear back to table body context
5701                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5702                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5703                  !!!cp ('t257');                  !!!cp ('t257');
5704  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5705                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5706                }                }
5707    
5708                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5709                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5710                  !!!nack ('t257.1');
5711                !!!next-token;                !!!next-token;
5712                redo B;                next B;
5713              } elsif ({              } elsif ({
5714                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5715                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5716                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5717                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5718                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5719                !!!cp ('t258');            !!!cp ('t258');
5720                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5721                ## Ignore the token            ## Ignore the token
5722                !!!next-token;            !!!nack ('t258.1');
5723                redo B;             !!!next-token;
5724              next B;
5725          } else {          } else {
5726            !!!cp ('t259');            !!!cp ('t259');
5727            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5728    
5729            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5730            #            #
5731          }          }
5732          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5733            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5734                    @{$self->{open_elements}} == 1) { # redundant, maybe
5735              !!!parse-error (type => 'in body:#eof', token => $token);
5736              !!!cp ('t259.1');
5737              #
5738            } else {
5739              !!!cp ('t259.2');
5740              #
5741            }
5742    
5743            ## Stop parsing
5744            last B;
5745        } else {        } else {
5746          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5747        }        }
# Line 4803  sub _tree_construction_main ($) { Line 5752  sub _tree_construction_main ($) {
5752                unless (length $token->{data}) {                unless (length $token->{data}) {
5753                  !!!cp ('t260');                  !!!cp ('t260');
5754                  !!!next-token;                  !!!next-token;
5755                  redo B;                  next B;
5756                }                }
5757              }              }
5758                            
# Line 4812  sub _tree_construction_main ($) { Line 5761  sub _tree_construction_main ($) {
5761            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5762              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5763                !!!cp ('t262');                !!!cp ('t262');
5764                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5765                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5766                  !!!ack ('t262.1');
5767                !!!next-token;                !!!next-token;
5768                redo B;                next B;
5769              } else {              } else {
5770                !!!cp ('t263');                !!!cp ('t263');
5771                #                #
5772              }              }
5773            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5774              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5775                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5776                  !!!cp ('t264');                  !!!cp ('t264');
5777                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5778                  ## Ignore the token                  ## Ignore the token
5779                  !!!next-token;                  !!!next-token;
5780                  redo B;                  next B;
5781                } else {                } else {
5782                  !!!cp ('t265');                  !!!cp ('t265');
5783                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5784                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5785                  !!!next-token;                  !!!next-token;
5786                  redo B;                              next B;            
5787                }                }
5788              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5789                !!!cp ('t266');                !!!cp ('t266');
5790                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5791                ## Ignore the token                ## Ignore the token
5792                !!!next-token;                !!!next-token;
5793                redo B;                next B;
5794              } else {              } else {
5795                !!!cp ('t267');                !!!cp ('t267');
5796                #                #
5797              }              }
5798            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5799              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5800            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5801              !!!cp ('t270.2');
5802              ## Stop parsing.
5803              last B;
5804            } else {
5805              ## NOTE: As if </colgroup>.
5806              !!!cp ('t270.1');
5807              pop @{$self->{open_elements}}; # colgroup
5808              $self->{insertion_mode} = IN_TABLE_IM;
5809              ## Reprocess.
5810              next B;
5811            }
5812          } else {
5813            die "$0: $token->{type}: Unknown token type";
5814          }
5815    
5816            ## As if </colgroup>            ## As if </colgroup>
5817            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5818              !!!cp ('t269');              !!!cp ('t269');
5819              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5820                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5821              ## Ignore the token              ## Ignore the token
5822                !!!nack ('t269.1');
5823              !!!next-token;              !!!next-token;
5824              redo B;              next B;
5825            } else {            } else {
5826              !!!cp ('t270');              !!!cp ('t270');
5827              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5828              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5829                !!!ack-later;
5830              ## reprocess              ## reprocess
5831              redo B;              next B;
5832            }            }
5833      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5834        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5835          !!!cp ('t271');          !!!cp ('t271');
5836          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5837          !!!next-token;          !!!next-token;
5838          redo B;          next B;
5839        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5840              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5841                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5842                  !!!cp ('t272');              !!!cp ('t272');
5843                  ## As if </option>              ## As if </option>
5844                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5845                } else {            } else {
5846                  !!!cp ('t273');              !!!cp ('t273');
5847                }            }
5848    
5849                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5850                !!!next-token;            !!!nack ('t273.1');
5851                redo B;            !!!next-token;
5852              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5853                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5854                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5855                  ## As if </option>              !!!cp ('t274');
5856                  pop @{$self->{open_elements}};              ## As if </option>
5857                } else {              pop @{$self->{open_elements}};
5858                  !!!cp ('t275');            } else {
5859                }              !!!cp ('t275');
5860              }
5861    
5862                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5863                  !!!cp ('t276');              !!!cp ('t276');
5864                  ## As if </optgroup>              ## As if </optgroup>
5865                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5866                } else {            } else {
5867                  !!!cp ('t277');              !!!cp ('t277');
5868                }            }
5869    
5870                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5871                !!!next-token;            !!!nack ('t277.1');
5872                redo B;            !!!next-token;
5873              } elsif ($token->{tag_name} eq 'select') {            next B;
5874  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
5875                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
5876                ## As if </select> instead                   }->{$token->{tag_name}} or
5877                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5878                my $i;                    {
5879                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5880                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5881                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5882                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
5883                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
5884                    last INSCOPE;            !!!parse-error (type => 'not closed:select', token => $token);
5885                  } elsif ({            ## NOTE: As if the token were </select> (<select> case) or
5886                            table => 1, html => 1,            ## as if there were </select> (otherwise).
5887                           }->{$node->[1]}) {            ## have an element in table scope
5888                    !!!cp ('t279');            my $i;
5889                    last INSCOPE;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                  }              my $node = $self->{open_elements}->[$_];
5891                } # INSCOPE              if ($node->[1] & SELECT_EL) {
5892                unless (defined $i) {                !!!cp ('t278');
5893                  !!!cp ('t280');                $i = $_;
5894                  !!!parse-error (type => 'unmatched end tag:select');                last INSCOPE;
5895                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5896                  !!!next-token;                !!!cp ('t279');
5897                  redo B;                last INSCOPE;
5898                }              }
5899              } # INSCOPE
5900              unless (defined $i) {
5901                !!!cp ('t280');
5902                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5903                ## Ignore the token
5904                !!!nack ('t280.1');
5905                !!!next-token;
5906                next B;
5907              }
5908                                
5909                !!!cp ('t281');            !!!cp ('t281');
5910                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5911    
5912                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5913    
5914                !!!next-token;            if ($token->{tag_name} eq 'select') {
5915                redo B;              !!!nack ('t281.2');
5916                !!!next-token;
5917                next B;
5918              } else {
5919                !!!cp ('t281.1');
5920                !!!ack-later;
5921                ## Reprocess the token.
5922                next B;
5923              }
5924          } else {          } else {
5925            !!!cp ('t282');            !!!cp ('t282');
5926            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5927            ## Ignore the token            ## Ignore the token
5928              !!!nack ('t282.1');
5929            !!!next-token;            !!!next-token;
5930            redo B;            next B;
5931          }          }
5932        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5933              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5934                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5935                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5936                  !!!cp ('t283');              !!!cp ('t283');
5937                  ## As if </option>              ## As if </option>
5938                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5939                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5940                  !!!cp ('t284');              !!!cp ('t284');
5941                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5942                } else {            } else {
5943                  !!!cp ('t285');              !!!cp ('t285');
5944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## Ignore the token              ## Ignore the token
5946                }            }
5947                !!!next-token;            !!!nack ('t285.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'option') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5951                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  pop @{$self->{open_elements}};              !!!cp ('t286');
5953                } else {              pop @{$self->{open_elements}};
5954                  !!!cp ('t287');            } else {
5955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5956                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5957                }              ## Ignore the token
5958                !!!next-token;            }
5959                redo B;            !!!nack ('t287.1');
5960              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5961                ## have an element in table scope            next B;
5962                my $i;          } elsif ($token->{tag_name} eq 'select') {
5963                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5964                  my $node = $self->{open_elements}->[$_];            my $i;
5965                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5966                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5967                    $i = $_;              if ($node->[1] & SELECT_EL) {
5968                    last INSCOPE;                !!!cp ('t288');
5969                  } elsif ({                $i = $_;
5970                            table => 1, html => 1,                last INSCOPE;
5971                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5972                    !!!cp ('t289');                !!!cp ('t289');
5973                    last INSCOPE;                last INSCOPE;
5974                  }              }
5975                } # INSCOPE            } # INSCOPE
5976                unless (defined $i) {            unless (defined $i) {
5977                  !!!cp ('t290');              !!!cp ('t290');
5978                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5979                  ## Ignore the token              ## Ignore the token
5980                  !!!next-token;              !!!nack ('t290.1');
5981                  redo B;              !!!next-token;
5982                }              next B;
5983              }
5984                                
5985                !!!cp ('t291');            !!!cp ('t291');
5986                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5987    
5988                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5989    
5990                !!!next-token;            !!!nack ('t291.1');
5991                redo B;            !!!next-token;
5992              } elsif ({            next B;
5993                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5994                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5995                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5996                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5997                     }->{$token->{tag_name}}) {
5998  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5999                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6000                                
6001                ## have an element in table scope            ## have an element in table scope
6002                my $i;            my $i;
6003                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6004                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6005                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6006                    !!!cp ('t292');                !!!cp ('t292');
6007                    $i = $_;                $i = $_;
6008                    last INSCOPE;                last INSCOPE;
6009                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6010                            table => 1, html => 1,                !!!cp ('t293');
6011                           }->{$node->[1]}) {                last INSCOPE;
6012                    !!!cp ('t293');              }
6013                    last INSCOPE;            } # INSCOPE
6014                  }            unless (defined $i) {
6015                } # INSCOPE              !!!cp ('t294');
6016                unless (defined $i) {              ## Ignore the token
6017                  !!!cp ('t294');              !!!nack ('t294.1');
6018                  ## Ignore the token              !!!next-token;
6019                  !!!next-token;              next B;
6020                  redo B;            }
               }  
6021                                
6022                ## As if </select>            ## As if </select>
6023                ## have an element in table scope            ## have an element in table scope
6024                undef $i;            undef $i;
6025                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6026                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6027                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6028                    !!!cp ('t295');                !!!cp ('t295');
6029                    $i = $_;                $i = $_;
6030                    last INSCOPE;                last INSCOPE;
6031                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6032  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6033                    !!!cp ('t296');                !!!cp ('t296');
6034                    last INSCOPE;                last INSCOPE;
6035                  }              }
6036                } # INSCOPE            } # INSCOPE
6037                unless (defined $i) {            unless (defined $i) {
6038                  !!!cp ('t297');              !!!cp ('t297');
6039  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6040                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6041                  ## Ignore the </select> token              ## Ignore the </select> token
6042                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6043                  redo B;              !!!next-token; ## TODO: ok?
6044                }              next B;
6045              }
6046                                
6047                !!!cp ('t298');            !!!cp ('t298');
6048                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6049    
6050                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6051    
6052                ## reprocess            !!!ack-later;
6053                redo B;            ## reprocess
6054              next B;
6055          } else {          } else {
6056            !!!cp ('t299');            !!!cp ('t299');
6057            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6058            ## Ignore the token            ## Ignore the token
6059              !!!nack ('t299.3');
6060            !!!next-token;            !!!next-token;
6061            redo B;            next B;
6062            }
6063          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6064            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6065                    @{$self->{open_elements}} == 1) { # redundant, maybe
6066              !!!cp ('t299.1');
6067              !!!parse-error (type => 'in body:#eof', token => $token);
6068            } else {
6069              !!!cp ('t299.2');
6070          }          }
6071    
6072            ## Stop parsing.
6073            last B;
6074        } else {        } else {
6075          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6076        }        }
# Line 5086  sub _tree_construction_main ($) { Line 6086  sub _tree_construction_main ($) {
6086            unless (length $token->{data}) {            unless (length $token->{data}) {
6087              !!!cp ('t300');              !!!cp ('t300');
6088              !!!next-token;              !!!next-token;
6089              redo B;              next B;
6090            }            }
6091          }          }
6092                    
6093          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6094            !!!cp ('t301');            !!!cp ('t301');
6095            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6096    
6097            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6098          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 6100  sub _tree_construction_main ($) {
6100          }          }
6101                    
6102          ## "after body" insertion mode          ## "after body" insertion mode
6103          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6104    
6105          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6106          ## reprocess          ## reprocess
6107          redo B;          next B;
6108        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6109          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6110            !!!cp ('t303');            !!!cp ('t303');
6111            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6112                        
6113            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6114          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 6116  sub _tree_construction_main ($) {
6116          }          }
6117    
6118          ## "after body" insertion mode          ## "after body" insertion mode
6119          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6120    
6121          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6122            !!!ack-later;
6123          ## reprocess          ## reprocess
6124          redo B;          next B;
6125        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6126          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6127            !!!cp ('t305');            !!!cp ('t305');
6128            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6129                        
6130            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6131            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 6137  sub _tree_construction_main ($) {
6137          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6138            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6139              !!!cp ('t307');              !!!cp ('t307');
6140              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6141              ## Ignore the token              ## Ignore the token
6142              !!!next-token;              !!!next-token;
6143              redo B;              next B;
6144            } else {            } else {
6145              !!!cp ('t308');              !!!cp ('t308');
6146              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6147              !!!next-token;              !!!next-token;
6148              redo B;              next B;
6149            }            }
6150          } else {          } else {
6151            !!!cp ('t309');            !!!cp ('t309');
6152            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6153    
6154            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6155            ## reprocess            ## reprocess
6156            redo B;            next B;
6157          }          }
6158          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6159            !!!cp ('t309.2');
6160            ## Stop parsing
6161            last B;
6162        } else {        } else {
6163          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6164        }        }
# Line 5165  sub _tree_construction_main ($) { Line 6170  sub _tree_construction_main ($) {
6170            unless (length $token->{data}) {            unless (length $token->{data}) {
6171              !!!cp ('t310');              !!!cp ('t310');
6172              !!!next-token;              !!!next-token;
6173              redo B;              next B;
6174            }            }
6175          }          }
6176                    
6177          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6178            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6179              !!!cp ('t311');              !!!cp ('t311');
6180              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6181            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6182              !!!cp ('t312');              !!!cp ('t312');
6183              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6184            } else { # "after html frameset"            } else { # "after html frameset"
6185              !!!cp ('t313');              !!!cp ('t313');
6186              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6187    
6188              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6189              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6190              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6191            }            }
6192                        
6193            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 6198  sub _tree_construction_main ($) {
6198              !!!cp ('t315');              !!!cp ('t315');
6199              !!!next-token;              !!!next-token;
6200            }            }
6201            redo B;            next B;
6202          }          }
6203                    
6204          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6205        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6206          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6207            !!!cp ('t316');            !!!cp ('t316');
6208            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6209    
6210            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6211            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 6216  sub _tree_construction_main ($) {
6216          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6217              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6218            !!!cp ('t318');            !!!cp ('t318');
6219            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220              !!!nack ('t318.1');
6221            !!!next-token;            !!!next-token;
6222            redo B;            next B;
6223          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6224                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6225            !!!cp ('t319');            !!!cp ('t319');
6226            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6227            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6228              !!!ack ('t319.1');
6229            !!!next-token;            !!!next-token;
6230            redo B;            next B;
6231          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6232            !!!cp ('t320');            !!!cp ('t320');
6233            ## NOTE: As if in body.            ## NOTE: As if in head.
6234            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6235            redo B;            next B;
6236          } else {          } else {
6237            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6238              !!!cp ('t321');              !!!cp ('t321');
6239              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6240            } else {            } else {
6241              !!!cp ('t322');              !!!cp ('t322');
6242              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6243            }            }
6244            ## Ignore the token            ## Ignore the token
6245              !!!nack ('t322.1');
6246            !!!next-token;            !!!next-token;
6247            redo B;            next B;
6248          }          }
6249        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6250          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6251            !!!cp ('t323');            !!!cp ('t323');
6252            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6253    
6254            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6255            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5251  sub _tree_construction_main ($) { Line 6259  sub _tree_construction_main ($) {
6259    
6260          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6261              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6262            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6264              !!!cp ('t325');              !!!cp ('t325');
6265              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6266              ## Ignore the token              ## Ignore the token
6267              !!!next-token;              !!!next-token;
6268            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 6272  sub _tree_construction_main ($) {
6272            }            }
6273    
6274            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6275                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6276              !!!cp ('t327');              !!!cp ('t327');
6277              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6278            } else {            } else {
6279              !!!cp ('t328');              !!!cp ('t328');
6280            }            }
6281            redo B;            next B;
6282          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6283                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6284            !!!cp ('t329');            !!!cp ('t329');
6285            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6286            !!!next-token;            !!!next-token;
6287            redo B;            next B;
6288          } else {          } else {
6289            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290              !!!cp ('t330');              !!!cp ('t330');
6291              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6292            } else {            } else {
6293              !!!cp ('t331');              !!!cp ('t331');
6294              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6295            }            }
6296            ## Ignore the token            ## Ignore the token
6297            !!!next-token;            !!!next-token;
6298            redo B;            next B;
6299          }          }
6300          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6301            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6302                    @{$self->{open_elements}} == 1) { # redundant, maybe
6303              !!!cp ('t331.1');
6304              !!!parse-error (type => 'in body:#eof', token => $token);
6305            } else {
6306              !!!cp ('t331.2');
6307            }
6308            
6309            ## Stop parsing
6310            last B;
6311        } else {        } else {
6312          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6313        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6322  sub _tree_construction_main ($) {
6322        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6323          !!!cp ('t332');          !!!cp ('t332');
6324          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6325          $script_start_tag->($insert);          $script_start_tag->();
6326          redo B;          next B;
6327        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6328          !!!cp ('t333');          !!!cp ('t333');
6329          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6330          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6331          redo B;          next B;
6332        } elsif ({        } elsif ({
6333                  base => 1, link => 1,                  base => 1, link => 1,
6334                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6335          !!!cp ('t334');          !!!cp ('t334');
6336          ## 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
6337          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6339            !!!ack ('t334.1');
6340          !!!next-token;          !!!next-token;
6341          redo B;          next B;
6342        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6343          ## 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
6344          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6345          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.
6346    
6347          unless ($self->{confident}) {          unless ($self->{confident}) {
6348            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6349              !!!cp ('t335');              !!!cp ('t335');
6350                ## NOTE: Whether the encoding is supported or not is handled
6351                ## in the {change_encoding} callback.
6352              $self->{change_encoding}              $self->{change_encoding}
6353                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6354                            
6355              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6356                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6357                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6358                                           ->{has_reference});                                           ->{has_reference});
6359            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6360              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6361                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6362                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6363                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6364                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6365                !!!cp ('t336');                !!!cp ('t336');
6366                  ## NOTE: Whether the encoding is supported or not is handled
6367                  ## in the {change_encoding} callback.
6368                $self->{change_encoding}                $self->{change_encoding}
6369                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6370                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6371                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6372                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6390  sub _tree_construction_main ($) {
6390            }            }
6391          }          }
6392    
6393            !!!ack ('t338.1');
6394          !!!next-token;          !!!next-token;
6395          redo B;          next B;
6396        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6397          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6398          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6399          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6400            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6401        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6402          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6403                                
6404          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6405              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6406            !!!cp ('t342');            !!!cp ('t342');
6407            ## Ignore the token            ## Ignore the token
6408          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6416  sub _tree_construction_main ($) {
6416              }              }
6417            }            }
6418          }          }
6419            !!!nack ('t343.1');
6420          !!!next-token;          !!!next-token;
6421          redo B;          next B;
6422        } elsif ({        } elsif ({
6423                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6424                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6425                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6426                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6427                  pre => 1,                  pre => 1, listing => 1,
6428                    form => 1,
6429                    table => 1,
6430                    hr => 1,
6431                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6432            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6433              !!!cp ('t350');
6434              !!!parse-error (type => 'in form:form', token => $token);
6435              ## Ignore the token
6436              !!!nack ('t350.1');
6437              !!!next-token;
6438              next B;
6439            }
6440    
6441          ## has a p element in scope          ## has a p element in scope
6442          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6443            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6444              !!!cp ('t344');              !!!cp ('t344');
6445              !!!back-token;              !!!back-token; # <form>
6446              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6447              redo B;                        line => $token->{line}, column => $token->{column}};
6448            } elsif ({              next B;
6449                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6450              !!!cp ('t345');              !!!cp ('t345');
6451              last INSCOPE;              last INSCOPE;
6452            }            }
6453          } # INSCOPE          } # INSCOPE
6454                        
6455          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6457              !!!nack ('t346.1');
6458            !!!next-token;            !!!next-token;
6459            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6460              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6467  sub _tree_construction_main ($) {
6467            } else {            } else {
6468              !!!cp ('t348');              !!!cp ('t348');
6469            }            }
6470          } else {          } elsif ($token->{tag_name} eq 'form') {
6471            !!!cp ('t347');            !!!cp ('t347.1');
6472              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6473    
6474              !!!nack ('t347.2');
6475            !!!next-token;            !!!next-token;
6476          }          } elsif ($token->{tag_name} eq 'table') {
6477          redo B;            !!!cp ('t382');
6478        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6479          if (defined $self->{form_element}) {            
6480            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6481            !!!parse-error (type => 'in form:form');  
6482            ## Ignore the token            !!!nack ('t382.1');
6483              !!!next-token;
6484            } elsif ($token->{tag_name} eq 'hr') {
6485              !!!cp ('t386');
6486              pop @{$self->{open_elements}};
6487            
6488              !!!nack ('t386.1');
6489            !!!next-token;            !!!next-token;
           redo B;  
6490          } else {          } else {
6491            ## 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];  
6492            !!!next-token;            !!!next-token;
           redo B;  
6493          }          }
6494        } elsif ($token->{tag_name} eq 'li') {          next B;
6495          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6496          ## has a p element in scope          ## has a p element in scope
6497          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6498            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6499              !!!cp ('t353');              !!!cp ('t353');
6500              !!!back-token;              !!!back-token; # <x>
6501              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6502              redo B;                        line => $token->{line}, column => $token->{column}};
6503            } elsif ({              next B;
6504                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6505              !!!cp ('t354');              !!!cp ('t354');
6506              last INSCOPE;              last INSCOPE;
6507            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6510  sub _tree_construction_main ($) {
6510          ## Step 1          ## Step 1
6511          my $i = -1;          my $i = -1;
6512          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6513            my $li_or_dtdd = {li => {li => 1},
6514                              dt => {dt => 1, dd => 1},
6515                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6516          LI: {          LI: {
6517            ## Step 2            ## Step 2
6518            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6519              if ($i != -1) {              if ($i != -1) {
6520                !!!cp ('t355');                !!!cp ('t355');
6521                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6522                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6523                                      ->manakai_local_name,
6524                                  token => $token);
6525              } else {              } else {
6526                !!!cp ('t356');                !!!cp ('t356');
6527              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6532  sub _tree_construction_main ($) {
6532            }            }
6533                        
6534            ## Step 3            ## Step 3
6535            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6536                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6537                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6538                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6539                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6540                  not ($node->[1] & DIV_EL)) {
6541              !!!cp ('t358');              !!!cp ('t358');
6542              last LI;              last LI;
6543            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6549  sub _tree_construction_main ($) {
6549            redo LI;            redo LI;
6550          } # LI          } # LI
6551                        
6552          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6553            !!!nack ('t359.1');
6554          !!!next-token;          !!!next-token;
6555          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6556        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6557          ## has a p element in scope          ## has a p element in scope
6558          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6559            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6560              !!!cp ('t367');              !!!cp ('t367');
6561              !!!back-token;              !!!back-token; # <plaintext>
6562              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6563              redo B;                        line => $token->{line}, column => $token->{column}};
6564            } elsif ({              next B;
6565                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6566              !!!cp ('t368');              !!!cp ('t368');
6567              last INSCOPE;              last INSCOPE;
6568            }            }
6569          } # INSCOPE          } # INSCOPE
6570                        
6571          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6572                        
6573          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6574                        
6575            !!!nack ('t368.1');
6576          !!!next-token;          !!!next-token;
6577          redo B;          next B;
6578        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6579          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6580            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6581            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6582              !!!cp ('t371');              !!!cp ('t371');
6583              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6584                            
6585              !!!back-token;              !!!back-token; # <a>
6586              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6587              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6588                $formatting_end_tag->($token);
6589                            
6590              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6591                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 6610  sub _tree_construction_main ($) {
6610                        
6611          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6612    
6613          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6614          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6615    
6616            !!!nack ('t374.1');
6617          !!!next-token;          !!!next-token;
6618          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;  
6619        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6620          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6621    
6622          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6623          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6624            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6625            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6626              !!!cp ('t376');              !!!cp ('t376');
6627              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6628              !!!back-token;              !!!back-token; # <nobr>
6629              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6630              redo B;                        line => $token->{line}, column => $token->{column}};
6631            } elsif ({              next B;
6632                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6633              !!!cp ('t377');              !!!cp ('t377');
6634              last INSCOPE;              last INSCOPE;
6635            }            }
6636          } # INSCOPE          } # INSCOPE
6637                    
6638          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6639          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6640                    
6641            !!!nack ('t377.1');
6642          !!!next-token;          !!!next-token;
6643          redo B;          next B;
6644        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6645          ## has a button element in scope          ## has a button element in scope
6646          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6647            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6648            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6649              !!!cp ('t378');              !!!cp ('t378');
6650              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6651              !!!back-token;              !!!back-token; # <button>
6652              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6653              redo B;                        line => $token->{line}, column => $token->{column}};
6654            } elsif ({              next B;
6655                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6656              !!!cp ('t379');              !!!cp ('t379');
6657              last INSCOPE;              last INSCOPE;
6658            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6660  sub _tree_construction_main ($) {
6660                        
6661          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6662                        
6663          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6664    
6665          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6666    
6667          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6668    
6669            !!!nack ('t379.1');
6670          !!!next-token;          !!!next-token;
6671          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6672        } elsif ({        } elsif ({
6673                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6674                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6675                  image => 1,                  noembed => 1,
6676                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6677                    noscript => 0, ## TODO: 1 if scripting is enabled
6678                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6679          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6680            !!!cp ('t384');            !!!cp ('t381');
6681            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6682          } else {          } else {
6683            !!!cp ('t385');            !!!cp ('t399');
6684          }          }
6685            ## NOTE: There is an "as if in body" code clone.
6686          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6687          $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;  
6688        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6689          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6690                    
6691          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6692            !!!cp ('t389');            !!!cp ('t389');
6693            ## Ignore the token            ## Ignore the token
6694              !!!nack ('t389'); ## NOTE: Not acknowledged.
6695            !!!next-token;            !!!next-token;
6696            redo B;            next B;
6697          } else {          } else {
6698              !!!ack ('t391.1');
6699    
6700            my $at = $token->{attributes};            my $at = $token->{attributes};
6701            my $form_attrs;            my $form_attrs;
6702            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5825  sub _tree_construction_main ($) { Line 6706  sub _tree_construction_main ($) {
6706            delete $at->{prompt};            delete $at->{prompt};
6707            my @tokens = (            my @tokens = (
6708                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6709                           attributes => $form_attrs},                           attributes => $form_attrs,
6710                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6711                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6712                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6713                            {type => START_TAG_TOKEN, tag_name => 'p',
6714                             line => $token->{line}, column => $token->{column}},
6715                            {type => START_TAG_TOKEN, tag_name => 'label',
6716                             line => $token->{line}, column => $token->{column}},
6717                         );                         );
6718            if ($prompt_attr) {            if ($prompt_attr) {
6719              !!!cp ('t390');              !!!cp ('t390');
6720              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6721                               #line => $token->{line}, column => $token->{column},
6722                              };
6723            } else {            } else {
6724              !!!cp ('t391');              !!!cp ('t391');
6725              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6726                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6727                               #line => $token->{line}, column => $token->{column},
6728                              }; # SHOULD
6729              ## TODO: make this configurable              ## TODO: make this configurable
6730            }            }
6731            push @tokens,            push @tokens,
6732                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6733                             line => $token->{line}, column => $token->{column}},
6734                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6735                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6736                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6737                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6738                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6739            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6740                             line => $token->{line}, column => $token->{column}},
6741                            {type => END_TAG_TOKEN, tag_name => 'form',
6742                             line => $token->{line}, column => $token->{column}};
6743            !!!back-token (@tokens);            !!!back-token (@tokens);
6744            redo B;            !!!next-token;
6745              next B;
6746          }          }
6747        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6748          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6749          my $el;          my $el;
6750          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6751                    
6752          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6753          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 6756  sub _tree_construction_main ($) {
6756          $insert->($el);          $insert->($el);
6757                    
6758          my $text = '';          my $text = '';
6759            !!!nack ('t392.1');
6760          !!!next-token;          !!!next-token;
6761          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6762            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 6787  sub _tree_construction_main ($) {
6787            ## Ignore the token            ## Ignore the token
6788          } else {          } else {
6789            !!!cp ('t398');            !!!cp ('t398');
6790            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6791          }          }
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6795                  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');  
6796          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6797    
6798          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6799    
6800            ## "adjust foreign attributes" - done in insert-element-f
6801            
6802            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6803                    
6804          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6805              pop @{$self->{open_elements}};
6806              !!!ack ('t398.1');
6807            } else {
6808              !!!cp ('t398.2');
6809              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6810              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6811              ## mode, "in body" (not "in foreign content") secondary insertion
6812              ## mode, maybe.
6813            }
6814    
6815          !!!next-token;          !!!next-token;
6816          redo B;          next B;
6817        } elsif ({        } elsif ({
6818                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6819                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 6821  sub _tree_construction_main ($) {
6821                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6822                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6823          !!!cp ('t401');          !!!cp ('t401');
6824          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6825          ## Ignore the token          ## Ignore the token
6826            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6827          !!!next-token;          !!!next-token;
6828          redo B;          next B;
6829                    
6830          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6831        } else {        } else {
6832          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6833              !!!cp ('t384');
6834              !!!parse-error (type => 'image', token => $token);
6835              $token->{tag_name} = 'img';
6836            } else {
6837              !!!cp ('t385');
6838            }
6839    
6840            ## NOTE: There is an "as if <br>" code clone.
6841          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6842                    
6843          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6844    
6845            if ({
6846                 applet => 1, marquee => 1, object => 1,
6847                }->{$token->{tag_name}}) {
6848              !!!cp ('t380');
6849              push @$active_formatting_elements, ['#marker', ''];
6850              !!!nack ('t380.1');
6851            } elsif ({
6852                      b => 1, big => 1, em => 1, font => 1, i => 1,
6853                      s => 1, small => 1, strile => 1,
6854                      strong => 1, tt => 1, u => 1,
6855                     }->{$token->{tag_name}}) {
6856              !!!cp ('t375');
6857              push @$active_formatting_elements, $self->{open_elements}->[-1];
6858              !!!nack ('t375.1');
6859            } elsif ($token->{tag_name} eq 'input') {
6860              !!!cp ('t388');
6861              ## TODO: associate with $self->{form_element} if defined
6862              pop @{$self->{open_elements}};
6863              !!!ack ('t388.2');
6864            } elsif ({
6865                      area => 1, basefont => 1, bgsound => 1, br => 1,
6866                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6867                      #image => 1,
6868                     }->{$token->{tag_name}}) {
6869              !!!cp ('t388.1');
6870              pop @{$self->{open_elements}};
6871              !!!ack ('t388.3');
6872            } elsif ($token->{tag_name} eq 'select') {
6873              ## TODO: associate with $self->{form_element} if defined
6874            
6875              if ($self->{insertion_mode} & TABLE_IMS or
6876                  $self->{insertion_mode} & BODY_TABLE_IMS or
6877                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6878                !!!cp ('t400.1');
6879                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6880              } else {
6881                !!!cp ('t400.2');
6882                $self->{insertion_mode} = IN_SELECT_IM;
6883              }
6884              !!!nack ('t400.3');
6885            } else {
6886              !!!nack ('t402');
6887            }
6888                    
6889          !!!next-token;          !!!next-token;
6890          redo B;          next B;
6891        }        }
6892      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6893        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6894          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6895              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6896            for (@{$self->{open_elements}}) {          INSCOPE: {
6897              unless ({            for (reverse @{$self->{open_elements}}) {
6898                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6899                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6900                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6901                      }->{$_->[1]}) {                last INSCOPE;
6902                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6903                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6904              } else {                last;
               !!!cp ('t404');  
6905              }              }
6906            }            }
6907    
6908            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6909            !!!next-token;                            value => $token->{tag_name}, token => $token);
6910            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6911            !!!next-token;            !!!next-token;
6912            redo B;            next B;
6913            } # INSCOPE
6914    
6915            for (@{$self->{open_elements}}) {
6916              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6917                !!!cp ('t403');
6918                !!!parse-error (type => 'not closed',
6919                                value => $_->[0]->manakai_local_name,
6920                                token => $token);
6921                last;
6922              } else {
6923                !!!cp ('t404');
6924              }
6925          }          }
6926    
6927            $self->{insertion_mode} = AFTER_BODY_IM;
6928            !!!next-token;
6929            next B;
6930        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6931          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
6932            ## up-to-date, though it has same effect as speced.
6933            if (@{$self->{open_elements}} > 1 and
6934                $self->{open_elements}->[1]->[1] & BODY_EL) {
6935            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6936            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6937              !!!cp ('t406');              !!!cp ('t406');
6938              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6939                                value => $self->{open_elements}->[1]->[0]
6940                                    ->manakai_local_name,
6941                                token => $token);
6942            } else {            } else {
6943              !!!cp ('t407');              !!!cp ('t407');
6944            }            }
6945            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6946            ## reprocess            ## reprocess
6947            redo B;            next B;
6948          } else {          } else {
6949            !!!cp ('t408');            !!!cp ('t408');
6950            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6951            ## Ignore the token            ## Ignore the token
6952            !!!next-token;            !!!next-token;
6953            redo B;            next B;
6954          }          }
6955        } elsif ({        } elsif ({
6956                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6957                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6958                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6959                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6960                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6961                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6962          ## has an element in scope          ## has an element in scope
6963          my $i;          my $i;
6964          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6965            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6966            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => ($token->{tag_name} ne 'dd'),  
                     dt => ($token->{tag_name} ne 'dt'),  
                     li => ($token->{tag_name} ne 'li'),  
                     p => ($token->{tag_name} ne 'p'),  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               pop @{$self->{open_elements}};  
             }  
               
6967              !!!cp ('t410');              !!!cp ('t410');
6968              $i = $_;              $i = $_;
6969              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6970            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6971              !!!cp ('t411');              !!!cp ('t411');
6972              last INSCOPE;              last INSCOPE;
6973            }            }
6974          } # INSCOPE          } # INSCOPE
6975            
6976          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6977            if (defined $i) {            !!!cp ('t413');
6978              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6979            } else {
6980              ## Step 1. generate implied end tags
6981              while ({
6982                      dd => ($token->{tag_name} ne 'dd'),
6983                      dt => ($token->{tag_name} ne 'dt'),
6984                      li => ($token->{tag_name} ne 'li'),
6985                      p => 1,
6986                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6987                !!!cp ('t409');
6988                pop @{$self->{open_elements}};
6989              }
6990    
6991              ## Step 2.
6992              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6993                      ne $token->{tag_name}) {
6994              !!!cp ('t412');              !!!cp ('t412');
6995              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6996                                value => $self->{open_elements}->[-1]->[0]
6997                                    ->manakai_local_name,
6998                                token => $token);
6999            } else {            } else {
7000              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7001            }            }
7002          }  
7003                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7004            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7005          } elsif ($token->{tag_name} eq 'p') {  
7006            !!!cp ('t415');            ## Step 4.
7007            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7008            my $el;                if {
7009            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7010            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7011          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7012          !!!next-token;          !!!next-token;
7013          redo B;          next B;
7014        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7015            undef $self->{form_element};
7016    
7017          ## has an element in scope          ## has an element in scope
7018            my $i;
7019          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7020            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7021            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
7022              !!!cp ('t418');              !!!cp ('t418');
7023                $i = $_;
7024              last INSCOPE;              last INSCOPE;
7025            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7026              !!!cp ('t419');              !!!cp ('t419');
7027              last INSCOPE;              last INSCOPE;
7028            }            }
7029          } # INSCOPE          } # INSCOPE
7030            
7031          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 {  
7032            !!!cp ('t421');            !!!cp ('t421');
7033            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7034            } else {
7035              ## Step 1. generate implied end tags
7036              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7037                !!!cp ('t417');
7038                pop @{$self->{open_elements}};
7039              }
7040              
7041              ## Step 2.
7042              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7043                      ne $token->{tag_name}) {
7044                !!!cp ('t417.1');
7045                !!!parse-error (type => 'not closed',
7046                                value => $self->{open_elements}->[-1]->[0]
7047                                    ->manakai_local_name,
7048                                token => $token);
7049              } else {
7050                !!!cp ('t420');
7051              }  
7052              
7053              ## Step 3.
7054              splice @{$self->{open_elements}}, $i;
7055          }          }
7056    
         undef $self->{form_element};  
7057          !!!next-token;          !!!next-token;
7058          redo B;          next B;
7059        } elsif ({        } elsif ({
7060                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7061                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6091  sub _tree_construction_main ($) { Line 7063  sub _tree_construction_main ($) {
7063          my $i;          my $i;
7064          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7065            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7066            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
7067              !!!cp ('t423');              !!!cp ('t423');
7068              $i = $_;              $i = $_;
7069              last INSCOPE;              last INSCOPE;
7070            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7071              !!!cp ('t424');              !!!cp ('t424');
7072              last INSCOPE;              last INSCOPE;
7073            }            }
7074          } # INSCOPE          } # INSCOPE
7075    
7076            unless (defined $i) { # has an element in scope
7077              !!!cp ('t425.1');
7078              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7079            } else {
7080              ## Step 1. generate implied end tags
7081              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7082                !!!cp ('t422');
7083                pop @{$self->{open_elements}};
7084              }
7085              
7086              ## Step 2.
7087              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7088                      ne $token->{tag_name}) {
7089                !!!cp ('t425');
7090                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7091              } else {
7092                !!!cp ('t426');
7093              }
7094    
7095              ## Step 3.
7096              splice @{$self->{open_elements}}, $i;
7097            }
7098                    
7099          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7100            !!!cp ('t425');          next B;
7101            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7102            ## has an element in scope
7103            my $i;
7104            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7105              my $node = $self->{open_elements}->[$_];
7106              if ($node->[1] & P_EL) {
7107                !!!cp ('t410.1');
7108                $i = $_;
7109                last INSCOPE;
7110              } elsif ($node->[1] & SCOPING_EL) {
7111                !!!cp ('t411.1');
7112                last INSCOPE;
7113              }
7114            } # INSCOPE
7115    
7116            if (defined $i) {
7117              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7118                      ne $token->{tag_name}) {
7119                !!!cp ('t412.1');
7120                !!!parse-error (type => 'not closed',
7121                                value => $self->{open_elements}->[-1]->[0]
7122                                    ->manakai_local_name,
7123                                token => $token);
7124              } else {
7125                !!!cp ('t414.1');
7126              }
7127    
7128              splice @{$self->{open_elements}}, $i;
7129          } else {          } else {
7130            !!!cp ('t426');            !!!cp ('t413.1');
7131              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7132    
7133              !!!cp ('t415.1');
7134              ## As if <p>, then reprocess the current token
7135              my $el;
7136              !!!create-element ($el, $HTML_NS, 'p',, $token);
7137              $insert->($el);
7138              ## NOTE: Not inserted into |$self->{open_elements}|.
7139          }          }
7140            
         splice @{$self->{open_elements}}, $i if defined $i;  
7141          !!!next-token;          !!!next-token;
7142          redo B;          next B;
7143        } elsif ({        } elsif ({
7144                  a => 1,                  a => 1,
7145                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6131  sub _tree_construction_main ($) { Line 7147  sub _tree_construction_main ($) {
7147                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7148                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7149          !!!cp ('t427');          !!!cp ('t427');
7150          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7151          redo B;          next B;
7152        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7153          !!!cp ('t428');          !!!cp ('t428');
7154          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7155    
7156          ## As if <br>          ## As if <br>
7157          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7158                    
7159          my $el;          my $el;
7160          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7161          $insert->($el);          $insert->($el);
7162                    
7163          ## Ignore the token.          ## Ignore the token.
7164          !!!next-token;          !!!next-token;
7165          redo B;          next B;
7166        } elsif ({        } elsif ({
7167                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7168                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6160  sub _tree_construction_main ($) { Line 7176  sub _tree_construction_main ($) {
7176                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7177                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7178          !!!cp ('t429');          !!!cp ('t429');
7179          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7180          ## Ignore the token          ## Ignore the token
7181          !!!next-token;          !!!next-token;
7182          redo B;          next B;
7183                    
7184          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7185                    
# Line 6174  sub _tree_construction_main ($) { Line 7190  sub _tree_construction_main ($) {
7190    
7191          ## Step 2          ## Step 2
7192          S2: {          S2: {
7193            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7194              ## Step 1              ## Step 1
7195              ## generate implied end tags              ## generate implied end tags
7196              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7197                !!!cp ('t430');                !!!cp ('t430');
7198                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7199                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7200              }              }
7201                    
7202              ## Step 2              ## Step 2
7203              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7204                        ne $token->{tag_name}) {
7205                !!!cp ('t431');                !!!cp ('t431');
7206                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7207                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7208                                  value => $self->{open_elements}->[-1]->[0]
7209                                      ->manakai_local_name,
7210                                  token => $token);
7211              } else {              } else {
7212                !!!cp ('t432');                !!!cp ('t432');
7213              }              }
# Line 6201  sub _tree_construction_main ($) { Line 7219  sub _tree_construction_main ($) {
7219              last S2;              last S2;
7220            } else {            } else {
7221              ## Step 3              ## Step 3
7222              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7223                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7224                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7225                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7226                !!!cp ('t433');                !!!cp ('t433');
7227                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7228                ## Ignore the token                ## Ignore the token
7229                !!!next-token;                !!!next-token;
7230                last S2;                last S2;
# Line 6222  sub _tree_construction_main ($) { Line 7240  sub _tree_construction_main ($) {
7240            ## Step 5;            ## Step 5;
7241            redo S2;            redo S2;
7242          } # S2          } # S2
7243          redo B;          next B;
7244        }        }
7245      }      }
7246      redo B;      next B;
7247      } continue { # B
7248        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7249          ## NOTE: The code below is executed in cases where it does not have
7250          ## to be, but it it is harmless even in those cases.
7251          ## has an element in scope
7252          INSCOPE: {
7253            for (reverse 0..$#{$self->{open_elements}}) {
7254              my $node = $self->{open_elements}->[$_];
7255              if ($node->[1] & FOREIGN_EL) {
7256                last INSCOPE;
7257              } elsif ($node->[1] & SCOPING_EL) {
7258                last;
7259              }
7260            }
7261            
7262            ## NOTE: No foreign element in scope.
7263            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7264          } # INSCOPE
7265        }
7266    } # B    } # B
7267    
7268    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6271  sub set_inner_html ($$$) { Line 7308  sub set_inner_html ($$$) {
7308    
7309      ## Step 8 # MUST      ## Step 8 # MUST
7310      my $i = 0;      my $i = 0;
7311      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7312      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7313      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7314        my $self = shift;        my $self = shift;
7315    
# Line 6281  sub set_inner_html ($$$) { Line 7318  sub set_inner_html ($$$) {
7318    
7319        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7320        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7321        $column++;  
7322          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7323          $p->{column}++;
7324    
7325        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7326          $line++;          $p->{line}++;
7327          $column = 0;          $p->{column} = 0;
7328          !!!cp ('i1');          !!!cp ('i1');
7329        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7330          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7331          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7332          $line++;          $p->{line}++;
7333          $column = 0;          $p->{column} = 0;
7334          !!!cp ('i2');          !!!cp ('i2');
7335        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7336          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6300  sub set_inner_html ($$$) { Line 7339  sub set_inner_html ($$$) {
7339          !!!cp ('i4');          !!!cp ('i4');
7340          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7341          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7342          } elsif ($self->{next_char} <= 0x0008 or
7343                   (0x000E <= $self->{next_char} and
7344                    $self->{next_char} <= 0x001F) or
7345                   (0x007F <= $self->{next_char} and
7346                    $self->{next_char} <= 0x009F) or
7347                   (0xD800 <= $self->{next_char} and
7348                    $self->{next_char} <= 0xDFFF) or
7349                   (0xFDD0 <= $self->{next_char} and
7350                    $self->{next_char} <= 0xFDDF) or
7351                   {
7352                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7353                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7354                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7355                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7356                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7357                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7358                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7359                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7360                    0x10FFFE => 1, 0x10FFFF => 1,
7361                   }->{$self->{next_char}}) {
7362            !!!cp ('i4.1');
7363            !!!parse-error (type => 'control char', level => $self->{must_level});
7364    ## TODO: error type documentation
7365        }        }
7366      };      };
7367      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6307  sub set_inner_html ($$$) { Line 7369  sub set_inner_html ($$$) {
7369            
7370      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7371        my (%opt) = @_;        my (%opt) = @_;
7372        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7373          my $column = $opt{column};
7374          if (defined $opt{token} and defined $opt{token}->{line}) {
7375            $line = $opt{token}->{line};
7376            $column = $opt{token}->{column};
7377          }
7378          warn "Parse error ($opt{type}) at line $line column $column\n";
7379      };      };
7380      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7381        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7382      };      };
7383            
7384      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6334  sub set_inner_html ($$$) { Line 7402  sub set_inner_html ($$$) {
7402          unless defined $p->{content_model};          unless defined $p->{content_model};
7403          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7404    
7405      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7406          ## TODO: Foreign element OK?
7407    
7408      ## Step 3      ## Step 3
7409      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 7413  sub set_inner_html ($$$) {
7413      $doc->append_child ($root);      $doc->append_child ($root);
7414    
7415      ## Step 5 # MUST      ## Step 5 # MUST
7416      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7417    
7418      undef $p->{head_element};      undef $p->{head_element};
7419    
# Line 6390  sub set_inner_html ($$$) { Line 7459  sub set_inner_html ($$$) {
7459      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7460    
7461      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7462    
7463        delete $p->{parse_error}; # delete loop
7464    } else {    } else {
7465      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";
7466    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24