/[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.84 by wakaba, Thu Mar 6 15:23:17 2008 UTC revision 1.134 by wakaba, Sat May 17 05:34:23 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    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 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  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset_name = shift;
337    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    my $s;    my $s;
339      
340    if (defined $charset) {    my $onerror = $_[2] || sub {
341      require Encode; ## TODO: decode(utf8) don't delete BOM      my (%opt) = @_;
342      $s = \ (Encode::decode ($charset, $$bytes_s));      warn "Parse error ($opt{type})\n";
343      $self->{input_encoding} = lc $charset; ## TODO: normalize name    };
344      $self->{confident} = 1;    $self->{parse_error} = $onerror; # updated later by parse_char_string
345    } else {  
346      ## TODO: Implement HTML5 detection algorithm    ## HTML5 encoding sniffing algorithm
347      require Message::Charset::Info;
348      my $charset;
349      my ($e, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($e, $e_status) = $charset->get_perl_encoding
359              (allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($e) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          }
365        }
366    
367        ## Step 2
368        # wait
369    
370        ## Step 3
371        my $head = substr ($$bytes_s, 0, 3);
372        if ($head =~ /^\xFE\xFF/) {
373          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
374          ($e, $e_status) = $charset->get_perl_encoding
375              (allow_error_reporting => 1,
376               allow_fallback => 1);
377          $self->{confident} = 1;
378          last SNIFFING;
379        } elsif ($head =~ /^\xFF\xFE/) {
380          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
381          ($e, $e_status) = $charset->get_perl_encoding
382              (allow_error_reporting => 1,
383               allow_fallback => 1);
384          $self->{confident} = 1;
385          last SNIFFING;
386        } elsif ($head eq "\xEF\xBB\xBF") {
387          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
388          ($e, $e_status) = $charset->get_perl_encoding
389              (allow_error_reporting => 1,
390               allow_fallback => 1);
391          $self->{confident} = 1;
392          last SNIFFING;
393        }
394    
395        ## Step 4
396        ## TODO: <meta charset>
397    
398        ## Step 5
399        ## TODO: from history
400    
401        ## Step 6
402      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
403      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
404          (substr ($$bytes_s, 0, 1024));          (substr ($$bytes_s, 0, 1024));
405      $charset ||= 'windows-1252';      if (defined $charset_name) {
406      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
407      $self->{input_encoding} = $charset;  
408          ## ISSUE: Unsupported encoding is not ignored according to the spec.
409          ($e, $e_status) = $charset->get_perl_encoding
410              (allow_error_reporting => 1,
411               allow_fallback => 1);
412          if ($e) {
413            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
414                            value => $charset_name,
415                            level => $self->{info_level},
416                            line => 1, column => 1);
417            $self->{confident} = 0;
418            last SNIFFING;
419          }
420        }
421    
422        ## Step 7: default
423        ## TODO: Make this configurable.
424        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
425            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
426            ## detectable in the step 6.
427        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
428                                                       allow_fallback => 1);
429        !!!parse-error (type => 'sniffing:default', ## TODO: type name
430                        value => 'windows-1252',
431                        level => $self->{info_level},
432                        line => 1, column => 1);
433      $self->{confident} = 0;      $self->{confident} = 0;
434      } # SNIFFING
435    
436      $self->{input_encoding} = $charset->get_iana_name;
437      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
438        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
439                        value => $e->name,
440                        level => $self->{unsupported_level},
441                        line => 1, column => 1);
442      } elsif (not ($e_status &
443                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
444        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
445                        value => $self->{input_encoding},
446                        level => $self->{unsupported_level},
447                        line => 1, column => 1);
448    }    }
449      $s = \ $e->decode ($$bytes_s);
450    
451    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
452      my $self = shift;      my $self = shift;
453      my $charset = lc shift;      $charset_name = shift;
454      ## TODO: if $charset is supported      my $token = shift;
     ## TODO: normalize charset name  
   
     ## "Change the encoding" algorithm:  
   
     ## Step 1      
     if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
       $charset = 'utf-8';  
     }  
455    
456      ## Step 2      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
457      if (defined $self->{input_encoding} and      ($e, $e_status) = $charset->get_perl_encoding
458          $self->{input_encoding} eq $charset) {          (allow_error_reporting => 1, allow_fallback => 1);
459        $self->{confident} = 1;      
460        return;      if ($e) { # if supported
461      }        ## "Change the encoding" algorithm:
   
     !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.  
         ':'.$charset, level => 'w');  
