/[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.166 by wakaba, Sat Sep 13 08:21:35 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 177  sub parse_string ($$$;$) { Line 641  sub parse_string ($$$;$) {
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669          $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
677        $line++;        $self->{line}++;
678        $column = 0;        $self->{column} = 0;
679      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 714  sub parse_string ($$$;$) {
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738        level => {must => 'm',
739                  should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
745      $self->{next_char} = -1;      $self->{next_char} = -1;
746    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 802  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
802  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_SECTION_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
814    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
815    
816  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
817  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 828  sub TABLE_IMS ()      { 0b1000000 }
828  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
829  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
830  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
831    sub SELECT_IMS ()     { 0b10000000000 }
832    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
833        ## NOTE: "in foreign content" insertion mode is special; it is combined
834        ## with the secondary insertion mode.  In this parser, they are stored
835        ## together in the bit-or'ed form.
836    
837  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
838    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 855  sub IN_TABLE_IM () { TABLE_IMS }
855  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
856  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
857  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
858  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
859    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
860  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
861    
862  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 333  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 864  sub IN_COLUMN_GROUP_IM () { 0b10 }
864  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
865    my $self = shift;    my $self = shift;
866    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
867      #$self->{state_keyword}; # initialized when used
868    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
869    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
870    undef $self->{current_attribute};    undef $self->{current_attribute};
871    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
872    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
873      delete $self->{self_closing};
874    $self->{char} = [];    $self->{char} = [];
875    # $self->{next_char}    # $self->{next_char}
876    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 891  sub _initialize_tokenizer ($) {
891  ##        ->{value}  ##        ->{value}
892  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
893  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
894    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
895    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
896    ##     while the token is pushed back to the stack.
897    
898  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
899    
# Line 384  sub _initialize_tokenizer ($) { Line 920  sub _initialize_tokenizer ($) {
920    
921  sub _get_next_token ($) {  sub _get_next_token ($) {
922    my $self = shift;    my $self = shift;
923    
924      if ($self->{self_closing}) {
925        !!!parse-error (type => 'nestc', token => $self->{current_token});
926        ## NOTE: The |self_closing| flag is only set by start tag token.
927        ## In addition, when a start tag token is emitted, it is always set to
928        ## |current_token|.
929        delete $self->{self_closing};
930      }
931    
932    if (@{$self->{token}}) {    if (@{$self->{token}}) {
933        $self->{self_closing} = $self->{token}->[0]->{self_closing};
934      return shift @{$self->{token}};      return shift @{$self->{token}};
935    }    }
936    
# Line 447  sub _get_next_token ($) { Line 993  sub _get_next_token ($) {
993          #          #
994        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
995          !!!cp (11);          !!!cp (11);
996          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
997                      line => $self->{line}, column => $self->{column}});
998          last A; ## TODO: ok?          last A; ## TODO: ok?
999        } else {        } else {
1000          !!!cp (12);          !!!cp (12);
1001        }        }
1002        # Anything else        # Anything else
1003        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1004                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1005                       line => $self->{line}, column => $self->{column},
1006                      };
1007        ## Stay in the data state        ## Stay in the data state
1008        !!!next-input-character;        !!!next-input-character;
1009    
# Line 463  sub _get_next_token ($) { Line 1012  sub _get_next_token ($) {
1012        redo A;        redo A;
1013      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1014        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1015    
1016          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1017                
1018        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1019    
# Line 471  sub _get_next_token ($) { Line 1022  sub _get_next_token ($) {
1022    
1023        unless (defined $token) {        unless (defined $token) {
1024          !!!cp (13);          !!!cp (13);
1025          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
1026                      line => $l, column => $c,
1027                     });
1028        } else {        } else {
1029          !!!cp (14);          !!!cp (14);
1030          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 1043  sub _get_next_token ($) {
1043            ## reconsume            ## reconsume
1044            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1045    
1046            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1047                        line => $self->{line_prev},
1048                        column => $self->{column_prev},
1049                       });
1050    
1051            redo A;            redo A;
1052          }          }
# Line 510  sub _get_next_token ($) { Line 1066  sub _get_next_token ($) {
1066            !!!cp (19);            !!!cp (19);
1067            $self->{current_token}            $self->{current_token}
1068              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1069                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1070                   line => $self->{line_prev},
1071                   column => $self->{column_prev}};
1072            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1073            !!!next-input-character;            !!!next-input-character;
1074            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1076  sub _get_next_token ($) {
1076                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1077            !!!cp (20);            !!!cp (20);
1078            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1079                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1080                                        line => $self->{line_prev},
1081                                        column => $self->{column_prev}};
1082            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1083            !!!next-input-character;            !!!next-input-character;
1084            redo A;            redo A;
1085          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1086            !!!cp (21);            !!!cp (21);
1087            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1088                              line => $self->{line_prev},
1089                              column => $self->{column_prev});
1090            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1091            !!!next-input-character;            !!!next-input-character;
1092    
1093            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1094                        line => $self->{line_prev},
1095                        column => $self->{column_prev},
1096                       });
1097    
1098            redo A;            redo A;
1099          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1100            !!!cp (22);            !!!cp (22);
1101            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1102                              line => $self->{line_prev},
1103                              column => $self->{column_prev});
1104            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1105              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1106                                        line => $self->{line_prev},
1107                                        column => $self->{column_prev},
1108                                       };
1109            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1110            redo A;            redo A;
1111          } else {          } else {
1112            !!!cp (23);            !!!cp (23);
1113            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1114                              line => $self->{line_prev},
1115                              column => $self->{column_prev});
1116            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1117            ## reconsume            ## reconsume
1118    
1119            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1120                        line => $self->{line_prev},
1121                        column => $self->{column_prev},
1122                       });
1123    
1124            redo A;            redo A;
1125          }          }
# Line 551  sub _get_next_token ($) { Line 1127  sub _get_next_token ($) {
1127          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1128        }        }
1129      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1130          ## NOTE: The "close tag open state" in the spec is implemented as
1131          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1132    
1133          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1134        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1135          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1136            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1137            my @next_char;            $self->{state_keyword} = '';
1138            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1139              push @next_char, $self->{next_char};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1140          } else {          } else {
1141            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1142              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1143            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1144            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1145            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1146              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                        line => $l, column => $c,
1148                       });
1149            redo A;            redo A;
1150          }          }
1151        }        }
1152          
1153        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1154            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1155          !!!cp (29);          !!!cp (29);
1156          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1157                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1158                   tag_name => chr ($self->{next_char} + 0x0020),
1159                   line => $l, column => $c};
1160          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1161          !!!next-input-character;          !!!next-input-character;
1162          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1165          !!!cp (30);          !!!cp (30);
1166          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1167                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1168                                      line => $l, column => $c};
1169          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1170          !!!next-input-character;          !!!next-input-character;
1171          redo A;          redo A;
1172        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1173          !!!cp (31);          !!!cp (31);
1174          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1175                            line => $self->{line_prev}, ## "<" in "</>"
1176                            column => $self->{column_prev} - 1);
1177          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1183  sub _get_next_token ($) {
1183          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1184          # reconsume          # reconsume
1185    
1186          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1187                      line => $l, column => $c,
1188                     });
1189    
1190          redo A;          redo A;
1191        } else {        } else {
1192          !!!cp (33);          !!!cp (33);
1193          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1194          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1195          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1196          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1197                                      column => $self->{column_prev} - 1,
1198                                     };
1199            ## NOTE: $self->{next_char} is intentionally left as is.
1200            ## Although the "anything else" case of the spec not explicitly
1201            ## states that the next input character is to be reconsumed,
1202            ## it will be included to the |data| of the comment token
1203            ## generated from the bogus end tag, as defined in the
1204            ## "bogus comment state" entry.
1205            redo A;
1206          }
1207        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1208          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1209          if (length $ch) {
1210            my $CH = $ch;
1211            $ch =~ tr/a-z/A-Z/;
1212            my $nch = chr $self->{next_char};
1213            if ($nch eq $ch or $nch eq $CH) {
1214              !!!cp (24);
1215              ## Stay in the state.
1216              $self->{state_keyword} .= $nch;
1217              !!!next-input-character;
1218              redo A;
1219            } else {
1220              !!!cp (25);
1221              $self->{state} = DATA_STATE;
1222              ## Reconsume.
1223              !!!emit ({type => CHARACTER_TOKEN,
1224                        data => '</' . $self->{state_keyword},
1225                        line => $self->{line_prev},
1226                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1227                       });
1228              redo A;
1229            }
1230          } else { # after "<{tag-name}"
1231            unless ({
1232                     0x0009 => 1, # HT
1233                     0x000A => 1, # LF
1234                     0x000B => 1, # VT
1235                     0x000C => 1, # FF
1236                     0x0020 => 1, # SP
1237                     0x003E => 1, # >
1238                     0x002F => 1, # /
1239                     -1 => 1, # EOF
1240                    }->{$self->{next_char}}) {
1241              !!!cp (26);
1242              ## Reconsume.
1243              $self->{state} = DATA_STATE;
1244              !!!emit ({type => CHARACTER_TOKEN,
1245                        data => '</' . $self->{state_keyword},
1246                        line => $self->{line_prev},
1247                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1248                       });
1249              redo A;
1250            } else {
1251              !!!cp (27);
1252              $self->{current_token}
1253                  = {type => END_TAG_TOKEN,
1254                     tag_name => $self->{last_emitted_start_tag_name},
1255                     line => $self->{line_prev},
1256                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1257              $self->{state} = TAG_NAME_STATE;
1258              ## Reconsume.
1259              redo A;
1260            }
1261        }        }
1262      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1263        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 657  sub _get_next_token ($) { Line 1272  sub _get_next_token ($) {
1272        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1273          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1274            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1275            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1276          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1277            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1303  sub _get_next_token ($) {
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323    
1324          redo A;          redo A;
1325        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1326            !!!cp (42);
1327            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1328          !!!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  
1329          redo A;          redo A;
1330        } else {        } else {
1331          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1349          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1350            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1351            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1352          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1353            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1370                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1371          !!!cp (49);          !!!cp (49);
1372          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1373                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1374                   value => '',
1375                   line => $self->{line}, column => $self->{column}};
1376          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1377          !!!next-input-character;          !!!next-input-character;
1378          redo A;          redo A;
1379        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1380            !!!cp (50);
1381            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1382          !!!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  
1383          redo A;          redo A;
1384        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1385          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1386          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1387            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1388            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1389          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1390            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1414  sub _get_next_token ($) {
1414          } else {          } else {
1415            !!!cp (56);            !!!cp (56);
1416          }          }
1417          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1418                                value => ''};              = {name => chr ($self->{next_char}),
1419                   value => '',
1420                   line => $self->{line}, column => $self->{column}};
1421          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1427  sub _get_next_token ($) {
1427          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1428              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1429            !!!cp (57);            !!!cp (57);
1430            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1431            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1432          } else {          } else {
1433            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1456  sub _get_next_token ($) {
1456          $before_leave->();          $before_leave->();
1457          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1458            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1459            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1460          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1461            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1480  sub _get_next_token ($) {
1480          !!!next-input-character;          !!!next-input-character;
1481          redo A;          redo A;
1482        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1483            !!!cp (64);
1484          $before_leave->();          $before_leave->();
1485            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1486          !!!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  
1487          redo A;          redo A;
1488        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1489          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1490          $before_leave->();          $before_leave->();
1491          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1492            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1493            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1494          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1495            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1540  sub _get_next_token ($) {
1540        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1541          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1542            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1543            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1544          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1545            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1562  sub _get_next_token ($) {
1562        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1563                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1564          !!!cp (76);          !!!cp (76);
1565          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1566                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1567                   value => '',
1568                   line => $self->{line}, column => $self->{column}};
1569          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1573            !!!cp (77);
1574            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1575          !!!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  
1576          redo A;          redo A;
1577        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1578          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1579          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1580            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1581            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1582          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1583            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1034  sub _get_next_token ($) { Line 1598  sub _get_next_token ($) {
1598    
1599          redo A;          redo A;
1600        } else {        } else {
1601          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1602          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1603                                value => ''};            !!!cp (78);
1604              !!!parse-error (type => 'bad attribute name');
1605            } else {
1606              !!!cp (82);
1607            }
1608            $self->{current_attribute}
1609                = {name => chr ($self->{next_char}),
1610                   value => '',
1611                   line => $self->{line}, column => $self->{column}};
1612          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;                  redo A;        
# Line 1067  sub _get_next_token ($) { Line 1639  sub _get_next_token ($) {
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1642            !!!parse-error (type => 'empty unquoted attribute value');
1643          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1644            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1645            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1646          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1647            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1666          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1712  sub _get_next_token ($) {
1712          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1713          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1714            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1716          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1717            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1754  sub _get_next_token ($) {
1754          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1755          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1756            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1757            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1758          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1759            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1799  sub _get_next_token ($) {
1799        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1800          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1801            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1802            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1803          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1804            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1822  sub _get_next_token ($) {
1822          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1823          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1824            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1825            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1826          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1827            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1892  sub _get_next_token ($) {
1892        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1893          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1894            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1896          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1897            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1912  sub _get_next_token ($) {
1912    
1913          redo A;          redo A;
1914        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1915            !!!cp (122);
1916            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1917          !!!next-input-character;          !!!next-input-character;
1918          if ($self->{next_char} == 0x003E and # >          redo A;
1919              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1920              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1921            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1922            !!!cp (122);            !!!cp (122.3);
1923            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1924            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1925              if ($self->{current_token}->{attributes}) {
1926                !!!cp (122.1);
1927                !!!parse-error (type => 'end tag attribute');
1928              } else {
1929                ## NOTE: This state should never be reached.
1930                !!!cp (122.2);
1931              }
1932          } else {          } else {
1933            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1934          }          }
1935          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1936          # next-input-character is already done          ## Reconsume.
1937            !!!emit ($self->{current_token}); # start tag or end tag
1938          redo A;          redo A;
1939        } else {        } else {
1940          !!!cp (124);          !!!cp ('124.1');
1941          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1942          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1943          ## reconsume          ## reconsume
1944          redo A;          redo A;
1945        }        }
1946        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1947          if ($self->{next_char} == 0x003E) { # >
1948            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1949              !!!cp ('124.2');
1950              !!!parse-error (type => 'nestc', token => $self->{current_token});
1951              ## TODO: Different type than slash in start tag
1952              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1953              if ($self->{current_token}->{attributes}) {
1954                !!!cp ('124.4');
1955                !!!parse-error (type => 'end tag attribute');
1956              } else {
1957                !!!cp ('124.5');
1958              }
1959              ## TODO: Test |<title></title/>|
1960            } else {
1961              !!!cp ('124.3');
1962              $self->{self_closing} = 1;
1963            }
1964    
1965            $self->{state} = DATA_STATE;
1966            !!!next-input-character;
1967    
1968            !!!emit ($self->{current_token}); # start tag or end tag
1969    
1970            redo A;
1971          } elsif ($self->{next_char} == -1) {
1972            !!!parse-error (type => 'unclosed tag');
1973            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1974              !!!cp (124.7);
1975              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1976            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1977              if ($self->{current_token}->{attributes}) {
1978                !!!cp (124.5);
1979                !!!parse-error (type => 'end tag attribute');
1980              } else {
1981                ## NOTE: This state should never be reached.
1982                !!!cp (124.6);
1983              }
1984            } else {
1985              die "$0: $self->{current_token}->{type}: Unknown token type";
1986            }
1987            $self->{state} = DATA_STATE;
1988            ## Reconsume.
1989            !!!emit ($self->{current_token}); # start tag or end tag
1990            redo A;
1991          } else {
1992            !!!cp ('124.4');
1993            !!!parse-error (type => 'nestc');
1994            ## TODO: This error type is wrong.
1995            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1996            ## Reconsume.
1997            redo A;
1998          }
1999      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2000        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2001                
2002        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
2003          #my $token = {type => COMMENT_TOKEN, data => ''};
2004    
2005        BC: {        BC: {
2006          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 2008  sub _get_next_token ($) {
2008            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
2009            !!!next-input-character;            !!!next-input-character;
2010    
2011            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
2012    
2013            redo A;            redo A;
2014          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 2016  sub _get_next_token ($) {
2016            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
2017            ## reconsume            ## reconsume
2018    
2019            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
2020    
2021            redo A;            redo A;
2022          } else {          } else {
2023            !!!cp (126);            !!!cp (126);
2024            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2025            !!!next-input-character;            !!!next-input-character;
2026            redo BC;            redo BC;
2027          }          }
# Line 1407  sub _get_next_token ($) { Line 2030  sub _get_next_token ($) {
2030        die "$0: _get_next_token: unexpected case [BC]";        die "$0: _get_next_token: unexpected case [BC]";
2031      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2032        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2033                
2034        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2035            !!!cp (133);
2036            $self->{state} = MD_HYPHEN_STATE;
2037          !!!next-input-character;          !!!next-input-character;
2038          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2039        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2040                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2041            ## ASCII case-insensitive.
2042            !!!cp (130);
2043            $self->{state} = MD_DOCTYPE_STATE;
2044            $self->{state_keyword} = chr $self->{next_char};
2045          !!!next-input-character;          !!!next-input-character;
2046          push @next_char, $self->{next_char};          redo A;
2047          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2048              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2049            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2050            push @next_char, $self->{next_char};          !!!cp (135.4);                
2051            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2052                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2053              !!!next-input-character;          !!!next-input-character;
2054              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2055        } else {        } else {
2056          !!!cp (136);          !!!cp (136);
2057        }        }
2058    
2059        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2060        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2061        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2062          ## Reconsume.
2063        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2064          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065                                    line => $self->{line_prev},
2066                                    column => $self->{column_prev} - 1,
2067                                   };
2068        redo A;        redo A;
2069              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2071        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2072            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073                                      line => $self->{line_prev},
2074                                      column => $self->{column_prev} - 2,
2075                                     };
2076            $self->{state} = COMMENT_START_STATE;
2077            !!!next-input-character;
2078            redo A;
2079          } else {
2080            !!!cp (128);
2081            !!!parse-error (type => 'bogus comment',
2082                            line => $self->{line_prev},
2083                            column => $self->{column_prev} - 2);
2084            $self->{state} = BOGUS_COMMENT_STATE;
2085            ## Reconsume.
2086            $self->{current_token} = {type => COMMENT_TOKEN,
2087                                      data => '-',
2088                                      line => $self->{line_prev},
2089                                      column => $self->{column_prev} - 2,
2090                                     };
2091            redo A;
2092          }
2093        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2094          ## ASCII case-insensitive.
2095          if ($self->{next_char} == [
2096                undef,
2097                0x004F, # O
2098                0x0043, # C
2099                0x0054, # T
2100                0x0059, # Y
2101                0x0050, # P
2102              ]->[length $self->{state_keyword}] or
2103              $self->{next_char} == [
2104                undef,
2105                0x006F, # o
2106                0x0063, # c
2107                0x0074, # t
2108                0x0079, # y
2109                0x0070, # p
2110              ]->[length $self->{state_keyword}]) {
2111            !!!cp (131);
2112            ## Stay in the state.
2113            $self->{state_keyword} .= chr $self->{next_char};
2114            !!!next-input-character;
2115            redo A;
2116          } elsif ((length $self->{state_keyword}) == 6 and
2117                   ($self->{next_char} == 0x0045 or # E
2118                    $self->{next_char} == 0x0065)) { # e
2119            !!!cp (129);
2120            $self->{state} = DOCTYPE_STATE;
2121            $self->{current_token} = {type => DOCTYPE_TOKEN,
2122                                      quirks => 1,
2123                                      line => $self->{line_prev},
2124                                      column => $self->{column_prev} - 7,
2125                                     };
2126            !!!next-input-character;
2127            redo A;
2128          } else {
2129            !!!cp (132);        
2130            !!!parse-error (type => 'bogus comment',
2131                            line => $self->{line_prev},
2132                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2133            $self->{state} = BOGUS_COMMENT_STATE;
2134            ## Reconsume.
2135            $self->{current_token} = {type => COMMENT_TOKEN,
2136                                      data => $self->{state_keyword},
2137                                      line => $self->{line_prev},
2138                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2139                                     };
2140            redo A;
2141          }
2142        } elsif ($self->{state} == MD_CDATA_STATE) {
2143          if ($self->{next_char} == {
2144                '[' => 0x0043, # C
2145                '[C' => 0x0044, # D
2146                '[CD' => 0x0041, # A
2147                '[CDA' => 0x0054, # T
2148                '[CDAT' => 0x0041, # A
2149              }->{$self->{state_keyword}}) {
2150            !!!cp (135.1);
2151            ## Stay in the state.
2152            $self->{state_keyword} .= chr $self->{next_char};
2153            !!!next-input-character;
2154            redo A;
2155          } elsif ($self->{state_keyword} eq '[CDATA' and
2156                   $self->{next_char} == 0x005B) { # [
2157            !!!cp (135.2);
2158            $self->{current_token} = {type => CHARACTER_TOKEN,
2159                                      data => '',
2160                                      line => $self->{line_prev},
2161                                      column => $self->{column_prev} - 7};
2162            $self->{state} = CDATA_SECTION_STATE;
2163            !!!next-input-character;
2164            redo A;
2165          } else {
2166            !!!cp (135.3);
2167            !!!parse-error (type => 'bogus comment',
2168                            line => $self->{line_prev},
2169                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2170            $self->{state} = BOGUS_COMMENT_STATE;
2171            ## Reconsume.
2172            $self->{current_token} = {type => COMMENT_TOKEN,
2173                                      data => $self->{state_keyword},
2174                                      line => $self->{line_prev},
2175                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2176                                     };
2177            redo A;
2178          }
2179      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2180        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2181          !!!cp (137);          !!!cp (137);
# Line 1603  sub _get_next_token ($) { Line 2295  sub _get_next_token ($) {
2295          redo A;          redo A;
2296        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2297          !!!cp (152);          !!!cp (152);
2298          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2299                            line => $self->{line_prev},
2300                            column => $self->{column_prev});
2301          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2302          ## Stay in the state          ## Stay in the state
2303          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2313  sub _get_next_token ($) {
2313          redo A;          redo A;
2314        } else {        } else {
2315          !!!cp (154);          !!!cp (154);
2316          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2317                            line => $self->{line_prev},
2318                            column => $self->{column_prev});
2319          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2320          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2321          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2354  sub _get_next_token ($) {
2354          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2355          !!!next-input-character;          !!!next-input-character;
2356    
2357          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2358    
2359          redo A;          redo A;
2360        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2363  sub _get_next_token ($) {
2363          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2364          ## reconsume          ## reconsume
2365    
2366          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2367    
2368          redo A;          redo A;
2369        } else {        } else {
2370          !!!cp (160);          !!!cp (160);
2371          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2372              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2373  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2374          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2375          !!!next-input-character;          !!!next-input-character;
# Line 1749  sub _get_next_token ($) { Line 2442  sub _get_next_token ($) {
2442          redo A;          redo A;
2443        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2444                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2445            $self->{state} = PUBLIC_STATE;
2446            $self->{state_keyword} = chr $self->{next_char};
2447          !!!next-input-character;          !!!next-input-character;
2448          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
2449        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2450                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2451            $self->{state} = SYSTEM_STATE;
2452            $self->{state_keyword} = chr $self->{next_char};
2453          !!!next-input-character;          !!!next-input-character;
2454          if ($self->{next_char} == 0x0059 or # Y          redo A;
             $self->{next_char} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0053 or # S  
               $self->{next_char} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0045 or # E  
                   $self->{next_char} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x004D or # M  
                     $self->{next_char} == 0x006D) { # m  
                   !!!cp (174);  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (175);  
                 }  
               } else {  
                 !!!cp (176);  
               }  
             } else {  
               !!!cp (177);  
             }  
           } else {  
             !!!cp (178);  
           }  
         } else {  
           !!!cp (179);  
         }  
   
         #  
2455        } else {        } else {
2456          !!!cp (180);          !!!cp (180);
2457            !!!parse-error (type => 'string after DOCTYPE name');
2458            $self->{current_token}->{quirks} = 1;
2459    
2460            $self->{state} = BOGUS_DOCTYPE_STATE;
2461          !!!next-input-character;          !!!next-input-character;
2462          #          redo A;
2463        }        }
2464        } elsif ($self->{state} == PUBLIC_STATE) {
2465          ## ASCII case-insensitive
2466          if ($self->{next_char} == [
2467                undef,
2468                0x0055, # U
2469                0x0042, # B
2470                0x004C, # L
2471                0x0049, # I
2472              ]->[length $self->{state_keyword}] or
2473              $self->{next_char} == [
2474                undef,
2475                0x0075, # u
2476                0x0062, # b
2477                0x006C, # l
2478                0x0069, # i
2479              ]->[length $self->{state_keyword}]) {
2480            !!!cp (175);
2481            ## Stay in the state.
2482            $self->{state_keyword} .= chr $self->{next_char};
2483            !!!next-input-character;
2484            redo A;
2485          } elsif ((length $self->{state_keyword}) == 5 and
2486                   ($self->{next_char} == 0x0043 or # C
2487                    $self->{next_char} == 0x0063)) { # c
2488            !!!cp (168);
2489            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2490            !!!next-input-character;
2491            redo A;
2492          } else {
2493            !!!cp (169);
2494            !!!parse-error (type => 'string after DOCTYPE name',
2495                            line => $self->{line_prev},
2496                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2497            $self->{current_token}->{quirks} = 1;
2498    
2499        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2500        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2501            redo A;
2502          }
2503        } elsif ($self->{state} == SYSTEM_STATE) {
2504          ## ASCII case-insensitive
2505          if ($self->{next_char} == [
2506                undef,
2507                0x0059, # Y
2508                0x0053, # S
2509                0x0054, # T
2510                0x0045, # E
2511              ]->[length $self->{state_keyword}] or
2512              $self->{next_char} == [
2513                undef,
2514                0x0079, # y
2515                0x0073, # s
2516                0x0074, # t
2517                0x0065, # e
2518              ]->[length $self->{state_keyword}]) {
2519            !!!cp (170);
2520            ## Stay in the state.
2521            $self->{state_keyword} .= chr $self->{next_char};
2522            !!!next-input-character;
2523            redo A;
2524          } elsif ((length $self->{state_keyword}) == 5 and
2525                   ($self->{next_char} == 0x004D or # M
2526                    $self->{next_char} == 0x006D)) { # m
2527            !!!cp (171);
2528            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2529            !!!next-input-character;
2530            redo A;
2531          } else {
2532            !!!cp (172);
2533            !!!parse-error (type => 'string after DOCTYPE name',
2534                            line => $self->{line_prev},
2535                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2536            $self->{current_token}->{quirks} = 1;
2537    
2538        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2539        # next-input-character is already done          ## Reconsume.
2540        redo A;          redo A;
2541          }
2542      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2543        if ({        if ({
2544              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2067  sub _get_next_token ($) { Line 2771  sub _get_next_token ($) {
2771          redo A;          redo A;
2772        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2773          !!!cp (208);          !!!cp (208);
2774          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2775    
2776          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2777          !!!next-input-character;          !!!next-input-character;
# Line 2103  sub _get_next_token ($) { Line 2807  sub _get_next_token ($) {
2807          redo A;          redo A;
2808        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2809          !!!cp (212);          !!!cp (212);
2810          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2811    
2812          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2813          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2855  sub _get_next_token ($) {
2855        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2856          !!!cp (217);          !!!cp (217);
2857          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2859          ## reconsume          ## reconsume
2860    
# Line 2192  sub _get_next_token ($) { Line 2895  sub _get_next_token ($) {
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
2897        }        }
2898        } elsif ($self->{state} == CDATA_SECTION_STATE) {
2899          ## NOTE: "CDATA section state" in the state is jointly implemented
2900          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2901          ## and |CDATA_SECTION_MSE2_STATE|.
2902          
2903          if ($self->{next_char} == 0x005D) { # ]
2904            !!!cp (221.1);
2905            $self->{state} = CDATA_SECTION_MSE1_STATE;
2906            !!!next-input-character;
2907            redo A;
2908          } elsif ($self->{next_char} == -1) {
2909            $self->{state} = DATA_STATE;
2910            !!!next-input-character;
2911            if (length $self->{current_token}->{data}) { # character
2912              !!!cp (221.2);
2913              !!!emit ($self->{current_token}); # character
2914            } else {
2915              !!!cp (221.3);
2916              ## No token to emit. $self->{current_token} is discarded.
2917            }        
2918            redo A;
2919          } else {
2920            !!!cp (221.4);
2921            $self->{current_token}->{data} .= chr $self->{next_char};
2922            ## Stay in the state.
2923            !!!next-input-character;
2924            redo A;
2925          }
2926    
2927          ## ISSUE: "text tokens" in spec.
2928        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2929          if ($self->{next_char} == 0x005D) { # ]
2930            !!!cp (221.5);
2931            $self->{state} = CDATA_SECTION_MSE2_STATE;
2932            !!!next-input-character;
2933            redo A;
2934          } else {
2935            !!!cp (221.6);
2936            $self->{current_token}->{data} .= ']';
2937            $self->{state} = CDATA_SECTION_STATE;
2938            ## Reconsume.
2939            redo A;
2940          }
2941        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2942          if ($self->{next_char} == 0x003E) { # >
2943            $self->{state} = DATA_STATE;
2944            !!!next-input-character;
2945            if (length $self->{current_token}->{data}) { # character
2946              !!!cp (221.7);
2947              !!!emit ($self->{current_token}); # character
2948            } else {
2949              !!!cp (221.8);
2950              ## No token to emit. $self->{current_token} is discarded.
2951            }
2952            redo A;
2953          } elsif ($self->{next_char} == 0x005D) { # ]
2954            !!!cp (221.9); # character
2955            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2956            ## Stay in the state.
2957            !!!next-input-character;
2958            redo A;
2959          } else {
2960            !!!cp (221.11);
2961            $self->{current_token}->{data} .= ']]'; # character
2962            $self->{state} = CDATA_SECTION_STATE;
2963            ## Reconsume.
2964            redo A;
2965          }
2966      } else {      } else {
2967        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2968      }      }
# Line 2203  sub _get_next_token ($) { Line 2974  sub _get_next_token ($) {
2974  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2975    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2976    
2977      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2978    
2979    if ({    if ({
2980         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2981         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 3016  sub _tokenize_attempt_to_consume_an_enti
3016            redo X;            redo X;
3017          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
3018            !!!cp (1005);            !!!cp (1005);
3019            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
3020            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
3021            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
3022            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 3025  sub _tokenize_attempt_to_consume_an_enti
3025            !!!next-input-character;            !!!next-input-character;
3026          } else {          } else {
3027            !!!cp (1007);            !!!cp (1007);
3028            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
3029          }          }
3030    
3031          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3032            !!!cp (1008);            !!!cp (1008);
3033            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => 'invalid character reference',
3034                              text => (sprintf 'U+%04X', $code),
3035                              line => $l, column => $c);
3036            $code = 0xFFFD;            $code = 0xFFFD;
3037          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
3038            !!!cp (1009);            !!!cp (1009);
3039            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => 'invalid character reference',
3040                              text => (sprintf 'U-%08X', $code),
3041                              line => $l, column => $c);
3042            $code = 0xFFFD;            $code = 0xFFFD;
3043          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
3044            !!!cp (1010);            !!!cp (1010);
3045            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3046            $code = 0x000A;            $code = 0x000A;
3047          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
3048            !!!cp (1011);            !!!cp (1011);
3049            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3050            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
3051          }          }
3052    
3053          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
3054                  has_reference => 1};                  has_reference => 1,
3055                    line => $l, column => $c,
3056                   };
3057        } # X        } # X
3058      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
3059               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 3074  sub _tokenize_attempt_to_consume_an_enti
3074          !!!next-input-character;          !!!next-input-character;
3075        } else {        } else {
3076          !!!cp (1014);          !!!cp (1014);
3077          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
3078        }        }
3079    
3080        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3081          !!!cp (1015);          !!!cp (1015);
3082          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3083                            text => (sprintf 'U+%04X', $code),
3084                            line => $l, column => $c);
3085          $code = 0xFFFD;          $code = 0xFFFD;
3086        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3087          !!!cp (1016);          !!!cp (1016);
3088          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3089                            text => (sprintf 'U-%08X', $code),
3090                            line => $l, column => $c);
3091          $code = 0xFFFD;          $code = 0xFFFD;
3092        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3093          !!!cp (1017);          !!!cp (1017);
3094          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3095                            line => $l, column => $c);
3096          $code = 0x000A;          $code = 0x000A;
3097        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3098          !!!cp (1018);          !!!cp (1018);
3099          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3100                            text => (sprintf 'U+%04X', $code),
3101                            line => $l, column => $c);
3102          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3103        }        }
3104                
3105        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3106                  line => $l, column => $c,
3107                 };
3108      } else {      } else {
3109        !!!cp (1019);        !!!cp (1019);
3110        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3111        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
3112        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
3113        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 3124  sub _tokenize_attempt_to_consume_an_enti
3124      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3125      our $EntityChar;      our $EntityChar;
3126    
3127      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3128             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3129             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3130               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 3157  sub _tokenize_attempt_to_consume_an_enti
3157            
3158      if ($match > 0) {      if ($match > 0) {
3159        !!!cp (1023);        !!!cp (1023);
3160        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3161                  line => $l, column => $c,
3162                 };
3163      } elsif ($match < 0) {      } elsif ($match < 0) {
3164        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3165        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3166          !!!cp (1024);          !!!cp (1024);
3167          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3168                    line => $l, column => $c,
3169                   };
3170        } else {        } else {
3171          !!!cp (1025);          !!!cp (1025);
3172          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3173                    line => $l, column => $c,
3174                   };
3175        }        }
3176      } else {      } else {
3177        !!!cp (1026);        !!!cp (1026);
3178        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3179        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3180        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3181                  line => $l, column => $c,
3182                 };
3183      }      }
3184    } else {    } else {
3185      !!!cp (1027);      !!!cp (1027);
3186      ## no characters are consumed      ## no characters are consumed
3187      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3188      return undef;      return undef;
3189    }    }
3190  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2400  sub _initialize_tree_constructor ($) { Line 3196  sub _initialize_tree_constructor ($) {
3196    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3197    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3198    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3199      $self->{document}->set_user_data (manakai_source_line => 1);
3200      $self->{document}->set_user_data (manakai_source_column => 1);
3201  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3202    
3203  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2454  sub _tree_construction_initial ($) { Line 3252  sub _tree_construction_initial ($) {
3252        ## language.        ## language.
3253        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3254        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3255        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3256        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3257            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3258          !!!cp ('t1');          !!!cp ('t1');
3259          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3260        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3261          !!!cp ('t2');          !!!cp ('t2');
3262          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3263          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3264            if ($token->{public_identifier} eq 'XSLT-compat') {
3265              !!!cp ('t1.2');
3266              !!!parse-error (type => 'XSLT-compat', token => $token,
3267                              level => $self->{level}->{should});
3268            } else {
3269              !!!parse-error (type => 'not HTML5', token => $token);
3270            }
3271        } else {        } else {
3272          !!!cp ('t3');          !!!cp ('t3');
3273            #
3274        }        }
3275                
3276        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3277          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3278          ## NOTE: Default value for both |public_id| and |system_id| attributes
3279          ## are empty strings, so that we don't set any value in missing cases.
3280        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3281            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3282        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3291  sub _tree_construction_initial ($) {
3291        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3292          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3293          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3294          if ({          my $prefix = [
3295            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3296            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3297            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3298            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3299            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3300            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3301            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3302            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3303            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3304            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3305            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3306            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3307            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3308            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3309            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3310            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3311            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3312            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3313            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3314            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3315            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3316            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3317            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3318            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3319            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3320            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3321            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3322            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3323            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3324            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3325            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3326            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3327            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3328            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3329            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3330            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3331            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3332            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3333            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3334            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3335            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3336            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3337            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3338            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3339            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3340            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3341            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3342            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3343            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3344            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3345            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3346            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3347            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3348            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3349            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3350            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3351            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3352            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3353            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3354            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3355            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3356            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3357            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3358            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3359            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3360            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3361            "-//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}) {  
3362            !!!cp ('t5');            !!!cp ('t5');
3363            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3364          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3365                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3366            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3367              !!!cp ('t6');              !!!cp ('t6');
3368              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3370  sub _tree_construction_initial ($) {
3370              !!!cp ('t7');              !!!cp ('t7');
3371              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3372            }            }
3373          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3374                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3375            !!!cp ('t8');            !!!cp ('t8');
3376            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3377          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3384  sub _tree_construction_initial ($) {
3384          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3385          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3386          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") {
3387            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3388              ## marked as quirks.
3389            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3390            !!!cp ('t11');            !!!cp ('t11');
3391          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3404  sub _tree_construction_initial ($) {
3404                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3405               }->{$token->{type}}) {               }->{$token->{type}}) {
3406        !!!cp ('t14');        !!!cp ('t14');
3407        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3408        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3409        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3410        ## reprocess        ## reprocess
3411          !!!ack-later;
3412        return;        return;
3413      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3414        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 3426  sub _tree_construction_initial ($) {
3426          !!!cp ('t17');          !!!cp ('t17');
3427        }        }
3428    
3429        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3430        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3431        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3432        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3455  sub _tree_construction_root_element ($)
3455    B: {    B: {
3456        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3457          !!!cp ('t19');          !!!cp ('t19');
3458          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3459          ## Ignore the token          ## Ignore the token
3460          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3461          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3489  sub _tree_construction_root_element ($)
3489        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3490          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3491            my $root_element;            my $root_element;
3492            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3493            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3494            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3495                  [$root_element, $el_category->{html}];
3496    
3497            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3498              !!!cp ('t24');              !!!cp ('t24');
3499              $self->{application_cache_selection}              $self->{application_cache_selection}
3500                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3501              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3502                ## According to Hixie (#whatwg 2008-03-19), it should be
3503                ## resolved against the base URI of the document in HTML
3504                ## or xml:base of the element in XHTML.
3505            } else {            } else {
3506              !!!cp ('t25');              !!!cp ('t25');
3507              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3508            }            }
3509    
3510              !!!nack ('t25c');
3511    
3512            !!!next-token;            !!!next-token;
3513            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3514          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3525  sub _tree_construction_root_element ($)
3525          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3526        }        }
3527    
3528      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3529        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3530      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3531      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3532    
3533      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3534    
3535      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3536        !!!ack-later;
3537      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3538    
3539      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3554  sub _reset_insertion_mode ($) {
3554            
3555      ## Step 3      ## Step 3
3556      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"!?  
3557        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3558          $last = 1;          $last = 1;
3559          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3560            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3561                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3562              !!!cp ('t27');          } else {
3563              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3564          }          }
3565        }        }
3566              
3567        ## Step 4..13        ## Step 4..14
3568        my $new_mode = {        my $new_mode;
3569          if ($node->[1] & FOREIGN_EL) {
3570            !!!cp ('t28.1');
3571            ## NOTE: Strictly spaking, the line below only applies to MathML and
3572            ## SVG elements.  Currently the HTML syntax supports only MathML and
3573            ## SVG elements as foreigners.
3574            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3575          } elsif ($node->[1] & TABLE_CELL_EL) {
3576            if ($last) {
3577              !!!cp ('t28.2');
3578              #
3579            } else {
3580              !!!cp ('t28.3');
3581              $new_mode = IN_CELL_IM;
3582            }
3583          } else {
3584            !!!cp ('t28.4');
3585            $new_mode = {
3586                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3587                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3588                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3589                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3590                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3591                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3596  sub _reset_insertion_mode ($) {
3596                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3597                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3598                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3599                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3600          }
3601        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3602                
3603        ## Step 14        ## Step 15
3604        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3605          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3606            !!!cp ('t29');            !!!cp ('t29');
3607            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3615  sub _reset_insertion_mode ($) {
3615          !!!cp ('t31');          !!!cp ('t31');
3616        }        }
3617                
3618        ## Step 15        ## Step 16
3619        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3620                
3621        ## Step 16        ## Step 17
3622        $i--;        $i--;
3623        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3624                
3625        ## Step 17        ## Step 18
3626        redo S3;        redo S3;
3627      } # S3      } # S3
3628    
# Line 2908  sub _tree_construction_main ($) { Line 3726  sub _tree_construction_main ($) {
3726      !!!cp ('t39');      !!!cp ('t39');
3727    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3728    
3729    my $parse_rcdata = sub ($$) {    my $insert;
3730      my ($content_model_flag, $insert) = @_;  
3731      my $parse_rcdata = sub ($) {
3732        my ($content_model_flag) = @_;
3733    
3734      ## Step 1      ## Step 1
3735      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3736      my $el;      my $el;
3737      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3738    
3739      ## Step 2      ## Step 2
3740      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3741    
3742      ## Step 3      ## Step 3
3743      $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 3745  sub _tree_construction_main ($) {
3745    
3746      ## Step 4      ## Step 4
3747      my $text = '';      my $text = '';
3748        !!!nack ('t40.1');
3749      !!!next-token;      !!!next-token;
3750      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3751        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3768  sub _tree_construction_main ($) {
3768          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3769        !!!cp ('t42');        !!!cp ('t42');
3770        ## 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});  
3771      } else {      } else {
3772        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3773          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3774            !!!cp ('t43');
3775            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3776          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3777            !!!cp ('t44');
3778            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3779          } else {
3780            die "$0: $content_model_flag in parse_rcdata";
3781          }
3782      }      }
3783      !!!next-token;      !!!next-token;
3784    }; # $parse_rcdata    }; # $parse_rcdata
3785    
3786    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3787      my $script_el;      my $script_el;
3788      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3789      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3790    
3791      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3792      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3793            
3794      my $text = '';      my $text = '';
3795        !!!nack ('t45.1');
3796      !!!next-token;      !!!next-token;
3797      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3798        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812        ## Ignore the token        ## Ignore the token
3813      } else {      } else {
3814        !!!cp ('t48');        !!!cp ('t48');
3815        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3816        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3817        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3818      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3835  sub _tree_construction_main ($) {
3835      !!!next-token;      !!!next-token;
3836    }; # $script_start_tag    }; # $script_start_tag
3837    
3838      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3839      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3840      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3841    
3842    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3843      my $tag_name = shift;      my $end_tag_token = shift;
3844        my $tag_name = $end_tag_token->{tag_name};
3845    
3846        ## NOTE: The adoption agency algorithm (AAA).
3847    
3848      FET: {      FET: {
3849        ## Step 1        ## Step 1
3850        my $formatting_element;        my $formatting_element;
3851        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3852        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3853          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3854              !!!cp ('t52');
3855              last AFE;
3856            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3857                         eq $tag_name) {
3858            !!!cp ('t51');            !!!cp ('t51');
3859            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3860            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3861            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3862          }          }
3863        } # AFE        } # AFE
3864        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3865          !!!cp ('t53');          !!!cp ('t53');
3866          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3867          ## Ignore the token          ## Ignore the token
3868          !!!next-token;          !!!next-token;
3869          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3880  sub _tree_construction_main ($) {
3880              last INSCOPE;              last INSCOPE;
3881            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3882              !!!cp ('t55');              !!!cp ('t55');
3883              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
3884                                text => $token->{tag_name},
3885                                token => $end_tag_token);
3886              ## Ignore the token              ## Ignore the token
3887              !!!next-token;              !!!next-token;
3888              return;              return;
3889            }            }
3890          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3891            !!!cp ('t56');            !!!cp ('t56');
3892            $in_scope = 0;            $in_scope = 0;
3893          }          }
3894        } # INSCOPE        } # INSCOPE
3895        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3896          !!!cp ('t57');          !!!cp ('t57');
3897          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
3898                            text => $token->{tag_name},
3899                            token => $end_tag_token);
3900          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3901          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3902          return;          return;
3903        }        }
3904        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3905          !!!cp ('t58');          !!!cp ('t58');
3906          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3907                            text => $self->{open_elements}->[-1]->[0]
3908                                ->manakai_local_name,
3909                            token => $end_tag_token);
3910        }        }
3911                
3912        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3915        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3916          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3917          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3918              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3919              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3920               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3921            !!!cp ('t59');            !!!cp ('t59');
3922            $furthest_block = $node;            $furthest_block = $node;
3923            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003        } # S7          } # S7  
4004                
4005        ## Step 8        ## Step 8
4006        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4007            my $foster_parent_element;
4008            my $next_sibling;
4009            OE: for (reverse 0..$#{$self->{open_elements}}) {
4010              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4011                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4012                                 if (defined $parent and $parent->node_type == 1) {
4013                                   !!!cp ('t65.1');
4014                                   $foster_parent_element = $parent;
4015                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4016                                 } else {
4017                                   !!!cp ('t65.2');
4018                                   $foster_parent_element
4019                                     = $self->{open_elements}->[$_ - 1]->[0];
4020                                 }
4021                                 last OE;
4022                               }
4023                             } # OE
4024                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4025                               unless defined $foster_parent_element;
4026            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4027            $open_tables->[-1]->[1] = 1; # tainted
4028          } else {
4029            !!!cp ('t65.3');
4030            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4031          }
4032                
4033        ## Step 9        ## Step 9
4034        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 4074  sub _tree_construction_main ($) {
4074      } # FET      } # FET
4075    }; # $formatting_end_tag    }; # $formatting_end_tag
4076    
4077    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4078      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4079    }; # $insert_to_current    }; # $insert_to_current
4080    
4081    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4082                         my $child = shift;      my $child = shift;
4083                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4084                              table => 1, tbody => 1, tfoot => 1,        # MUST
4085                              thead => 1, tr => 1,        my $foster_parent_element;
4086                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4087                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4088                           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') {  
4089                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4090                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4091                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 4103  sub _tree_construction_main ($) {
4103                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4104                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4105                             ($child, $next_sibling);                             ($child, $next_sibling);
4106                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4107                           !!!cp ('t72');      } else {
4108                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4109                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4110        }
4111    }; # $insert_to_foster    }; # $insert_to_foster
4112    
4113    my $insert;    B: while (1) {
   
   B: {  
4114      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4115        !!!cp ('t73');        !!!cp ('t73');
4116        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4117        ## Ignore the token        ## Ignore the token
4118        ## Stay in the phase        ## Stay in the phase
4119        !!!next-token;        !!!next-token;
4120        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;  
4121      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4122               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4123        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4124          !!!cp ('t79');          !!!cp ('t79');
4125          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4126          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4127        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4128          !!!cp ('t80');          !!!cp ('t80');
4129          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4130          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4131        } else {        } else {
4132          !!!cp ('t81');          !!!cp ('t81');
4133        }        }
4134    
4135        !!!cp ('t82');        !!!cp ('t82');
4136        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4137        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4138        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4139          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 4143  sub _tree_construction_main ($) {
4143               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4144          }          }
4145        }        }
4146          !!!nack ('t84.1');
4147        !!!next-token;        !!!next-token;
4148        redo B;        next B;
4149      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4150        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4151        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 4159  sub _tree_construction_main ($) {
4159          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4160        }        }
4161        !!!next-token;        !!!next-token;
4162        redo B;        next B;
4163      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4164          if ($token->{type} == CHARACTER_TOKEN) {
4165            !!!cp ('t87.1');
4166            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4167            !!!next-token;
4168            next B;
4169          } elsif ($token->{type} == START_TAG_TOKEN) {
4170            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4171                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4172                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4173                ($token->{tag_name} eq 'svg' and
4174                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4175              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4176              !!!cp ('t87.2');
4177              #
4178            } elsif ({
4179                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4180                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4181                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4182                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4183                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4184                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4185                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4186                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4187                     }->{$token->{tag_name}}) {
4188              !!!cp ('t87.2');
4189              !!!parse-error (type => 'not closed',
4190                              text => $self->{open_elements}->[-1]->[0]
4191                                  ->manakai_local_name,
4192                              token => $token);
4193    
4194              pop @{$self->{open_elements}}
4195                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4196    
4197              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4198              ## Reprocess.
4199              next B;
4200            } else {
4201              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4202              my $tag_name = $token->{tag_name};
4203              if ($nsuri eq $SVG_NS) {
4204                $tag_name = {
4205                   altglyph => 'altGlyph',
4206                   altglyphdef => 'altGlyphDef',
4207                   altglyphitem => 'altGlyphItem',
4208                   animatecolor => 'animateColor',
4209                   animatemotion => 'animateMotion',
4210                   animatetransform => 'animateTransform',
4211                   clippath => 'clipPath',
4212                   feblend => 'feBlend',
4213                   fecolormatrix => 'feColorMatrix',
4214                   fecomponenttransfer => 'feComponentTransfer',
4215                   fecomposite => 'feComposite',
4216                   feconvolvematrix => 'feConvolveMatrix',
4217                   fediffuselighting => 'feDiffuseLighting',
4218                   fedisplacementmap => 'feDisplacementMap',
4219                   fedistantlight => 'feDistantLight',
4220                   feflood => 'feFlood',
4221                   fefunca => 'feFuncA',
4222                   fefuncb => 'feFuncB',
4223                   fefuncg => 'feFuncG',
4224                   fefuncr => 'feFuncR',
4225                   fegaussianblur => 'feGaussianBlur',
4226                   feimage => 'feImage',
4227                   femerge => 'feMerge',
4228                   femergenode => 'feMergeNode',
4229                   femorphology => 'feMorphology',
4230                   feoffset => 'feOffset',
4231                   fepointlight => 'fePointLight',
4232                   fespecularlighting => 'feSpecularLighting',
4233                   fespotlight => 'feSpotLight',
4234                   fetile => 'feTile',
4235                   feturbulence => 'feTurbulence',
4236                   foreignobject => 'foreignObject',
4237                   glyphref => 'glyphRef',
4238                   lineargradient => 'linearGradient',
4239                   radialgradient => 'radialGradient',
4240                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4241                   textpath => 'textPath',  
4242                }->{$tag_name} || $tag_name;
4243              }
4244    
4245              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4246    
4247              ## "adjust foreign attributes" - done in insert-element-f
4248    
4249              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4250    
4251              if ($self->{self_closing}) {
4252                pop @{$self->{open_elements}};
4253                !!!ack ('t87.3');
4254              } else {
4255                !!!cp ('t87.4');
4256              }
4257    
4258              !!!next-token;
4259              next B;
4260            }
4261          } elsif ($token->{type} == END_TAG_TOKEN) {
4262            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4263            !!!cp ('t87.5');
4264            #
4265          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4266            !!!cp ('t87.6');
4267            !!!parse-error (type => 'not closed',
4268                            text => $self->{open_elements}->[-1]->[0]
4269                                ->manakai_local_name,
4270                            token => $token);
4271    
4272            pop @{$self->{open_elements}}
4273                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4274    
4275            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4276            ## Reprocess.
4277            next B;
4278          } else {
4279            die "$0: $token->{type}: Unknown token type";        
4280          }
4281        }
4282    
4283        if ($self->{insertion_mode} & HEAD_IMS) {
4284        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4285          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4286            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4287                !!!cp ('t88.2');
4288                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4289              } else {
4290                !!!cp ('t88.1');
4291                ## Ignore the token.
4292                !!!next-token;
4293                next B;
4294              }
4295            unless (length $token->{data}) {            unless (length $token->{data}) {
4296              !!!cp ('t88');              !!!cp ('t88');
4297              !!!next-token;              !!!next-token;
4298              redo B;              next B;
4299            }            }
4300          }          }
4301    
4302          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4303            !!!cp ('t89');            !!!cp ('t89');
4304            ## As if <head>            ## As if <head>
4305            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4306            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4307            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4308                  [$self->{head_element}, $el_category->{head}];
4309    
4310            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4311            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315            !!!cp ('t90');            !!!cp ('t90');
4316            ## As if </noscript>            ## As if </noscript>
4317            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4318            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4319                        
4320            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4321            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 4331  sub _tree_construction_main ($) {
4331            !!!cp ('t92');            !!!cp ('t92');
4332          }          }
4333    
4334              ## "after head" insertion mode          ## "after head" insertion mode
4335              ## As if <body>          ## As if <body>
4336              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4337              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4338              ## reprocess          ## reprocess
4339              redo B;          next B;
4340            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4341              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4342                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4343                  !!!cp ('t93');              !!!cp ('t93');
4344                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4345                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4346                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4347                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4348                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4349                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4350                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4351                  !!!cp ('t94');              !!!next-token;
4352                  #              next B;
4353                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4354                  !!!cp ('t95');              !!!cp ('t93.2');
4355                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4356                  ## Ignore the token                              token => $token);
4357                  !!!next-token;              ## Ignore the token
4358                  redo B;              !!!nack ('t93.3');
4359                }              !!!next-token;
4360              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4361                !!!cp ('t96');            } else {
4362                ## As if <head>              !!!cp ('t95');
4363                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4364                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4365                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4366                !!!nack ('t95.1');
4367                !!!next-token;
4368                next B;
4369              }
4370            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4371              !!!cp ('t96');
4372              ## As if <head>
4373              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4374              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4375              push @{$self->{open_elements}},
4376                  [$self->{head_element}, $el_category->{head}];
4377    
4378                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4379                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4380              } else {          } else {
4381                !!!cp ('t97');            !!!cp ('t97');
4382              }          }
4383    
4384              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4385                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                  !!!cp ('t98');                  !!!cp ('t98');
4387                  ## As if </noscript>                  ## As if </noscript>
4388                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4389                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4390                                    token => $token);
4391                                
4392                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4393                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 4398  sub _tree_construction_main ($) {
4398                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4399                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4400                  !!!cp ('t100');                  !!!cp ('t100');
4401                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4402                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4403                    push @{$self->{open_elements}},
4404                        [$self->{head_element}, $el_category->{head}];
4405                } else {                } else {
4406                  !!!cp ('t101');                  !!!cp ('t101');
4407                }                }
4408                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4409                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4410                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4411                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4412                  !!!nack ('t101.1');
4413                !!!next-token;                !!!next-token;
4414                redo B;                next B;
4415              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4416                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4417                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4418                  !!!cp ('t102');                  !!!cp ('t102');
4419                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4420                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4421                    push @{$self->{open_elements}},
4422                        [$self->{head_element}, $el_category->{head}];
4423                } else {                } else {
4424                  !!!cp ('t103');                  !!!cp ('t103');
4425                }                }
4426                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4427                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4428                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4429                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                  !!!ack ('t103.1');
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4434                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4435                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436                  !!!cp ('t104');                  !!!cp ('t104');
4437                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4438                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4439                    push @{$self->{open_elements}},
4440                        [$self->{head_element}, $el_category->{head}];
4441                } else {                } else {
4442                  !!!cp ('t105');                  !!!cp ('t105');
4443                }                }
4444                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445                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.
4446    
4447                unless ($self->{confident}) {                unless ($self->{confident}) {
4448                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4449                    !!!cp ('t106');                    !!!cp ('t106');
4450                      ## NOTE: Whether the encoding is supported or not is handled
4451                      ## in the {change_encoding} callback.
4452                    $self->{change_encoding}                    $self->{change_encoding}
4453                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4454                             $token);
4455                                        
4456                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4457                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4458                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4459                                                 ->{has_reference});                                                 ->{has_reference});
4460                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4461                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4462                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4463                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4464                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4465                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4466                      !!!cp ('t107');                      !!!cp ('t107');
4467                        ## NOTE: Whether the encoding is supported or not is handled
4468                        ## in the {change_encoding} callback.
4469                      $self->{change_encoding}                      $self->{change_encoding}
4470                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4471                               $token);
4472                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4473                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4474                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4494  sub _tree_construction_main ($) {
4494                  }                  }
4495                }                }
4496    
4497                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4498                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4499                  !!!ack ('t110.1');
4500                !!!next-token;                !!!next-token;
4501                redo B;                next B;
4502              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4503                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                  !!!cp ('t111');                  !!!cp ('t111');
4505                  ## As if </noscript>                  ## As if </noscript>
4506                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4507                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4508                                    token => $token);
4509                                
4510                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4511                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4512                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4513                  !!!cp ('t112');                  !!!cp ('t112');
4514                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4515                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4516                    push @{$self->{open_elements}},
4517                        [$self->{head_element}, $el_category->{head}];
4518                } else {                } else {
4519                  !!!cp ('t113');                  !!!cp ('t113');
4520                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4522  sub _tree_construction_main ($) {
4522                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4523                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4524                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4525                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4526                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4527                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4528                redo B;                next B;
4529              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4530                         $token->{tag_name} eq 'noframes') {
4531                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4532                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4533                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4534                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4535                  !!!cp ('t114');                  !!!cp ('t114');
4536                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4537                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4538                    push @{$self->{open_elements}},
4539                        [$self->{head_element}, $el_category->{head}];
4540                } else {                } else {
4541                  !!!cp ('t115');                  !!!cp ('t115');
4542                }                }
4543                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4544                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4545                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4546                redo B;                next B;
4547              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4548                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4549                  !!!cp ('t116');                  !!!cp ('t116');
4550                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4551                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4552                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4553                    !!!nack ('t116.1');
4554                  !!!next-token;                  !!!next-token;
4555                  redo B;                  next B;
4556                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4557                  !!!cp ('t117');                  !!!cp ('t117');
4558                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4559                                    token => $token);
4560                  ## Ignore the token                  ## Ignore the token
4561                    !!!nack ('t117.1');
4562                  !!!next-token;                  !!!next-token;
4563                  redo B;                  next B;
4564                } else {                } else {
4565                  !!!cp ('t118');                  !!!cp ('t118');
4566                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570                  !!!cp ('t119');                  !!!cp ('t119');
4571                  ## As if </noscript>                  ## As if </noscript>
4572                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4573                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4574                                    token => $token);
4575                                
4576                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4577                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4578                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579                  !!!cp ('t120');                  !!!cp ('t120');
4580                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4581                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4582                    push @{$self->{open_elements}},
4583                        [$self->{head_element}, $el_category->{head}];
4584                } else {                } else {
4585                  !!!cp ('t121');                  !!!cp ('t121');
4586                }                }
4587    
4588                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4589                $script_start_tag->($insert_to_current);                $script_start_tag->();
4590                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4591                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4592                redo B;                next B;
4593              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4594                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4595                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4596                  !!!cp ('t122');                  !!!cp ('t122');
4597                  ## As if </noscript>                  ## As if </noscript>
4598                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4599                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4600                                    text => $token->{tag_name}, token => $token);
4601                                    
4602                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4603                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4614  sub _tree_construction_main ($) {
4614                }                }
4615    
4616                ## "after head" insertion mode                ## "after head" insertion mode
4617                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4618                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4619                  !!!cp ('t126');                  !!!cp ('t126');
4620                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624                } else {                } else {
4625                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4626                }                }
4627                  !!!nack ('t127.1');
4628                !!!next-token;                !!!next-token;
4629                redo B;                next B;
4630              } else {              } else {
4631                !!!cp ('t128');                !!!cp ('t128');
4632                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4636  sub _tree_construction_main ($) {
4636                !!!cp ('t129');                !!!cp ('t129');
4637                ## As if </noscript>                ## As if </noscript>
4638                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4639                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4640                                  text => $token->{tag_name}, token => $token);
4641                                
4642                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4643                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4656  sub _tree_construction_main ($) {
4656    
4657              ## "after head" insertion mode              ## "after head" insertion mode
4658              ## As if <body>              ## As if <body>
4659              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4660              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4661              ## reprocess              ## reprocess
4662              redo B;              !!!ack-later;
4663                next B;
4664            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4665              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4666                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4667                  !!!cp ('t132');                  !!!cp ('t132');
4668                  ## As if <head>                  ## As if <head>
4669                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4670                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4671                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4672                        [$self->{head_element}, $el_category->{head}];
4673    
4674                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4675                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4676                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4677                  !!!next-token;                  !!!next-token;
4678                  redo B;                  next B;
4679                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4680                  !!!cp ('t133');                  !!!cp ('t133');
4681                  ## As if </noscript>                  ## As if </noscript>
4682                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4683                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4684                                    text => 'head', token => $token);
4685                                    
4686                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4687                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4688                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4689                  !!!next-token;                  !!!next-token;
4690                  redo B;                  next B;
4691                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4692                  !!!cp ('t134');                  !!!cp ('t134');
4693                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4694                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4695                  !!!next-token;                  !!!next-token;
4696                  redo B;                  next B;
4697                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4698                    !!!cp ('t134.1');
4699                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4700                                    token => $token);
4701                    ## Ignore the token
4702                    !!!next-token;
4703                    next B;
4704                } else {                } else {
4705                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4706                }                }
4707              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4708                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3706  sub _tree_construction_main ($) { Line 4710  sub _tree_construction_main ($) {
4710                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4711                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4712                  !!!next-token;                  !!!next-token;
4713                  redo B;                  next B;
4714                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4715                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4716                  !!!cp ('t137');                  !!!cp ('t137');
4717                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4718                                    text => 'noscript', token => $token);
4719                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4720                  !!!next-token;                  !!!next-token;
4721                  redo B;                  next B;
4722                } else {                } else {
4723                  !!!cp ('t138');                  !!!cp ('t138');
4724                  #                  #
# Line 3720  sub _tree_construction_main ($) { Line 4726  sub _tree_construction_main ($) {
4726              } elsif ({              } elsif ({
4727                        body => 1, html => 1,                        body => 1, html => 1,
4728                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4729                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4730                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4731                  ## 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) {  
4732                  !!!cp ('t140');                  !!!cp ('t140');
4733                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4734                                    text => $token->{tag_name}, token => $token);
4735                  ## Ignore the token                  ## Ignore the token
4736                  !!!next-token;                  !!!next-token;
4737                  redo B;                  next B;
4738                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4739                    !!!cp ('t140.1');
4740                    !!!parse-error (type => 'unmatched end tag',
4741                                    text => $token->{tag_name}, token => $token);
4742                    ## Ignore the token
4743                    !!!next-token;
4744                    next B;
4745                } else {                } else {
4746                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4747                }                }
4748                              } elsif ($token->{tag_name} eq 'p') {
4749                #                !!!cp ('t142');
4750              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4751                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4752                       }->{$token->{tag_name}}) {                ## Ignore the token
4753                  !!!next-token;
4754                  next B;
4755                } elsif ($token->{tag_name} eq 'br') {
4756                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4757                  !!!cp ('t142');                  !!!cp ('t142.2');
4758                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4759                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4760                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4761                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4762      
4763                    ## Reprocess in the "after head" insertion mode...
4764                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4765                    !!!cp ('t143.2');
4766                    ## As if </head>
4767                    pop @{$self->{open_elements}};
4768                    $self->{insertion_mode} = AFTER_HEAD_IM;
4769      
4770                    ## Reprocess in the "after head" insertion mode...
4771                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                    !!!cp ('t143.3');
4773                    ## ISSUE: Two parse errors for <head><noscript></br>
4774                    !!!parse-error (type => 'unmatched end tag',
4775                                    text => 'br', token => $token);
4776                    ## As if </noscript>
4777                    pop @{$self->{open_elements}};
4778                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4779    
4780                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4781                } else {                  ## As if </head>
4782                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4783                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4784    
4785                #                  ## Reprocess in the "after head" insertion mode...
4786              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4787                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4788                  #                  #
4789                } else {                } else {
4790                  !!!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;  
4791                }                }
4792    
4793                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4794                  !!!parse-error (type => 'unmatched end tag',
4795                                  text => 'br', token => $token);
4796                  ## Ignore the token
4797                  !!!next-token;
4798                  next B;
4799                } else {
4800                  !!!cp ('t145');
4801                  !!!parse-error (type => 'unmatched end tag',
4802                                  text => $token->{tag_name}, token => $token);
4803                  ## Ignore the token
4804                  !!!next-token;
4805                  next B;
4806              }              }
4807    
4808              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809                !!!cp ('t146');                !!!cp ('t146');
4810                ## As if </noscript>                ## As if </noscript>
4811                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4812                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4813                                  text => $token->{tag_name}, token => $token);
4814                                
4815                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4816                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4827  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4828                !!!cp ('t148');                !!!cp ('t148');
4829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4830                                  text => $token->{tag_name}, token => $token);
4831                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4832                !!!next-token;                !!!next-token;
4833                redo B;                next B;
4834              } else {              } else {
4835                !!!cp ('t149');                !!!cp ('t149');
4836              }              }
4837    
4838              ## "after head" insertion mode              ## "after head" insertion mode
4839              ## As if <body>              ## As if <body>
4840              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4841              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4842              ## reprocess              ## reprocess
4843              redo B;              next B;
4844            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4845              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4846            }            !!!cp ('t149.1');
4847    
4848              ## NOTE: As if <head>
4849              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4850              $self->{open_elements}->[-1]->[0]->append_child
4851                  ($self->{head_element});
4852              #push @{$self->{open_elements}},
4853              #    [$self->{head_element}, $el_category->{head}];
4854              #$self->{insertion_mode} = IN_HEAD_IM;
4855              ## NOTE: Reprocess.
4856    
4857              ## NOTE: As if </head>
4858              #pop @{$self->{open_elements}};
4859              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4860              ## NOTE: Reprocess.
4861              
4862              #
4863            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4864              !!!cp ('t149.2');
4865    
4866              ## NOTE: As if </head>
4867              pop @{$self->{open_elements}};
4868              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4869              ## NOTE: Reprocess.
4870    
4871              #
4872            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4873              !!!cp ('t149.3');
4874    
4875              !!!parse-error (type => 'in noscript:#eof', token => $token);
4876    
4877              ## As if </noscript>
4878              pop @{$self->{open_elements}};
4879              #$self->{insertion_mode} = IN_HEAD_IM;
4880              ## NOTE: Reprocess.
4881    
4882              ## NOTE: As if </head>
4883              pop @{$self->{open_elements}};
4884              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4885              ## NOTE: Reprocess.
4886    
4887              #
4888            } else {
4889              !!!cp ('t149.4');
4890              #
4891            }
4892    
4893            ## NOTE: As if <body>
4894            !!!insert-element ('body',, $token);
4895            $self->{insertion_mode} = IN_BODY_IM;
4896            ## NOTE: Reprocess.
4897            next B;
4898          } else {
4899            die "$0: $token->{type}: Unknown token type";
4900          }
4901    
4902            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4903      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4909  sub _tree_construction_main ($) {
4909              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4910    
4911              !!!next-token;              !!!next-token;
4912              redo B;              next B;
4913            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4914              if ({              if ({
4915                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4917  sub _tree_construction_main ($) {
4917                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4918                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4919                  ## have an element in table scope                  ## have an element in table scope
4920                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4921                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4922                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4923                      !!!cp ('t151');                      !!!cp ('t151');
4924                      $tn = $node->[1];  
4925                      last INSCOPE;                      ## Close the cell
4926                    } elsif ({                      !!!back-token; # <x>
4927                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4928                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4929                                  line => $token->{line},
4930                                  column => $token->{column}};
4931                        next B;
4932                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4933                      !!!cp ('t152');                      !!!cp ('t152');
4934                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4935                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4936                    }                    }
4937                                    }
4938                  !!!cp ('t154');  
4939                  ## Close the cell                  !!!cp ('t153');
4940                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4941                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      text => $token->{tag_name}, token => $token);
4942                  redo B;                  ## Ignore the token
4943                    !!!nack ('t153.1');
4944                    !!!next-token;
4945                    next B;
4946                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4947                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4948                                    token => $token);
4949                                    
4950                  ## As if </caption>                  ## NOTE: As if </caption>.
4951                  ## have a table element in table scope                  ## have a table element in table scope
4952                  my $i;                  my $i;
4953                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4954                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4955                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4956                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4957                      $i = $_;                        !!!cp ('t155');
4958                      last INSCOPE;                        $i = $_;
4959                    } elsif ({                        last INSCOPE;
4960                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4961                             }->{$node->[1]}) {                        !!!cp ('t156');
4962                      !!!cp ('t156');                        last;
4963                      last INSCOPE;                      }
4964                    }                    }
4965    
4966                      !!!cp ('t157');
4967                      !!!parse-error (type => 'start tag not allowed',
4968                                      text => $token->{tag_name}, token => $token);
4969                      ## Ignore the token
4970                      !!!nack ('t157.1');
4971                      !!!next-token;
4972                      next B;
4973                  } # 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;  
                   }  
4974                                    
4975                  ## generate implied end tags                  ## generate implied end tags
4976                  while ({                  while ($self->{open_elements}->[-1]->[1]
4977                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4978                    !!!cp ('t158');                    !!!cp ('t158');
4979                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4980                  }                  }
4981    
4982                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                    !!!cp ('t159');                    !!!cp ('t159');
4984                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4985                                      text => $self->{open_elements}->[-1]->[0]
4986                                          ->manakai_local_name,
4987                                      token => $token);
4988                  } else {                  } else {
4989                    !!!cp ('t160');                    !!!cp ('t160');
4990                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4997                                    
4998                  ## reprocess                  ## reprocess
4999                  redo B;                  !!!ack-later;
5000                    next B;
5001                } else {                } else {
5002                  !!!cp ('t161');                  !!!cp ('t161');
5003                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 5013  sub _tree_construction_main ($) {
5013                  my $i;                  my $i;
5014                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5016                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5017                      !!!cp ('t163');                      !!!cp ('t163');
5018                      $i = $_;                      $i = $_;
5019                      last INSCOPE;                      last INSCOPE;
5020                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5021                      !!!cp ('t164');                      !!!cp ('t164');
5022                      last INSCOPE;                      last INSCOPE;
5023                    }                    }
5024                  } # INSCOPE                  } # INSCOPE
5025                    unless (defined $i) {                    unless (defined $i) {
5026                      !!!cp ('t165');                      !!!cp ('t165');
5027                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5028                                        text => $token->{tag_name},
5029                                        token => $token);
5030                      ## Ignore the token                      ## Ignore the token
5031                      !!!next-token;                      !!!next-token;
5032                      redo B;                      next B;
5033                    }                    }
5034                                    
5035                  ## generate implied end tags                  ## generate implied end tags
5036                  while ({                  while ($self->{open_elements}->[-1]->[1]
5037                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5038                    !!!cp ('t166');                    !!!cp ('t166');
5039                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5040                  }                  }
5041    
5042                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5043                            ne $token->{tag_name}) {
5044                    !!!cp ('t167');                    !!!cp ('t167');
5045                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5046                                      text => $self->{open_elements}->[-1]->[0]
5047                                          ->manakai_local_name,
5048                                      token => $token);
5049                  } else {                  } else {
5050                    !!!cp ('t168');                    !!!cp ('t168');
5051                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 5057  sub _tree_construction_main ($) {
5057                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5058                                    
5059                  !!!next-token;                  !!!next-token;
5060                  redo B;                  next B;
5061                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5062                  !!!cp ('t169');                  !!!cp ('t169');
5063                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5064                                    text => $token->{tag_name}, token => $token);
5065                  ## Ignore the token                  ## Ignore the token
5066                  !!!next-token;                  !!!next-token;
5067                  redo B;                  next B;
5068                } else {                } else {
5069                  !!!cp ('t170');                  !!!cp ('t170');
5070                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 5073  sub _tree_construction_main ($) {
5073                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5074                  ## have a table element in table scope                  ## have a table element in table scope
5075                  my $i;                  my $i;
5076                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5077                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5078                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5079                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5080                      $i = $_;                        !!!cp ('t171');
5081                      last INSCOPE;                        $i = $_;
5082                    } elsif ({                        last INSCOPE;
5083                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5084                             }->{$node->[1]}) {                        !!!cp ('t172');
5085                      !!!cp ('t172');                        last;
5086                      last INSCOPE;                      }
5087                    }                    }
5088    
5089                      !!!cp ('t173');
5090                      !!!parse-error (type => 'unmatched end tag',
5091                                      text => $token->{tag_name}, token => $token);
5092                      ## Ignore the token
5093                      !!!next-token;
5094                      next B;
5095                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5096                                    
5097                  ## generate implied end tags                  ## generate implied end tags
5098                  while ({                  while ($self->{open_elements}->[-1]->[1]
5099                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5100                    !!!cp ('t174');                    !!!cp ('t174');
5101                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5102                  }                  }
5103                                    
5104                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5105                    !!!cp ('t175');                    !!!cp ('t175');
5106                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5107                                      text => $self->{open_elements}->[-1]->[0]
5108                                          ->manakai_local_name,
5109                                      token => $token);
5110                  } else {                  } else {
5111                    !!!cp ('t176');                    !!!cp ('t176');
5112                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 5118  sub _tree_construction_main ($) {
5118                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5119                                    
5120                  !!!next-token;                  !!!next-token;
5121                  redo B;                  next B;
5122                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5123                  !!!cp ('t177');                  !!!cp ('t177');
5124                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5125                                    text => $token->{tag_name}, token => $token);
5126                  ## Ignore the token                  ## Ignore the token
5127                  !!!next-token;                  !!!next-token;
5128                  redo B;                  next B;
5129                } else {                } else {
5130                  !!!cp ('t178');                  !!!cp ('t178');
5131                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 5138  sub _tree_construction_main ($) {
5138                ## have an element in table scope                ## have an element in table scope
5139                my $i;                my $i;
5140                my $tn;                my $tn;
5141                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5142                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5143                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5144                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5145                    $i = $_;                      !!!cp ('t179');
5146                    last INSCOPE;                      $i = $_;
5147                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5148                    !!!cp ('t180');                      ## Close the cell
5149                    $tn = $node->[1];                      !!!back-token; # </x>
5150                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5151                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5152                  } elsif ({                                column => $token->{column}};
5153                            table => 1, html => 1,                      next B;
5154                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5155                    !!!cp ('t181');                      !!!cp ('t180');
5156                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5157                        ## NOTE: There is exactly one |td| or |th| element
5158                        ## in scope in the stack of open elements by definition.
5159                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5160                        ## ISSUE: Can this be reached?
5161                        !!!cp ('t181');
5162                        last;
5163                      }
5164                  }                  }
5165                } # INSCOPE  
               unless (defined $i) {  
5166                  !!!cp ('t182');                  !!!cp ('t182');
5167                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5168                        text => $token->{tag_name}, token => $token);
5169                  ## Ignore the token                  ## Ignore the token
5170                  !!!next-token;                  !!!next-token;
5171                  redo B;                  next B;
5172                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5173              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5174                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5176                                  token => $token);
5177    
5178                ## As if </caption>                ## As if </caption>
5179                ## have a table element in table scope                ## have a table element in table scope
5180                my $i;                my $i;
5181                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5183                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5184                    !!!cp ('t184');                    !!!cp ('t184');
5185                    $i = $_;                    $i = $_;
5186                    last INSCOPE;                    last INSCOPE;
5187                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5188                    !!!cp ('t185');                    !!!cp ('t185');
5189                    last INSCOPE;                    last INSCOPE;
5190                  }                  }
5191                } # INSCOPE                } # INSCOPE
5192                unless (defined $i) {                unless (defined $i) {
5193                  !!!cp ('t186');                  !!!cp ('t186');
5194                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5195                                    text => 'caption', token => $token);
5196                  ## Ignore the token                  ## Ignore the token
5197                  !!!next-token;                  !!!next-token;
5198                  redo B;                  next B;
5199                }                }
5200                                
5201                ## generate implied end tags                ## generate implied end tags
5202                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5203                  !!!cp ('t187');                  !!!cp ('t187');
5204                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5205                }                }
5206    
5207                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                  !!!cp ('t188');                  !!!cp ('t188');
5209                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5210                                    text => $self->{open_elements}->[-1]->[0]
5211                                        ->manakai_local_name,
5212                                    token => $token);
5213                } else {                } else {
5214                  !!!cp ('t189');                  !!!cp ('t189');
5215                }                }
# Line 4120  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5222    
5223                ## reprocess                ## reprocess
5224                redo B;                next B;
5225              } elsif ({              } elsif ({
5226                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5227                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5228                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5229                  !!!cp ('t190');                  !!!cp ('t190');
5230                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5231                                    text => $token->{tag_name}, token => $token);
5232                  ## Ignore the token                  ## Ignore the token
5233                  !!!next-token;                  !!!next-token;
5234                  redo B;                  next B;
5235                } else {                } else {
5236                  !!!cp ('t191');                  !!!cp ('t191');
5237                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 5242  sub _tree_construction_main ($) {
5242                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5243                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5244                !!!cp ('t192');                !!!cp ('t192');
5245                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5246                                  text => $token->{tag_name}, token => $token);
5247                ## Ignore the token                ## Ignore the token
5248                !!!next-token;                !!!next-token;
5249                redo B;                next B;
5250              } else {              } else {
5251                !!!cp ('t193');                !!!cp ('t193');
5252                #                #
5253              }              }
5254          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5255            for my $entry (@{$self->{open_elements}}) {
5256              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5257                !!!cp ('t75');
5258                !!!parse-error (type => 'in body:#eof', token => $token);
5259                last;
5260              }
5261            }
5262    
5263            ## Stop parsing.
5264            last B;
5265        } else {        } else {
5266          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5267        }        }
# Line 4156  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270        #        #
5271      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5272        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5273              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5274                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5275              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5276                                
5277                unless (length $token->{data}) {            unless (length $token->{data}) {
5278                  !!!cp ('t194');              !!!cp ('t194');
5279                  !!!next-token;              !!!next-token;
5280                  redo B;              next B;
5281                } else {            } else {
5282                  !!!cp ('t195');              !!!cp ('t195');
5283                }            }
5284              }          }
5285    
5286              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5287    
5288              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5289              ## 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 5291  sub _tree_construction_main ($) {
5291              ## result in a new Text node.              ## result in a new Text node.
5292              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5293                            
5294              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]}) {  
5295                # MUST                # MUST
5296                my $foster_parent_element;                my $foster_parent_element;
5297                my $next_sibling;                my $next_sibling;
5298                my $prev_sibling;                my $prev_sibling;
5299                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5300                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5301                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5302                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5303                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 5325  sub _tree_construction_main ($) {
5325                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5326                     $next_sibling);                     $next_sibling);
5327                }                }
5328              } else {            $open_tables->[-1]->[1] = 1; # tainted
5329                !!!cp ('t200');          } else {
5330                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5331              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5332            }
5333                            
5334              !!!next-token;          !!!next-token;
5335              redo B;          next B;
5336        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5337              if ({          if ({
5338                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5339                   th => 1, td => 1,               th => 1, td => 1,
5340                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5341                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5342                  ## Clear back to table context              ## Clear back to table context
5343                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5344                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5345                    !!!cp ('t201');                !!!cp ('t201');
5346                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5347                    pop @{$self->{open_elements}};              }
5348                  }              
5349                                !!!insert-element ('tbody',, $token);
5350                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5351                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5352                  ## reprocess in the "in table body" insertion mode...            }
5353                }            
5354              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5355                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5356                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5357                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5358                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5359                                    
5360                  ## Clear back to table body context              ## Clear back to table body context
5361                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5362                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5363                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5364                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5365                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5366                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
5367                                    
5368                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5369                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5370                    !!!cp ('t204');                    !!!cp ('t204');
5371                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5372                      !!!nack ('t204');
5373                    !!!next-token;                    !!!next-token;
5374                    redo B;                    next B;
5375                  } else {                  } else {
5376                    !!!cp ('t205');                    !!!cp ('t205');
5377                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5378                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5379                  }                  }
5380                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 5382  sub _tree_construction_main ($) {
5382                }                }
5383    
5384                ## Clear back to table row context                ## Clear back to table row context
5385                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5386                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5387                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5388                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5389                }                }
5390                                
5391                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5392                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5393    
5394                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5395                                
5396                  !!!nack ('t207.1');
5397                !!!next-token;                !!!next-token;
5398                redo B;                next B;
5399              } elsif ({              } elsif ({
5400                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5401                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 5407  sub _tree_construction_main ($) {
5407                  my $i;                  my $i;
5408                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5409                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5410                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5411                      !!!cp ('t208');                      !!!cp ('t208');
5412                      $i = $_;                      $i = $_;
5413                      last INSCOPE;                      last INSCOPE;
5414                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5415                      !!!cp ('t209');                      !!!cp ('t209');
5416                      last INSCOPE;                      last INSCOPE;
5417                    }                    }
5418                  } # INSCOPE                  } # INSCOPE
5419                  unless (defined $i) {                  unless (defined $i) {
5420                   !!!cp ('t210');                    !!!cp ('t210');
5421  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5422                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5423                                      text => $token->{tag_name}, token => $token);
5424                    ## Ignore the token                    ## Ignore the token
5425                      !!!nack ('t210.1');
5426                    !!!next-token;                    !!!next-token;
5427                    redo B;                    next B;
5428                  }                  }
5429                                    
5430                  ## Clear back to table row context                  ## Clear back to table row context
5431                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5432                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5433                    !!!cp ('t211');                    !!!cp ('t211');
5434                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5435                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5436                  }                  }
5437                                    
# Line 4335  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5441                    !!!cp ('t212');                    !!!cp ('t212');
5442                    ## reprocess                    ## reprocess
5443                    redo B;                    !!!ack-later;
5444                      next B;
5445                  } else {                  } else {
5446                    !!!cp ('t213');                    !!!cp ('t213');
5447                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 5453  sub _tree_construction_main ($) {
5453                  my $i;                  my $i;
5454                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5455                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5456                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5457                      !!!cp ('t214');                      !!!cp ('t214');
5458                      $i = $_;                      $i = $_;
5459                      last INSCOPE;                      last INSCOPE;
5460                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5461                      !!!cp ('t215');                      !!!cp ('t215');
5462                      last INSCOPE;                      last INSCOPE;
5463                    }                    }
5464                  } # INSCOPE                  } # INSCOPE
5465                  unless (defined $i) {                  unless (defined $i) {
5466                    !!!cp ('t216');                    !!!cp ('t216');
5467  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5468                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5469                                      text => $token->{tag_name}, token => $token);
5470                    ## Ignore the token                    ## Ignore the token
5471                      !!!nack ('t216.1');
5472                    !!!next-token;                    !!!next-token;
5473                    redo B;                    next B;
5474                  }                  }
5475    
5476                  ## Clear back to table body context                  ## Clear back to table body context
5477                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5478                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5479                    !!!cp ('t217');                    !!!cp ('t217');
5480                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5481                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5482                  }                  }
5483                                    
# Line 4395  sub _tree_construction_main ($) { Line 5497  sub _tree_construction_main ($) {
5497    
5498                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5499                  ## Clear back to table context                  ## Clear back to table context
5500                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5501                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5502                    !!!cp ('t219');                    !!!cp ('t219');
5503                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5504                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5505                  }                  }
5506                                    
5507                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5508                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5509                  ## reprocess                  ## reprocess
5510                  redo B;                  !!!ack-later;
5511                    next B;
5512                } elsif ({                } elsif ({
5513                          caption => 1,                          caption => 1,
5514                          colgroup => 1,                          colgroup => 1,
5515                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5516                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5517                  ## Clear back to table context                  ## Clear back to table context
5518                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5519                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5520                    !!!cp ('t220');                    !!!cp ('t220');
5521                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5522                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5523                  }                  }
5524                                    
5525                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5526                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5527                                    
5528                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5529                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5530                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5531                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5534  sub _tree_construction_main ($) {
5534                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5535                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5536                  !!!next-token;                  !!!next-token;
5537                  redo B;                  !!!nack ('t220.1');
5538                    next B;
5539                } else {                } else {
5540                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5541                }                }
5542              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5543                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5544                                  text => $self->{open_elements}->[-1]->[0]
5545                                      ->manakai_local_name,
5546                                  token => $token);
5547    
5548                ## As if </table>                ## As if </table>
5549                ## have a table element in table scope                ## have a table element in table scope
5550                my $i;                my $i;
5551                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5552                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5553                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5554                    !!!cp ('t221');                    !!!cp ('t221');
5555                    $i = $_;                    $i = $_;
5556                    last INSCOPE;                    last INSCOPE;
5557                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5558                    !!!cp ('t222');                    !!!cp ('t222');
5559                    last INSCOPE;                    last INSCOPE;
5560                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5562  sub _tree_construction_main ($) {
5562                unless (defined $i) {                unless (defined $i) {
5563                  !!!cp ('t223');                  !!!cp ('t223');
5564  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5565                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5566                                    token => $token);
5567                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5568                    !!!nack ('t223.1');
5569                  !!!next-token;                  !!!next-token;
5570                  redo B;                  next B;
5571                }                }
5572                                
5573    ## TODO: Followings are removed from the latest spec.
5574                ## generate implied end tags                ## generate implied end tags
5575                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5576                  !!!cp ('t224');                  !!!cp ('t224');
5577                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5578                }                }
5579    
5580                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5581                  !!!cp ('t225');                  !!!cp ('t225');
5582  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5583                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5584                                    text => $self->{open_elements}->[-1]->[0]
5585                                        ->manakai_local_name,
5586                                    token => $token);
5587                } else {                } else {
5588                  !!!cp ('t226');                  !!!cp ('t226');
5589                }                }
5590    
5591                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5592                  pop @{$open_tables};
5593    
5594                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5595    
5596                ## reprocess            ## reprocess
5597                redo B;            !!!ack-later;
5598              next B;
5599            } elsif ($token->{tag_name} eq 'style') {
5600              if (not $open_tables->[-1]->[1]) { # tainted
5601                !!!cp ('t227.8');
5602                ## NOTE: This is a "as if in head" code clone.
5603                $parse_rcdata->(CDATA_CONTENT_MODEL);
5604                next B;
5605              } else {
5606                !!!cp ('t227.7');
5607                #
5608              }
5609            } elsif ($token->{tag_name} eq 'script') {
5610              if (not $open_tables->[-1]->[1]) { # tainted
5611                !!!cp ('t227.6');
5612                ## NOTE: This is a "as if in head" code clone.
5613                $script_start_tag->();
5614                next B;
5615              } else {
5616                !!!cp ('t227.5');
5617                #
5618              }
5619            } elsif ($token->{tag_name} eq 'input') {
5620              if (not $open_tables->[-1]->[1]) { # tainted
5621                if ($token->{attributes}->{type}) { ## TODO: case
5622                  my $type = lc $token->{attributes}->{type}->{value};
5623                  if ($type eq 'hidden') {
5624                    !!!cp ('t227.3');
5625                    !!!parse-error (type => 'in table',
5626                                    text => $token->{tag_name}, token => $token);
5627    
5628                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5629    
5630                    ## TODO: form element pointer
5631    
5632                    pop @{$self->{open_elements}};
5633    
5634                    !!!next-token;
5635                    !!!ack ('t227.2.1');
5636                    next B;
5637                  } else {
5638                    !!!cp ('t227.2');
5639                    #
5640                  }
5641                } else {
5642                  !!!cp ('t227.1');
5643                  #
5644                }
5645              } else {
5646                !!!cp ('t227.4');
5647                #
5648              }
5649          } else {          } else {
5650            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5651            #            #
5652          }          }
5653    
5654            !!!parse-error (type => 'in table', text => $token->{tag_name},
5655                            token => $token);
5656    
5657            $insert = $insert_to_foster;
5658            #
5659        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5660              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5661                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5663  sub _tree_construction_main ($) {
5663                my $i;                my $i;
5664                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5665                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5666                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5667                    !!!cp ('t228');                    !!!cp ('t228');
5668                    $i = $_;                    $i = $_;
5669                    last INSCOPE;                    last INSCOPE;
5670                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5671                    !!!cp ('t229');                    !!!cp ('t229');
5672                    last INSCOPE;                    last INSCOPE;
5673                  }                  }
5674                } # INSCOPE                } # INSCOPE
5675                unless (defined $i) {                unless (defined $i) {
5676                  !!!cp ('t230');                  !!!cp ('t230');
5677                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5678                                    text => $token->{tag_name}, token => $token);
5679                  ## Ignore the token                  ## Ignore the token
5680                    !!!nack ('t230.1');
5681                  !!!next-token;                  !!!next-token;
5682                  redo B;                  next B;
5683                } else {                } else {
5684                  !!!cp ('t232');                  !!!cp ('t232');
5685                }                }
5686    
5687                ## Clear back to table row context                ## Clear back to table row context
5688                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5689                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5690                  !!!cp ('t231');                  !!!cp ('t231');
5691  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5692                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5693                }                }
5694    
5695                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5696                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5697                !!!next-token;                !!!next-token;
5698                redo B;                !!!nack ('t231.1');
5699                  next B;
5700              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5701                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5702                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5704  sub _tree_construction_main ($) {
5704                  my $i;                  my $i;
5705                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5706                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5707                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5708                      !!!cp ('t233');                      !!!cp ('t233');
5709                      $i = $_;                      $i = $_;
5710                      last INSCOPE;                      last INSCOPE;
5711                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5712                      !!!cp ('t234');                      !!!cp ('t234');
5713                      last INSCOPE;                      last INSCOPE;
5714                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5716  sub _tree_construction_main ($) {
5716                  unless (defined $i) {                  unless (defined $i) {
5717                    !!!cp ('t235');                    !!!cp ('t235');
5718  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5719                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5720                                      text => $token->{type}, token => $token);
5721                    ## Ignore the token                    ## Ignore the token
5722                      !!!nack ('t236.1');
5723                    !!!next-token;                    !!!next-token;
5724                    redo B;                    next B;
5725                  }                  }
5726                                    
5727                  ## Clear back to table row context                  ## Clear back to table row context
5728                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5729                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5730                    !!!cp ('t236');                    !!!cp ('t236');
5731  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5732                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5733                  }                  }
5734                                    
# Line 4584  sub _tree_construction_main ($) { Line 5742  sub _tree_construction_main ($) {
5742                  my $i;                  my $i;
5743                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5744                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5745                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5746                      !!!cp ('t237');                      !!!cp ('t237');
5747                      $i = $_;                      $i = $_;
5748                      last INSCOPE;                      last INSCOPE;
5749                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5750                      !!!cp ('t238');                      !!!cp ('t238');
5751                      last INSCOPE;                      last INSCOPE;
5752                    }                    }
5753                  } # INSCOPE                  } # INSCOPE
5754                  unless (defined $i) {                  unless (defined $i) {
5755                    !!!cp ('t239');                    !!!cp ('t239');
5756                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5757                                      text => $token->{tag_name}, token => $token);
5758                    ## Ignore the token                    ## Ignore the token
5759                      !!!nack ('t239.1');
5760                    !!!next-token;                    !!!next-token;
5761                    redo B;                    next B;
5762                  }                  }
5763                                    
5764                  ## Clear back to table body context                  ## Clear back to table body context
5765                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5766                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5767                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5768                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5769                  }                  }
5770                                    
# Line 4626  sub _tree_construction_main ($) { Line 5780  sub _tree_construction_main ($) {
5780                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5781                }                }
5782    
5783                  ## NOTE: </table> in the "in table" insertion mode.
5784                  ## When you edit the code fragment below, please ensure that
5785                  ## the code for <table> in the "in table" insertion mode
5786                  ## is synced with it.
5787    
5788                ## have a table element in table scope                ## have a table element in table scope
5789                my $i;                my $i;
5790                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5791                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5792                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5793                    !!!cp ('t241');                    !!!cp ('t241');
5794                    $i = $_;                    $i = $_;
5795                    last INSCOPE;                    last INSCOPE;
5796                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5797                    !!!cp ('t242');                    !!!cp ('t242');
5798                    last INSCOPE;                    last INSCOPE;
5799                  }                  }
5800                } # INSCOPE                } # INSCOPE
5801                unless (defined $i) {                unless (defined $i) {
5802                  !!!cp ('t243');                  !!!cp ('t243');
5803                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5804                                    text => $token->{tag_name}, token => $token);
5805                  ## Ignore the token                  ## Ignore the token
5806                    !!!nack ('t243.1');
5807                  !!!next-token;                  !!!next-token;
5808                  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');  
5809                }                }
5810                                    
5811                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5812                  pop @{$open_tables};
5813                                
5814                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5815                                
5816                !!!next-token;                !!!next-token;
5817                redo B;                next B;
5818              } elsif ({              } elsif ({
5819                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5820                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 5824  sub _tree_construction_main ($) {
5824                  my $i;                  my $i;
5825                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5826                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5827                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5828                      !!!cp ('t247');                      !!!cp ('t247');
5829                      $i = $_;                      $i = $_;
5830                      last INSCOPE;                      last INSCOPE;
5831                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5832                      !!!cp ('t248');                      !!!cp ('t248');
5833                      last INSCOPE;                      last INSCOPE;
5834                    }                    }
5835                  } # INSCOPE                  } # INSCOPE
5836                    unless (defined $i) {                    unless (defined $i) {
5837                      !!!cp ('t249');                      !!!cp ('t249');
5838                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5839                                        text => $token->{tag_name}, token => $token);
5840                      ## Ignore the token                      ## Ignore the token
5841                        !!!nack ('t249.1');
5842                      !!!next-token;                      !!!next-token;
5843                      redo B;                      next B;
5844                    }                    }
5845                                    
5846                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 5848  sub _tree_construction_main ($) {
5848                  my $i;                  my $i;
5849                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5850                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5851                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5852                      !!!cp ('t250');                      !!!cp ('t250');
5853                      $i = $_;                      $i = $_;
5854                      last INSCOPE;                      last INSCOPE;
5855                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5856                      !!!cp ('t251');                      !!!cp ('t251');
5857                      last INSCOPE;                      last INSCOPE;
5858                    }                    }
5859                  } # INSCOPE                  } # INSCOPE
5860                    unless (defined $i) {                    unless (defined $i) {
5861                      !!!cp ('t252');                      !!!cp ('t252');
5862                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
5863                                        text => 'tr', token => $token);
5864                      ## Ignore the token                      ## Ignore the token
5865                        !!!nack ('t252.1');
5866                      !!!next-token;                      !!!next-token;
5867                      redo B;                      next B;
5868                    }                    }
5869                                    
5870                  ## Clear back to table row context                  ## Clear back to table row context
5871                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5872                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5873                    !!!cp ('t253');                    !!!cp ('t253');
5874  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5875                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5876                  }                  }
5877                                    
# Line 4742  sub _tree_construction_main ($) { Line 5884  sub _tree_construction_main ($) {
5884                my $i;                my $i;
5885                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5887                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5888                    !!!cp ('t254');                    !!!cp ('t254');
5889                    $i = $_;                    $i = $_;
5890                    last INSCOPE;                    last INSCOPE;
5891                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5892                    !!!cp ('t255');                    !!!cp ('t255');
5893                    last INSCOPE;                    last INSCOPE;
5894                  }                  }
5895                } # INSCOPE                } # INSCOPE
5896                unless (defined $i) {                unless (defined $i) {
5897                  !!!cp ('t256');                  !!!cp ('t256');
5898                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5899                                    text => $token->{tag_name}, token => $token);
5900                  ## Ignore the token                  ## Ignore the token
5901                    !!!nack ('t256.1');
5902                  !!!next-token;                  !!!next-token;
5903                  redo B;                  next B;
5904                }                }
5905    
5906                ## Clear back to table body context                ## Clear back to table body context
5907                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5908                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5909                  !!!cp ('t257');                  !!!cp ('t257');
5910  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5911                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5912                }                }
5913    
5914                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5915                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5916                  !!!nack ('t257.1');
5917                !!!next-token;                !!!next-token;
5918                redo B;                next B;
5919              } elsif ({              } elsif ({
5920                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5921                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5922                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5923                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5924                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5925                !!!cp ('t258');            !!!cp ('t258');
5926                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5927                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5928                !!!next-token;            ## Ignore the token
5929                redo B;            !!!nack ('t258.1');
5930               !!!next-token;
5931              next B;
5932          } else {          } else {
5933            !!!cp ('t259');            !!!cp ('t259');
5934            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
5935                              text => $token->{tag_name}, token => $token);
5936    
5937            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5938            #            #
5939          }          }
5940          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5941            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5942                    @{$self->{open_elements}} == 1) { # redundant, maybe
5943              !!!parse-error (type => 'in body:#eof', token => $token);
5944              !!!cp ('t259.1');
5945              #
5946            } else {
5947              !!!cp ('t259.2');
5948              #
5949            }
5950    
5951            ## Stop parsing
5952            last B;
5953        } else {        } else {
5954          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5955        }        }
# Line 4803  sub _tree_construction_main ($) { Line 5960  sub _tree_construction_main ($) {
5960                unless (length $token->{data}) {                unless (length $token->{data}) {
5961                  !!!cp ('t260');                  !!!cp ('t260');
5962                  !!!next-token;                  !!!next-token;
5963                  redo B;                  next B;
5964                }                }
5965              }              }
5966                            
# Line 4812  sub _tree_construction_main ($) { Line 5969  sub _tree_construction_main ($) {
5969            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5970              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5971                !!!cp ('t262');                !!!cp ('t262');
5972                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5973                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5974                  !!!ack ('t262.1');
5975                !!!next-token;                !!!next-token;
5976                redo B;                next B;
5977              } else {              } else {
5978                !!!cp ('t263');                !!!cp ('t263');
5979                #                #
5980              }              }
5981            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5982              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5983                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5984                  !!!cp ('t264');                  !!!cp ('t264');
5985                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
5986                                    text => 'colgroup', token => $token);
5987                  ## Ignore the token                  ## Ignore the token
5988                  !!!next-token;                  !!!next-token;
5989                  redo B;                  next B;
5990                } else {                } else {
5991                  !!!cp ('t265');                  !!!cp ('t265');
5992                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5993                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5994                  !!!next-token;                  !!!next-token;
5995                  redo B;                              next B;            
5996                }                }
5997              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5998                !!!cp ('t266');                !!!cp ('t266');
5999                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6000                                  text => 'col', token => $token);
6001                ## Ignore the token                ## Ignore the token
6002                !!!next-token;                !!!next-token;
6003                redo B;                next B;
6004              } else {              } else {
6005                !!!cp ('t267');                !!!cp ('t267');
6006                #                #
6007              }              }
6008            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6009              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6010            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6011              !!!cp ('t270.2');
6012              ## Stop parsing.
6013              last B;
6014            } else {
6015              ## NOTE: As if </colgroup>.
6016              !!!cp ('t270.1');
6017              pop @{$self->{open_elements}}; # colgroup
6018              $self->{insertion_mode} = IN_TABLE_IM;
6019              ## Reprocess.
6020              next B;
6021            }
6022          } else {
6023            die "$0: $token->{type}: Unknown token type";
6024          }
6025    
6026            ## As if </colgroup>            ## As if </colgroup>
6027            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6028              !!!cp ('t269');              !!!cp ('t269');
6029              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6030                !!!parse-error (type => 'unmatched end tag',
6031                                text => 'colgroup', token => $token);
6032              ## Ignore the token              ## Ignore the token
6033                !!!nack ('t269.1');
6034              !!!next-token;              !!!next-token;
6035              redo B;              next B;
6036            } else {            } else {
6037              !!!cp ('t270');              !!!cp ('t270');
6038              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6039              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6040                !!!ack-later;
6041              ## reprocess              ## reprocess
6042              redo B;              next B;
6043            }            }
6044      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6045        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6046          !!!cp ('t271');          !!!cp ('t271');
6047          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6048          !!!next-token;          !!!next-token;
6049          redo B;          next B;
6050        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6051              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6052                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053                  !!!cp ('t272');              !!!cp ('t272');
6054                  ## As if </option>              ## As if </option>
6055                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6056                } else {            } else {
6057                  !!!cp ('t273');              !!!cp ('t273');
6058                }            }
6059    
6060                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6061                !!!next-token;            !!!nack ('t273.1');
6062                redo B;            !!!next-token;
6063              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6064                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6065                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6066                  ## As if </option>              !!!cp ('t274');
6067                  pop @{$self->{open_elements}};              ## As if </option>
6068                } else {              pop @{$self->{open_elements}};
6069                  !!!cp ('t275');            } else {
6070                }              !!!cp ('t275');
6071              }
6072    
6073                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6074                  !!!cp ('t276');              !!!cp ('t276');
6075                  ## As if </optgroup>              ## As if </optgroup>
6076                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6077                } else {            } else {
6078                  !!!cp ('t277');              !!!cp ('t277');
6079                }            }
6080    
6081                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6082                !!!next-token;            !!!nack ('t277.1');
6083                redo B;            !!!next-token;
6084              } elsif ($token->{tag_name} eq 'select') {            next B;
6085  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
6086                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
6087                ## As if </select> instead                   }->{$token->{tag_name}} or
6088                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6089                my $i;                    {
6090                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
6091                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
6092                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
6093                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
6094                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
6095                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
6096                  } elsif ({                            token => $token);
6097                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
6098                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
6099                    !!!cp ('t279');            ## have an element in table scope
6100                    last INSCOPE;            my $i;
6101                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6102                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6103                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6104                  !!!cp ('t280');                !!!cp ('t278');
6105                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6106                  ## Ignore the token                last INSCOPE;
6107                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6108                  redo B;                !!!cp ('t279');
6109                }                last INSCOPE;
6110                }
6111              } # INSCOPE
6112              unless (defined $i) {
6113                !!!cp ('t280');
6114                !!!parse-error (type => 'unmatched end tag',
6115                                text => 'select', token => $token);
6116                ## Ignore the token
6117                !!!nack ('t280.1');
6118                !!!next-token;
6119                next B;
6120              }
6121                                
6122                !!!cp ('t281');            !!!cp ('t281');
6123                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6124    
6125                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6126    
6127                !!!next-token;            if ($token->{tag_name} eq 'select') {
6128                redo B;              !!!nack ('t281.2');
6129                !!!next-token;
6130                next B;
6131              } else {
6132                !!!cp ('t281.1');
6133                !!!ack-later;
6134                ## Reprocess the token.
6135                next B;
6136              }
6137          } else {          } else {
6138            !!!cp ('t282');            !!!cp ('t282');
6139            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6140                              text => $token->{tag_name}, token => $token);
6141            ## Ignore the token            ## Ignore the token
6142              !!!nack ('t282.1');
6143            !!!next-token;            !!!next-token;
6144            redo B;            next B;
6145          }          }
6146        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6147              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6148                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6149                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6150                  !!!cp ('t283');              !!!cp ('t283');
6151                  ## As if </option>              ## As if </option>
6152                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6153                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6154                  !!!cp ('t284');              !!!cp ('t284');
6155                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6156                } else {            } else {
6157                  !!!cp ('t285');              !!!cp ('t285');
6158                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6159                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6160                }              ## Ignore the token
6161                !!!next-token;            }
6162                redo B;            !!!nack ('t285.1');
6163              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6164                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6165                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6166                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6167                } else {              !!!cp ('t286');
6168                  !!!cp ('t287');              pop @{$self->{open_elements}};
6169                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6170                  ## Ignore the token              !!!cp ('t287');
6171                }              !!!parse-error (type => 'unmatched end tag',
6172                !!!next-token;                              text => $token->{tag_name}, token => $token);
6173                redo B;              ## Ignore the token
6174              } elsif ($token->{tag_name} eq 'select') {            }
6175                ## have an element in table scope            !!!nack ('t287.1');
6176                my $i;            !!!next-token;
6177                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6178                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6179                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6180                    !!!cp ('t288');            my $i;
6181                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6182                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6183                  } elsif ({              if ($node->[1] & SELECT_EL) {
6184                            table => 1, html => 1,                !!!cp ('t288');
6185                           }->{$node->[1]}) {                $i = $_;
6186                    !!!cp ('t289');                last INSCOPE;
6187                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6188                  }                !!!cp ('t289');
6189                } # INSCOPE                last INSCOPE;
6190                unless (defined $i) {              }
6191                  !!!cp ('t290');            } # INSCOPE
6192                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6193                  ## Ignore the token              !!!cp ('t290');
6194                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6195                  redo B;                              text => $token->{tag_name}, token => $token);
6196                }              ## Ignore the token
6197                !!!nack ('t290.1');
6198                !!!next-token;
6199                next B;
6200              }
6201                                
6202                !!!cp ('t291');            !!!cp ('t291');
6203                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6204    
6205                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6206    
6207                !!!next-token;            !!!nack ('t291.1');
6208                redo B;            !!!next-token;
6209              } elsif ({            next B;
6210                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6211                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6212                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6213                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6214                     }->{$token->{tag_name}}) {
6215  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6216                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6217                              text => $token->{tag_name}, token => $token);
6218                                
6219                ## have an element in table scope            ## have an element in table scope
6220                my $i;            my $i;
6221                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6222                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6223                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6224                    !!!cp ('t292');                !!!cp ('t292');
6225                    $i = $_;                $i = $_;
6226                    last INSCOPE;                last INSCOPE;
6227                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6228                            table => 1, html => 1,                !!!cp ('t293');
6229                           }->{$node->[1]}) {                last INSCOPE;
6230                    !!!cp ('t293');              }
6231                    last INSCOPE;            } # INSCOPE
6232                  }            unless (defined $i) {
6233                } # INSCOPE              !!!cp ('t294');
6234                unless (defined $i) {              ## Ignore the token
6235                  !!!cp ('t294');              !!!nack ('t294.1');
6236                  ## Ignore the token              !!!next-token;
6237                  !!!next-token;              next B;
6238                  redo B;            }
               }  