462    
463      ## Step 3        ## Step 1    
464      # if (can) {        if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
465        ## change the encoding on the fly.          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
466        #$self->{confident} = 1;          ($e, $e_status) = $charset->get_perl_encoding;
467        #return;        }
468      # }        $charset_name = $charset->get_iana_name;
469          
470          ## Step 2
471          if (defined $self->{input_encoding} and
472              $self->{input_encoding} eq $charset_name) {
473            !!!parse-error (type => 'charset label:matching', ## TODO: type
474                            value => $charset_name,
475                            level => $self->{info_level});
476            $self->{confident} = 1;
477            return;
478          }
479    
480      ## Step 4        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
481      throw Whatpm::HTML::RestartParser (charset => $charset);            ':'.$charset_name, level => 'w', token => $token);
482          
483          ## Step 3
484          # if (can) {
485            ## change the encoding on the fly.
486            #$self->{confident} = 1;
487            #return;
488          # }
489          
490          ## Step 4
491          throw Whatpm::HTML::RestartParser ();
492        }
493    }; # $self->{change_encoding}    }; # $self->{change_encoding}
494    
495    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
# Line 144  sub parse_byte_string ($$$$;$) { Line 497  sub parse_byte_string ($$$$;$) {
497    try {    try {
498      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_string ($s, @args);  
499    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
500      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
501      $s = \ (Encode::decode ($charset, $$bytes_s));      
502      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
503        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
504          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
505                          value => $e->name,
506                          level => $self->{unsupported_level},
507                          line => 1, column => 1);
508        } elsif (not ($e_status &
509                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
510          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
511                          value => $self->{input_encoding},
512                          level => $self->{unsupported_level},
513                          line => 1, column => 1);
514        }
515        $s = \ $e->decode ($$bytes_s);
516      $self->{confident} = 1;      $self->{confident} = 1;
517      $return = $self->parse_char_string ($s, @args);      $return = $self->parse_char_string ($s, @args);
518    };    };
# Line 177  sub parse_string ($$$;$) { Line 543  sub parse_string ($$$;$) {
543        if defined $self->{input_encoding};        if defined $self->{input_encoding};
544    
545    my $i = 0;    my $i = 0;
546    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
547    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
548    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
549      my $self = shift;      my $self = shift;
550    
# Line 187  sub parse_string ($$$;$) { Line 553  sub parse_string ($$$;$) {
553    
554      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
555      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
556      $column++;  
557        ($self->{line_prev}, $self->{column_prev})
558            = ($self->{line}, $self->{column});
559        $self->{column}++;
560            
561      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
562        $line++;        !!!cp ('j1');
563        $column = 0;        $self->{line}++;
564          $self->{column} = 0;
565      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
566          !!!cp ('j2');
567        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
568        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
569        $line++;        $self->{line}++;
570        $column = 0;        $self->{column} = 0;
571      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
572          !!!cp ('j3');
573        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
574      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
575          !!!cp ('j4');
576        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
577        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
578        } elsif ($self->{next_char} <= 0x0008 or
579                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
580                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
581                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
582                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
583                 {
584                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
585                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
586                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
587                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
588                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
589                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
590                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
591                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
592                  0x10FFFE => 1, 0x10FFFF => 1,
593                 }->{$self->{next_char}}) {
594          !!!cp ('j5');
595          !!!parse-error (type => 'control char', level => $self->{must_level});
596    ## TODO: error type documentation
597      }      }
598    };    };
599    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 601  sub parse_string ($$$;$) {
601    
602    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
603      my (%opt) = @_;      my (%opt) = @_;
604      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
605        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
606        warn "Parse error ($opt{type}) at line $line column $column\n";
607    };    };
608    $self->{parse_error} = sub {    $self->{parse_error} = sub {
609      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
610    };    };
611    
612    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 614  sub parse_string ($$$;$) {
614    $self->_construct_tree;    $self->_construct_tree;
615    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
616    
617      delete $self->{parse_error}; # remove loop
618    
619    return $self->{document};    return $self->{document};
620  } # parse_string  } # parse_string
621    
622  sub new ($) {  sub new ($) {
623    my $class = shift;    my $class = shift;
624    my $self = bless {}, $class;    my $self = bless {
625        must_level => 'm',
626        should_level => 's',
627        good_level => 'w',
628        warn_level => 'w',
629        info_level => 'i',
630        unsupported_level => 'u',
631      }, $class;
632    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
633      $self->{next_char} = -1;      $self->{next_char} = -1;
634    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 690  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
690  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
691  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
692  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
693    sub SELF_CLOSING_START_TAG_STATE () { 34 }
694    sub CDATA_BLOCK_STATE () { 35 }
695    
696  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
697  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 708  sub TABLE_IMS ()      { 0b1000000 }
708  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
709  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
710  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
711    sub SELECT_IMS ()     { 0b10000000000 }
712    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
713        ## NOTE: "in foreign content" insertion mode is special; it is combined
714        ## with the secondary insertion mode.  In this parser, they are stored
715        ## together in the bit-or'ed form.
716    
717  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
718    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 735  sub IN_TABLE_IM () { TABLE_IMS }
735  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
736  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
737  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
738  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
739    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
740  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
741    
742  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 749  sub _initialize_tokenizer ($) {
749    undef $self->{current_attribute};    undef $self->{current_attribute};
750    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
751    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
752      delete $self->{self_closing};
753    $self->{char} = [];    $self->{char} = [];
754    # $self->{next_char}    # $self->{next_char}
755    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 770  sub _initialize_tokenizer ($) {
770  ##        ->{value}  ##        ->{value}
771  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
772  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
773    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
774    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
775    ##     while the token is pushed back to the stack.
776    
777    ## ISSUE: "When a DOCTYPE token is created, its
778    ## <i>self-closing flag</i> must be unset (its other state is that it
779    ## be set), and its attributes list must be empty.": Wrong subject?
780    
781  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
782    
# Line 384  sub _initialize_tokenizer ($) { Line 803  sub _initialize_tokenizer ($) {
803    
804  sub _get_next_token ($) {  sub _get_next_token ($) {
805    my $self = shift;    my $self = shift;
806    
807      if ($self->{self_closing}) {
808        !!!parse-error (type => 'nestc', token => $self->{current_token});
809        ## NOTE: The |self_closing| flag is only set by start tag token.
810        ## In addition, when a start tag token is emitted, it is always set to
811        ## |current_token|.
812        delete $self->{self_closing};
813      }
814    
815    if (@{$self->{token}}) {    if (@{$self->{token}}) {
816        $self->{self_closing} = $self->{token}->[0]->{self_closing};
817      return shift @{$self->{token}};      return shift @{$self->{token}};
818    }    }
819    
# Line 447  sub _get_next_token ($) { Line 876  sub _get_next_token ($) {
876          #          #
877        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
878          !!!cp (11);          !!!cp (11);
879          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
880                      line => $self->{line}, column => $self->{column}});
881          last A; ## TODO: ok?          last A; ## TODO: ok?
882        } else {        } else {
883          !!!cp (12);          !!!cp (12);
884        }        }
885        # Anything else        # Anything else
886        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
887                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
888                       line => $self->{line}, column => $self->{column},
889                      };
890        ## Stay in the data state        ## Stay in the data state
891        !!!next-input-character;        !!!next-input-character;
892    
# Line 463  sub _get_next_token ($) { Line 895  sub _get_next_token ($) {
895        redo A;        redo A;
896      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
897        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
898    
899          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
900                
901        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
902    
# Line 471  sub _get_next_token ($) { Line 905  sub _get_next_token ($) {
905    
906        unless (defined $token) {        unless (defined $token) {
907          !!!cp (13);          !!!cp (13);
908          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
909                      line => $l, column => $c,
910                     });
911        } else {        } else {
912          !!!cp (14);          !!!cp (14);
913          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 926  sub _get_next_token ($) {
926            ## reconsume            ## reconsume
927            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
928    
929            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
930                        line => $self->{line_prev},
931                        column => $self->{column_prev},
932                       });
933    
934            redo A;            redo A;
935          }          }
# Line 510  sub _get_next_token ($) { Line 949  sub _get_next_token ($) {
949            !!!cp (19);            !!!cp (19);
950            $self->{current_token}            $self->{current_token}
951              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
952                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
953                   line => $self->{line_prev},
954                   column => $self->{column_prev}};
955            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
956            !!!next-input-character;            !!!next-input-character;
957            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 959  sub _get_next_token ($) {
959                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
960            !!!cp (20);            !!!cp (20);
961            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
962                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
963                                        line => $self->{line_prev},
964                                        column => $self->{column_prev}};
965            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
966            !!!next-input-character;            !!!next-input-character;
967            redo A;            redo A;
968          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
969            !!!cp (21);            !!!cp (21);
970            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
971                              line => $self->{line_prev},
972                              column => $self->{column_prev});
973            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
974            !!!next-input-character;            !!!next-input-character;
975    
976            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
977                        line => $self->{line_prev},
978                        column => $self->{column_prev},
979                       });
980    
981            redo A;            redo A;
982          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
983            !!!cp (22);            !!!cp (22);
984            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
985                              line => $self->{line_prev},
986                              column => $self->{column_prev});
987            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
988              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
989                                        line => $self->{line_prev},
990                                        column => $self->{column_prev},
991                                       };
992            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
993            redo A;            redo A;
994          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 997  sub _get_next_token ($) {
997            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
998            ## reconsume            ## reconsume
999    
1000            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1001                        line => $self->{line_prev},
1002                        column => $self->{column_prev},
1003                       });
1004    
1005            redo A;            redo A;
1006          }          }
# Line 551  sub _get_next_token ($) { Line 1008  sub _get_next_token ($) {
1008          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1009        }        }
1010      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1011          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1012        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1013          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1014    
1015            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1016            my @next_char;            my @next_char;
1017            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 1028  sub _get_next_token ($) {
1028                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1029                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1030    
1031                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1032                            line => $l, column => $c,
1033                           });
1034        
1035                redo A;                redo A;
1036              }              }
# Line 588  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1050              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1051              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1052              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1053                          line => $l, column => $c,
1054                         });
1055              redo A;              redo A;
1056            } else {            } else {
1057              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1064  sub _get_next_token ($) {
1064            !!!cp (28);            !!!cp (28);
1065            # next-input-character is already done            # next-input-character is already done
1066            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1067            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1068                        line => $l, column => $c,
1069                       });
1070            redo A;            redo A;
1071          }          }
1072        }        }
# Line 609  sub _get_next_token ($) { Line 1074  sub _get_next_token ($) {
1074        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1075            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1076          !!!cp (29);          !!!cp (29);
1077          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1078                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1079                   tag_name => chr ($self->{next_char} + 0x0020),
1080                   line => $l, column => $c};
1081          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1086          !!!cp (30);          !!!cp (30);
1087          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1088                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1089                                      line => $l, column => $c};
1090          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1091          !!!next-input-character;          !!!next-input-character;
1092          redo A;          redo A;
1093        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1094          !!!cp (31);          !!!cp (31);
1095          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1096                            line => $self->{line_prev}, ## "<" in "</>"
1097                            column => $self->{column_prev} - 1);
1098          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1105          # reconsume          # reconsume
1106    
1107          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                      line => $l, column => $c,
1109                     });
1110    
1111          redo A;          redo A;
1112        } else {        } else {
1113          !!!cp (33);          !!!cp (33);
1114          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1115          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1116            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1117                                      line => $self->{line_prev}, # "<" of "</"
1118                                      column => $self->{column_prev} - 1,
1119                                     };
1120          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1121          redo A;          redo A;
1122        }        }
# Line 657  sub _get_next_token ($) { Line 1133  sub _get_next_token ($) {
1133        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1134          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1135            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1136            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1137          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1138            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1165          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1166            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1167            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1168          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1169            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1184  sub _get_next_token ($) {
1184    
1185          redo A;          redo A;
1186        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1187            !!!cp (42);
1188            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1189          !!!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  
1190          redo A;          redo A;
1191        } else {        } else {
1192          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1209  sub _get_next_token ($) {
1209        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1210          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1211            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1212            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1213          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1214            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1230  sub _get_next_token ($) {
1230        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1231                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1232          !!!cp (49);          !!!cp (49);
1233          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1234                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1235                   value => '',
1236                   line => $self->{line}, column => $self->{column}};
1237          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1238          !!!next-input-character;          !!!next-input-character;
1239          redo A;          redo A;
1240        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (50);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1244          redo A;          redo A;
1245        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1246          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1247          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1248            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1249            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1250          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1251            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1275  sub _get_next_token ($) {
1275          } else {          } else {
1276            !!!cp (56);            !!!cp (56);
1277          }          }
1278          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1279                                value => ''};              = {name => chr ($self->{next_char}),
1280                   value => '',
1281                   line => $self->{line}, column => $self->{column}};
1282          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1283          !!!next-input-character;          !!!next-input-character;
1284          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1288  sub _get_next_token ($) {
1288          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1289              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1290            !!!cp (57);            !!!cp (57);
1291            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1292            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1293          } else {          } else {
1294            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1317  sub _get_next_token ($) {
1317          $before_leave->();          $before_leave->();
1318          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1319            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1320            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1321          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1322            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1341  sub _get_next_token ($) {
1341          !!!next-input-character;          !!!next-input-character;
1342          redo A;          redo A;
1343        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1344            !!!cp (64);
1345          $before_leave->();          $before_leave->();
1346            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1347          !!!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  
1348          redo A;          redo A;
1349        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1350          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1351          $before_leave->();          $before_leave->();
1352          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1353            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1354            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1355          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1356            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1401  sub _get_next_token ($) {
1401        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1402          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1403            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1404            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1405          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1406            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1424                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1425          !!!cp (76);          !!!cp (76);
1426          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1427                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1428                   value => '',
1429                   line => $self->{line}, column => $self->{column}};
1430          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1431          !!!next-input-character;          !!!next-input-character;
1432          redo A;          redo A;
1433        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1434            !!!cp (77);
1435            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1436          !!!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  
1437          redo A;          redo A;
1438        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1439          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1440          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1441            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1442            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1443          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1444            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460          redo A;          redo A;
1461        } else {        } else {
1462          !!!cp (82);          !!!cp (82);
1463          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1464                                value => ''};              = {name => chr ($self->{next_char}),
1465                   value => '',
1466                   line => $self->{line}, column => $self->{column}};
1467          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1496  sub _get_next_token ($) {
1496        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1519  sub _get_next_token ($) {
1519          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1520          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1521            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1522            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1523          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1524            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1567          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1608  sub _get_next_token ($) {
1608          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1609          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1610            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1611            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1612          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1613            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1653  sub _get_next_token ($) {
1653        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1654          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1655            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1656            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1657          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1658            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1676  sub _get_next_token ($) {
1676          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1677          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1678            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1680          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1681            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1746  sub _get_next_token ($) {
1746        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1747          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1748            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1749            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1750          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1751            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1766  sub _get_next_token ($) {
1766    
1767          redo A;          redo A;
1768        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1769            !!!cp (122);
1770            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1771          !!!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 (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1772          redo A;          redo A;
1773        } else {        } else {
1774          !!!cp (124);          !!!cp ('124.1');
1775          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1776          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1777          ## reconsume          ## reconsume
1778          redo A;          redo A;
1779        }        }
1780        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1781          if ($self->{next_char} == 0x003E) { # >
1782            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1783              !!!cp ('124.2');
1784              !!!parse-error (type => 'nestc', token => $self->{current_token});
1785              ## TODO: Different type than slash in start tag
1786              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1787              if ($self->{current_token}->{attributes}) {
1788                !!!cp ('124.4');
1789                !!!parse-error (type => 'end tag attribute');
1790              } else {
1791                !!!cp ('124.5');
1792              }
1793              ## TODO: Test |<title></title/>|
1794            } else {
1795              !!!cp ('124.3');
1796              $self->{self_closing} = 1;
1797            }
1798    
1799            $self->{state} = DATA_STATE;
1800            !!!next-input-character;
1801    
1802            !!!emit ($self->{current_token}); # start tag or end tag
1803    
1804            redo A;
1805          } else {
1806            !!!cp ('124.4');
1807            !!!parse-error (type => 'nestc');
1808            ## TODO: This error type is wrong.
1809            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1810            ## Reconsume.
1811            redo A;
1812          }
1813      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1814        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1815                
1816        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1817          #my $token = {type => COMMENT_TOKEN, data => ''};
1818    
1819        BC: {        BC: {
1820          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1822  sub _get_next_token ($) {
1822            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1823            !!!next-input-character;            !!!next-input-character;
1824    
1825            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1826    
1827            redo A;            redo A;
1828          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1830  sub _get_next_token ($) {
1830            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1831            ## reconsume            ## reconsume
1832    
1833            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1834    
1835            redo A;            redo A;
1836          } else {          } else {
1837            !!!cp (126);            !!!cp (126);
1838            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1839            !!!next-input-character;            !!!next-input-character;
1840            redo BC;            redo BC;
1841          }          }
# Line 1408  sub _get_next_token ($) { Line 1845  sub _get_next_token ($) {
1845      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1846        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1847    
1848          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1849    
1850        my @next_char;        my @next_char;
1851        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1852                
# Line 1416  sub _get_next_token ($) { Line 1855  sub _get_next_token ($) {
1855          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1856          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1857            !!!cp (127);            !!!cp (127);
1858            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1859                                        line => $l, column => $c,
1860                                       };
1861            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1862            !!!next-input-character;            !!!next-input-character;
1863            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1893  sub _get_next_token ($) {
1893                      !!!cp (129);                      !!!cp (129);
1894                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1895                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1896                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1897                                                  quirks => 1,
1898                                                  line => $l, column => $c,
1899                                                 };
1900                      !!!next-input-character;                      !!!next-input-character;
1901                      redo A;                      redo A;
1902                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1917  sub _get_next_token ($) {
1917          } else {          } else {
1918            !!!cp (135);            !!!cp (135);
1919          }          }
1920          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1921                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1922                   $self->{next_char} == 0x005B) { # [
1923            !!!next-input-character;
1924            push @next_char, $self->{next_char};
1925            if ($self->{next_char} == 0x0043) { # C
1926              !!!next-input-character;
1927              push @next_char, $self->{next_char};
1928              if ($self->{next_char} == 0x0044) { # D
1929                !!!next-input-character;
1930                push @next_char, $self->{next_char};
1931                if ($self->{next_char} == 0x0041) { # A
1932                  !!!next-input-character;
1933                  push @next_char, $self->{next_char};
1934                  if ($self->{next_char} == 0x0054) { # T
1935                    !!!next-input-character;
1936                    push @next_char, $self->{next_char};
1937                    if ($self->{next_char} == 0x0041) { # A
1938                      !!!next-input-character;
1939                      push @next_char, $self->{next_char};
1940                      if ($self->{next_char} == 0x005B) { # [
1941                        !!!cp (135.1);
1942                        $self->{state} = CDATA_BLOCK_STATE;
1943                        !!!next-input-character;
1944                        redo A;
1945                      } else {
1946                        !!!cp (135.2);
1947                      }
1948                    } else {
1949                      !!!cp (135.3);
1950                    }
1951                  } else {
1952                    !!!cp (135.4);                
1953                  }
1954                } else {
1955                  !!!cp (135.5);
1956                }
1957              } else {
1958                !!!cp (135.6);
1959              }
1960            } else {
1961              !!!cp (135.7);
1962            }
1963        } else {        } else {
1964          !!!cp (136);          !!!cp (136);
1965        }        }
# Line 1480  sub _get_next_token ($) { Line 1968  sub _get_next_token ($) {
1968        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1969        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1970        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1971          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1972                                    line => $l, column => $c,
1973                                   };
1974        redo A;        redo A;
1975                
1976        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2094  sub _get_next_token ($) {
2094          redo A;          redo A;
2095        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2096          !!!cp (152);          !!!cp (152);
2097          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2098                            line => $self->{line_prev},
2099                            column => $self->{column_prev});
2100          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2101          ## Stay in the state          ## Stay in the state
2102          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2112  sub _get_next_token ($) {
2112          redo A;          redo A;
2113        } else {        } else {
2114          !!!cp (154);          !!!cp (154);
2115          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2116                            line => $self->{line_prev},
2117                            column => $self->{column_prev});
2118          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2119          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2120          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2153  sub _get_next_token ($) {
2153          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2154          !!!next-input-character;          !!!next-input-character;
2155    
2156          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2157    
2158          redo A;          redo A;
2159        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2162  sub _get_next_token ($) {
2162          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2163          ## reconsume          ## reconsume
2164    
2165          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2166    
2167          redo A;          redo A;
2168        } else {        } else {
2169          !!!cp (160);          !!!cp (160);
2170          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2171              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2172  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2173          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2174          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2684  sub _get_next_token ($) {
2684          !!!next-input-character;          !!!next-input-character;
2685          redo A;          redo A;
2686        }        }
2687        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2688          my $s = '';
2689          
2690          my ($l, $c) = ($self->{line}, $self->{column});
2691    
2692          CS: while ($self->{next_char} != -1) {
2693            if ($self->{next_char} == 0x005D) { # ]
2694              !!!next-input-character;
2695              if ($self->{next_char} == 0x005D) { # ]
2696                !!!next-input-character;
2697                MDC: {
2698                  if ($self->{next_char} == 0x003E) { # >
2699                    !!!cp (221.1);
2700                    !!!next-input-character;
2701                    last CS;
2702                  } elsif ($self->{next_char} == 0x005D) { # ]
2703                    !!!cp (221.2);
2704                    $s .= ']';
2705                    !!!next-input-character;
2706                    redo MDC;
2707                  } else {
2708                    !!!cp (221.3);
2709                    $s .= ']]';
2710                    #
2711                  }
2712                } # MDC
2713              } else {
2714                !!!cp (221.4);
2715                $s .= ']';
2716                #
2717              }
2718            } else {
2719              !!!cp (221.5);
2720              #
2721            }
2722            $s .= chr $self->{next_char};
2723            !!!next-input-character;
2724          } # CS
2725    
2726          $self->{state} = DATA_STATE;
2727          ## next-input-character done or EOF, which is reconsumed.
2728    
2729          if (length $s) {
2730            !!!cp (221.6);
2731            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2732                      line => $l, column => $c});
2733          } else {
2734            !!!cp (221.7);
2735          }
2736    
2737          redo A;
2738    
2739          ## ISSUE: "text tokens" in spec.
2740          ## TODO: Streaming support
2741      } else {      } else {
2742        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2743      }      }
# Line 2203  sub _get_next_token ($) { Line 2749  sub _get_next_token ($) {
2749  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2750    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2751    
2752      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2753    
2754    if ({    if ({
2755         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2756         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 2791  sub _tokenize_attempt_to_consume_an_enti
2791            redo X;            redo X;
2792          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2793            !!!cp (1005);            !!!cp (1005);
2794            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2795            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2796            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2797            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2800  sub _tokenize_attempt_to_consume_an_enti
2800            !!!next-input-character;            !!!next-input-character;
2801          } else {          } else {
2802            !!!cp (1007);            !!!cp (1007);
2803            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2804          }          }
2805    
2806          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2807            !!!cp (1008);            !!!cp (1008);
2808            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2809            $code = 0xFFFD;            $code = 0xFFFD;
2810          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2811            !!!cp (1009);            !!!cp (1009);
2812            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2813            $code = 0xFFFD;            $code = 0xFFFD;
2814          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2815            !!!cp (1010);            !!!cp (1010);
2816            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2817            $code = 0x000A;            $code = 0x000A;
2818          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2819            !!!cp (1011);            !!!cp (1011);
2820            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2821            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2822          }          }
2823    
2824          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2825                  has_reference => 1};                  has_reference => 1,
2826                    line => $l, column => $c,
2827                   };
2828        } # X        } # X
2829      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2830               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2845  sub _tokenize_attempt_to_consume_an_enti
2845          !!!next-input-character;          !!!next-input-character;
2846        } else {        } else {
2847          !!!cp (1014);          !!!cp (1014);
2848          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2849        }        }
2850    
2851        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2852          !!!cp (1015);          !!!cp (1015);
2853          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2854          $code = 0xFFFD;          $code = 0xFFFD;
2855        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2856          !!!cp (1016);          !!!cp (1016);
2857          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2858          $code = 0xFFFD;          $code = 0xFFFD;
2859        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2860          !!!cp (1017);          !!!cp (1017);
2861          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2862          $code = 0x000A;          $code = 0x000A;
2863        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2864          !!!cp (1018);          !!!cp (1018);
2865          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2866          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2867        }        }
2868                
2869        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2870                  line => $l, column => $c,
2871                 };
2872      } else {      } else {
2873        !!!cp (1019);        !!!cp (1019);
2874        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2875        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2876        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2877        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2888  sub _tokenize_attempt_to_consume_an_enti
2888      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2889      our $EntityChar;      our $EntityChar;
2890    
2891      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2892             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2893             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2894               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2921  sub _tokenize_attempt_to_consume_an_enti
2921            
2922      if ($match > 0) {      if ($match > 0) {
2923        !!!cp (1023);        !!!cp (1023);
2924        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2925                  line => $l, column => $c,
2926                 };
2927      } elsif ($match < 0) {      } elsif ($match < 0) {
2928        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2929        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2930          !!!cp (1024);          !!!cp (1024);
2931          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2932                    line => $l, column => $c,
2933                   };
2934        } else {        } else {
2935          !!!cp (1025);          !!!cp (1025);
2936          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2937                    line => $l, column => $c,
2938                   };
2939        }        }
2940      } else {      } else {
2941        !!!cp (1026);        !!!cp (1026);
2942        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2943        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2944        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2945                  line => $l, column => $c,
2946                 };
2947      }      }
2948    } else {    } else {
2949      !!!cp (1027);      !!!cp (1027);
2950      ## no characters are consumed      ## no characters are consumed
2951      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2952      return undef;      return undef;
2953    }    }
2954  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 3019  sub _tree_construction_initial ($) {
3019            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3020            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3021          !!!cp ('t1');          !!!cp ('t1');
3022          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3023        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3024          !!!cp ('t2');          !!!cp ('t2');
3025          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3026          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3027        } else {        } else {
3028          !!!cp ('t3');          !!!cp ('t3');
3029        }        }
3030                
3031        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3032          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3033          ## NOTE: Default value for both |public_id| and |system_id| attributes
3034          ## are empty strings, so that we don't set any value in missing cases.
3035        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3036            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3037        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3164  sub _tree_construction_initial ($) {
3164                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3165               }->{$token->{type}}) {               }->{$token->{type}}) {
3166        !!!cp ('t14');        !!!cp ('t14');
3167        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3168        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3169        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3170        ## reprocess        ## reprocess
3171          !!!ack-later;
3172        return;        return;
3173      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3174        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 3186  sub _tree_construction_initial ($) {
3186          !!!cp ('t17');          !!!cp ('t17');
3187        }        }
3188    
3189        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3190        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3191        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3192        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3215  sub _tree_construction_root_element ($)
3215    B: {    B: {
3216        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3217          !!!cp ('t19');          !!!cp ('t19');
3218          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3219          ## Ignore the token          ## Ignore the token
3220          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3221          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3249  sub _tree_construction_root_element ($)
3249        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3250          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3251            my $root_element;            my $root_element;
3252            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3253            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3254            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3255                  [$root_element, $el_category->{html}];
3256    
3257            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3258              !!!cp ('t24');              !!!cp ('t24');
3259              $self->{application_cache_selection}              $self->{application_cache_selection}
3260                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3261              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3262                ## According to Hixie (#whatwg 2008-03-19), it should be
3263                ## resolved against the base URI of the document in HTML
3264                ## or xml:base of the element in XHTML.
3265            } else {            } else {
3266              !!!cp ('t25');              !!!cp ('t25');
3267              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3268            }            }
3269    
3270              !!!nack ('t25c');
3271    
3272            !!!next-token;            !!!next-token;
3273            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3274          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3285  sub _tree_construction_root_element ($)
3285          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3286        }        }
3287    
3288      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3289        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3290      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3291      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3292    
3293      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3294    
3295      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3296        !!!ack-later;
3297      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3298    
3299      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3314  sub _reset_insertion_mode ($) {
3314            
3315      ## Step 3      ## Step 3
3316      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"!?  
3317        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3318          $last = 1;          $last = 1;
3319          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3320            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3321              !!!cp ('t27');              !!!cp ('t27');
3322              #              #
3323            } else {            } else {
# Line 2762  sub _reset_insertion_mode ($) { Line 3327  sub _reset_insertion_mode ($) {
3327          }          }
3328        }        }
3329            
3330        ## Step 4..13      ## Step 4..14
3331        my $new_mode = {      my $new_mode;
3332        if ($node->[1] & FOREIGN_EL) {
3333          ## NOTE: Strictly spaking, the line below only applies to MathML and
3334          ## SVG elements.  Currently the HTML syntax supports only MathML and
3335          ## SVG elements as foreigners.
3336          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3337          ## ISSUE: What is set as the secondary insertion mode?
3338        } else {
3339          $new_mode = {
3340                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3341                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3342                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2779  sub _reset_insertion_mode ($) { Line 3352  sub _reset_insertion_mode ($) {
3352                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3353                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3354                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3355                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3356        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3357        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3358                
3359        ## Step 14        ## Step 15
3360        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3361          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3362            !!!cp ('t29');            !!!cp ('t29');
3363            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3371  sub _reset_insertion_mode ($) {
3371          !!!cp ('t31');          !!!cp ('t31');
3372        }        }
3373                
3374        ## Step 15        ## Step 16
3375        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3376                
3377        ## Step 16        ## Step 17
3378        $i--;        $i--;
3379        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3380                
3381        ## Step 17        ## Step 18
3382        redo S3;        redo S3;
3383      } # S3      } # S3
3384    
# Line 2908  sub _tree_construction_main ($) { Line 3482  sub _tree_construction_main ($) {
3482      !!!cp ('t39');      !!!cp ('t39');
3483    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3484    
3485    my $parse_rcdata = sub ($$) {    my $insert;
3486      my ($content_model_flag, $insert) = @_;  
3487      my $parse_rcdata = sub ($) {
3488        my ($content_model_flag) = @_;
3489    
3490      ## Step 1      ## Step 1
3491      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3492      my $el;      my $el;
3493      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3494    
3495      ## Step 2      ## Step 2
3496      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3497    
3498      ## Step 3      ## Step 3
3499      $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 3501  sub _tree_construction_main ($) {
3501    
3502      ## Step 4      ## Step 4
3503      my $text = '';      my $text = '';
3504        !!!nack ('t40.1');
3505      !!!next-token;      !!!next-token;
3506      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3507        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3524  sub _tree_construction_main ($) {
3524          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3525        !!!cp ('t42');        !!!cp ('t42');
3526        ## 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});  
3527      } else {      } else {
3528        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3529          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3530            !!!cp ('t43');
3531            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3532          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3533            !!!cp ('t44');
3534            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3535          } else {
3536            die "$0: $content_model_flag in parse_rcdata";
3537          }
3538      }      }
3539      !!!next-token;      !!!next-token;
3540    }; # $parse_rcdata    }; # $parse_rcdata
3541    
3542    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3543      my $script_el;      my $script_el;
3544      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3545      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3546    
3547      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3548      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3549            
3550      my $text = '';      my $text = '';
3551        !!!nack ('t45.1');
3552      !!!next-token;      !!!next-token;
3553      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3554        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3568  sub _tree_construction_main ($) {
3568        ## Ignore the token        ## Ignore the token
3569      } else {      } else {
3570        !!!cp ('t48');        !!!cp ('t48');
3571        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3573        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3574      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
3591      !!!next-token;      !!!next-token;
3592    }; # $script_start_tag    }; # $script_start_tag
3593    
3594      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3595      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3596      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3597    
3598    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3599      my $tag_name = shift;      my $end_tag_token = shift;
3600        my $tag_name = $end_tag_token->{tag_name};
3601    
3602        ## NOTE: The adoption agency algorithm (AAA).
3603    
3604      FET: {      FET: {
3605        ## Step 1        ## Step 1
3606        my $formatting_element;        my $formatting_element;
3607        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3608        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3609          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3610              !!!cp ('t52');
3611              last AFE;
3612            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3613                         eq $tag_name) {
3614            !!!cp ('t51');            !!!cp ('t51');
3615            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3616            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3617            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3618          }          }
3619        } # AFE        } # AFE
3620        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3621          !!!cp ('t53');          !!!cp ('t53');
3622          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3623          ## Ignore the token          ## Ignore the token
3624          !!!next-token;          !!!next-token;
3625          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3636  sub _tree_construction_main ($) {
3636              last INSCOPE;              last INSCOPE;
3637            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3638              !!!cp ('t55');              !!!cp ('t55');
3639              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3640                                token => $end_tag_token);
3641              ## Ignore the token              ## Ignore the token
3642              !!!next-token;              !!!next-token;
3643              return;              return;
3644            }            }
3645          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3646            !!!cp ('t56');            !!!cp ('t56');
3647            $in_scope = 0;            $in_scope = 0;
3648          }          }
3649        } # INSCOPE        } # INSCOPE
3650        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3651          !!!cp ('t57');          !!!cp ('t57');
3652          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3653                            token => $end_tag_token);
3654          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3655          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3656          return;          return;
3657        }        }
3658        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3659          !!!cp ('t58');          !!!cp ('t58');
3660          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3661                            value => $self->{open_elements}->[-1]->[0]
3662                                ->manakai_local_name,
3663                            token => $end_tag_token);
3664        }        }
3665                
3666        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3669        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3670          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3671          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3672              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3673              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3674               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3675            !!!cp ('t59');            !!!cp ('t59');
3676            $furthest_block = $node;            $furthest_block = $node;
3677            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3757  sub _tree_construction_main ($) {
3757        } # S7          } # S7  
3758                
3759        ## Step 8        ## Step 8
3760        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3761            my $foster_parent_element;
3762            my $next_sibling;
3763            OE: for (reverse 0..$#{$self->{open_elements}}) {
3764              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3765                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3766                                 if (defined $parent and $parent->node_type == 1) {
3767                                   !!!cp ('t65.1');
3768                                   $foster_parent_element = $parent;
3769                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3770                                 } else {
3771                                   !!!cp ('t65.2');
3772                                   $foster_parent_element
3773                                     = $self->{open_elements}->[$_ - 1]->[0];
3774                                 }
3775                                 last OE;
3776                               }
3777                             } # OE
3778                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3779                               unless defined $foster_parent_element;
3780            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3781            $open_tables->[-1]->[1] = 1; # tainted
3782          } else {
3783            !!!cp ('t65.3');
3784            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3785          }
3786                
3787        ## Step 9        ## Step 9
3788        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828      } # FET      } # FET
3829    }; # $formatting_end_tag    }; # $formatting_end_tag
3830    
3831    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3832      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3833    }; # $insert_to_current    }; # $insert_to_current
3834    
3835    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3836                         my $child = shift;      my $child = shift;
3837                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3838                              table => 1, tbody => 1, tfoot => 1,        # MUST
3839                              thead => 1, tr => 1,        my $foster_parent_element;
3840                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3841                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3842                           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') {  
3843                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3844                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3845                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3857  sub _tree_construction_main ($) {
3857                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3858                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3859                             ($child, $next_sibling);                             ($child, $next_sibling);
3860                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3861                           !!!cp ('t72');      } else {
3862                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3863                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3864        }
3865    }; # $insert_to_foster    }; # $insert_to_foster
3866    
3867    my $insert;    B: while (1) {
   
   B: {  
3868      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3869        !!!cp ('t73');        !!!cp ('t73');
3870        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3871        ## Ignore the token        ## Ignore the token
3872        ## Stay in the phase        ## Stay in the phase
3873        !!!next-token;        !!!next-token;
3874        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3875      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3876               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3877        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3878          !!!cp ('t79');          !!!cp ('t79');
3879          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3880          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3881        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3882          !!!cp ('t80');          !!!cp ('t80');
3883          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3884          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3885        } else {        } else {
3886          !!!cp ('t81');          !!!cp ('t81');
3887        }        }
3888    
3889        !!!cp ('t82');        !!!cp ('t82');
3890        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3891        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3892        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3893          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3321  sub _tree_construction_main ($) { Line 3897  sub _tree_construction_main ($) {
3897               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3898          }          }
3899        }        }
3900          !!!nack ('t84.1');
3901        !!!next-token;        !!!next-token;
3902        redo B;        next B;
3903      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3904        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3905        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3336  sub _tree_construction_main ($) { Line 3913  sub _tree_construction_main ($) {
3913          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3914        }        }
3915        !!!next-token;        !!!next-token;
3916        redo B;        next B;
3917      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3918          if ($token->{type} == CHARACTER_TOKEN) {
3919            !!!cp ('t87.1');
3920            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3921            !!!next-token;
3922            next B;
3923          } elsif ($token->{type} == START_TAG_TOKEN) {
3924            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3925                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3926                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3927                ($token->{tag_name} eq 'svg' and
3928                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3929              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3930              !!!cp ('t87.2');
3931              #
3932            } elsif ({
3933                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3934                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3935                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3936                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3937                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3938                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3939                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3940                      var => 1,
3941                     }->{$token->{tag_name}}) {
3942              !!!cp ('t87.2');
3943              !!!parse-error (type => 'not closed',
3944                              value => $self->{open_elements}->[-1]->[0]
3945                                  ->manakai_local_name,
3946                              token => $token);
3947    
3948              pop @{$self->{open_elements}}
3949                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3950    
3951              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3952              ## Reprocess.
3953              next B;
3954            } else {
3955              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3956              my $tag_name = $token->{tag_name};
3957              if ($nsuri eq $SVG_NS) {
3958                $tag_name = {
3959                   altglyph => 'altGlyph',
3960                   altglyphdef => 'altGlyphDef',
3961                   altglyphitem => 'altGlyphItem',
3962                   animatecolor => 'animateColor',
3963                   animatemotion => 'animateMotion',
3964                   animatetransform => 'animateTransform',
3965                   clippath => 'clipPath',
3966                   feblend => 'feBlend',
3967                   fecolormatrix => 'feColorMatrix',
3968                   fecomponenttransfer => 'feComponentTransfer',
3969                   fecomposite => 'feComposite',
3970                   feconvolvematrix => 'feConvolveMatrix',
3971                   fediffuselighting => 'feDiffuseLighting',
3972                   fedisplacementmap => 'feDisplacementMap',
3973                   fedistantlight => 'feDistantLight',
3974                   feflood => 'feFlood',
3975                   fefunca => 'feFuncA',
3976                   fefuncb => 'feFuncB',
3977                   fefuncg => 'feFuncG',
3978                   fefuncr => 'feFuncR',
3979                   fegaussianblur => 'feGaussianBlur',
3980                   feimage => 'feImage',
3981                   femerge => 'feMerge',
3982                   femergenode => 'feMergeNode',
3983                   femorphology => 'feMorphology',
3984                   feoffset => 'feOffset',
3985                   fepointlight => 'fePointLight',
3986                   fespecularlighting => 'feSpecularLighting',
3987                   fespotlight => 'feSpotLight',
3988                   fetile => 'feTile',
3989                   feturbulence => 'feTurbulence',
3990                   foreignobject => 'foreignObject',
3991                   glyphref => 'glyphRef',
3992                   lineargradient => 'linearGradient',
3993                   radialgradient => 'radialGradient',
3994                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3995                   textpath => 'textPath',  
3996                }->{$tag_name} || $tag_name;
3997              }
3998    
3999              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4000    
4001              ## "adjust foreign attributes" - done in insert-element-f
4002    
4003              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4004    
4005              if ($self->{self_closing}) {
4006                pop @{$self->{open_elements}};
4007                !!!ack ('t87.3');
4008              } else {
4009                !!!cp ('t87.4');
4010              }
4011    
4012              !!!next-token;
4013              next B;
4014            }
4015          } elsif ($token->{type} == END_TAG_TOKEN) {
4016            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4017            !!!cp ('t87.5');
4018            #
4019          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4020            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4021            !!!cp ('t87.6');
4022            #
4023            ## TODO: ...
4024          } else {
4025            die "$0: $token->{type}: Unknown token type";        
4026          }
4027        }
4028    
4029        if ($self->{insertion_mode} & HEAD_IMS) {
4030        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4031          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4032            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033                !!!cp ('t88.2');
4034                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4035              } else {
4036                !!!cp ('t88.1');
4037                ## Ignore the token.
4038                !!!next-token;
4039                next B;
4040              }
4041            unless (length $token->{data}) {            unless (length $token->{data}) {
4042              !!!cp ('t88');              !!!cp ('t88');
4043              !!!next-token;              !!!next-token;
4044              redo B;              next B;
4045            }            }
4046          }          }
4047    
4048          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4049            !!!cp ('t89');            !!!cp ('t89');
4050            ## As if <head>            ## As if <head>
4051            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4052            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4053            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4054                  [$self->{head_element}, $el_category->{head}];
4055    
4056            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4057            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3363  sub _tree_construction_main ($) { Line 4061  sub _tree_construction_main ($) {
4061            !!!cp ('t90');            !!!cp ('t90');
4062            ## As if </noscript>            ## As if </noscript>
4063            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4064            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4065                        
4066            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4067            ## As if </head>            ## As if </head>
# Line 3379  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077            !!!cp ('t92');            !!!cp ('t92');
4078          }          }
4079    
4080              ## "after head" insertion mode          ## "after head" insertion mode
4081              ## As if <body>          ## As if <body>
4082              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4083              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4084              ## reprocess          ## reprocess
4085              redo B;          next B;
4086            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4087              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4088                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089                  !!!cp ('t93');              !!!cp ('t93');
4090                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4091                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4092                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4093                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4094                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4095                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4096                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4097                  !!!cp ('t94');              !!!next-token;
4098                  #              next B;
4099                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4100                  !!!cp ('t95');              !!!cp ('t94');
4101                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
4102                  ## Ignore the token            } else {
4103                  !!!next-token;              !!!cp ('t95');
4104                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4105                }              ## Ignore the token
4106              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4107                !!!cp ('t96');              !!!next-token;
4108                ## As if <head>              next B;
4109                !!!create-element ($self->{head_element}, 'head');            }
4110                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4112              ## As if <head>
4113              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115              push @{$self->{open_elements}},
4116                  [$self->{head_element}, $el_category->{head}];
4117    
4118                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4119                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4120              } else {          } else {
4121                !!!cp ('t97');            !!!cp ('t97');
4122              }          }
4123    
4124              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4125                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4126                  !!!cp ('t98');                  !!!cp ('t98');
4127                  ## As if </noscript>                  ## As if </noscript>
4128                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4129                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4130                                
4131                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4132                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3434  sub _tree_construction_main ($) { Line 4137  sub _tree_construction_main ($) {
4137                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4138                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4139                  !!!cp ('t100');                  !!!cp ('t100');
4140                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4141                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4142                        [$self->{head_element}, $el_category->{head}];
4143                } else {                } else {
4144                  !!!cp ('t101');                  !!!cp ('t101');
4145                }                }
4146                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4147                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4148                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4149                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4150                  !!!nack ('t101.1');
4151                !!!next-token;                !!!next-token;
4152                redo B;                next B;
4153              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4154                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4155                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4156                  !!!cp ('t102');                  !!!cp ('t102');
4157                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4158                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4159                        [$self->{head_element}, $el_category->{head}];
4160                } else {                } else {
4161                  !!!cp ('t103');                  !!!cp ('t103');
4162                }                }
4163                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4164                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4165                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4166                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4167                  !!!ack ('t103.1');
4168                !!!next-token;                !!!next-token;
4169                redo B;                next B;
4170              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4171                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4172                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4173                  !!!cp ('t104');                  !!!cp ('t104');
4174                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4175                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4176                        [$self->{head_element}, $el_category->{head}];
4177                } else {                } else {
4178                  !!!cp ('t105');                  !!!cp ('t105');
4179                }                }
4180                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4181                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.
4182    
4183                unless ($self->{confident}) {                unless ($self->{confident}) {
4184                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4185                    !!!cp ('t106');                    !!!cp ('t106');
4186                      ## NOTE: Whether the encoding is supported or not is handled
4187                      ## in the {change_encoding} callback.
4188                    $self->{change_encoding}                    $self->{change_encoding}
4189                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4190                             $token);
4191                                        
4192                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4193                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4194                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4195                                                 ->{has_reference});                                                 ->{has_reference});
4196                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4197                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4198                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4199                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4200                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4201                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4202                      !!!cp ('t107');                      !!!cp ('t107');
4203                        ## NOTE: Whether the encoding is supported or not is handled
4204                        ## in the {change_encoding} callback.
4205                      $self->{change_encoding}                      $self->{change_encoding}
4206                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4207                               $token);
4208                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4209                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4210                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3517  sub _tree_construction_main ($) { Line 4230  sub _tree_construction_main ($) {
4230                  }                  }
4231                }                }
4232    
4233                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4234                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4235                  !!!ack ('t110.1');
4236                !!!next-token;                !!!next-token;
4237                redo B;                next B;
4238              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4239                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4240                  !!!cp ('t111');                  !!!cp ('t111');
4241                  ## As if </noscript>                  ## As if </noscript>
4242                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4243                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4244                                
4245                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4246                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4247                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4248                  !!!cp ('t112');                  !!!cp ('t112');
4249                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4250                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4251                        [$self->{head_element}, $el_category->{head}];
4252                } else {                } else {
4253                  !!!cp ('t113');                  !!!cp ('t113');
4254                }                }
# Line 3541  sub _tree_construction_main ($) { Line 4256  sub _tree_construction_main ($) {
4256                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4257                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4258                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4259                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4260                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4261                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4262                redo B;                next B;
4263              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4264                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4265                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4266                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4267                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4268                  !!!cp ('t114');                  !!!cp ('t114');
4269                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4270                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4271                        [$self->{head_element}, $el_category->{head}];
4272                } else {                } else {
4273                  !!!cp ('t115');                  !!!cp ('t115');
4274                }                }
4275                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4276                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4277                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4278                redo B;                next B;
4279              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4280                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4281                  !!!cp ('t116');                  !!!cp ('t116');
4282                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4283                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4284                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4285                    !!!nack ('t116.1');
4286                  !!!next-token;                  !!!next-token;
4287                  redo B;                  next B;
4288                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4289                  !!!cp ('t117');                  !!!cp ('t117');
4290                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4291                  ## Ignore the token                  ## Ignore the token
4292                    !!!nack ('t117.1');
4293                  !!!next-token;                  !!!next-token;
4294                  redo B;                  next B;
4295                } else {                } else {
4296                  !!!cp ('t118');                  !!!cp ('t118');
4297                  #                  #
# Line 3584  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301                  !!!cp ('t119');                  !!!cp ('t119');
4302                  ## As if </noscript>                  ## As if </noscript>
4303                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4304                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4305                                
4306                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4307                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4308                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4309                  !!!cp ('t120');                  !!!cp ('t120');
4310                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4311                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4312                        [$self->{head_element}, $el_category->{head}];
4313                } else {                } else {
4314                  !!!cp ('t121');                  !!!cp ('t121');
4315                }                }
4316    
4317                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4318                $script_start_tag->($insert_to_current);                $script_start_tag->();
4319                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4320                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4321                redo B;                next B;
4322              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4323                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4324                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4325                  !!!cp ('t122');                  !!!cp ('t122');
4326                  ## As if </noscript>                  ## As if </noscript>
4327                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4328                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4329                                    
4330                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4331                  ## As if </head>                  ## As if </head>
# Line 3624  sub _tree_construction_main ($) { Line 4342  sub _tree_construction_main ($) {
4342                }                }
4343    
4344                ## "after head" insertion mode                ## "after head" insertion mode
4345                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4346                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4347                  !!!cp ('t126');                  !!!cp ('t126');
4348                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3634  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352                } else {                } else {
4353                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4354                }                }
4355                  !!!nack ('t127.1');
4356                !!!next-token;                !!!next-token;
4357                redo B;                next B;
4358              } else {              } else {
4359                !!!cp ('t128');                !!!cp ('t128');
4360                #                #
# Line 3645  sub _tree_construction_main ($) { Line 4364  sub _tree_construction_main ($) {
4364                !!!cp ('t129');                !!!cp ('t129');
4365                ## As if </noscript>                ## As if </noscript>
4366                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4367                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4368                                
4369                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4370                ## As if </head>                ## As if </head>
# Line 3664  sub _tree_construction_main ($) { Line 4383  sub _tree_construction_main ($) {
4383    
4384              ## "after head" insertion mode              ## "after head" insertion mode
4385              ## As if <body>              ## As if <body>
4386              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4387              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4388              ## reprocess              ## reprocess
4389              redo B;              !!!ack-later;
4390                next B;
4391            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4392              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4393                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4394                  !!!cp ('t132');                  !!!cp ('t132');
4395                  ## As if <head>                  ## As if <head>
4396                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4397                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4398                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4399                        [$self->{head_element}, $el_category->{head}];
4400    
4401                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4402                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4403                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4404                  !!!next-token;                  !!!next-token;
4405                  redo B;                  next B;
4406                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4407                  !!!cp ('t133');                  !!!cp ('t133');
4408                  ## As if </noscript>                  ## As if </noscript>
4409                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4410                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4411                                    
4412                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4413                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4414                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4415                  !!!next-token;                  !!!next-token;
4416                  redo B;                  next B;
4417                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4418                  !!!cp ('t134');                  !!!cp ('t134');
4419                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4420                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4421                  !!!next-token;                  !!!next-token;
4422                  redo B;                  next B;
4423                } else {                } else {
4424                  !!!cp ('t135');                  !!!cp ('t135');
4425                  #                  #
# Line 3709  sub _tree_construction_main ($) { Line 4430  sub _tree_construction_main ($) {
4430                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4431                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4432                  !!!next-token;                  !!!next-token;
4433                  redo B;                  next B;
4434                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4435                  !!!cp ('t137');                  !!!cp ('t137');
4436                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4437                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4438                  !!!next-token;                  !!!next-token;
4439                  redo B;                  next B;
4440                } else {                } else {
4441                  !!!cp ('t138');                  !!!cp ('t138');
4442                  #                  #
# Line 3726  sub _tree_construction_main ($) { Line 4447  sub _tree_construction_main ($) {
4447                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4448                  !!!cp ('t139');                  !!!cp ('t139');
4449                  ## As if <head>                  ## As if <head>
4450                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4451                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4452                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4453                        [$self->{head_element}, $el_category->{head}];
4454    
4455                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4456                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4457                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4458                  !!!cp ('t140');                  !!!cp ('t140');
4459                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4460                  ## Ignore the token                  ## Ignore the token
4461                  !!!next-token;                  !!!next-token;
4462                  redo B;                  next B;
4463                } else {                } else {
4464                  !!!cp ('t141');                  !!!cp ('t141');
4465                }                }
# Line 3749  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4472                  !!!cp ('t142');                  !!!cp ('t142');
4473                  ## As if <head>                  ## As if <head>
4474                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4475                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4476                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4477                        [$self->{head_element}, $el_category->{head}];
4478    
4479                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4480                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3766  sub _tree_construction_main ($) { Line 4489  sub _tree_construction_main ($) {
4489                  #                  #
4490                } else {                } else {
4491                  !!!cp ('t145');                  !!!cp ('t145');
4492                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4493                  ## Ignore the token                  ## Ignore the token
4494                  !!!next-token;                  !!!next-token;
4495                  redo B;                  next B;
4496                }                }
4497              }              }
4498    
# Line 3777  sub _tree_construction_main ($) { Line 4500  sub _tree_construction_main ($) {
4500                !!!cp ('t146');                !!!cp ('t146');
4501                ## As if </noscript>                ## As if </noscript>
4502                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4503                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4504                                
4505                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4506                ## As if </head>                ## As if </head>
# Line 3793  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4517  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4518                !!!cp ('t148');                !!!cp ('t148');
4519                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4520                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4521                !!!next-token;                !!!next-token;
4522                redo B;                next B;
4523              } else {              } else {
4524                !!!cp ('t149');                !!!cp ('t149');
4525              }              }
4526    
4527              ## "after head" insertion mode              ## "after head" insertion mode
4528              ## As if <body>              ## As if <body>
4529              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4530              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4531              ## reprocess              ## reprocess
4532              redo B;              next B;
4533            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4534              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4535            }            !!!cp ('t149.1');
4536    
4537              ## NOTE: As if <head>
4538              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4539              $self->{open_elements}->[-1]->[0]->append_child
4540                  ($self->{head_element});
4541              #push @{$self->{open_elements}},
4542              #    [$self->{head_element}, $el_category->{head}];
4543              #$self->{insertion_mode} = IN_HEAD_IM;
4544              ## NOTE: Reprocess.
4545    
4546              ## NOTE: As if </head>
4547              #pop @{$self->{open_elements}};
4548              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4549              ## NOTE: Reprocess.
4550              
4551              #
4552            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4553              !!!cp ('t149.2');
4554    
4555              ## NOTE: As if </head>
4556              pop @{$self->{open_elements}};
4557              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4558              ## NOTE: Reprocess.
4559    
4560              #
4561            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4562              !!!cp ('t149.3');
4563    
4564              !!!parse-error (type => 'in noscript:#eof', token => $token);
4565    
4566              ## As if </noscript>
4567              pop @{$self->{open_elements}};
4568              #$self->{insertion_mode} = IN_HEAD_IM;
4569              ## NOTE: Reprocess.
4570    
4571              ## NOTE: As if </head>
4572              pop @{$self->{open_elements}};
4573              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4574              ## NOTE: Reprocess.
4575    
4576              #
4577            } else {
4578              !!!cp ('t149.4');
4579              #
4580            }
4581    
4582            ## NOTE: As if <body>
4583            !!!insert-element ('body',, $token);
4584            $self->{insertion_mode} = IN_BODY_IM;
4585            ## NOTE: Reprocess.
4586            next B;
4587          } else {
4588            die "$0: $token->{type}: Unknown token type";
4589          }
4590    
4591            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4592      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3821  sub _tree_construction_main ($) { Line 4598  sub _tree_construction_main ($) {
4598              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4599    
4600              !!!next-token;              !!!next-token;
4601              redo B;              next B;
4602            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4603              if ({              if ({
4604                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3829  sub _tree_construction_main ($) { Line 4606  sub _tree_construction_main ($) {
4606                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4607                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4608                  ## have an element in table scope                  ## have an element in table scope
4609                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4610                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4611                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4612                      !!!cp ('t151');                      !!!cp ('t151');
4613                      $tn = $node->[1];  
4614                      last INSCOPE;                      ## Close the cell
4615                    } elsif ({                      !!!back-token; # <x>
4616                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4617                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4618                                  line => $token->{line},
4619                                  column => $token->{column}};
4620                        next B;
4621                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4622                      !!!cp ('t152');                      !!!cp ('t152');
4623                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4624                    }                      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;  
4625                    }                    }
4626                                    }
4627                  !!!cp ('t154');  
4628                  ## Close the cell                  !!!cp ('t153');
4629                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4630                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4631                  redo B;                  ## Ignore the token
4632                    !!!nack ('t153.1');
4633                    !!!next-token;
4634                    next B;
4635                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4636                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4637                                    
4638                  ## As if </caption>                  ## NOTE: As if </caption>.
4639                  ## have a table element in table scope                  ## have a table element in table scope
4640                  my $i;                  my $i;
4641                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4642                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4643                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4644                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4645                      $i = $_;                        !!!cp ('t155');
4646                      last INSCOPE;                        $i = $_;
4647                    } elsif ({                        last INSCOPE;
4648                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4649                             }->{$node->[1]}) {                        !!!cp ('t156');
4650                      !!!cp ('t156');                        last;
4651                      last INSCOPE;                      }
4652                    }                    }
4653    
4654                      !!!cp ('t157');
4655                      !!!parse-error (type => 'start tag not allowed',
4656                                      value => $token->{tag_name}, token => $token);
4657                      ## Ignore the token
4658                      !!!nack ('t157.1');
4659                      !!!next-token;
4660                      next B;
4661                  } # 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;  
                   }  