6239                                
6240                ## As if </select>            ## As if </select>
6241                ## have an element in table scope            ## have an element in table scope
6242                undef $i;            undef $i;
6243                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6244                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6245                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6246                    !!!cp ('t295');                !!!cp ('t295');
6247                    $i = $_;                $i = $_;
6248                    last INSCOPE;                last INSCOPE;
6249                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6250  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6251                    !!!cp ('t296');                !!!cp ('t296');
6252                    last INSCOPE;                last INSCOPE;
6253                  }              }
6254                } # INSCOPE            } # INSCOPE
6255                unless (defined $i) {            unless (defined $i) {
6256                  !!!cp ('t297');              !!!cp ('t297');
6257  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6258                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6259                  ## Ignore the </select> token                              text => 'select', token => $token);
6260                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6261                  redo B;              !!!nack ('t297.1');
6262                }              !!!next-token; ## TODO: ok?
6263                next B;
6264              }
6265                                
6266                !!!cp ('t298');            !!!cp ('t298');
6267                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6268    
6269                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6270    
6271                ## reprocess            !!!ack-later;
6272                redo B;            ## reprocess
6273              next B;
6274          } else {          } else {
6275            !!!cp ('t299');            !!!cp ('t299');
6276            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6277                              text => $token->{tag_name}, token => $token);
6278            ## Ignore the token            ## Ignore the token
6279              !!!nack ('t299.3');
6280            !!!next-token;            !!!next-token;
6281            redo B;            next B;
6282            }
6283          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6284            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6285                    @{$self->{open_elements}} == 1) { # redundant, maybe
6286              !!!cp ('t299.1');
6287              !!!parse-error (type => 'in body:#eof', token => $token);
6288            } else {
6289              !!!cp ('t299.2');
6290          }          }
6291    
6292            ## Stop parsing.
6293            last B;
6294        } else {        } else {
6295          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6296        }        }
# Line 5086  sub _tree_construction_main ($) { Line 6306  sub _tree_construction_main ($) {
6306            unless (length $token->{data}) {            unless (length $token->{data}) {
6307              !!!cp ('t300');              !!!cp ('t300');
6308              !!!next-token;              !!!next-token;
6309              redo B;              next B;
6310            }            }
6311          }          }
6312                    
6313          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6314            !!!cp ('t301');            !!!cp ('t301');
6315            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6316    
6317            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6318          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 6320  sub _tree_construction_main ($) {
6320          }          }
6321                    
6322          ## "after body" insertion mode          ## "after body" insertion mode
6323          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6324    
6325          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6326          ## reprocess          ## reprocess
6327          redo B;          next B;
6328        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6329          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6330            !!!cp ('t303');            !!!cp ('t303');
6331            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6332                              text => $token->{tag_name}, token => $token);
6333                        
6334            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6335          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 6337  sub _tree_construction_main ($) {
6337          }          }
6338    
6339          ## "after body" insertion mode          ## "after body" insertion mode
6340          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6341                            text => $token->{tag_name}, token => $token);
6342    
6343          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6344            !!!ack-later;
6345          ## reprocess          ## reprocess
6346          redo B;          next B;
6347        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6348          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6349            !!!cp ('t305');            !!!cp ('t305');
6350            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6351                              text => $token->{tag_name}, token => $token);
6352                        
6353            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6354            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 6360  sub _tree_construction_main ($) {
6360          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6361            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6362              !!!cp ('t307');              !!!cp ('t307');
6363              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6364                                text => 'html', token => $token);
6365              ## Ignore the token              ## Ignore the token
6366              !!!next-token;              !!!next-token;
6367              redo B;              next B;
6368            } else {            } else {
6369              !!!cp ('t308');              !!!cp ('t308');
6370              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6371              !!!next-token;              !!!next-token;
6372              redo B;              next B;
6373            }            }
6374          } else {          } else {
6375            !!!cp ('t309');            !!!cp ('t309');
6376            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6377                              text => $token->{tag_name}, token => $token);
6378    
6379            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6380            ## reprocess            ## reprocess
6381            redo B;            next B;
6382          }          }
6383          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6384            !!!cp ('t309.2');
6385            ## Stop parsing
6386            last B;
6387        } else {        } else {
6388          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6389        }        }
# Line 5165  sub _tree_construction_main ($) { Line 6395  sub _tree_construction_main ($) {
6395            unless (length $token->{data}) {            unless (length $token->{data}) {
6396              !!!cp ('t310');              !!!cp ('t310');
6397              !!!next-token;              !!!next-token;
6398              redo B;              next B;
6399            }            }
6400          }          }
6401                    
6402          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6403            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404              !!!cp ('t311');              !!!cp ('t311');
6405              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6406            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6407              !!!cp ('t312');              !!!cp ('t312');
6408              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6409            } else { # "after html frameset"            } else { # "after after frameset"
6410              !!!cp ('t313');              !!!cp ('t313');
6411              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character');  
6412            }            }
6413                        
6414            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 6419  sub _tree_construction_main ($) {
6419              !!!cp ('t315');              !!!cp ('t315');
6420              !!!next-token;              !!!next-token;
6421            }            }
6422            redo B;            next B;
6423          }          }
6424                    
6425          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6426        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6427          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6428              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6429            !!!cp ('t318');            !!!cp ('t318');
6430            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6431              !!!nack ('t318.1');
6432            !!!next-token;            !!!next-token;
6433            redo B;            next B;
6434          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6435                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6436            !!!cp ('t319');            !!!cp ('t319');
6437            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6438            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6439              !!!ack ('t319.1');
6440            !!!next-token;            !!!next-token;
6441            redo B;            next B;
6442          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6443            !!!cp ('t320');            !!!cp ('t320');
6444            ## NOTE: As if in body.            ## NOTE: As if in head.
6445            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6446            redo B;            next B;
6447    
6448              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6449              ## has no parse error.
6450          } else {          } else {
6451            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6452              !!!cp ('t321');              !!!cp ('t321');
6453              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6454            } else {                              text => $token->{tag_name}, token => $token);
6455              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6456              !!!cp ('t322');              !!!cp ('t322');
6457              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6458                                text => $token->{tag_name}, token => $token);
6459              } else { # "after after frameset"
6460                !!!cp ('t322.2');
6461                !!!parse-error (type => 'after after frameset',
6462                                text => $token->{tag_name}, token => $token);
6463            }            }
6464            ## Ignore the token            ## Ignore the token
6465              !!!nack ('t322.1');
6466            !!!next-token;            !!!next-token;
6467            redo B;            next B;
6468          }          }
6469        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6470          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6471              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6472            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6473                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6474              !!!cp ('t325');              !!!cp ('t325');
6475              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6476                                text => $token->{tag_name}, token => $token);
6477              ## Ignore the token              ## Ignore the token
6478              !!!next-token;              !!!next-token;
6479            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 6483  sub _tree_construction_main ($) {
6483            }            }
6484    
6485            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6486                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6487              !!!cp ('t327');              !!!cp ('t327');
6488              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6489            } else {            } else {
6490              !!!cp ('t328');              !!!cp ('t328');
6491            }            }
6492            redo B;            next B;
6493          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6494                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6495            !!!cp ('t329');            !!!cp ('t329');
6496            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6497            !!!next-token;            !!!next-token;
6498            redo B;            next B;
6499          } else {          } else {
6500            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6501              !!!cp ('t330');              !!!cp ('t330');
6502              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6503            } else {                              text => $token->{tag_name}, token => $token);
6504              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6505                !!!cp ('t330.1');
6506                !!!parse-error (type => 'after frameset:/',
6507                                text => $token->{tag_name}, token => $token);
6508              } else { # "after after html"
6509              !!!cp ('t331');              !!!cp ('t331');
6510              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6511                                text => $token->{tag_name}, token => $token);
6512            }            }
6513            ## Ignore the token            ## Ignore the token
6514            !!!next-token;            !!!next-token;
6515            redo B;            next B;
6516          }          }
6517          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6518            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6519                    @{$self->{open_elements}} == 1) { # redundant, maybe
6520              !!!cp ('t331.1');
6521              !!!parse-error (type => 'in body:#eof', token => $token);
6522            } else {
6523              !!!cp ('t331.2');
6524            }
6525            
6526            ## Stop parsing
6527            last B;
6528        } else {        } else {
6529          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6530        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6539  sub _tree_construction_main ($) {
6539        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6540          !!!cp ('t332');          !!!cp ('t332');
6541          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6542          $script_start_tag->($insert);          $script_start_tag->();
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6545          !!!cp ('t333');          !!!cp ('t333');
6546          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6547          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6548          redo B;          next B;
6549        } elsif ({        } elsif ({
6550                  base => 1, link => 1,                  base => 1, link => 1,
6551                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6552          !!!cp ('t334');          !!!cp ('t334');
6553          ## 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
6554          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6555          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6556            !!!ack ('t334.1');
6557          !!!next-token;          !!!next-token;
6558          redo B;          next B;
6559        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6560          ## 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
6561          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6562          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.
6563    
6564          unless ($self->{confident}) {          unless ($self->{confident}) {
6565            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6566              !!!cp ('t335');              !!!cp ('t335');
6567                ## NOTE: Whether the encoding is supported or not is handled
6568                ## in the {change_encoding} callback.
6569              $self->{change_encoding}              $self->{change_encoding}
6570                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6571                            
6572              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6573                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6574                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6575                                           ->{has_reference});                                           ->{has_reference});
6576            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6577              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6578                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6579                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6580                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6581                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6582                !!!cp ('t336');                !!!cp ('t336');
6583                  ## NOTE: Whether the encoding is supported or not is handled
6584                  ## in the {change_encoding} callback.
6585                $self->{change_encoding}                $self->{change_encoding}
6586                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6587                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6588                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6589                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6607  sub _tree_construction_main ($) {
6607            }            }
6608          }          }
6609    
6610            !!!ack ('t338.1');
6611          !!!next-token;          !!!next-token;
6612          redo B;          next B;
6613        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6614          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6615          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6616          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6617            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6618        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6619          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6620                                
6621          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6622              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6623            !!!cp ('t342');            !!!cp ('t342');
6624            ## Ignore the token            ## Ignore the token
6625          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6633  sub _tree_construction_main ($) {
6633              }              }
6634            }            }
6635          }          }
6636            !!!nack ('t343.1');
6637          !!!next-token;          !!!next-token;
6638          redo B;          next B;
6639        } elsif ({        } elsif ({
6640                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6641                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6642                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6643                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6644                  pre => 1,                  pre => 1, listing => 1,
6645                    form => 1,
6646                    table => 1,
6647                    hr => 1,
6648                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6649            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6650              !!!cp ('t350');
6651              !!!parse-error (type => 'in form:form', token => $token);
6652              ## Ignore the token
6653              !!!nack ('t350.1');
6654              !!!next-token;
6655              next B;
6656            }
6657    
6658          ## has a p element in scope          ## has a p element in scope
6659          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6660            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6661              !!!cp ('t344');              !!!cp ('t344');
6662              !!!back-token;              !!!back-token; # <form>
6663              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6664              redo B;                        line => $token->{line}, column => $token->{column}};
6665            } elsif ({              next B;
6666                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6667              !!!cp ('t345');              !!!cp ('t345');
6668              last INSCOPE;              last INSCOPE;
6669            }            }
6670          } # INSCOPE          } # INSCOPE
6671                        
6672          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6674              !!!nack ('t346.1');
6675            !!!next-token;            !!!next-token;
6676            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6677              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6684  sub _tree_construction_main ($) {
6684            } else {            } else {
6685              !!!cp ('t348');              !!!cp ('t348');
6686            }            }
6687          } else {          } elsif ($token->{tag_name} eq 'form') {
6688            !!!cp ('t347');            !!!cp ('t347.1');
6689              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6690    
6691              !!!nack ('t347.2');
6692            !!!next-token;            !!!next-token;
6693          }          } elsif ($token->{tag_name} eq 'table') {
6694          redo B;            !!!cp ('t382');
6695        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6696          if (defined $self->{form_element}) {            
6697            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6698            !!!parse-error (type => 'in form:form');  
6699            ## Ignore the token            !!!nack ('t382.1');
6700              !!!next-token;
6701            } elsif ($token->{tag_name} eq 'hr') {
6702              !!!cp ('t386');
6703              pop @{$self->{open_elements}};
6704            
6705              !!!nack ('t386.1');
6706            !!!next-token;            !!!next-token;
           redo B;  
6707          } else {          } else {
6708            ## 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];  
6709            !!!next-token;            !!!next-token;
           redo B;  
6710          }          }
6711        } elsif ($token->{tag_name} eq 'li') {          next B;
6712          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6713          ## has a p element in scope          ## has a p element in scope
6714          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6715            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6716              !!!cp ('t353');              !!!cp ('t353');
6717              !!!back-token;              !!!back-token; # <x>
6718              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6719              redo B;                        line => $token->{line}, column => $token->{column}};
6720            } elsif ({              next B;
6721                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6722              !!!cp ('t354');              !!!cp ('t354');
6723              last INSCOPE;              last INSCOPE;
6724            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6727  sub _tree_construction_main ($) {
6727          ## Step 1          ## Step 1
6728          my $i = -1;          my $i = -1;
6729          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6730            my $li_or_dtdd = {li => {li => 1},
6731                              dt => {dt => 1, dd => 1},
6732                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6733          LI: {          LI: {
6734            ## Step 2            ## Step 2
6735            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6736              if ($i != -1) {              if ($i != -1) {
6737                !!!cp ('t355');                !!!cp ('t355');
6738                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6739                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6740                                      ->manakai_local_name,
6741                                  token => $token);
6742              } else {              } else {
6743                !!!cp ('t356');                !!!cp ('t356');
6744              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6749  sub _tree_construction_main ($) {
6749            }            }
6750                        
6751            ## Step 3            ## Step 3
6752            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6753                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6754                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6755                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6756                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6757                  not ($node->[1] & DIV_EL)) {
6758              !!!cp ('t358');              !!!cp ('t358');
6759              last LI;              last LI;
6760            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6766  sub _tree_construction_main ($) {
6766            redo LI;            redo LI;
6767          } # LI          } # LI
6768                        
6769          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6770            !!!nack ('t359.1');
6771          !!!next-token;          !!!next-token;
6772          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;  
6773        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6774          ## has a p element in scope          ## has a p element in scope
6775          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6776            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6777              !!!cp ('t367');              !!!cp ('t367');
6778              !!!back-token;              !!!back-token; # <plaintext>
6779              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6780              redo B;                        line => $token->{line}, column => $token->{column}};
6781            } elsif ({              next B;
6782                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6783              !!!cp ('t368');              !!!cp ('t368');
6784              last INSCOPE;              last INSCOPE;
6785            }            }
6786          } # INSCOPE          } # INSCOPE
6787                        
6788          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6789                        
6790          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6791                        
6792            !!!nack ('t368.1');
6793          !!!next-token;          !!!next-token;
6794          redo B;          next B;
6795        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6796          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6797            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6798            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6799              !!!cp ('t371');              !!!cp ('t371');
6800              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6801                            
6802              !!!back-token;              !!!back-token; # <a>
6803              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6804              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6805                $formatting_end_tag->($token);
6806                            
6807              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6808                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 6827  sub _tree_construction_main ($) {
6827                        
6828          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6829    
6830          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6831          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6832    
6833            !!!nack ('t374.1');
6834          !!!next-token;          !!!next-token;
6835          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;  
6836        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6837          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6838    
6839          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6840          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6841            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6842            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6843              !!!cp ('t376');              !!!cp ('t376');
6844              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6845              !!!back-token;              !!!back-token; # <nobr>
6846              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6847              redo B;                        line => $token->{line}, column => $token->{column}};
6848            } elsif ({              next B;
6849                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6850              !!!cp ('t377');              !!!cp ('t377');
6851              last INSCOPE;              last INSCOPE;
6852            }            }
6853          } # INSCOPE          } # INSCOPE
6854                    
6855          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6856          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6857                    
6858            !!!nack ('t377.1');
6859          !!!next-token;          !!!next-token;
6860          redo B;          next B;
6861        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6862          ## has a button element in scope          ## has a button element in scope
6863          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6864            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6865            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6866              !!!cp ('t378');              !!!cp ('t378');
6867              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6868              !!!back-token;              !!!back-token; # <button>
6869              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6870              redo B;                        line => $token->{line}, column => $token->{column}};
6871            } elsif ({              next B;
6872                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6873              !!!cp ('t379');              !!!cp ('t379');
6874              last INSCOPE;              last INSCOPE;
6875            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6877  sub _tree_construction_main ($) {
6877                        
6878          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6879                        
6880          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6881    
6882          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6883    
6884          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6885    
6886            !!!nack ('t379.1');
6887          !!!next-token;          !!!next-token;
6888          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;  
6889        } elsif ({        } elsif ({
6890                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6891                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6892                  image => 1,                  noembed => 1,
6893                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6894                    noscript => 0, ## TODO: 1 if scripting is enabled
6895                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6896          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6897            !!!cp ('t384');            !!!cp ('t381');
6898            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6899          } else {          } else {
6900            !!!cp ('t385');            !!!cp ('t399');
6901          }          }
6902            ## NOTE: There is an "as if in body" code clone.
6903          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6904          $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;  
6905        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6906          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6907                    
6908          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6909            !!!cp ('t389');            !!!cp ('t389');
6910            ## Ignore the token            ## Ignore the token
6911              !!!nack ('t389'); ## NOTE: Not acknowledged.
6912            !!!next-token;            !!!next-token;
6913            redo B;            next B;
6914          } else {          } else {
6915              !!!ack ('t391.1');
6916    
6917            my $at = $token->{attributes};            my $at = $token->{attributes};
6918            my $form_attrs;            my $form_attrs;
6919            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5825  sub _tree_construction_main ($) { Line 6923  sub _tree_construction_main ($) {
6923            delete $at->{prompt};            delete $at->{prompt};
6924            my @tokens = (            my @tokens = (
6925                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6926                           attributes => $form_attrs},                           attributes => $form_attrs,
6927                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6928                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6929                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6930                            {type => START_TAG_TOKEN, tag_name => 'p',
6931                             line => $token->{line}, column => $token->{column}},
6932                            {type => START_TAG_TOKEN, tag_name => 'label',
6933                             line => $token->{line}, column => $token->{column}},
6934                         );                         );
6935            if ($prompt_attr) {            if ($prompt_attr) {
6936              !!!cp ('t390');              !!!cp ('t390');
6937              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6938                               #line => $token->{line}, column => $token->{column},
6939                              };
6940            } else {            } else {
6941              !!!cp ('t391');              !!!cp ('t391');
6942              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6943                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6944                               #line => $token->{line}, column => $token->{column},
6945                              }; # SHOULD
6946              ## TODO: make this configurable              ## TODO: make this configurable
6947            }            }
6948            push @tokens,            push @tokens,
6949                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6950                             line => $token->{line}, column => $token->{column}},
6951                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6952                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6953                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6954                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6955                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6956            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6957                             line => $token->{line}, column => $token->{column}},
6958                            {type => END_TAG_TOKEN, tag_name => 'form',
6959                             line => $token->{line}, column => $token->{column}};
6960            !!!back-token (@tokens);            !!!back-token (@tokens);
6961            redo B;            !!!next-token;
6962              next B;
6963          }          }
6964        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6965          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6966          my $el;          my $el;
6967          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6968                    
6969          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6970          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 6973  sub _tree_construction_main ($) {
6973          $insert->($el);          $insert->($el);
6974                    
6975          my $text = '';          my $text = '';
6976            !!!nack ('t392.1');
6977          !!!next-token;          !!!next-token;
6978          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6979            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 7004  sub _tree_construction_main ($) {
7004            ## Ignore the token            ## Ignore the token
7005          } else {          } else {
7006            !!!cp ('t398');            !!!cp ('t398');
7007            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7008          }          }
7009          !!!next-token;          !!!next-token;
7010            next B;
7011          } elsif ($token->{tag_name} eq 'rt' or
7012                   $token->{tag_name} eq 'rp') {
7013            ## has a |ruby| element in scope
7014            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7015              my $node = $self->{open_elements}->[$_];
7016              if ($node->[1] & RUBY_EL) {
7017                !!!cp ('t398.1');
7018                ## generate implied end tags
7019                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7020                  !!!cp ('t398.2');
7021                  pop @{$self->{open_elements}};
7022                }
7023                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7024                  !!!cp ('t398.3');
7025                  !!!parse-error (type => 'not closed',
7026                                  text => $self->{open_elements}->[-1]->[0]
7027                                      ->manakai_local_name,
7028                                  token => $token);
7029                  pop @{$self->{open_elements}}
7030                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7031                }
7032                last INSCOPE;
7033              } elsif ($node->[1] & SCOPING_EL) {
7034                !!!cp ('t398.4');
7035                last INSCOPE;
7036              }
7037            } # INSCOPE
7038    
7039            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7040    
7041            !!!nack ('t398.5');
7042            !!!next-token;
7043          redo B;          redo B;
7044        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7045                  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');  
7046          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7047    
7048          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7049    
7050            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7051    
7052            ## "adjust foreign attributes" - done in insert-element-f
7053            
7054            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7055                    
7056          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7057              pop @{$self->{open_elements}};
7058              !!!ack ('t398.1');
7059            } else {
7060              !!!cp ('t398.2');
7061              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7062              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7063              ## mode, "in body" (not "in foreign content") secondary insertion
7064              ## mode, maybe.
7065            }
7066    
7067          !!!next-token;          !!!next-token;
7068          redo B;          next B;
7069        } elsif ({        } elsif ({
7070                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7071                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 7073  sub _tree_construction_main ($) {
7073                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7074                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7075          !!!cp ('t401');          !!!cp ('t401');
7076          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7077                            text => $token->{tag_name}, token => $token);
7078          ## Ignore the token          ## Ignore the token
7079            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7080          !!!next-token;          !!!next-token;
7081          redo B;          next B;
7082                    
7083          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7084        } else {        } else {
7085          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7086              !!!cp ('t384');
7087              !!!parse-error (type => 'image', token => $token);
7088              $token->{tag_name} = 'img';
7089            } else {
7090              !!!cp ('t385');
7091            }
7092    
7093            ## NOTE: There is an "as if <br>" code clone.
7094          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7095                    
7096          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7097    
7098            if ({
7099                 applet => 1, marquee => 1, object => 1,
7100                }->{$token->{tag_name}}) {
7101              !!!cp ('t380');
7102              push @$active_formatting_elements, ['#marker', ''];
7103              !!!nack ('t380.1');
7104            } elsif ({
7105                      b => 1, big => 1, em => 1, font => 1, i => 1,
7106                      s => 1, small => 1, strile => 1,
7107                      strong => 1, tt => 1, u => 1,
7108                     }->{$token->{tag_name}}) {
7109              !!!cp ('t375');
7110              push @$active_formatting_elements, $self->{open_elements}->[-1];
7111              !!!nack ('t375.1');
7112            } elsif ($token->{tag_name} eq 'input') {
7113              !!!cp ('t388');
7114              ## TODO: associate with $self->{form_element} if defined
7115              pop @{$self->{open_elements}};
7116              !!!ack ('t388.2');
7117            } elsif ({
7118                      area => 1, basefont => 1, bgsound => 1, br => 1,
7119                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7120                      #image => 1,
7121                     }->{$token->{tag_name}}) {
7122              !!!cp ('t388.1');
7123              pop @{$self->{open_elements}};
7124              !!!ack ('t388.3');
7125            } elsif ($token->{tag_name} eq 'select') {
7126              ## TODO: associate with $self->{form_element} if defined
7127            
7128              if ($self->{insertion_mode} & TABLE_IMS or
7129                  $self->{insertion_mode} & BODY_TABLE_IMS or
7130                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7131                !!!cp ('t400.1');
7132                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7133              } else {
7134                !!!cp ('t400.2');
7135                $self->{insertion_mode} = IN_SELECT_IM;
7136              }
7137              !!!nack ('t400.3');
7138            } else {
7139              !!!nack ('t402');
7140            }
7141                    
7142          !!!next-token;          !!!next-token;
7143          redo B;          next B;
7144        }        }
7145      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7146        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7147          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7148              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7149            for (@{$self->{open_elements}}) {          INSCOPE: {
7150              unless ({            for (reverse @{$self->{open_elements}}) {
7151                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7152                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7153                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7154                      }->{$_->[1]}) {                last INSCOPE;
7155                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7156                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7157              } else {                last;
               !!!cp ('t404');  
7158              }              }
7159            }            }
7160    
7161            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7162                              text => $token->{tag_name}, token => $token);
7163              ## NOTE: Ignore the token.
7164            !!!next-token;            !!!next-token;
7165            redo B;            next B;
7166          } else {          } # INSCOPE
7167            !!!cp ('t405');  
7168            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7169            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7170            !!!next-token;              !!!cp ('t403');
7171            redo B;              !!!parse-error (type => 'not closed',
7172                                text => $_->[0]->manakai_local_name,
7173                                token => $token);
7174                last;
7175              } else {
7176                !!!cp ('t404');
7177              }
7178          }          }
7179    
7180            $self->{insertion_mode} = AFTER_BODY_IM;
7181            !!!next-token;
7182            next B;
7183        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7184          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
7185            ## up-to-date, though it has same effect as speced.
7186            if (@{$self->{open_elements}} > 1 and
7187                $self->{open_elements}->[1]->[1] & BODY_EL) {
7188            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7189            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7190              !!!cp ('t406');              !!!cp ('t406');
7191              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7192                                text => $self->{open_elements}->[1]->[0]
7193                                    ->manakai_local_name,
7194                                token => $token);
7195            } else {            } else {
7196              !!!cp ('t407');              !!!cp ('t407');
7197            }            }
7198            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7199            ## reprocess            ## reprocess
7200            redo B;            next B;
7201          } else {          } else {
7202            !!!cp ('t408');            !!!cp ('t408');
7203            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7204                              text => $token->{tag_name}, token => $token);
7205            ## Ignore the token            ## Ignore the token
7206            !!!next-token;            !!!next-token;
7207            redo B;            next B;
7208          }          }
7209        } elsif ({        } elsif ({
7210                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7211                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7212                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7213                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7214                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7215                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7216          ## has an element in scope          ## has an element in scope
7217          my $i;          my $i;
7218          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7219            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7220            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}};  
             }  
               
7221              !!!cp ('t410');              !!!cp ('t410');
7222              $i = $_;              $i = $_;
7223              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7224            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7225              !!!cp ('t411');              !!!cp ('t411');
7226              last INSCOPE;              last INSCOPE;
7227            }            }
7228          } # INSCOPE          } # INSCOPE
7229            
7230          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7231            if (defined $i) {            !!!cp ('t413');
7232              !!!parse-error (type => 'unmatched end tag',
7233                              text => $token->{tag_name}, token => $token);
7234              ## NOTE: Ignore the token.
7235            } else {
7236              ## Step 1. generate implied end tags
7237              while ({
7238                      ## END_TAG_OPTIONAL_EL
7239                      dd => ($token->{tag_name} ne 'dd'),
7240                      dt => ($token->{tag_name} ne 'dt'),
7241                      li => ($token->{tag_name} ne 'li'),
7242                      p => 1,
7243                      rt => 1,
7244                      rp => 1,
7245                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7246                !!!cp ('t409');
7247                pop @{$self->{open_elements}};
7248              }
7249    
7250              ## Step 2.
7251              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7252                      ne $token->{tag_name}) {
7253              !!!cp ('t412');              !!!cp ('t412');
7254              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7255                                text => $self->{open_elements}->[-1]->[0]
7256                                    ->manakai_local_name,
7257                                token => $token);
7258            } else {            } else {
7259              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7260            }            }
7261          }  
7262                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7263            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7264          } elsif ($token->{tag_name} eq 'p') {  
7265            !!!cp ('t415');            ## Step 4.
7266            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7267            my $el;                if {
7268            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7269            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7270          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7271          !!!next-token;          !!!next-token;
7272          redo B;          next B;
7273        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7274            undef $self->{form_element};
7275    
7276          ## has an element in scope          ## has an element in scope
7277            my $i;
7278          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7279            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7280            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}};  
             }  
   