4662                                    
4663                  ## generate implied end tags                  ## generate implied end tags
4664                  if ({                  while ($self->{open_elements}->[-1]->[1]
4665                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: Maybe the following elements never appear here.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4666                    !!!cp ('t158');                    !!!cp ('t158');
4667                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4668                  }                  }
4669    
4670                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4671                    !!!cp ('t159');                    !!!cp ('t159');
4672                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4673                                      value => $self->{open_elements}->[-1]->[0]
4674                                          ->manakai_local_name,
4675                                      token => $token);
4676                  } else {                  } else {
4677                    !!!cp ('t160');                    !!!cp ('t160');
4678                  }                  }
# Line 3916  sub _tree_construction_main ($) { Line 4684  sub _tree_construction_main ($) {
4684                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4685                                    
4686                  ## reprocess                  ## reprocess
4687                  redo B;                  !!!ack-later;
4688                    next B;
4689                } else {                } else {
4690                  !!!cp ('t161');                  !!!cp ('t161');
4691                  #                  #
# Line 3932  sub _tree_construction_main ($) { Line 4701  sub _tree_construction_main ($) {
4701                  my $i;                  my $i;
4702                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4705                      !!!cp ('t163');                      !!!cp ('t163');
4706                      $i = $_;                      $i = $_;
4707                      last INSCOPE;                      last INSCOPE;
4708                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4709                      !!!cp ('t164');                      !!!cp ('t164');
4710                      last INSCOPE;                      last INSCOPE;
4711                    }                    }
4712                  } # INSCOPE                  } # INSCOPE
4713                    unless (defined $i) {                    unless (defined $i) {
4714                      !!!cp ('t165');                      !!!cp ('t165');
4715                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4716                      ## Ignore the token                      ## Ignore the token
4717                      !!!next-token;                      !!!next-token;
4718                      redo B;                      next B;
4719                    }                    }
4720                                    
4721                  ## generate implied end tags                  ## generate implied end tags
4722                  if ({                  while ($self->{open_elements}->[-1]->[1]
4723                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
   
                      ## NOTE: Maybe the following elements never appear here.  
                      tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4724                    !!!cp ('t166');                    !!!cp ('t166');
4725                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4726                  }                  }
4727                    
4728                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4729                            ne $token->{tag_name}) {
4730                    !!!cp ('t167');                    !!!cp ('t167');
4731                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4732                                      value => $self->{open_elements}->[-1]->[0]
4733                                          ->manakai_local_name,
4734                                      token => $token);
4735                  } else {                  } else {
4736                    !!!cp ('t168');                    !!!cp ('t168');
4737                  }                  }
# Line 3982  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4744                                    
4745                  !!!next-token;                  !!!next-token;
4746                  redo B;                  next B;
4747                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4748                  !!!cp ('t169');                  !!!cp ('t169');
4749                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4750                  ## Ignore the token                  ## Ignore the token
4751                  !!!next-token;                  !!!next-token;
4752                  redo B;                  next B;
4753                } else {                } else {
4754                  !!!cp ('t170');                  !!!cp ('t170');
4755                  #                  #
# Line 3997  sub _tree_construction_main ($) { Line 4758  sub _tree_construction_main ($) {
4758                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4759                  ## have a table element in table scope                  ## have a table element in table scope
4760                  my $i;                  my $i;
4761                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4762                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4763                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4764                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4765                      $i = $_;                        !!!cp ('t171');
4766                      last INSCOPE;                        $i = $_;
4767                    } elsif ({                        last INSCOPE;
4768                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4769                             }->{$node->[1]}) {                        !!!cp ('t172');
4770                      !!!cp ('t172');                        last;
4771                      last INSCOPE;                      }
4772                    }                    }
4773    
4774                      !!!cp ('t173');
4775                      !!!parse-error (type => 'unmatched end tag',
4776                                      value => $token->{tag_name}, token => $token);
4777                      ## Ignore the token
4778                      !!!next-token;
4779                      next B;
4780                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4781                                    
4782                  ## generate implied end tags                  ## generate implied end tags
4783                  if ({                  while ($self->{open_elements}->[-1]->[1]
4784                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: The following elements never appear here, maybe.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4785                    !!!cp ('t174');                    !!!cp ('t174');
4786                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4787                  }                  }
4788                                    
4789                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4790                    !!!cp ('t175');                    !!!cp ('t175');
4791                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4792                                      value => $self->{open_elements}->[-1]->[0]
4793                                          ->manakai_local_name,
4794                                      token => $token);
4795                  } else {                  } else {
4796                    !!!cp ('t176');                    !!!cp ('t176');
4797                  }                  }
# Line 4047  sub _tree_construction_main ($) { Line 4803  sub _tree_construction_main ($) {
4803                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4804                                    
4805                  !!!next-token;                  !!!next-token;
4806                  redo B;                  next B;
4807                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4808                  !!!cp ('t177');                  !!!cp ('t177');
4809                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4810                  ## Ignore the token                  ## Ignore the token
4811                  !!!next-token;                  !!!next-token;
4812                  redo B;                  next B;
4813                } else {                } else {
4814                  !!!cp ('t178');                  !!!cp ('t178');
4815                  #                  #
# Line 4066  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822                ## have an element in table scope                ## have an element in table scope
4823                my $i;                my $i;
4824                my $tn;                my $tn;
4825                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4826                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4827                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4828                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                    $i = $_;                      !!!cp ('t179');
4830                    last INSCOPE;                      $i = $_;
4831                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4832                    !!!cp ('t180');                      ## Close the cell
4833                    $tn = $node->[1];                      !!!back-token; # </x>
4834                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4835                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4836                  } elsif ({                                column => $token->{column}};
4837                            table => 1, html => 1,                      next B;
4838                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4839                    !!!cp ('t181');                      !!!cp ('t180');
4840                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4841                        ## NOTE: There is exactly one |td| or |th| element
4842                        ## in scope in the stack of open elements by definition.
4843                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4844                        ## ISSUE: Can this be reached?
4845                        !!!cp ('t181');
4846                        last;
4847                      }
4848                  }                  }
4849                } # INSCOPE  
               unless (defined $i) {  
4850                  !!!cp ('t182');                  !!!cp ('t182');
4851                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4852                        value => $token->{tag_name}, token => $token);
4853                  ## Ignore the token                  ## Ignore the token
4854                  !!!next-token;                  !!!next-token;
4855                  redo B;                  next B;
4856                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4857              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4858                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4859                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4860    
4861                ## As if </caption>                ## As if </caption>
4862                ## have a table element in table scope                ## have a table element in table scope
4863                my $i;                my $i;
4864                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4865                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4866                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4867                    !!!cp ('t184');                    !!!cp ('t184');
4868                    $i = $_;                    $i = $_;
4869                    last INSCOPE;                    last INSCOPE;
4870                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4871                    !!!cp ('t185');                    !!!cp ('t185');
4872                    last INSCOPE;                    last INSCOPE;
4873                  }                  }
4874                } # INSCOPE                } # INSCOPE
4875                unless (defined $i) {                unless (defined $i) {
4876                  !!!cp ('t186');                  !!!cp ('t186');
4877                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4878                  ## Ignore the token                  ## Ignore the token
4879                  !!!next-token;                  !!!next-token;
4880                  redo B;                  next B;
4881                }                }
4882                                
4883                ## generate implied end tags                ## generate implied end tags
4884                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
   
                    ## NOTE: The following elements never appear, maybe.  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4885                  !!!cp ('t187');                  !!!cp ('t187');
4886                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4887                }                }
4888    
4889                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4890                  !!!cp ('t188');                  !!!cp ('t188');
4891                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4892                                    value => $self->{open_elements}->[-1]->[0]
4893                                        ->manakai_local_name,
4894                                    token => $token);
4895                } else {                } else {
4896                  !!!cp ('t189');                  !!!cp ('t189');
4897                }                }
# Line 4157  sub _tree_construction_main ($) { Line 4903  sub _tree_construction_main ($) {
4903                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4904    
4905                ## reprocess                ## reprocess
4906                redo B;                next B;
4907              } elsif ({              } elsif ({
4908                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4909                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4910                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4911                  !!!cp ('t190');                  !!!cp ('t190');
4912                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4913                  ## Ignore the token                  ## Ignore the token
4914                  !!!next-token;                  !!!next-token;
4915                  redo B;                  next B;
4916                } else {                } else {
4917                  !!!cp ('t191');                  !!!cp ('t191');
4918                  #                  #
# Line 4177  sub _tree_construction_main ($) { Line 4923  sub _tree_construction_main ($) {
4923                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4924                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4925                !!!cp ('t192');                !!!cp ('t192');
4926                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4927                ## Ignore the token                ## Ignore the token
4928                !!!next-token;                !!!next-token;
4929                redo B;                next B;
4930              } else {              } else {
4931                !!!cp ('t193');                !!!cp ('t193');
4932                #                #
4933              }              }
4934          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4935            for my $entry (@{$self->{open_elements}}) {
4936              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4937                !!!cp ('t75');
4938                !!!parse-error (type => 'in body:#eof', token => $token);
4939                last;
4940              }
4941            }
4942    
4943            ## Stop parsing.
4944            last B;
4945        } else {        } else {
4946          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4947        }        }
# Line 4193  sub _tree_construction_main ($) { Line 4950  sub _tree_construction_main ($) {
4950        #        #
4951      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4952        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4953              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4954                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4955              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4956                                
4957                unless (length $token->{data}) {            unless (length $token->{data}) {
4958                  !!!cp ('t194');              !!!cp ('t194');
4959                  !!!next-token;              !!!next-token;
4960                  redo B;              next B;
4961                } else {            } else {
4962                  !!!cp ('t195');              !!!cp ('t195');
4963                }            }
4964              }          }
4965    
4966              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4967    
4968              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4969              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 4971  sub _tree_construction_main ($) {
4971              ## result in a new Text node.              ## result in a new Text node.
4972              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4973                            
4974              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]}) {  
4975                # MUST                # MUST
4976                my $foster_parent_element;                my $foster_parent_element;
4977                my $next_sibling;                my $next_sibling;
4978                my $prev_sibling;                my $prev_sibling;
4979                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4980                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4981                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4982                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4983                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4250  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5006                     $next_sibling);                     $next_sibling);
5007                }                }
5008              } else {            $open_tables->[-1]->[1] = 1; # tainted
5009                !!!cp ('t200');          } else {
5010                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5011              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5012            }
5013                            
5014              !!!next-token;          !!!next-token;
5015              redo B;          next B;
5016        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5017              if ({              if ({
5018                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4264  sub _tree_construction_main ($) { Line 5020  sub _tree_construction_main ($) {
5020                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5021                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5022                  ## Clear back to table context                  ## Clear back to table context
5023                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5024                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5025                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5026                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5027                  }                  }
5028                                    
5029                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5030                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5031                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5032                }                }
# Line 4279  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5035                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5036                    !!!cp ('t202');                    !!!cp ('t202');
5037                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5038                  }                  }
5039                                    
5040                  ## Clear back to table body context                  ## Clear back to table body context
5041                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5042                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5043                    !!!cp ('t203');                    !!!cp ('t203');
5044                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5045                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5046                  }                  }
5047                                    
5048                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5049                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5050                    !!!cp ('t204');                    !!!cp ('t204');
5051                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5052                      !!!nack ('t204');
5053                    !!!next-token;                    !!!next-token;
5054                    redo B;                    next B;
5055                  } else {                  } else {
5056                    !!!cp ('t205');                    !!!cp ('t205');
5057                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5058                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5059                  }                  }
5060                } else {                } else {
# Line 4308  sub _tree_construction_main ($) { Line 5062  sub _tree_construction_main ($) {
5062                }                }
5063    
5064                ## Clear back to table row context                ## Clear back to table row context
5065                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5066                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5067                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5068                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5069                }                }
5070                                
5071                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5072                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5073    
5074                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5075                                
5076                  !!!nack ('t207.1');
5077                !!!next-token;                !!!next-token;
5078                redo B;                next B;
5079              } elsif ({              } elsif ({
5080                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5081                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4334  sub _tree_construction_main ($) { Line 5087  sub _tree_construction_main ($) {
5087                  my $i;                  my $i;
5088                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5089                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5090                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5091                      !!!cp ('t208');                      !!!cp ('t208');
5092                      $i = $_;                      $i = $_;
5093                      last INSCOPE;                      last INSCOPE;
5094                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5095                      !!!cp ('t209');                      !!!cp ('t209');
5096                      last INSCOPE;                      last INSCOPE;
5097                    }                    }
5098                  } # INSCOPE                  } # INSCOPE
5099                  unless (defined $i) {                  unless (defined $i) {
5100                   !!!cp ('t210');                    !!!cp ('t210');
5101  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5102                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5103                    ## Ignore the token                    ## Ignore the token
5104                      !!!nack ('t210.1');
5105                    !!!next-token;                    !!!next-token;
5106                    redo B;                    next B;
5107                  }                  }
5108                                    
5109                  ## Clear back to table row context                  ## Clear back to table row context
5110                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5111                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5112                    !!!cp ('t211');                    !!!cp ('t211');
5113                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5114                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5115                  }                  }
5116                                    
# Line 4372  sub _tree_construction_main ($) { Line 5119  sub _tree_construction_main ($) {
5119                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5120                    !!!cp ('t212');                    !!!cp ('t212');
5121                    ## reprocess                    ## reprocess
5122                    redo B;                    !!!ack-later;
5123                      next B;
5124                  } else {                  } else {
5125                    !!!cp ('t213');                    !!!cp ('t213');
5126                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5132  sub _tree_construction_main ($) {
5132                  my $i;                  my $i;
5133                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5134                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5135                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5136                      !!!cp ('t214');                      !!!cp ('t214');
5137                      $i = $_;                      $i = $_;
5138                      last INSCOPE;                      last INSCOPE;
5139                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5140                      !!!cp ('t215');                      !!!cp ('t215');
5141                      last INSCOPE;                      last INSCOPE;
5142                    }                    }
# Line 4400  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144                  unless (defined $i) {                  unless (defined $i) {
5145                    !!!cp ('t216');                    !!!cp ('t216');
5146  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5147                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5148                    ## Ignore the token                    ## Ignore the token
5149                      !!!nack ('t216.1');
5150                    !!!next-token;                    !!!next-token;
5151                    redo B;                    next B;
5152                  }                  }
5153    
5154                  ## Clear back to table body context                  ## Clear back to table body context
5155                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5156                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5157                    !!!cp ('t217');                    !!!cp ('t217');
5158                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5159                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5160                  }                  }
5161                                    
# Line 4432  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175    
5176                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5177                  ## Clear back to table context                  ## Clear back to table context
5178                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5179                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5180                    !!!cp ('t219');                    !!!cp ('t219');
5181                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5182                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5183                  }                  }
5184                                    
5185                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5186                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5187                  ## reprocess                  ## reprocess
5188                  redo B;                  !!!ack-later;
5189                    next B;
5190                } elsif ({                } elsif ({
5191                          caption => 1,                          caption => 1,
5192                          colgroup => 1,                          colgroup => 1,
5193                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5194                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5195                  ## Clear back to table context                  ## Clear back to table context
5196                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5197                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5198                    !!!cp ('t220');                    !!!cp ('t220');
5199                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5200                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5201                  }                  }
5202                                    
5203                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5204                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5205                                    
5206                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5207                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5208                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5209                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4470  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5213                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5214                  !!!next-token;                  !!!next-token;
5215                  redo B;                  !!!nack ('t220.1');
5216                    next B;
5217                } else {                } else {
5218                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5219                }                }
5220              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5221                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5222                                  value => $self->{open_elements}->[-1]->[0]
5223                                      ->manakai_local_name,
5224                                  token => $token);
5225    
5226                ## As if </table>                ## As if </table>
5227                ## have a table element in table scope                ## have a table element in table scope
5228                my $i;                my $i;
5229                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5231                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5232                    !!!cp ('t221');                    !!!cp ('t221');
5233                    $i = $_;                    $i = $_;
5234                    last INSCOPE;                    last INSCOPE;
5235                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5236                    !!!cp ('t222');                    !!!cp ('t222');
5237                    last INSCOPE;                    last INSCOPE;
5238                  }                  }
# Line 4497  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240                unless (defined $i) {                unless (defined $i) {
5241                  !!!cp ('t223');                  !!!cp ('t223');
5242  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5243                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5244                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5245                    !!!nack ('t223.1');
5246                  !!!next-token;                  !!!next-token;
5247                  redo B;                  next B;
5248                }                }
5249                                
5250    ## TODO: Followings are removed from the latest spec.
5251                ## generate implied end tags                ## generate implied end tags
5252                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5253                  !!!cp ('t224');                  !!!cp ('t224');
5254                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5255                }                }
5256    
5257                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5258                  !!!cp ('t225');                  !!!cp ('t225');
5259  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5260                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5261                                    value => $self->{open_elements}->[-1]->[0]
5262                                        ->manakai_local_name,
5263                                    token => $token);
5264                } else {                } else {
5265                  !!!cp ('t226');                  !!!cp ('t226');
5266                }                }
5267    
5268                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5269                  pop @{$open_tables};
5270    
5271                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5272    
5273                ## reprocess            ## reprocess
5274                redo B;            !!!ack-later;
5275              next B;
5276            } elsif ($token->{tag_name} eq 'style') {
5277              if (not $open_tables->[-1]->[1]) { # tainted
5278                !!!cp ('t227.8');
5279                ## NOTE: This is a "as if in head" code clone.
5280                $parse_rcdata->(CDATA_CONTENT_MODEL);
5281                next B;
5282              } else {
5283                !!!cp ('t227.7');
5284                #
5285              }
5286            } elsif ($token->{tag_name} eq 'script') {
5287              if (not $open_tables->[-1]->[1]) { # tainted
5288                !!!cp ('t227.6');
5289                ## NOTE: This is a "as if in head" code clone.
5290                $script_start_tag->();
5291                next B;
5292              } else {
5293                !!!cp ('t227.5');
5294                #
5295              }
5296            } elsif ($token->{tag_name} eq 'input') {
5297              if (not $open_tables->[-1]->[1]) { # tainted
5298                if ($token->{attributes}->{type}) { ## TODO: case
5299                  my $type = lc $token->{attributes}->{type}->{value};
5300                  if ($type eq 'hidden') {
5301                    !!!cp ('t227.3');
5302                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5303    
5304                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5305    
5306                    ## TODO: form element pointer
5307    
5308                    pop @{$self->{open_elements}};
5309    
5310                    !!!next-token;
5311                    !!!ack ('t227.2.1');
5312                    next B;
5313                  } else {
5314                    !!!cp ('t227.2');
5315                    #
5316                  }
5317                } else {
5318                  !!!cp ('t227.1');
5319                  #
5320                }
5321              } else {
5322                !!!cp ('t227.4');
5323                #
5324              }
5325          } else {          } else {
5326            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5327            #            #
5328          }          }
5329    
5330            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5331    
5332            $insert = $insert_to_foster;
5333            #
5334        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5335              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5336                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4546  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                my $i;                my $i;
5339                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5341                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5342                    !!!cp ('t228');                    !!!cp ('t228');
5343                    $i = $_;                    $i = $_;
5344                    last INSCOPE;                    last INSCOPE;
5345                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5346                    !!!cp ('t229');                    !!!cp ('t229');
5347                    last INSCOPE;                    last INSCOPE;
5348                  }                  }
5349                } # INSCOPE                } # INSCOPE
5350                unless (defined $i) {                unless (defined $i) {
5351                  !!!cp ('t230');                  !!!cp ('t230');
5352                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5353                  ## Ignore the token                  ## Ignore the token
5354                    !!!nack ('t230.1');
5355                  !!!next-token;                  !!!next-token;
5356                  redo B;                  next B;
5357                } else {                } else {
5358                  !!!cp ('t232');                  !!!cp ('t232');
5359                }                }
5360    
5361                ## Clear back to table row context                ## Clear back to table row context
5362                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5363                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5364                  !!!cp ('t231');                  !!!cp ('t231');
5365  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5366                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5367                }                }
5368    
5369                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5370                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                !!!next-token;                !!!next-token;
5372                redo B;                !!!nack ('t231.1');
5373                  next B;
5374              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5375                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5376                  ## As if </tr>                  ## As if </tr>
# Line 4588  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                  my $i;                  my $i;
5379                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5381                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5382                      !!!cp ('t233');                      !!!cp ('t233');
5383                      $i = $_;                      $i = $_;
5384                      last INSCOPE;                      last INSCOPE;
5385                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5386                      !!!cp ('t234');                      !!!cp ('t234');
5387                      last INSCOPE;                      last INSCOPE;
5388                    }                    }
# Line 4602  sub _tree_construction_main ($) { Line 5390  sub _tree_construction_main ($) {
5390                  unless (defined $i) {                  unless (defined $i) {
5391                    !!!cp ('t235');                    !!!cp ('t235');
5392  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5393                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5394                    ## Ignore the token                    ## Ignore the token
5395                      !!!nack ('t236.1');
5396                    !!!next-token;                    !!!next-token;
5397                    redo B;                    next B;
5398                  }                  }
5399                                    
5400                  ## Clear back to table row context                  ## Clear back to table row context
5401                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5402                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5403                    !!!cp ('t236');                    !!!cp ('t236');
5404  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5405                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5406                  }                  }
5407                                    
# Line 4628  sub _tree_construction_main ($) { Line 5415  sub _tree_construction_main ($) {
5415                  my $i;                  my $i;
5416                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5417                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5418                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5419                      !!!cp ('t237');                      !!!cp ('t237');
5420                      $i = $_;                      $i = $_;
5421                      last INSCOPE;                      last INSCOPE;
5422                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5423                      !!!cp ('t238');                      !!!cp ('t238');
5424                      last INSCOPE;                      last INSCOPE;
5425                    }                    }
5426                  } # INSCOPE                  } # INSCOPE
5427                  unless (defined $i) {                  unless (defined $i) {
5428                    !!!cp ('t239');                    !!!cp ('t239');
5429                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5430                    ## Ignore the token                    ## Ignore the token
5431                      !!!nack ('t239.1');
5432                    !!!next-token;                    !!!next-token;
5433                    redo B;                    next B;
5434                  }                  }
5435                                    
5436                  ## Clear back to table body context                  ## Clear back to table body context
5437                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5438                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5439                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5440                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5441                  }                  }
5442                                    
# Line 4670  sub _tree_construction_main ($) { Line 5452  sub _tree_construction_main ($) {
5452                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5453                }                }
5454    
5455                  ## NOTE: </table> in the "in table" insertion mode.
5456                  ## When you edit the code fragment below, please ensure that
5457                  ## the code for <table> in the "in table" insertion mode
5458                  ## is synced with it.
5459    
5460                ## have a table element in table scope                ## have a table element in table scope
5461                my $i;                my $i;
5462                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5463                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5464                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5465                    !!!cp ('t241');                    !!!cp ('t241');
5466                    $i = $_;                    $i = $_;
5467                    last INSCOPE;                    last INSCOPE;
5468                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5469                    !!!cp ('t242');                    !!!cp ('t242');
5470                    last INSCOPE;                    last INSCOPE;
5471                  }                  }
5472                } # INSCOPE                } # INSCOPE
5473                unless (defined $i) {                unless (defined $i) {
5474                  !!!cp ('t243');                  !!!cp ('t243');
5475                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5476                  ## Ignore the token                  ## Ignore the token
5477                    !!!nack ('t243.1');
5478                  !!!next-token;                  !!!next-token;
5479                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5480                }                }
5481                                    
5482                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5483                  pop @{$open_tables};
5484                                
5485                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5486                                
5487                !!!next-token;                !!!next-token;
5488                redo B;                next B;
5489              } elsif ({              } elsif ({
5490                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5491                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4729  sub _tree_construction_main ($) { Line 5495  sub _tree_construction_main ($) {
5495                  my $i;                  my $i;
5496                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5497                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5498                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5499                      !!!cp ('t247');                      !!!cp ('t247');
5500                      $i = $_;                      $i = $_;
5501                      last INSCOPE;                      last INSCOPE;
5502                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5503                      !!!cp ('t248');                      !!!cp ('t248');
5504                      last INSCOPE;                      last INSCOPE;
5505                    }                    }
5506                  } # INSCOPE                  } # INSCOPE
5507                    unless (defined $i) {                    unless (defined $i) {
5508                      !!!cp ('t249');                      !!!cp ('t249');
5509                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5510                      ## Ignore the token                      ## Ignore the token
5511                        !!!nack ('t249.1');
5512                      !!!next-token;                      !!!next-token;
5513                      redo B;                      next B;
5514                    }                    }
5515                                    
5516                  ## As if </tr>                  ## As if </tr>
# Line 4753  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518                  my $i;                  my $i;
5519                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5520                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5521                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5522                      !!!cp ('t250');                      !!!cp ('t250');
5523                      $i = $_;                      $i = $_;
5524                      last INSCOPE;                      last INSCOPE;
5525                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5526                      !!!cp ('t251');                      !!!cp ('t251');
5527                      last INSCOPE;                      last INSCOPE;
5528                    }                    }
5529                  } # INSCOPE                  } # INSCOPE
5530                    unless (defined $i) {                    unless (defined $i) {
5531                      !!!cp ('t252');                      !!!cp ('t252');
5532                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5533                      ## Ignore the token                      ## Ignore the token
5534                        !!!nack ('t252.1');
5535                      !!!next-token;                      !!!next-token;
5536                      redo B;                      next B;
5537                    }                    }
5538                                    
5539                  ## Clear back to table row context                  ## Clear back to table row context
5540                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5541                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5542                    !!!cp ('t253');                    !!!cp ('t253');
5543  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5544                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5545                  }                  }
5546                                    
# Line 4791  sub _tree_construction_main ($) { Line 5553  sub _tree_construction_main ($) {
5553                my $i;                my $i;
5554                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5555                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5556                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5557                    !!!cp ('t254');                    !!!cp ('t254');
5558                    $i = $_;                    $i = $_;
5559                    last INSCOPE;                    last INSCOPE;
5560                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5561                    !!!cp ('t255');                    !!!cp ('t255');
5562                    last INSCOPE;                    last INSCOPE;
5563                  }                  }
5564                } # INSCOPE                } # INSCOPE
5565                unless (defined $i) {                unless (defined $i) {
5566                  !!!cp ('t256');                  !!!cp ('t256');
5567                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5568                  ## Ignore the token                  ## Ignore the token
5569                    !!!nack ('t256.1');
5570                  !!!next-token;                  !!!next-token;
5571                  redo B;                  next B;
5572                }                }
5573    
5574                ## Clear back to table body context                ## Clear back to table body context
5575                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5576                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5577                  !!!cp ('t257');                  !!!cp ('t257');
5578  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5579                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5580                }                }
5581    
5582                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5583                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5584                  !!!nack ('t257.1');
5585                !!!next-token;                !!!next-token;
5586                redo B;                next B;
5587              } elsif ({              } elsif ({
5588                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5589                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5590                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5591                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5592                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5593                !!!cp ('t258');            !!!cp ('t258');
5594                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5595                ## Ignore the token            ## Ignore the token
5596                !!!next-token;            !!!nack ('t258.1');
5597                redo B;             !!!next-token;
5598              next B;
5599          } else {          } else {
5600            !!!cp ('t259');            !!!cp ('t259');
5601            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5602    
5603            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5604            #            #
5605          }          }
5606          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5607            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5608                    @{$self->{open_elements}} == 1) { # redundant, maybe
5609              !!!parse-error (type => 'in body:#eof', token => $token);
5610              !!!cp ('t259.1');
5611              #
5612            } else {
5613              !!!cp ('t259.2');
5614              #
5615            }
5616    
5617            ## Stop parsing
5618            last B;
5619        } else {        } else {
5620          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5621        }        }
# Line 4852  sub _tree_construction_main ($) { Line 5626  sub _tree_construction_main ($) {
5626                unless (length $token->{data}) {                unless (length $token->{data}) {
5627                  !!!cp ('t260');                  !!!cp ('t260');
5628                  !!!next-token;                  !!!next-token;
5629                  redo B;                  next B;
5630                }                }
5631              }              }
5632                            
# Line 4861  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5636              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5637                !!!cp ('t262');                !!!cp ('t262');
5638                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5639                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5640                  !!!ack ('t262.1');
5641                !!!next-token;                !!!next-token;
5642                redo B;                next B;
5643              } else {              } else {
5644                !!!cp ('t263');                !!!cp ('t263');
5645                #                #
5646              }              }
5647            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5648              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5649                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5650                  !!!cp ('t264');                  !!!cp ('t264');
5651                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5652                  ## Ignore the token                  ## Ignore the token
5653                  !!!next-token;                  !!!next-token;
5654                  redo B;                  next B;
5655                } else {                } else {
5656                  !!!cp ('t265');                  !!!cp ('t265');
5657                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5658                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5659                  !!!next-token;                  !!!next-token;
5660                  redo B;                              next B;            
5661                }                }
5662              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5663                !!!cp ('t266');                !!!cp ('t266');
5664                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5665                ## Ignore the token                ## Ignore the token
5666                !!!next-token;                !!!next-token;
5667                redo B;                next B;
5668              } else {              } else {
5669                !!!cp ('t267');                !!!cp ('t267');
5670                #                #
5671              }              }
5672            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5673              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5674            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5675              !!!cp ('t270.2');
5676              ## Stop parsing.
5677              last B;
5678            } else {
5679              ## NOTE: As if </colgroup>.
5680              !!!cp ('t270.1');
5681              pop @{$self->{open_elements}}; # colgroup
5682              $self->{insertion_mode} = IN_TABLE_IM;
5683              ## Reprocess.
5684              next B;
5685            }
5686          } else {
5687            die "$0: $token->{type}: Unknown token type";
5688          }
5689    
5690            ## As if </colgroup>            ## As if </colgroup>
5691            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5692              !!!cp ('t269');              !!!cp ('t269');
5693              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5694                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5695              ## Ignore the token              ## Ignore the token
5696                !!!nack ('t269.1');
5697              !!!next-token;              !!!next-token;
5698              redo B;              next B;
5699            } else {            } else {
5700              !!!cp ('t270');              !!!cp ('t270');
5701              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5702              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5703                !!!ack-later;
5704              ## reprocess              ## reprocess
5705              redo B;              next B;
5706            }            }
5707      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5708        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5709          !!!cp ('t271');          !!!cp ('t271');
5710          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5711          !!!next-token;          !!!next-token;
5712          redo B;          next B;
5713        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5714              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5715                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5716                  !!!cp ('t272');              !!!cp ('t272');
5717                  ## As if </option>              ## As if </option>
5718                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5719                } else {            } else {
5720                  !!!cp ('t273');              !!!cp ('t273');
5721                }            }
5722    
5723                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5724                !!!next-token;            !!!nack ('t273.1');
5725                redo B;            !!!next-token;
5726              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5727                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5728                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5729                  ## As if </option>              !!!cp ('t274');
5730                  pop @{$self->{open_elements}};              ## As if </option>
5731                } else {              pop @{$self->{open_elements}};
5732                  !!!cp ('t275');            } else {
5733                }              !!!cp ('t275');
5734              }
5735    
5736                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5737                  !!!cp ('t276');              !!!cp ('t276');
5738                  ## As if </optgroup>              ## As if </optgroup>
5739                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5740                } else {            } else {
5741                  !!!cp ('t277');              !!!cp ('t277');
5742                }            }
5743    
5744                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5745                !!!next-token;            !!!nack ('t277.1');
5746                redo B;            !!!next-token;
5747              } elsif ($token->{tag_name} eq 'select') {            next B;
5748  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5749                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5750                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5751                ## have an element in table scope                    {
5752                my $i;                     caption => 1, table => 1,
5753                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5754                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5755                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5756                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5757                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5758                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5759                  } elsif ({            ## as if there were </select> (otherwise).
5760                            table => 1, html => 1,            ## have an element in table scope
5761                           }->{$node->[1]}) {            my $i;
5762                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5763                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5764                  }              if ($node->[1] & SELECT_EL) {
5765                } # INSCOPE                !!!cp ('t278');
5766                unless (defined $i) {                $i = $_;
5767                  !!!cp ('t280');                last INSCOPE;
5768                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5769                  ## Ignore the token                !!!cp ('t279');
5770                  !!!next-token;                last INSCOPE;
5771                  redo B;              }
5772                }            } # INSCOPE
5773              unless (defined $i) {
5774                !!!cp ('t280');
5775                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5776                ## Ignore the token
5777                !!!nack ('t280.1');
5778                !!!next-token;
5779                next B;
5780              }
5781                                
5782                !!!cp ('t281');            !!!cp ('t281');
5783                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5784    
5785                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5786    
5787                !!!next-token;            if ($token->{tag_name} eq 'select') {
5788                redo B;              !!!nack ('t281.2');
5789                !!!next-token;
5790                next B;
5791              } else {
5792                !!!cp ('t281.1');
5793                !!!ack-later;
5794                ## Reprocess the token.
5795                next B;
5796              }
5797          } else {          } else {
5798            !!!cp ('t282');            !!!cp ('t282');
5799            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5800            ## Ignore the token            ## Ignore the token
5801              !!!nack ('t282.1');
5802            !!!next-token;            !!!next-token;
5803            redo B;            next B;
5804          }          }
5805        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5806              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5807                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5808                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5809                  !!!cp ('t283');              !!!cp ('t283');
5810                  ## As if </option>              ## As if </option>
5811                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5812                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5813                  !!!cp ('t284');              !!!cp ('t284');
5814                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5815                } else {            } else {
5816                  !!!cp ('t285');              !!!cp ('t285');
5817                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5818                  ## Ignore the token              ## Ignore the token
5819                }            }
5820                !!!next-token;            !!!nack ('t285.1');
5821                redo B;            !!!next-token;
5822              } elsif ($token->{tag_name} eq 'option') {            next B;
5823                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5824                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5825                  pop @{$self->{open_elements}};              !!!cp ('t286');
5826                } else {              pop @{$self->{open_elements}};
5827                  !!!cp ('t287');            } else {
5828                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5829                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5830                }              ## Ignore the token
5831                !!!next-token;            }
5832                redo B;            !!!nack ('t287.1');
5833              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5834                ## have an element in table scope            next B;
5835                my $i;          } elsif ($token->{tag_name} eq 'select') {
5836                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5837                  my $node = $self->{open_elements}->[$_];            my $i;
5838                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5839                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5840                    $i = $_;              if ($node->[1] & SELECT_EL) {
5841                    last INSCOPE;                !!!cp ('t288');
5842                  } elsif ({                $i = $_;
5843                            table => 1, html => 1,                last INSCOPE;
5844                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5845                    !!!cp ('t289');                !!!cp ('t289');
5846                    last INSCOPE;                last INSCOPE;
5847                  }              }
5848                } # INSCOPE            } # INSCOPE
5849                unless (defined $i) {            unless (defined $i) {
5850                  !!!cp ('t290');              !!!cp ('t290');
5851                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5852                  ## Ignore the token              ## Ignore the token
5853                  !!!next-token;              !!!nack ('t290.1');
5854                  redo B;              !!!next-token;
5855                }              next B;
5856              }
5857                                
5858                !!!cp ('t291');            !!!cp ('t291');
5859                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5860    
5861                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5862    
5863                !!!next-token;            !!!nack ('t291.1');
5864                redo B;            !!!next-token;
5865              } elsif ({            next B;
5866                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5867                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5868                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5869                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5870                     }->{$token->{tag_name}}) {
5871  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5872                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5873                                
5874                ## have an element in table scope            ## have an element in table scope
5875                my $i;            my $i;
5876                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5877                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5878                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5879                    !!!cp ('t292');                !!!cp ('t292');
5880                    $i = $_;                $i = $_;
5881                    last INSCOPE;                last INSCOPE;
5882                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5883                            table => 1, html => 1,                !!!cp ('t293');
5884                           }->{$node->[1]}) {                last INSCOPE;
5885                    !!!cp ('t293');              }
5886                    last INSCOPE;            } # INSCOPE
5887                  }            unless (defined $i) {
5888                } # INSCOPE              !!!cp ('t294');
5889                unless (defined $i) {              ## Ignore the token
5890                  !!!cp ('t294');              !!!nack ('t294.1');
5891                  ## Ignore the token              !!!next-token;
5892                  !!!next-token;              next B;
5893                  redo B;            }
               }  