7281              !!!cp ('t418');              !!!cp ('t418');
7282                $i = $_;
7283              last INSCOPE;              last INSCOPE;
7284            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7285              !!!cp ('t419');              !!!cp ('t419');
7286              last INSCOPE;              last INSCOPE;
7287            }            }
7288          } # INSCOPE          } # INSCOPE
7289            
7290          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 {  
7291            !!!cp ('t421');            !!!cp ('t421');
7292            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7293                              text => $token->{tag_name}, token => $token);
7294              ## NOTE: Ignore the token.
7295            } else {
7296              ## Step 1. generate implied end tags
7297              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7298                !!!cp ('t417');
7299                pop @{$self->{open_elements}};
7300              }
7301              
7302              ## Step 2.
7303              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7304                      ne $token->{tag_name}) {
7305                !!!cp ('t417.1');
7306                !!!parse-error (type => 'not closed',
7307                                text => $self->{open_elements}->[-1]->[0]
7308                                    ->manakai_local_name,
7309                                token => $token);
7310              } else {
7311                !!!cp ('t420');
7312              }  
7313              
7314              ## Step 3.
7315              splice @{$self->{open_elements}}, $i;
7316          }          }
7317    
         undef $self->{form_element};  
7318          !!!next-token;          !!!next-token;
7319          redo B;          next B;
7320        } elsif ({        } elsif ({
7321                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7322                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6091  sub _tree_construction_main ($) { Line 7324  sub _tree_construction_main ($) {
7324          my $i;          my $i;
7325          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7326            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7327            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}};  
             }  
   
7328              !!!cp ('t423');              !!!cp ('t423');
7329              $i = $_;              $i = $_;
7330              last INSCOPE;              last INSCOPE;
7331            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7332              !!!cp ('t424');              !!!cp ('t424');
7333              last INSCOPE;              last INSCOPE;
7334            }            }
7335          } # INSCOPE          } # INSCOPE
7336    
7337            unless (defined $i) { # has an element in scope
7338              !!!cp ('t425.1');
7339              !!!parse-error (type => 'unmatched end tag',
7340                              text => $token->{tag_name}, token => $token);
7341              ## NOTE: Ignore the token.
7342            } else {
7343              ## Step 1. generate implied end tags
7344              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7345                !!!cp ('t422');
7346                pop @{$self->{open_elements}};
7347              }
7348              
7349              ## Step 2.
7350              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7351                      ne $token->{tag_name}) {
7352                !!!cp ('t425');
7353                !!!parse-error (type => 'unmatched end tag',
7354                                text => $token->{tag_name}, token => $token);
7355              } else {
7356                !!!cp ('t426');
7357              }
7358    
7359              ## Step 3.
7360              splice @{$self->{open_elements}}, $i;
7361            }
7362                    
7363          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7364            !!!cp ('t425');          next B;
7365            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7366            ## has an element in scope
7367            my $i;
7368            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7369              my $node = $self->{open_elements}->[$_];
7370              if ($node->[1] & P_EL) {
7371                !!!cp ('t410.1');
7372                $i = $_;
7373                last INSCOPE;
7374              } elsif ($node->[1] & SCOPING_EL) {
7375                !!!cp ('t411.1');
7376                last INSCOPE;
7377              }
7378            } # INSCOPE
7379    
7380            if (defined $i) {
7381              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7382                      ne $token->{tag_name}) {
7383                !!!cp ('t412.1');
7384                !!!parse-error (type => 'not closed',
7385                                text => $self->{open_elements}->[-1]->[0]
7386                                    ->manakai_local_name,
7387                                token => $token);
7388              } else {
7389                !!!cp ('t414.1');
7390              }
7391    
7392              splice @{$self->{open_elements}}, $i;
7393          } else {          } else {
7394            !!!cp ('t426');            !!!cp ('t413.1');
7395              !!!parse-error (type => 'unmatched end tag',
7396                              text => $token->{tag_name}, token => $token);
7397    
7398              !!!cp ('t415.1');
7399              ## As if <p>, then reprocess the current token
7400              my $el;
7401              !!!create-element ($el, $HTML_NS, 'p',, $token);
7402              $insert->($el);
7403              ## NOTE: Not inserted into |$self->{open_elements}|.
7404          }          }
7405            
         splice @{$self->{open_elements}}, $i if defined $i;  