5894                                
5895                ## As if </select>            ## As if </select>
5896                ## have an element in table scope            ## have an element in table scope
5897                undef $i;            undef $i;
5898                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5899                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5900                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5901                    !!!cp ('t295');                !!!cp ('t295');
5902                    $i = $_;                $i = $_;
5903                    last INSCOPE;                last INSCOPE;
5904                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5905  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5906                    !!!cp ('t296');                !!!cp ('t296');
5907                    last INSCOPE;                last INSCOPE;
5908                  }              }
5909                } # INSCOPE            } # INSCOPE
5910                unless (defined $i) {            unless (defined $i) {
5911                  !!!cp ('t297');              !!!cp ('t297');
5912  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5913                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5914                  ## Ignore the </select> token              ## Ignore the </select> token
5915                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5916                  redo B;              !!!next-token; ## TODO: ok?
5917                }              next B;
5918              }
5919                                
5920                !!!cp ('t298');            !!!cp ('t298');
5921                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5922    
5923                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5924    
5925                ## reprocess            !!!ack-later;
5926                redo B;            ## reprocess
5927              next B;
5928          } else {          } else {
5929            !!!cp ('t299');            !!!cp ('t299');
5930            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5931            ## Ignore the token            ## Ignore the token
5932              !!!nack ('t299.3');
5933            !!!next-token;            !!!next-token;
5934            redo B;            next B;
5935            }
5936          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5937            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5938                    @{$self->{open_elements}} == 1) { # redundant, maybe
5939              !!!cp ('t299.1');
5940              !!!parse-error (type => 'in body:#eof', token => $token);
5941            } else {
5942              !!!cp ('t299.2');
5943          }          }
5944    
5945            ## Stop parsing.
5946            last B;
5947        } else {        } else {
5948          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5949        }        }
# Line 5135  sub _tree_construction_main ($) { Line 5959  sub _tree_construction_main ($) {
5959            unless (length $token->{data}) {            unless (length $token->{data}) {
5960              !!!cp ('t300');              !!!cp ('t300');
5961              !!!next-token;              !!!next-token;
5962              redo B;              next B;
5963            }            }
5964          }          }
5965                    
5966          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5967            !!!cp ('t301');            !!!cp ('t301');
5968            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5969    
5970            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5971          } else {          } else {
# Line 5149  sub _tree_construction_main ($) { Line 5973  sub _tree_construction_main ($) {
5973          }          }
5974                    
5975          ## "after body" insertion mode          ## "after body" insertion mode
5976          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5977    
5978          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5979          ## reprocess          ## reprocess
5980          redo B;          next B;
5981        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5982          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5983            !!!cp ('t303');            !!!cp ('t303');
5984            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5985                        
5986            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5987          } else {          } else {
# Line 5165  sub _tree_construction_main ($) { Line 5989  sub _tree_construction_main ($) {
5989          }          }
5990    
5991          ## "after body" insertion mode          ## "after body" insertion mode
5992          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5993    
5994          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5995            !!!ack-later;
5996          ## reprocess          ## reprocess
5997          redo B;          next B;
5998        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5999          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6000            !!!cp ('t305');            !!!cp ('t305');
6001            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6002                        
6003            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6004            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5185  sub _tree_construction_main ($) { Line 6010  sub _tree_construction_main ($) {
6010          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6011            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6012              !!!cp ('t307');              !!!cp ('t307');
6013              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6014              ## Ignore the token              ## Ignore the token
6015              !!!next-token;              !!!next-token;
6016              redo B;              next B;
6017            } else {            } else {
6018              !!!cp ('t308');              !!!cp ('t308');
6019              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6020              !!!next-token;              !!!next-token;
6021              redo B;              next B;
6022            }            }
6023          } else {          } else {
6024            !!!cp ('t309');            !!!cp ('t309');
6025            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6026    
6027            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6028            ## reprocess            ## reprocess
6029            redo B;            next B;
6030          }          }
6031          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6032            !!!cp ('t309.2');
6033            ## Stop parsing
6034            last B;
6035        } else {        } else {
6036          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6037        }        }
# Line 5214  sub _tree_construction_main ($) { Line 6043  sub _tree_construction_main ($) {
6043            unless (length $token->{data}) {            unless (length $token->{data}) {
6044              !!!cp ('t310');              !!!cp ('t310');
6045              !!!next-token;              !!!next-token;
6046              redo B;              next B;
6047            }            }
6048          }          }
6049                    
6050          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6051            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6052              !!!cp ('t311');              !!!cp ('t311');
6053              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6054            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6055              !!!cp ('t312');              !!!cp ('t312');
6056              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6057            } else { # "after html frameset"            } else { # "after html frameset"
6058              !!!cp ('t313');              !!!cp ('t313');
6059              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6060    
6061              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6062              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6063              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6064            }            }
6065                        
6066            ## Ignore the token.            ## Ignore the token.
# Line 5242  sub _tree_construction_main ($) { Line 6071  sub _tree_construction_main ($) {
6071              !!!cp ('t315');              !!!cp ('t315');
6072              !!!next-token;              !!!next-token;
6073            }            }
6074            redo B;            next B;
6075          }          }
6076                    
6077          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6078        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6079          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6080            !!!cp ('t316');            !!!cp ('t316');
6081            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6082    
6083            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6084            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5260  sub _tree_construction_main ($) { Line 6089  sub _tree_construction_main ($) {
6089          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6090              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6091            !!!cp ('t318');            !!!cp ('t318');
6092            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6093              !!!nack ('t318.1');
6094            !!!next-token;            !!!next-token;
6095            redo B;            next B;
6096          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6097                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6098            !!!cp ('t319');            !!!cp ('t319');
6099            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6100            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6101              !!!ack ('t319.1');
6102            !!!next-token;            !!!next-token;
6103            redo B;            next B;
6104          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6105            !!!cp ('t320');            !!!cp ('t320');
6106            ## NOTE: As if in body.            ## NOTE: As if in body.
6107            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6108            redo B;            next B;
6109          } else {          } else {
6110            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111              !!!cp ('t321');              !!!cp ('t321');
6112              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6113            } else {            } else {
6114              !!!cp ('t322');              !!!cp ('t322');
6115              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6116            }            }
6117            ## Ignore the token            ## Ignore the token
6118              !!!nack ('t322.1');
6119            !!!next-token;            !!!next-token;
6120            redo B;            next B;
6121          }          }
6122        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6123          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6124            !!!cp ('t323');            !!!cp ('t323');
6125            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126    
6127            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6128            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5300  sub _tree_construction_main ($) { Line 6132  sub _tree_construction_main ($) {
6132    
6133          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6134              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6135            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6136                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6137              !!!cp ('t325');              !!!cp ('t325');
6138              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6139              ## Ignore the token              ## Ignore the token
6140              !!!next-token;              !!!next-token;
6141            } else {            } else {
# Line 5313  sub _tree_construction_main ($) { Line 6145  sub _tree_construction_main ($) {
6145            }            }
6146    
6147            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6148                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6149              !!!cp ('t327');              !!!cp ('t327');
6150              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6151            } else {            } else {
6152              !!!cp ('t328');              !!!cp ('t328');
6153            }            }
6154            redo B;            next B;
6155          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6156                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6157            !!!cp ('t329');            !!!cp ('t329');
6158            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6159            !!!next-token;            !!!next-token;
6160            redo B;            next B;
6161          } else {          } else {
6162            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6163              !!!cp ('t330');              !!!cp ('t330');
6164              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6165            } else {            } else {
6166              !!!cp ('t331');              !!!cp ('t331');
6167              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6168            }            }
6169            ## Ignore the token            ## Ignore the token
6170            !!!next-token;            !!!next-token;
6171            redo B;            next B;
6172          }          }
6173          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6174            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6175                    @{$self->{open_elements}} == 1) { # redundant, maybe
6176              !!!cp ('t331.1');
6177              !!!parse-error (type => 'in body:#eof', token => $token);
6178            } else {
6179              !!!cp ('t331.2');
6180            }
6181            
6182            ## Stop parsing
6183            last B;
6184        } else {        } else {
6185          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6186        }        }
# Line 5352  sub _tree_construction_main ($) { Line 6195  sub _tree_construction_main ($) {
6195        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6196          !!!cp ('t332');          !!!cp ('t332');
6197          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6198          $script_start_tag->($insert);          $script_start_tag->();
6199          redo B;          next B;
6200        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6201          !!!cp ('t333');          !!!cp ('t333');
6202          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6203          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6204          redo B;          next B;
6205        } elsif ({        } elsif ({
6206                  base => 1, link => 1,                  base => 1, link => 1,
6207                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6208          !!!cp ('t334');          !!!cp ('t334');
6209          ## 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
6210          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6211          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6212            !!!ack ('t334.1');
6213          !!!next-token;          !!!next-token;
6214          redo B;          next B;
6215        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6216          ## 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
6217          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6218          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.
6219    
6220          unless ($self->{confident}) {          unless ($self->{confident}) {
6221            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6222              !!!cp ('t335');              !!!cp ('t335');
6223                ## NOTE: Whether the encoding is supported or not is handled
6224                ## in the {change_encoding} callback.
6225              $self->{change_encoding}              $self->{change_encoding}
6226                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6227                            
6228              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6229                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6230                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6231                                           ->{has_reference});                                           ->{has_reference});
6232            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6233              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6234                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6235                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6236                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6237                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6238                !!!cp ('t336');                !!!cp ('t336');
6239                  ## NOTE: Whether the encoding is supported or not is handled
6240                  ## in the {change_encoding} callback.
6241                $self->{change_encoding}                $self->{change_encoding}
6242                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6243                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6244                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6245                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5416  sub _tree_construction_main ($) { Line 6263  sub _tree_construction_main ($) {
6263            }            }
6264          }          }
6265    
6266            !!!ack ('t338.1');
6267          !!!next-token;          !!!next-token;
6268          redo B;          next B;
6269        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6270          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6271          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6272          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6273            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6274        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6275          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6276                                
6277          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6278              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6279            !!!cp ('t342');            !!!cp ('t342');
6280            ## Ignore the token            ## Ignore the token
6281          } else {          } else {
# Line 5450  sub _tree_construction_main ($) { Line 6289  sub _tree_construction_main ($) {
6289              }              }
6290            }            }
6291          }          }
6292            !!!nack ('t343.1');
6293          !!!next-token;          !!!next-token;
6294          redo B;          next B;
6295        } elsif ({        } elsif ({
6296                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6297                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6298                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6299                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6300                  pre => 1,                  pre => 1, listing => 1,
6301                    form => 1,
6302                    table => 1,
6303                    hr => 1,
6304                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6305            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6306              !!!cp ('t350');
6307              !!!parse-error (type => 'in form:form', token => $token);
6308              ## Ignore the token
6309              !!!nack ('t350.1');
6310              !!!next-token;
6311              next B;
6312            }
6313    
6314          ## has a p element in scope          ## has a p element in scope
6315          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6316            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6317              !!!cp ('t344');              !!!cp ('t344');
6318              !!!back-token;              !!!back-token; # <form>
6319              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6320              redo B;                        line => $token->{line}, column => $token->{column}};
6321            } elsif ({              next B;
6322                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6323              !!!cp ('t345');              !!!cp ('t345');
6324              last INSCOPE;              last INSCOPE;
6325            }            }
6326          } # INSCOPE          } # INSCOPE
6327                        
6328          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6329          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6330              !!!nack ('t346.1');
6331            !!!next-token;            !!!next-token;
6332            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6333              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5488  sub _tree_construction_main ($) { Line 6340  sub _tree_construction_main ($) {
6340            } else {            } else {
6341              !!!cp ('t348');              !!!cp ('t348');
6342            }            }
6343          } else {          } elsif ($token->{tag_name} eq 'form') {
6344            !!!cp ('t347');            !!!cp ('t347.1');
6345              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6346    
6347              !!!nack ('t347.2');
6348            !!!next-token;            !!!next-token;
6349          }          } elsif ($token->{tag_name} eq 'table') {
6350          redo B;            !!!cp ('t382');
6351        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6352          if (defined $self->{form_element}) {            
6353            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6354            !!!parse-error (type => 'in form:form');  
6355            ## Ignore the token            !!!nack ('t382.1');
6356              !!!next-token;
6357            } elsif ($token->{tag_name} eq 'hr') {
6358              !!!cp ('t386');
6359              pop @{$self->{open_elements}};
6360            
6361              !!!nack ('t386.1');
6362            !!!next-token;            !!!next-token;
           redo B;  
6363          } else {          } else {
6364            ## 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];  
6365            !!!next-token;            !!!next-token;
           redo B;  
6366          }          }
6367        } elsif ($token->{tag_name} eq 'li') {          next B;
6368          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6369          ## has a p element in scope          ## has a p element in scope
6370          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6371            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6372              !!!cp ('t353');              !!!cp ('t353');
6373              !!!back-token;              !!!back-token; # <x>
6374              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6375              redo B;                        line => $token->{line}, column => $token->{column}};
6376            } elsif ({              next B;
6377                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6378              !!!cp ('t354');              !!!cp ('t354');
6379              last INSCOPE;              last INSCOPE;
6380            }            }
# Line 5542  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383          ## Step 1          ## Step 1
6384          my $i = -1;          my $i = -1;
6385          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6386            my $li_or_dtdd = {li => {li => 1},
6387                              dt => {dt => 1, dd => 1},
6388                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6389          LI: {          LI: {
6390            ## Step 2            ## Step 2
6391            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6392              if ($i != -1) {              if ($i != -1) {
6393                !!!cp ('t355');                !!!cp ('t355');
6394                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6395                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6396                                      ->manakai_local_name,
6397                                  token => $token);
6398              } else {              } else {
6399                !!!cp ('t356');                !!!cp ('t356');
6400              }              }
# Line 5559  sub _tree_construction_main ($) { Line 6405  sub _tree_construction_main ($) {
6405            }            }
6406                        
6407            ## Step 3            ## Step 3
6408            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6409                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6410                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6411                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6412                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6413                  not ($node->[1] & DIV_EL)) {
6414              !!!cp ('t358');              !!!cp ('t358');
6415              last LI;              last LI;
6416            }            }
# Line 5575  sub _tree_construction_main ($) { Line 6422  sub _tree_construction_main ($) {
6422            redo LI;            redo LI;
6423          } # LI          } # LI
6424                        
6425          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6426            !!!nack ('t359.1');
6427          !!!next-token;          !!!next-token;
6428          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;  
6429        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6430          ## has a p element in scope          ## has a p element in scope
6431          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6432            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6433              !!!cp ('t367');              !!!cp ('t367');
6434              !!!back-token;              !!!back-token; # <plaintext>
6435              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6436              redo B;                        line => $token->{line}, column => $token->{column}};
6437            } elsif ({              next B;
6438                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6439              !!!cp ('t368');              !!!cp ('t368');
6440              last INSCOPE;              last INSCOPE;
6441            }            }
6442          } # INSCOPE          } # INSCOPE
6443                        
6444          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6445                        
6446          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6447                        
6448            !!!nack ('t368.1');
6449          !!!next-token;          !!!next-token;
6450          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6451        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6452          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6453            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6454            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6455              !!!cp ('t371');              !!!cp ('t371');
6456              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6457                            
6458              !!!back-token;              !!!back-token; # <a>
6459              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6460              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6461                $formatting_end_tag->($token);
6462                            
6463              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6464                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5738  sub _tree_construction_main ($) { Line 6483  sub _tree_construction_main ($) {
6483                        
6484          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6485    
6486          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6487          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6488    
6489            !!!nack ('t374.1');
6490          !!!next-token;          !!!next-token;
6491          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;  
6492        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6493          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6494    
6495          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6496          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6497            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6498            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6499              !!!cp ('t376');              !!!cp ('t376');
6500              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6501              !!!back-token;              !!!back-token; # <nobr>
6502              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6503              redo B;                        line => $token->{line}, column => $token->{column}};
6504            } elsif ({              next B;
6505                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6506              !!!cp ('t377');              !!!cp ('t377');
6507              last INSCOPE;              last INSCOPE;
6508            }            }
6509          } # INSCOPE          } # INSCOPE
6510                    
6511          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6512          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6513                    
6514            !!!nack ('t377.1');
6515          !!!next-token;          !!!next-token;
6516          redo B;          next B;
6517        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6518          ## has a button element in scope          ## has a button element in scope
6519          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6520            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6521            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6522              !!!cp ('t378');              !!!cp ('t378');
6523              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6524              !!!back-token;              !!!back-token; # <button>
6525              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6526              redo B;                        line => $token->{line}, column => $token->{column}};
6527            } elsif ({              next B;
6528                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6529              !!!cp ('t379');              !!!cp ('t379');
6530              last INSCOPE;              last INSCOPE;
6531            }            }
# Line 5803  sub _tree_construction_main ($) { Line 6533  sub _tree_construction_main ($) {
6533                        
6534          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6535                        
6536          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6537          push @$active_formatting_elements, ['#marker', ''];  
6538            ## TODO: associate with $self->{form_element} if defined
6539    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6540          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6541            
6542          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6543          !!!next-token;          !!!next-token;
6544          redo B;          next B;
6545        } elsif ({        } elsif ({
6546                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6547                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6548                  image => 1,                  noembed => 1,
6549                    noframes => 1,
6550                    noscript => 0, ## TODO: 1 if scripting is enabled
6551                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6552          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6553            !!!cp ('t384');            !!!cp ('t381');
6554            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6555          } else {          } else {
6556            !!!cp ('t385');            !!!cp ('t399');
6557          }          }
6558            ## NOTE: There is an "as if in body" code clone.
6559          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6560          $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;  
6561        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6562          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6563                    
6564          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6565            !!!cp ('t389');            !!!cp ('t389');
6566            ## Ignore the token            ## Ignore the token
6567              !!!nack ('t389'); ## NOTE: Not acknowledged.
6568            !!!next-token;            !!!next-token;
6569            redo B;            next B;
6570          } else {          } else {
6571            my $at = $token->{attributes};            my $at = $token->{attributes};
6572            my $form_attrs;            my $form_attrs;
# Line 5917  sub _tree_construction_main ($) { Line 6577  sub _tree_construction_main ($) {
6577            delete $at->{prompt};            delete $at->{prompt};
6578            my @tokens = (            my @tokens = (
6579                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6580                           attributes => $form_attrs},                           attributes => $form_attrs,
6581                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6582                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6583                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6584                            {type => START_TAG_TOKEN, tag_name => 'p',
6585                             line => $token->{line}, column => $token->{column}},
6586                            {type => START_TAG_TOKEN, tag_name => 'label',
6587                             line => $token->{line}, column => $token->{column}},
6588                         );                         );
6589            if ($prompt_attr) {            if ($prompt_attr) {
6590              !!!cp ('t390');              !!!cp ('t390');
6591              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6592                               #line => $token->{line}, column => $token->{column},
6593                              };
6594            } else {            } else {
6595              !!!cp ('t391');              !!!cp ('t391');
6596              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6597                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6598                               #line => $token->{line}, column => $token->{column},
6599                              }; # SHOULD
6600              ## TODO: make this configurable              ## TODO: make this configurable
6601            }            }
6602            push @tokens,            push @tokens,
6603                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6604                             line => $token->{line}, column => $token->{column}},
6605                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6606                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6607                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6608                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6609                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6610            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6611                             line => $token->{line}, column => $token->{column}},
6612                            {type => END_TAG_TOKEN, tag_name => 'form',
6613                             line => $token->{line}, column => $token->{column}};
6614              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6615            !!!back-token (@tokens);            !!!back-token (@tokens);
6616            redo B;            !!!next-token;
6617              next B;
6618          }          }
6619        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6620          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6621          my $el;          my $el;
6622          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6623                    
6624          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6625          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5954  sub _tree_construction_main ($) { Line 6628  sub _tree_construction_main ($) {
6628          $insert->($el);          $insert->($el);
6629                    
6630          my $text = '';          my $text = '';
6631            !!!nack ('t392.1');
6632          !!!next-token;          !!!next-token;
6633          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6634            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5984  sub _tree_construction_main ($) { Line 6659  sub _tree_construction_main ($) {
6659            ## Ignore the token            ## Ignore the token
6660          } else {          } else {
6661            !!!cp ('t398');            !!!cp ('t398');
6662            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6663          }          }
6664          !!!next-token;          !!!next-token;
6665          redo B;          next B;
6666        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6667                  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');  
6668          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6669    
6670            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6671    
6672            ## "adjust foreign attributes" - done in insert-element-f
6673                    
6674          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6675                    
6676          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6677              pop @{$self->{open_elements}};
6678              !!!ack ('t398.1');
6679            } else {
6680              !!!cp ('t398.2');
6681              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6682              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6683              ## mode, "in body" (not "in foreign content") secondary insertion
6684              ## mode, maybe.
6685            }
6686    
6687          !!!next-token;          !!!next-token;
6688          redo B;          next B;
6689        } elsif ({        } elsif ({
6690                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6691                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6014  sub _tree_construction_main ($) { Line 6693  sub _tree_construction_main ($) {
6693                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6694                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6695          !!!cp ('t401');          !!!cp ('t401');
6696          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6697          ## Ignore the token          ## Ignore the token
6698            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6699          !!!next-token;          !!!next-token;
6700          redo B;          next B;
6701                    
6702          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6703        } else {        } else {
6704          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6705              !!!cp ('t384');
6706              !!!parse-error (type => 'image', token => $token);
6707              $token->{tag_name} = 'img';
6708            } else {
6709              !!!cp ('t385');
6710            }
6711    
6712            ## NOTE: There is an "as if <br>" code clone.
6713          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6714                    
6715          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6716    
6717            if ({
6718                 applet => 1, marquee => 1, object => 1,
6719                }->{$token->{tag_name}}) {
6720              !!!cp ('t380');
6721              push @$active_formatting_elements, ['#marker', ''];
6722              !!!nack ('t380.1');
6723            } elsif ({
6724                      b => 1, big => 1, em => 1, font => 1, i => 1,
6725                      s => 1, small => 1, strile => 1,
6726                      strong => 1, tt => 1, u => 1,
6727                     }->{$token->{tag_name}}) {
6728              !!!cp ('t375');
6729              push @$active_formatting_elements, $self->{open_elements}->[-1];
6730              !!!nack ('t375.1');
6731            } elsif ($token->{tag_name} eq 'input') {
6732              !!!cp ('t388');
6733              ## TODO: associate with $self->{form_element} if defined
6734              pop @{$self->{open_elements}};
6735              !!!ack ('t388.2');
6736            } elsif ({
6737                      area => 1, basefont => 1, bgsound => 1, br => 1,
6738                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6739                      #image => 1,
6740                     }->{$token->{tag_name}}) {
6741              !!!cp ('t388.1');
6742              pop @{$self->{open_elements}};
6743              !!!ack ('t388.3');
6744            } elsif ($token->{tag_name} eq 'select') {
6745              ## TODO: associate with $self->{form_element} if defined
6746            
6747              if ($self->{insertion_mode} & TABLE_IMS or
6748                  $self->{insertion_mode} & BODY_TABLE_IMS or
6749                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6750                !!!cp ('t400.1');
6751                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6752              } else {
6753                !!!cp ('t400.2');
6754                $self->{insertion_mode} = IN_SELECT_IM;
6755              }
6756              !!!nack ('t400.3');
6757            } else {
6758              !!!nack ('t402');
6759            }
6760                    
6761          !!!next-token;          !!!next-token;
6762          redo B;          next B;
6763        }        }
6764      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6765        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6766          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6767              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6768            for (@{$self->{open_elements}}) {          INSCOPE: {
6769              unless ({            for (reverse @{$self->{open_elements}}) {
6770                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6771                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6772                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6773                      }->{$_->[1]}) {                last INSCOPE;
6774                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6775                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6776              } else {                last;
               !!!cp ('t404');  
6777              }              }
6778            }            }
6779    
6780            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6781            !!!next-token;                            value => $token->{tag_name}, token => $token);
6782            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6783            !!!next-token;            !!!next-token;
6784            redo B;            next B;
6785            } # INSCOPE
6786    
6787            for (@{$self->{open_elements}}) {
6788              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6789                !!!cp ('t403');
6790                !!!parse-error (type => 'not closed',
6791                                value => $_->[0]->manakai_local_name,
6792                                token => $token);
6793                last;
6794              } else {
6795                !!!cp ('t404');
6796              }
6797          }          }
6798    
6799            $self->{insertion_mode} = AFTER_BODY_IM;
6800            !!!next-token;
6801            next B;
6802        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6803          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
6804            ## up-to-date, though it has same effect as speced.
6805            if (@{$self->{open_elements}} > 1 and
6806                $self->{open_elements}->[1]->[1] & BODY_EL) {
6807            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6808            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6809              !!!cp ('t406');              !!!cp ('t406');
6810              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6811                                value => $self->{open_elements}->[1]->[0]
6812                                    ->manakai_local_name,
6813                                token => $token);
6814            } else {            } else {
6815              !!!cp ('t407');              !!!cp ('t407');
6816            }            }
6817            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6818            ## reprocess            ## reprocess
6819            redo B;            next B;
6820          } else {          } else {
6821            !!!cp ('t408');            !!!cp ('t408');
6822            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6823            ## Ignore the token            ## Ignore the token
6824            !!!next-token;            !!!next-token;
6825            redo B;            next B;
6826          }          }
6827        } elsif ({        } elsif ({
6828                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6829                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6830                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6831                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6832                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6833                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6834          ## has an element in scope          ## has an element in scope
6835          my $i;          my $i;
6836          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6838            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6839              !!!cp ('t410');              !!!cp ('t410');
6840              $i = $_;              $i = $_;
6841              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6842            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6843              !!!cp ('t411');              !!!cp ('t411');
6844              last INSCOPE;              last INSCOPE;
6845            }            }
6846          } # INSCOPE          } # INSCOPE
6847            
6848          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6849            if (defined $i) {            !!!cp ('t413');
6850              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851            } else {
6852              ## Step 1. generate implied end tags
6853              while ({
6854                      dd => ($token->{tag_name} ne 'dd'),
6855                      dt => ($token->{tag_name} ne 'dt'),
6856                      li => ($token->{tag_name} ne 'li'),
6857                      p => 1,
6858                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6859                !!!cp ('t409');
6860                pop @{$self->{open_elements}};
6861              }
6862    
6863              ## Step 2.
6864              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6865                      ne $token->{tag_name}) {
6866              !!!cp ('t412');              !!!cp ('t412');
6867              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6868                                value => $self->{open_elements}->[-1]->[0]
6869                                    ->manakai_local_name,
6870                                token => $token);
6871            } else {            } else {
6872              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6873            }            }
6874          }  
6875                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6876            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6877          } elsif ($token->{tag_name} eq 'p') {  
6878            !!!cp ('t415');            ## Step 4.
6879            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6880            my $el;                if {
6881            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6882            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6883          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6884          !!!next-token;          !!!next-token;
6885          redo B;          next B;
6886        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6887            undef $self->{form_element};
6888    
6889          ## has an element in scope          ## has an element in scope
6890            my $i;
6891          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6892            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6893            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
   
                  ## NOTE: The following elements never appear here, maybe.  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6894              !!!cp ('t418');              !!!cp ('t418');
6895                $i = $_;
6896              last INSCOPE;              last INSCOPE;
6897            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6898              !!!cp ('t419');              !!!cp ('t419');
6899              last INSCOPE;              last INSCOPE;
6900            }            }
6901          } # INSCOPE          } # INSCOPE
6902            
6903          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 {  
6904            !!!cp ('t421');            !!!cp ('t421');
6905            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6906            } else {
6907              ## Step 1. generate implied end tags
6908              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6909                !!!cp ('t417');
6910                pop @{$self->{open_elements}};
6911              }
6912              
6913              ## Step 2.
6914              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6915                      ne $token->{tag_name}) {
6916                !!!cp ('t417.1');
6917                !!!parse-error (type => 'not closed',
6918                                value => $self->{open_elements}->[-1]->[0]
6919                                    ->manakai_local_name,
6920                                token => $token);
6921              } else {
6922                !!!cp ('t420');
6923              }  
6924              
6925              ## Step 3.
6926              splice @{$self->{open_elements}}, $i;
6927          }          }
6928    
         undef $self->{form_element};  
6929          !!!next-token;          !!!next-token;
6930          redo B;          next B;
6931        } elsif ({        } elsif ({
6932                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6933                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6193  sub _tree_construction_main ($) { Line 6935  sub _tree_construction_main ($) {
6935          my $i;          my $i;
6936          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6937            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6938            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
6939              !!!cp ('t423');              !!!cp ('t423');
6940              $i = $_;              $i = $_;
6941              last INSCOPE;              last INSCOPE;
6942            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6943              !!!cp ('t424');              !!!cp ('t424');
6944              last INSCOPE;              last INSCOPE;
6945            }            }
6946          } # INSCOPE          } # INSCOPE
6947    
6948            unless (defined $i) { # has an element in scope
6949              !!!cp ('t425.1');
6950              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6951            } else {
6952              ## Step 1. generate implied end tags
6953              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6954                !!!cp ('t422');
6955                pop @{$self->{open_elements}};
6956              }
6957              
6958              ## Step 2.
6959              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6960                      ne $token->{tag_name}) {
6961                !!!cp ('t425');
6962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6963              } else {
6964                !!!cp ('t426');
6965              }
6966    
6967              ## Step 3.
6968              splice @{$self->{open_elements}}, $i;
6969            }
6970                    
6971          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
6972            !!!cp ('t425');          next B;
6973            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
6974            ## has an element in scope
6975            my $i;
6976            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977              my $node = $self->{open_elements}->[$_];
6978              if ($node->[1] & P_EL) {
6979                !!!cp ('t410.1');
6980                $i = $_;
6981                last INSCOPE;
6982              } elsif ($node->[1] & SCOPING_EL) {
6983                !!!cp ('t411.1');
6984                last INSCOPE;
6985              }
6986            } # INSCOPE
6987    
6988            if (defined $i) {
6989              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6990                      ne $token->{tag_name}) {
6991                !!!cp ('t412.1');
6992                !!!parse-error (type => 'not closed',
6993                                value => $self->{open_elements}->[-1]->[0]
6994                                    ->manakai_local_name,
6995                                token => $token);
6996              } else {
6997                !!!cp ('t414.1');
6998              }
6999    
7000              splice @{$self->{open_elements}}, $i;
7001          } else {          } else {
7002            !!!cp ('t426');            !!!cp ('t413.1');
7003              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004    
7005              !!!cp ('t415.1');
7006              ## As if <p>, then reprocess the current token
7007              my $el;
7008              !!!create-element ($el, $HTML_NS, 'p',, $token);
7009              $insert->($el);
7010              ## NOTE: Not inserted into |$self->{open_elements}|.
7011          }          }
7012            
         splice @{$self->{open_elements}}, $i if defined $i;  