7406          !!!next-token;          !!!next-token;
7407          redo B;          next B;
7408        } elsif ({        } elsif ({
7409                  a => 1,                  a => 1,
7410                  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 7412  sub _tree_construction_main ($) {
7412                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7413                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7414          !!!cp ('t427');          !!!cp ('t427');
7415          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7416          redo B;          next B;
7417        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7418          !!!cp ('t428');          !!!cp ('t428');
7419          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7420                            text => 'br', token => $token);
7421    
7422          ## As if <br>          ## As if <br>
7423          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7424                    
7425          my $el;          my $el;
7426          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7427          $insert->($el);          $insert->($el);
7428                    
7429          ## Ignore the token.          ## Ignore the token.
7430          !!!next-token;          !!!next-token;
7431          redo B;          next B;
7432        } elsif ({        } elsif ({
7433                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7434                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6160  sub _tree_construction_main ($) { Line 7442  sub _tree_construction_main ($) {
7442                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7443                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7444          !!!cp ('t429');          !!!cp ('t429');
7445          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7446                            text => $token->{tag_name}, token => $token);
7447          ## Ignore the token          ## Ignore the token
7448          !!!next-token;          !!!next-token;
7449          redo B;          next B;
7450                    
7451          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7452                    
# Line 6174  sub _tree_construction_main ($) { Line 7457  sub _tree_construction_main ($) {
7457    
7458          ## Step 2          ## Step 2
7459          S2: {          S2: {
7460            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7461              ## Step 1              ## Step 1
7462              ## generate implied end tags              ## generate implied end tags
7463              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7464                !!!cp ('t430');                !!!cp ('t430');
7465                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7466                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7467                  ## which seems wrong.
7468                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7469                  $node_i++;
7470              }              }
7471                    
7472              ## Step 2              ## Step 2
7473              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7474                        ne $token->{tag_name}) {
7475                !!!cp ('t431');                !!!cp ('t431');
7476                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7477                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7478                                  text => $self->{open_elements}->[-1]->[0]
7479                                      ->manakai_local_name,
7480                                  token => $token);
7481              } else {              } else {
7482                !!!cp ('t432');                !!!cp ('t432');
7483              }              }
7484                            
7485              ## Step 3              ## Step 3
7486              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7487    
7488              !!!next-token;              !!!next-token;
7489              last S2;              last S2;
7490            } else {            } else {
7491              ## Step 3              ## Step 3
7492              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7493                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7494                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7495                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7496                !!!cp ('t433');                !!!cp ('t433');
7497                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7498                                  text => $token->{tag_name}, token => $token);
7499                ## Ignore the token                ## Ignore the token
7500                !!!next-token;                !!!next-token;
7501                last S2;                last S2;
# Line 6222  sub _tree_construction_main ($) { Line 7511  sub _tree_construction_main ($) {
7511            ## Step 5;            ## Step 5;
7512            redo S2;            redo S2;
7513          } # S2          } # S2
7514          redo B;          next B;
7515        }        }
7516      }      }
7517      redo B;      next B;
7518      } continue { # B
7519        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7520          ## NOTE: The code below is executed in cases where it does not have
7521          ## to be, but it it is harmless even in those cases.
7522          ## has an element in scope
7523          INSCOPE: {
7524            for (reverse 0..$#{$self->{open_elements}}) {
7525              my $node = $self->{open_elements}->[$_];
7526              if ($node->[1] & FOREIGN_EL) {
7527                last INSCOPE;
7528              } elsif ($node->[1] & SCOPING_EL) {
7529                last;
7530              }
7531            }
7532            
7533            ## NOTE: No foreign element in scope.
7534            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7535          } # INSCOPE
7536        }
7537    } # B    } # B
7538    
7539    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6233  sub _tree_construction_main ($) { Line 7541  sub _tree_construction_main ($) {
7541    ## TODO: script stuffs    ## TODO: script stuffs
7542  } # _tree_construct_main  } # _tree_construct_main
7543    
7544  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7545    my $class = shift;    my $class = shift;
7546    my $node = shift;    my $node = shift;
7547    my $s = \$_[0];    my $s = \$_[0];
7548    my $onerror = $_[1];    my $onerror = $_[1];
7549      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7550    
7551    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7552    
# Line 6256  sub set_inner_html ($$$) { Line 7565  sub set_inner_html ($$$) {
7565      }      }
7566    
7567      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7568      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7569    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7570      ## TODO: If non-html element      ## TODO: If non-html element
7571    
7572      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7573    
7574    ## TODO: Support for $get_wrapper
7575    
7576      ## Step 1 # MUST      ## Step 1 # MUST
7577      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7578      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6271  sub set_inner_html ($$$) { Line 7582  sub set_inner_html ($$$) {
7582    
7583      ## Step 8 # MUST      ## Step 8 # MUST
7584      my $i = 0;      my $i = 0;
7585      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7586      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7587      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7588        my $self = shift;        my $self = shift;
7589    
# Line 6281  sub set_inner_html ($$$) { Line 7592  sub set_inner_html ($$$) {
7592    
7593        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7594        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7595        $column++;  
7596          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7597          $p->{column}++;
7598    
7599        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7600          $line++;          $p->{line}++;
7601          $column = 0;          $p->{column} = 0;
7602          !!!cp ('i1');          !!!cp ('i1');
7603        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7604          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7605          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7606          $line++;          $p->{line}++;
7607          $column = 0;          $p->{column} = 0;
7608          !!!cp ('i2');          !!!cp ('i2');
7609        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7610          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6300  sub set_inner_html ($$$) { Line 7613  sub set_inner_html ($$$) {
7613          !!!cp ('i4');          !!!cp ('i4');
7614          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7615          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7616          } elsif ($self->{next_char} <= 0x0008 or
7617                   (0x000E <= $self->{next_char} and
7618                    $self->{next_char} <= 0x001F) or
7619                   (0x007F <= $self->{next_char} and
7620                    $self->{next_char} <= 0x009F) or
7621                   (0xD800 <= $self->{next_char} and
7622                    $self->{next_char} <= 0xDFFF) or
7623                   (0xFDD0 <= $self->{next_char} and
7624                    $self->{next_char} <= 0xFDDF) or
7625                   {
7626                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7627                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7628                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7629                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7630                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7631                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7632                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7633                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7634                    0x10FFFE => 1, 0x10FFFF => 1,
7635                   }->{$self->{next_char}}) {
7636            !!!cp ('i4.1');
7637            if ($self->{next_char} < 0x10000) {
7638              !!!parse-error (type => 'control char',
7639                              text => (sprintf 'U+%04X', $self->{next_char}));
7640            } else {
7641              !!!parse-error (type => 'control char',
7642                              text => (sprintf 'U-%08X', $self->{next_char}));
7643            }
7644        }        }
7645      };      };
7646      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6307  sub set_inner_html ($$$) { Line 7648  sub set_inner_html ($$$) {
7648            
7649      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7650        my (%opt) = @_;        my (%opt) = @_;
7651        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7652          my $column = $opt{column};
7653          if (defined $opt{token} and defined $opt{token}->{line}) {
7654            $line = $opt{token}->{line};
7655            $column = $opt{token}->{column};
7656          }
7657          warn "Parse error ($opt{type}) at line $line column $column\n";
7658      };      };
7659      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7660        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7661      };      };
7662            
7663      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6334  sub set_inner_html ($$$) { Line 7681  sub set_inner_html ($$$) {
7681          unless defined $p->{content_model};          unless defined $p->{content_model};
7682          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7683    
7684      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7685          ## TODO: Foreign element OK?
7686    
7687      ## Step 3      ## Step 3
7688      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 7692  sub set_inner_html ($$$) {
7692      $doc->append_child ($root);      $doc->append_child ($root);
7693    
7694      ## Step 5 # MUST      ## Step 5 # MUST
7695      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7696    
7697      undef $p->{head_element};      undef $p->{head_element};
7698    
# Line 6390  sub set_inner_html ($$$) { Line 7738  sub set_inner_html ($$$) {
7738      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7739    
7740      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7741    
7742        delete $p->{parse_error}; # delete loop
7743    } else {    } else {
7744      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";
7745    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24