7013          !!!next-token;          !!!next-token;
7014          redo B;          next B;
7015        } elsif ({        } elsif ({
7016                  a => 1,                  a => 1,
7017                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6238  sub _tree_construction_main ($) { Line 7019  sub _tree_construction_main ($) {
7019                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7020                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7021          !!!cp ('t427');          !!!cp ('t427');
7022          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7023          redo B;          next B;
7024        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7025          !!!cp ('t428');          !!!cp ('t428');
7026          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7027    
7028          ## As if <br>          ## As if <br>
7029          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7030                    
7031          my $el;          my $el;
7032          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7033          $insert->($el);          $insert->($el);
7034                    
7035          ## Ignore the token.          ## Ignore the token.
7036          !!!next-token;          !!!next-token;
7037          redo B;          next B;
7038        } elsif ({        } elsif ({
7039                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7040                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6267  sub _tree_construction_main ($) { Line 7048  sub _tree_construction_main ($) {
7048                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7049                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7050          !!!cp ('t429');          !!!cp ('t429');
7051          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7052          ## Ignore the token          ## Ignore the token
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055                    
7056          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7057                    
# Line 6281  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062    
7063          ## Step 2          ## Step 2
7064          S2: {          S2: {
7065            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7066              ## Step 1              ## Step 1
7067              ## generate implied end tags              ## generate implied end tags
7068              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7069                !!!cp ('t430');                !!!cp ('t430');
7070                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7071                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7072              }              }
7073                    
7074              ## Step 2              ## Step 2
7075              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7076                        ne $token->{tag_name}) {
7077                !!!cp ('t431');                !!!cp ('t431');
7078                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7079                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7080                                  value => $self->{open_elements}->[-1]->[0]
7081                                      ->manakai_local_name,
7082                                  token => $token);
7083              } else {              } else {
7084                !!!cp ('t432');                !!!cp ('t432');
7085              }              }
# Line 6313  sub _tree_construction_main ($) { Line 7091  sub _tree_construction_main ($) {
7091              last S2;              last S2;
7092            } else {            } else {
7093              ## Step 3              ## Step 3
7094              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7095                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7096                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7097                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7098                !!!cp ('t433');                !!!cp ('t433');
7099                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7100                ## Ignore the token                ## Ignore the token
7101                !!!next-token;                !!!next-token;
7102                last S2;                last S2;
# Line 6334  sub _tree_construction_main ($) { Line 7112  sub _tree_construction_main ($) {
7112            ## Step 5;            ## Step 5;
7113            redo S2;            redo S2;
7114          } # S2          } # S2
7115          redo B;          next B;
7116        }        }
7117      }      }
7118      redo B;      next B;
7119      } continue { # B
7120        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7121          ## NOTE: The code below is executed in cases where it does not have
7122          ## to be, but it it is harmless even in those cases.
7123          ## has an element in scope
7124          INSCOPE: {
7125            for (reverse 0..$#{$self->{open_elements}}) {
7126              my $node = $self->{open_elements}->[$_];
7127              if ($node->[1] & FOREIGN_EL) {
7128                last INSCOPE;
7129              } elsif ($node->[1] & SCOPING_EL) {
7130                last;
7131              }
7132            }
7133            
7134            ## NOTE: No foreign element in scope.
7135            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7136          } # INSCOPE
7137        }
7138    } # B    } # B
7139    
7140    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6383  sub set_inner_html ($$$) { Line 7180  sub set_inner_html ($$$) {
7180    
7181      ## Step 8 # MUST      ## Step 8 # MUST
7182      my $i = 0;      my $i = 0;
7183      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7184      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7185      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7186        my $self = shift;        my $self = shift;
7187    
# Line 6393  sub set_inner_html ($$$) { Line 7190  sub set_inner_html ($$$) {
7190    
7191        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7192        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7193        $column++;  
7194          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7195          $p->{column}++;
7196    
7197        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7198          $line++;          $p->{line}++;
7199          $column = 0;          $p->{column} = 0;
7200          !!!cp ('i1');          !!!cp ('i1');
7201        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7202          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7203          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7204          $line++;          $p->{line}++;
7205          $column = 0;          $p->{column} = 0;
7206          !!!cp ('i2');          !!!cp ('i2');
7207        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7208          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6412  sub set_inner_html ($$$) { Line 7211  sub set_inner_html ($$$) {
7211          !!!cp ('i4');          !!!cp ('i4');
7212          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7213          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7214          } elsif ($self->{next_char} <= 0x0008 or
7215                   (0x000E <= $self->{next_char} and
7216                    $self->{next_char} <= 0x001F) or
7217                   (0x007F <= $self->{next_char} and
7218                    $self->{next_char} <= 0x009F) or
7219                   (0xD800 <= $self->{next_char} and
7220                    $self->{next_char} <= 0xDFFF) or
7221                   (0xFDD0 <= $self->{next_char} and
7222                    $self->{next_char} <= 0xFDDF) or
7223                   {
7224                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7225                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7226                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7227                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7228                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7229                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7230                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7231                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7232                    0x10FFFE => 1, 0x10FFFF => 1,
7233                   }->{$self->{next_char}}) {
7234            !!!cp ('i4.1');
7235            !!!parse-error (type => 'control char', level => $self->{must_level});
7236    ## TODO: error type documentation
7237        }        }
7238      };      };
7239      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6419  sub set_inner_html ($$$) { Line 7241  sub set_inner_html ($$$) {
7241            
7242      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7243        my (%opt) = @_;        my (%opt) = @_;
7244        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7245          my $column = $opt{column};
7246          if (defined $opt{token} and defined $opt{token}->{line}) {
7247            $line = $opt{token}->{line};
7248            $column = $opt{token}->{column};
7249          }
7250          warn "Parse error ($opt{type}) at line $line column $column\n";
7251      };      };
7252      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7253        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7254      };      };
7255            
7256      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6446  sub set_inner_html ($$$) { Line 7274  sub set_inner_html ($$$) {
7274          unless defined $p->{content_model};          unless defined $p->{content_model};
7275          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7276    
7277      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7278          ## TODO: Foreign element OK?
7279    
7280      ## Step 3      ## Step 3
7281      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6456  sub set_inner_html ($$$) { Line 7285  sub set_inner_html ($$$) {
7285      $doc->append_child ($root);      $doc->append_child ($root);
7286    
7287      ## Step 5 # MUST      ## Step 5 # MUST
7288      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7289    
7290      undef $p->{head_element};      undef $p->{head_element};
7291    
# Line 6502  sub set_inner_html ($$$) { Line 7331  sub set_inner_html ($$$) {
7331      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7332    
7333      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7334    
7335        delete $p->{parse_error}; # delete loop
7336    } else {    } else {
7337      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";
7338    }    }

Legend:
Removed from v.1.84  
changed lines
  Added in v.1.134

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24