/[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.104 by wakaba, Sun Mar 9 08:47:33 2008 UTC revision 1.135 by wakaba, Sat May 17 07:31:49 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 = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
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 162  sub parse_byte_string ($$$$;$) { Line 528  sub parse_byte_string ($$$$;$) {
528  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
529  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
530    
531  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
532      my $self = shift;
533      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
534      return $self->parse_char_stream ($input, @_[1..$#_]);
535    } # parse_char_string
536    *parse_string = \&parse_char_string;
537    
538  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
539    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
540    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
541    $self->{document} = $_[1];    $self->{document} = $_[1];
542    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
543    
# Line 177  sub parse_string ($$$;$) { Line 548  sub parse_string ($$$;$) {
548        if defined $self->{input_encoding};        if defined $self->{input_encoding};
549    
550    my $i = 0;    my $i = 0;
551    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
552    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
553    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
554      my $self = shift;      my $self = shift;
555    
556      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
557      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
558    
559      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = $input->getc;
560      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
561      $column++;      $self->{next_char} = ord $char;
562    
563        ($self->{line_prev}, $self->{column_prev})
564            = ($self->{line}, $self->{column});
565        $self->{column}++;
566            
567      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
568        $line++;        !!!cp ('j1');
569        $column = 0;        $self->{line}++;
570          $self->{column} = 0;
571      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
572        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
573          my $next = $input->getc;
574          if ($next ne "\x0A") {
575            $input->ungetc ($next);
576          }
577        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
578        $line++;        $self->{line}++;
579        $column = 0;        $self->{column} = 0;
580      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
581          !!!cp ('j3');
582        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
583      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
584          !!!cp ('j4');
585        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
586        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
587        } elsif ($self->{next_char} <= 0x0008 or
588                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
589                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
590                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
591                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
592                 {
593                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
594                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
595                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
596                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
597                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
598                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
599                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
600                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
601                  0x10FFFE => 1, 0x10FFFF => 1,
602                 }->{$self->{next_char}}) {
603          !!!cp ('j5');
604          !!!parse-error (type => 'control char', level => $self->{must_level});
605    ## TODO: error type documentation
606      }      }
607    };    };
608    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 610  sub parse_string ($$$;$) {
610    
611    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
612      my (%opt) = @_;      my (%opt) = @_;
613      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
614        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
615        warn "Parse error ($opt{type}) at line $line column $column\n";
616    };    };
617    $self->{parse_error} = sub {    $self->{parse_error} = sub {
618      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
619    };    };
620    
621    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 623  sub parse_string ($$$;$) {
623    $self->_construct_tree;    $self->_construct_tree;
624    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
625    
626      delete $self->{parse_error}; # remove loop
627    
628    return $self->{document};    return $self->{document};
629  } # parse_string  } # parse_char_stream
630    
631  sub new ($) {  sub new ($) {
632    my $class = shift;    my $class = shift;
633    my $self = bless {}, $class;    my $self = bless {
634        must_level => 'm',
635        should_level => 's',
636        good_level => 'w',
637        warn_level => 'w',
638        info_level => 'i',
639        unsupported_level => 'u',
640      }, $class;
641    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
642      $self->{next_char} = -1;      $self->{next_char} = -1;
643    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 699  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
699  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
700  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
701  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
702    sub SELF_CLOSING_START_TAG_STATE () { 34 }
703    sub CDATA_BLOCK_STATE () { 35 }
704    
705  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
706  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 718  sub ROW_IMS ()        { 0b10000000 }
718  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
719  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
720  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
721    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
722        ## NOTE: "in foreign content" insertion mode is special; it is combined
723        ## with the secondary insertion mode.  In this parser, they are stored
724        ## together in the bit-or'ed form.
725    
726  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
727    
# Line 340  sub _initialize_tokenizer ($) { Line 758  sub _initialize_tokenizer ($) {
758    undef $self->{current_attribute};    undef $self->{current_attribute};
759    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
760    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
761      delete $self->{self_closing};
762    $self->{char} = [];    $self->{char} = [];
763    # $self->{next_char}    # $self->{next_char}
764    !!!next-input-character;    !!!next-input-character;
# Line 360  sub _initialize_tokenizer ($) { Line 779  sub _initialize_tokenizer ($) {
779  ##        ->{value}  ##        ->{value}
780  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
781  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
782    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
783    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
784    ##     while the token is pushed back to the stack.
785    
786    ## ISSUE: "When a DOCTYPE token is created, its
787    ## <i>self-closing flag</i> must be unset (its other state is that it
788    ## be set), and its attributes list must be empty.": Wrong subject?
789    
790  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
791    
# Line 386  sub _initialize_tokenizer ($) { Line 812  sub _initialize_tokenizer ($) {
812    
813  sub _get_next_token ($) {  sub _get_next_token ($) {
814    my $self = shift;    my $self = shift;
815    
816      if ($self->{self_closing}) {
817        !!!parse-error (type => 'nestc', token => $self->{current_token});
818        ## NOTE: The |self_closing| flag is only set by start tag token.
819        ## In addition, when a start tag token is emitted, it is always set to
820        ## |current_token|.
821        delete $self->{self_closing};
822      }
823    
824    if (@{$self->{token}}) {    if (@{$self->{token}}) {
825        $self->{self_closing} = $self->{token}->[0]->{self_closing};
826      return shift @{$self->{token}};      return shift @{$self->{token}};
827    }    }
828    
# Line 449  sub _get_next_token ($) { Line 885  sub _get_next_token ($) {
885          #          #
886        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
887          !!!cp (11);          !!!cp (11);
888          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
889                      line => $self->{line}, column => $self->{column}});
890          last A; ## TODO: ok?          last A; ## TODO: ok?
891        } else {        } else {
892          !!!cp (12);          !!!cp (12);
893        }        }
894        # Anything else        # Anything else
895        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
896                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
897                       line => $self->{line}, column => $self->{column},
898                      };
899        ## Stay in the data state        ## Stay in the data state
900        !!!next-input-character;        !!!next-input-character;
901    
# Line 465  sub _get_next_token ($) { Line 904  sub _get_next_token ($) {
904        redo A;        redo A;
905      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
906        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
907    
908          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
909                
910        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
911    
# Line 473  sub _get_next_token ($) { Line 914  sub _get_next_token ($) {
914    
915        unless (defined $token) {        unless (defined $token) {
916          !!!cp (13);          !!!cp (13);
917          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
918                      line => $l, column => $c,
919                     });
920        } else {        } else {
921          !!!cp (14);          !!!cp (14);
922          !!!emit ($token);          !!!emit ($token);
# Line 492  sub _get_next_token ($) { Line 935  sub _get_next_token ($) {
935            ## reconsume            ## reconsume
936            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
937    
938            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
939                        line => $self->{line_prev},
940                        column => $self->{column_prev},
941                       });
942    
943            redo A;            redo A;
944          }          }
# Line 512  sub _get_next_token ($) { Line 958  sub _get_next_token ($) {
958            !!!cp (19);            !!!cp (19);
959            $self->{current_token}            $self->{current_token}
960              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
961                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
962                   line => $self->{line_prev},
963                   column => $self->{column_prev}};
964            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
965            !!!next-input-character;            !!!next-input-character;
966            redo A;            redo A;
# Line 520  sub _get_next_token ($) { Line 968  sub _get_next_token ($) {
968                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
969            !!!cp (20);            !!!cp (20);
970            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
971                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
972                                        line => $self->{line_prev},
973                                        column => $self->{column_prev}};
974            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
975            !!!next-input-character;            !!!next-input-character;
976            redo A;            redo A;
977          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
978            !!!cp (21);            !!!cp (21);
979            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
980                              line => $self->{line_prev},
981                              column => $self->{column_prev});
982            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
983            !!!next-input-character;            !!!next-input-character;
984    
985            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
986                        line => $self->{line_prev},
987                        column => $self->{column_prev},
988                       });
989    
990            redo A;            redo A;
991          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
992            !!!cp (22);            !!!cp (22);
993            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
994                              line => $self->{line_prev},
995                              column => $self->{column_prev});
996            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
997              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
998                                        line => $self->{line_prev},
999                                        column => $self->{column_prev},
1000                                       };
1001            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1002            redo A;            redo A;
1003          } else {          } else {
# Line 545  sub _get_next_token ($) { Line 1006  sub _get_next_token ($) {
1006            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1007            ## reconsume            ## reconsume
1008    
1009            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1010                        line => $self->{line_prev},
1011                        column => $self->{column_prev},
1012                       });
1013    
1014            redo A;            redo A;
1015          }          }
# Line 553  sub _get_next_token ($) { Line 1017  sub _get_next_token ($) {
1017          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1018        }        }
1019      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1020          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1021        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1022          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1023    
1024            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1025            my @next_char;            my @next_char;
1026            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 571  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1038                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1039    
1040                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1041                            line => $l, column => $c,
1042                           });
1043        
1044                redo A;                redo A;
1045              }              }
# Line 590  sub _get_next_token ($) { Line 1058  sub _get_next_token ($) {
1058              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1059              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1060              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1061              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1062                          line => $l, column => $c,
1063                         });
1064              redo A;              redo A;
1065            } else {            } else {
1066              !!!cp (27);              !!!cp (27);
# Line 603  sub _get_next_token ($) { Line 1073  sub _get_next_token ($) {
1073            !!!cp (28);            !!!cp (28);
1074            # next-input-character is already done            # next-input-character is already done
1075            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1076            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1077                        line => $l, column => $c,
1078                       });
1079            redo A;            redo A;
1080          }          }
1081        }        }
# Line 611  sub _get_next_token ($) { Line 1083  sub _get_next_token ($) {
1083        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1084            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1085          !!!cp (29);          !!!cp (29);
1086          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1087                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1088                   tag_name => chr ($self->{next_char} + 0x0020),
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;
# Line 620  sub _get_next_token ($) { Line 1094  sub _get_next_token ($) {
1094                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1095          !!!cp (30);          !!!cp (30);
1096          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1097                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1098                                      line => $l, column => $c};
1099          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1100          !!!next-input-character;          !!!next-input-character;
1101          redo A;          redo A;
1102        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1103          !!!cp (31);          !!!cp (31);
1104          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1105                            line => $self->{line_prev}, ## "<" in "</>"
1106                            column => $self->{column_prev} - 1);
1107          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1108          !!!next-input-character;          !!!next-input-character;
1109          redo A;          redo A;
# Line 636  sub _get_next_token ($) { Line 1113  sub _get_next_token ($) {
1113          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1114          # reconsume          # reconsume
1115    
1116          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1117                      line => $l, column => $c,
1118                     });
1119    
1120          redo A;          redo A;
1121        } else {        } else {
1122          !!!cp (33);          !!!cp (33);
1123          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1124          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1125            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1126                                      line => $self->{line_prev}, # "<" of "</"
1127                                      column => $self->{column_prev} - 1,
1128                                     };
1129          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1130          redo A;          redo A;
1131        }        }
# Line 659  sub _get_next_token ($) { Line 1142  sub _get_next_token ($) {
1142        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1143          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1144            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1145            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1146          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1147            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 692  sub _get_next_token ($) { Line 1173  sub _get_next_token ($) {
1173          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1174          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1176            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1177          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1178            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 714  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193    
1194          redo A;          redo A;
1195        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1196            !!!cp (42);
1197            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1198          !!!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  
1199          redo A;          redo A;
1200        } else {        } else {
1201          !!!cp (44);          !!!cp (44);
# Line 749  sub _get_next_token ($) { Line 1218  sub _get_next_token ($) {
1218        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1219          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1220            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1221            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1222          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1223            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 772  sub _get_next_token ($) { Line 1239  sub _get_next_token ($) {
1239        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1240                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1241          !!!cp (49);          !!!cp (49);
1242          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1243                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1244                   value => '',
1245                   line => $self->{line}, column => $self->{column}};
1246          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1250            !!!cp (50);
1251            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1252          !!!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  
1253          redo A;          redo A;
1254        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1255          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1256          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1257            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1258            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1259          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1260            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 827  sub _get_next_token ($) { Line 1284  sub _get_next_token ($) {
1284          } else {          } else {
1285            !!!cp (56);            !!!cp (56);
1286          }          }
1287          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1288                                value => ''};              = {name => chr ($self->{next_char}),
1289                   value => '',
1290                   line => $self->{line}, column => $self->{column}};
1291          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
# Line 838  sub _get_next_token ($) { Line 1297  sub _get_next_token ($) {
1297          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1298              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1299            !!!cp (57);            !!!cp (57);
1300            !!!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});
1301            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1302          } else {          } else {
1303            !!!cp (58);            !!!cp (58);
# Line 867  sub _get_next_token ($) { Line 1326  sub _get_next_token ($) {
1326          $before_leave->();          $before_leave->();
1327          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1328            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1329            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1330          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1331            !!!cp (62);            !!!cp (62);
# Line 893  sub _get_next_token ($) { Line 1350  sub _get_next_token ($) {
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1353            !!!cp (64);
1354          $before_leave->();          $before_leave->();
1355            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1356          !!!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  
1357          redo A;          redo A;
1358        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1359          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1360          $before_leave->();          $before_leave->();
1361          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1362            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1363            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1364          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1365            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 965  sub _get_next_token ($) { Line 1410  sub _get_next_token ($) {
1410        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1411          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1412            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1413            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1414          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1415            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 989  sub _get_next_token ($) { Line 1432  sub _get_next_token ($) {
1432        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1433                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1434          !!!cp (76);          !!!cp (76);
1435          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1436                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1437                   value => '',
1438                   line => $self->{line}, column => $self->{column}};
1439          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1443            !!!cp (77);
1444            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1445          !!!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  
1446          redo A;          redo A;
1447        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1448          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1449          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1037  sub _get_next_token ($) { Line 1469  sub _get_next_token ($) {
1469          redo A;          redo A;
1470        } else {        } else {
1471          !!!cp (82);          !!!cp (82);
1472          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1473                                value => ''};              = {name => chr ($self->{next_char}),
1474                   value => '',
1475                   line => $self->{line}, column => $self->{column}};
1476          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1477          !!!next-input-character;          !!!next-input-character;
1478          redo A;                  redo A;        
# Line 1071  sub _get_next_token ($) { Line 1505  sub _get_next_token ($) {
1505        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1506          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1507            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1509          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1510            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1096  sub _get_next_token ($) { Line 1528  sub _get_next_token ($) {
1528          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1529          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1530            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1531            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1532          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1533            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1145  sub _get_next_token ($) { Line 1575  sub _get_next_token ($) {
1575          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1576          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1577            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1578            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1579          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1580            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1617  sub _get_next_token ($) {
1617          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1618          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1619            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1620            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1621          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1622            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1236  sub _get_next_token ($) { Line 1662  sub _get_next_token ($) {
1662        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1663          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1664            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1665            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1666          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1667            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1261  sub _get_next_token ($) { Line 1685  sub _get_next_token ($) {
1685          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1686          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1687            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1689          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1690            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1333  sub _get_next_token ($) { Line 1755  sub _get_next_token ($) {
1755        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1756          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1757            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1758            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1759          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1760            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1355  sub _get_next_token ($) { Line 1775  sub _get_next_token ($) {
1775    
1776          redo A;          redo A;
1777        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1778            !!!cp (122);
1779            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1780          !!!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  
1781          redo A;          redo A;
1782        } else {        } else {
1783          !!!cp (124);          !!!cp ('124.1');
1784          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1785          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1786          ## reconsume          ## reconsume
1787          redo A;          redo A;
1788        }        }
1789        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1790          if ($self->{next_char} == 0x003E) { # >
1791            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1792              !!!cp ('124.2');
1793              !!!parse-error (type => 'nestc', token => $self->{current_token});
1794              ## TODO: Different type than slash in start tag
1795              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1796              if ($self->{current_token}->{attributes}) {
1797                !!!cp ('124.4');
1798                !!!parse-error (type => 'end tag attribute');
1799              } else {
1800                !!!cp ('124.5');
1801              }
1802              ## TODO: Test |<title></title/>|
1803            } else {
1804              !!!cp ('124.3');
1805              $self->{self_closing} = 1;
1806            }
1807    
1808            $self->{state} = DATA_STATE;
1809            !!!next-input-character;
1810    
1811            !!!emit ($self->{current_token}); # start tag or end tag
1812    
1813            redo A;
1814          } else {
1815            !!!cp ('124.4');
1816            !!!parse-error (type => 'nestc');
1817            ## TODO: This error type is wrong.
1818            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1819            ## Reconsume.
1820            redo A;
1821          }
1822      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1823        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1824                
1825        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1826          #my $token = {type => COMMENT_TOKEN, data => ''};
1827    
1828        BC: {        BC: {
1829          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1387  sub _get_next_token ($) { Line 1831  sub _get_next_token ($) {
1831            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1832            !!!next-input-character;            !!!next-input-character;
1833    
1834            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1835    
1836            redo A;            redo A;
1837          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1395  sub _get_next_token ($) { Line 1839  sub _get_next_token ($) {
1839            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1840            ## reconsume            ## reconsume
1841    
1842            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1843    
1844            redo A;            redo A;
1845          } else {          } else {
1846            !!!cp (126);            !!!cp (126);
1847            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1848            !!!next-input-character;            !!!next-input-character;
1849            redo BC;            redo BC;
1850          }          }
# Line 1410  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1855        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1856    
1857          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1858    
1859        my @next_char;        my @next_char;
1860        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1861                
# Line 1418  sub _get_next_token ($) { Line 1864  sub _get_next_token ($) {
1864          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1865          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1866            !!!cp (127);            !!!cp (127);
1867            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1868                                        line => $l, column => $c,
1869                                       };
1870            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1871            !!!next-input-character;            !!!next-input-character;
1872            redo A;            redo A;
# Line 1454  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902                      !!!cp (129);                      !!!cp (129);
1903                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1904                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1905                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1906                                                  quirks => 1,
1907                                                  line => $l, column => $c,
1908                                                 };
1909                      !!!next-input-character;                      !!!next-input-character;
1910                      redo A;                      redo A;
1911                    } else {                    } else {
# Line 1474  sub _get_next_token ($) { Line 1926  sub _get_next_token ($) {
1926          } else {          } else {
1927            !!!cp (135);            !!!cp (135);
1928          }          }
1929          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1930                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1931                   $self->{next_char} == 0x005B) { # [
1932            !!!next-input-character;
1933            push @next_char, $self->{next_char};
1934            if ($self->{next_char} == 0x0043) { # C
1935              !!!next-input-character;
1936              push @next_char, $self->{next_char};
1937              if ($self->{next_char} == 0x0044) { # D
1938                !!!next-input-character;
1939                push @next_char, $self->{next_char};
1940                if ($self->{next_char} == 0x0041) { # A
1941                  !!!next-input-character;
1942                  push @next_char, $self->{next_char};
1943                  if ($self->{next_char} == 0x0054) { # T
1944                    !!!next-input-character;
1945                    push @next_char, $self->{next_char};
1946                    if ($self->{next_char} == 0x0041) { # A
1947                      !!!next-input-character;
1948                      push @next_char, $self->{next_char};
1949                      if ($self->{next_char} == 0x005B) { # [
1950                        !!!cp (135.1);
1951                        $self->{state} = CDATA_BLOCK_STATE;
1952                        !!!next-input-character;
1953                        redo A;
1954                      } else {
1955                        !!!cp (135.2);
1956                      }
1957                    } else {
1958                      !!!cp (135.3);
1959                    }
1960                  } else {
1961                    !!!cp (135.4);                
1962                  }
1963                } else {
1964                  !!!cp (135.5);
1965                }
1966              } else {
1967                !!!cp (135.6);
1968              }
1969            } else {
1970              !!!cp (135.7);
1971            }
1972        } else {        } else {
1973          !!!cp (136);          !!!cp (136);
1974        }        }
# Line 1482  sub _get_next_token ($) { Line 1977  sub _get_next_token ($) {
1977        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1978        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1979        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1980          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1981                                    line => $l, column => $c,
1982                                   };
1983        redo A;        redo A;
1984                
1985        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1605  sub _get_next_token ($) { Line 2103  sub _get_next_token ($) {
2103          redo A;          redo A;
2104        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2105          !!!cp (152);          !!!cp (152);
2106          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2107                            line => $self->{line_prev},
2108                            column => $self->{column_prev});
2109          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2110          ## Stay in the state          ## Stay in the state
2111          !!!next-input-character;          !!!next-input-character;
# Line 1621  sub _get_next_token ($) { Line 2121  sub _get_next_token ($) {
2121          redo A;          redo A;
2122        } else {        } else {
2123          !!!cp (154);          !!!cp (154);
2124          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2125                            line => $self->{line_prev},
2126                            column => $self->{column_prev});
2127          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2128          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2129          !!!next-input-character;          !!!next-input-character;
# Line 1660  sub _get_next_token ($) { Line 2162  sub _get_next_token ($) {
2162          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2163          !!!next-input-character;          !!!next-input-character;
2164    
2165          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2166    
2167          redo A;          redo A;
2168        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1669  sub _get_next_token ($) { Line 2171  sub _get_next_token ($) {
2171          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2172          ## reconsume          ## reconsume
2173    
2174          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2175    
2176          redo A;          redo A;
2177        } else {        } else {
2178          !!!cp (160);          !!!cp (160);
2179          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2180              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2181  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2182          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2183          !!!next-input-character;          !!!next-input-character;
# Line 2194  sub _get_next_token ($) { Line 2693  sub _get_next_token ($) {
2693          !!!next-input-character;          !!!next-input-character;
2694          redo A;          redo A;
2695        }        }
2696        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2697          my $s = '';
2698          
2699          my ($l, $c) = ($self->{line}, $self->{column});
2700    
2701          CS: while ($self->{next_char} != -1) {
2702            if ($self->{next_char} == 0x005D) { # ]
2703              !!!next-input-character;
2704              if ($self->{next_char} == 0x005D) { # ]
2705                !!!next-input-character;
2706                MDC: {
2707                  if ($self->{next_char} == 0x003E) { # >
2708                    !!!cp (221.1);
2709                    !!!next-input-character;
2710                    last CS;
2711                  } elsif ($self->{next_char} == 0x005D) { # ]
2712                    !!!cp (221.2);
2713                    $s .= ']';
2714                    !!!next-input-character;
2715                    redo MDC;
2716                  } else {
2717                    !!!cp (221.3);
2718                    $s .= ']]';
2719                    #
2720                  }
2721                } # MDC
2722              } else {
2723                !!!cp (221.4);
2724                $s .= ']';
2725                #
2726              }
2727            } else {
2728              !!!cp (221.5);
2729              #
2730            }
2731            $s .= chr $self->{next_char};
2732            !!!next-input-character;
2733          } # CS
2734    
2735          $self->{state} = DATA_STATE;
2736          ## next-input-character done or EOF, which is reconsumed.
2737    
2738          if (length $s) {
2739            !!!cp (221.6);
2740            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2741                      line => $l, column => $c});
2742          } else {
2743            !!!cp (221.7);
2744          }
2745    
2746          redo A;
2747    
2748          ## ISSUE: "text tokens" in spec.
2749          ## TODO: Streaming support
2750      } else {      } else {
2751        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2752      }      }
# Line 2205  sub _get_next_token ($) { Line 2758  sub _get_next_token ($) {
2758  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2759    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2760    
2761      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2762    
2763    if ({    if ({
2764         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2765         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2245  sub _tokenize_attempt_to_consume_an_enti Line 2800  sub _tokenize_attempt_to_consume_an_enti
2800            redo X;            redo X;
2801          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2802            !!!cp (1005);            !!!cp (1005);
2803            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2804            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2805            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2806            return undef;            return undef;
# Line 2254  sub _tokenize_attempt_to_consume_an_enti Line 2809  sub _tokenize_attempt_to_consume_an_enti
2809            !!!next-input-character;            !!!next-input-character;
2810          } else {          } else {
2811            !!!cp (1007);            !!!cp (1007);
2812            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2813          }          }
2814    
2815          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2816            !!!cp (1008);            !!!cp (1008);
2817            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2818            $code = 0xFFFD;            $code = 0xFFFD;
2819          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2820            !!!cp (1009);            !!!cp (1009);
2821            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2822            $code = 0xFFFD;            $code = 0xFFFD;
2823          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2824            !!!cp (1010);            !!!cp (1010);
2825            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2826            $code = 0x000A;            $code = 0x000A;
2827          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2828            !!!cp (1011);            !!!cp (1011);
2829            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2830            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2831          }          }
2832    
2833          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2834                  has_reference => 1};                  has_reference => 1,
2835                    line => $l, column => $c,
2836                   };
2837        } # X        } # X
2838      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2839               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2297  sub _tokenize_attempt_to_consume_an_enti Line 2854  sub _tokenize_attempt_to_consume_an_enti
2854          !!!next-input-character;          !!!next-input-character;
2855        } else {        } else {
2856          !!!cp (1014);          !!!cp (1014);
2857          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2858        }        }
2859    
2860        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2861          !!!cp (1015);          !!!cp (1015);
2862          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2863          $code = 0xFFFD;          $code = 0xFFFD;
2864        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2865          !!!cp (1016);          !!!cp (1016);
2866          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2867          $code = 0xFFFD;          $code = 0xFFFD;
2868        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2869          !!!cp (1017);          !!!cp (1017);
2870          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2871          $code = 0x000A;          $code = 0x000A;
2872        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2873          !!!cp (1018);          !!!cp (1018);
2874          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2875          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2876        }        }
2877                
2878        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2879                  line => $l, column => $c,
2880                 };
2881      } else {      } else {
2882        !!!cp (1019);        !!!cp (1019);
2883        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2884        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2885        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2886        return undef;        return undef;
# Line 2338  sub _tokenize_attempt_to_consume_an_enti Line 2897  sub _tokenize_attempt_to_consume_an_enti
2897      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2898      our $EntityChar;      our $EntityChar;
2899    
2900      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2901             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2902             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2903               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2371  sub _tokenize_attempt_to_consume_an_enti Line 2930  sub _tokenize_attempt_to_consume_an_enti
2930            
2931      if ($match > 0) {      if ($match > 0) {
2932        !!!cp (1023);        !!!cp (1023);
2933        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2934                  line => $l, column => $c,
2935                 };
2936      } elsif ($match < 0) {      } elsif ($match < 0) {
2937        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2938        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2939          !!!cp (1024);          !!!cp (1024);
2940          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2941                    line => $l, column => $c,
2942                   };
2943        } else {        } else {
2944          !!!cp (1025);          !!!cp (1025);
2945          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2946                    line => $l, column => $c,
2947                   };
2948        }        }
2949      } else {      } else {
2950        !!!cp (1026);        !!!cp (1026);
2951        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2952        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2953        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2954                  line => $l, column => $c,
2955                 };
2956      }      }
2957    } else {    } else {
2958      !!!cp (1027);      !!!cp (1027);
2959      ## no characters are consumed      ## no characters are consumed
2960      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2961      return undef;      return undef;
2962    }    }
2963  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2461  sub _tree_construction_initial ($) { Line 3028  sub _tree_construction_initial ($) {
3028            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3029            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3030          !!!cp ('t1');          !!!cp ('t1');
3031          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3032        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3033          !!!cp ('t2');          !!!cp ('t2');
3034          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3035          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3036        } else {        } else {
3037          !!!cp ('t3');          !!!cp ('t3');
3038        }        }
3039                
3040        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3041          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3042          ## NOTE: Default value for both |public_id| and |system_id| attributes
3043          ## are empty strings, so that we don't set any value in missing cases.
3044        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3045            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3046        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2604  sub _tree_construction_initial ($) { Line 3173  sub _tree_construction_initial ($) {
3173                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3174               }->{$token->{type}}) {               }->{$token->{type}}) {
3175        !!!cp ('t14');        !!!cp ('t14');
3176        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3177        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3178        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3179        ## reprocess        ## reprocess
3180          !!!ack-later;
3181        return;        return;
3182      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3183        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2625  sub _tree_construction_initial ($) { Line 3195  sub _tree_construction_initial ($) {
3195          !!!cp ('t17');          !!!cp ('t17');
3196        }        }
3197    
3198        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3199        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3200        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3201        ## reprocess        ## reprocess
# Line 2654  sub _tree_construction_root_element ($) Line 3224  sub _tree_construction_root_element ($)
3224    B: {    B: {
3225        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3226          !!!cp ('t19');          !!!cp ('t19');
3227          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3228          ## Ignore the token          ## Ignore the token
3229          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3230          !!!next-token;          !!!next-token;
# Line 2688  sub _tree_construction_root_element ($) Line 3258  sub _tree_construction_root_element ($)
3258        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3259          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3260            my $root_element;            my $root_element;
3261            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3262            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3263            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3264                  [$root_element, $el_category->{html}];
3265    
3266            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3267              !!!cp ('t24');              !!!cp ('t24');
3268              $self->{application_cache_selection}              $self->{application_cache_selection}
3269                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3270              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3271                ## According to Hixie (#whatwg 2008-03-19), it should be
3272                ## resolved against the base URI of the document in HTML
3273                ## or xml:base of the element in XHTML.
3274            } else {            } else {
3275              !!!cp ('t25');              !!!cp ('t25');
3276              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3277            }            }
3278    
3279              !!!nack ('t25c');
3280    
3281            !!!next-token;            !!!next-token;
3282            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3283          } else {          } else {
# Line 2718  sub _tree_construction_root_element ($) Line 3294  sub _tree_construction_root_element ($)
3294          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3295        }        }
3296    
3297      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3298        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3299      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3300      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3301    
3302      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3303    
3304      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3305        !!!ack-later;
3306      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3307    
3308      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2748  sub _reset_insertion_mode ($) { Line 3326  sub _reset_insertion_mode ($) {
3326        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3327          $last = 1;          $last = 1;
3328          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3329            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') {  
3330              !!!cp ('t27');              !!!cp ('t27');
3331              #              #
3332            } else {            } else {
# Line 2759  sub _reset_insertion_mode ($) { Line 3336  sub _reset_insertion_mode ($) {
3336          }          }
3337        }        }
3338            
3339        ## Step 4..13      ## Step 4..14
3340        my $new_mode = {      my $new_mode;
3341        if ($node->[1] & FOREIGN_EL) {
3342          ## NOTE: Strictly spaking, the line below only applies to MathML and
3343          ## SVG elements.  Currently the HTML syntax supports only MathML and
3344          ## SVG elements as foreigners.
3345          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3346          ## ISSUE: What is set as the secondary insertion mode?
3347        } else {
3348          $new_mode = {
3349                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3350                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3351                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2776  sub _reset_insertion_mode ($) { Line 3361  sub _reset_insertion_mode ($) {
3361                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3362                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3363                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3364                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3365        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3366        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3367                
3368        ## Step 14        ## Step 15
3369        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3370          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3371            !!!cp ('t29');            !!!cp ('t29');
3372            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2794  sub _reset_insertion_mode ($) { Line 3380  sub _reset_insertion_mode ($) {
3380          !!!cp ('t31');          !!!cp ('t31');
3381        }        }
3382                
3383        ## Step 15        ## Step 16
3384        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3385                
3386        ## Step 16        ## Step 17
3387        $i--;        $i--;
3388        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3389                
3390        ## Step 17        ## Step 18
3391        redo S3;        redo S3;
3392      } # S3      } # S3
3393    
# Line 2913  sub _tree_construction_main ($) { Line 3499  sub _tree_construction_main ($) {
3499      ## Step 1      ## Step 1
3500      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3501      my $el;      my $el;
3502      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3503    
3504      ## Step 2      ## Step 2
3505      $insert->($el);      $insert->($el);
# Line 2924  sub _tree_construction_main ($) { Line 3510  sub _tree_construction_main ($) {
3510    
3511      ## Step 4      ## Step 4
3512      my $text = '';      my $text = '';
3513        !!!nack ('t40.1');
3514      !!!next-token;      !!!next-token;
3515      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3516        !!!cp ('t40');        !!!cp ('t40');
# Line 2950  sub _tree_construction_main ($) { Line 3537  sub _tree_construction_main ($) {
3537        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3538        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3539          !!!cp ('t43');          !!!cp ('t43');
3540          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3541        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3542          !!!cp ('t44');          !!!cp ('t44');
3543          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3544        } else {        } else {
3545          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3546        }        }
# Line 2963  sub _tree_construction_main ($) { Line 3550  sub _tree_construction_main ($) {
3550    
3551    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3552      my $script_el;      my $script_el;
3553      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3554      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3555    
3556      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3557      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3558            
3559      my $text = '';      my $text = '';
3560        !!!nack ('t45.1');
3561      !!!next-token;      !!!next-token;
3562      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3563        !!!cp ('t45');        !!!cp ('t45');
# Line 2989  sub _tree_construction_main ($) { Line 3577  sub _tree_construction_main ($) {
3577        ## Ignore the token        ## Ignore the token
3578      } else {      } else {
3579        !!!cp ('t48');        !!!cp ('t48');
3580        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3581        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3582        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3583      }      }
# Line 3017  sub _tree_construction_main ($) { Line 3605  sub _tree_construction_main ($) {
3605    my $open_tables = [[$self->{open_elements}->[0]->[0]]];    my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3606    
3607    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3608      my $tag_name = shift;      my $end_tag_token = shift;
3609        my $tag_name = $end_tag_token->{tag_name};
3610    
3611      ## NOTE: The adoption agency algorithm (AAA).      ## NOTE: The adoption agency algorithm (AAA).
3612    
# Line 3026  sub _tree_construction_main ($) { Line 3615  sub _tree_construction_main ($) {
3615        my $formatting_element;        my $formatting_element;
3616        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3617        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3618          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3619              !!!cp ('t52');
3620              last AFE;
3621            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3622                         eq $tag_name) {
3623            !!!cp ('t51');            !!!cp ('t51');
3624            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3625            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3626            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3627          }          }
3628        } # AFE        } # AFE
3629        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3630          !!!cp ('t53');          !!!cp ('t53');
3631          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3632          ## Ignore the token          ## Ignore the token
3633          !!!next-token;          !!!next-token;
3634          return;          return;
# Line 3055  sub _tree_construction_main ($) { Line 3645  sub _tree_construction_main ($) {
3645              last INSCOPE;              last INSCOPE;
3646            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3647              !!!cp ('t55');              !!!cp ('t55');
3648              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3649                                token => $end_tag_token);
3650              ## Ignore the token              ## Ignore the token
3651              !!!next-token;              !!!next-token;
3652              return;              return;
3653            }            }
3654          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3655            !!!cp ('t56');            !!!cp ('t56');
3656            $in_scope = 0;            $in_scope = 0;
3657          }          }
3658        } # INSCOPE        } # INSCOPE
3659        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3660          !!!cp ('t57');          !!!cp ('t57');
3661          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3662                            token => $end_tag_token);
3663          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3664          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3665          return;          return;
3666        }        }
3667        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3668          !!!cp ('t58');          !!!cp ('t58');
3669          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3670                            value => $self->{open_elements}->[-1]->[0]
3671                                ->manakai_local_name,
3672                            token => $end_tag_token);
3673        }        }
3674                
3675        ## Step 2        ## Step 2
# Line 3085  sub _tree_construction_main ($) { Line 3677  sub _tree_construction_main ($) {
3677        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3678        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3679          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3680          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3681              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3682              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3683               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3684            !!!cp ('t59');            !!!cp ('t59');
3685            $furthest_block = $node;            $furthest_block = $node;
3686            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3174  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766        } # S7          } # S7  
3767                
3768        ## Step 8        ## Step 8
3769        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3770          my $foster_parent_element;          my $foster_parent_element;
3771          my $next_sibling;          my $next_sibling;
3772                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3773                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3774                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3775                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3776                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3253  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843    
3844    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3845      my $child = shift;      my $child = shift;
3846      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]}) {  
3847        # MUST        # MUST
3848        my $foster_parent_element;        my $foster_parent_element;
3849        my $next_sibling;        my $next_sibling;
3850                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3851                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3852                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3853                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3854                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3285  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873      }      }
3874    }; # $insert_to_foster    }; # $insert_to_foster
3875    
3876    B: {    B: while (1) {
3877      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3878        !!!cp ('t73');        !!!cp ('t73');
3879        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3880        ## Ignore the token        ## Ignore the token
3881        ## Stay in the phase        ## Stay in the phase
3882        !!!next-token;        !!!next-token;
3883        redo B;        next B;
3884      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3885               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3886        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3887          !!!cp ('t79');          !!!cp ('t79');
3888          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3889          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3890        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3891          !!!cp ('t80');          !!!cp ('t80');
3892          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3893          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3894        } else {        } else {
3895          !!!cp ('t81');          !!!cp ('t81');
3896        }        }
3897    
3898        !!!cp ('t82');        !!!cp ('t82');
3899        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3900        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3901        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3902          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3907          }          }
3908        }        }
3909          !!!nack ('t84.1');
3910        !!!next-token;        !!!next-token;
3911        redo B;        next B;
3912      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3913        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3914        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 3922  sub _tree_construction_main ($) {
3922          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3923        }        }
3924        !!!next-token;        !!!next-token;
3925        redo B;        next B;
3926      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3927          if ($token->{type} == CHARACTER_TOKEN) {
3928            !!!cp ('t87.1');
3929            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3930            !!!next-token;
3931            next B;
3932          } elsif ($token->{type} == START_TAG_TOKEN) {
3933            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3934                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3935                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3936                ($token->{tag_name} eq 'svg' and
3937                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3938              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3939              !!!cp ('t87.2');
3940              #
3941            } elsif ({
3942                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3943                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3944                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3945                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3946                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3947                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3948                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3949                      var => 1,
3950                     }->{$token->{tag_name}}) {
3951              !!!cp ('t87.2');
3952              !!!parse-error (type => 'not closed',
3953                              value => $self->{open_elements}->[-1]->[0]
3954                                  ->manakai_local_name,
3955                              token => $token);
3956    
3957              pop @{$self->{open_elements}}
3958                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3959    
3960              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3961              ## Reprocess.
3962              next B;
3963            } else {
3964              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3965              my $tag_name = $token->{tag_name};
3966              if ($nsuri eq $SVG_NS) {
3967                $tag_name = {
3968                   altglyph => 'altGlyph',
3969                   altglyphdef => 'altGlyphDef',
3970                   altglyphitem => 'altGlyphItem',
3971                   animatecolor => 'animateColor',
3972                   animatemotion => 'animateMotion',
3973                   animatetransform => 'animateTransform',
3974                   clippath => 'clipPath',
3975                   feblend => 'feBlend',
3976                   fecolormatrix => 'feColorMatrix',
3977                   fecomponenttransfer => 'feComponentTransfer',
3978                   fecomposite => 'feComposite',
3979                   feconvolvematrix => 'feConvolveMatrix',
3980                   fediffuselighting => 'feDiffuseLighting',
3981                   fedisplacementmap => 'feDisplacementMap',
3982                   fedistantlight => 'feDistantLight',
3983                   feflood => 'feFlood',
3984                   fefunca => 'feFuncA',
3985                   fefuncb => 'feFuncB',
3986                   fefuncg => 'feFuncG',
3987                   fefuncr => 'feFuncR',
3988                   fegaussianblur => 'feGaussianBlur',
3989                   feimage => 'feImage',
3990                   femerge => 'feMerge',
3991                   femergenode => 'feMergeNode',
3992                   femorphology => 'feMorphology',
3993                   feoffset => 'feOffset',
3994                   fepointlight => 'fePointLight',
3995                   fespecularlighting => 'feSpecularLighting',
3996                   fespotlight => 'feSpotLight',
3997                   fetile => 'feTile',
3998                   feturbulence => 'feTurbulence',
3999                   foreignobject => 'foreignObject',
4000                   glyphref => 'glyphRef',
4001                   lineargradient => 'linearGradient',
4002                   radialgradient => 'radialGradient',
4003                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4004                   textpath => 'textPath',  
4005                }->{$tag_name} || $tag_name;
4006              }
4007    
4008              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4009    
4010              ## "adjust foreign attributes" - done in insert-element-f
4011    
4012              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4013    
4014              if ($self->{self_closing}) {
4015                pop @{$self->{open_elements}};
4016                !!!ack ('t87.3');
4017              } else {
4018                !!!cp ('t87.4');
4019              }
4020    
4021              !!!next-token;
4022              next B;
4023            }
4024          } elsif ($token->{type} == END_TAG_TOKEN) {
4025            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4026            !!!cp ('t87.5');
4027            #
4028          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4029            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4030            !!!cp ('t87.6');
4031            #
4032            ## TODO: ...
4033          } else {
4034            die "$0: $token->{type}: Unknown token type";        
4035          }
4036        }
4037    
4038        if ($self->{insertion_mode} & HEAD_IMS) {
4039        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4040          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4041            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3344  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045              !!!cp ('t88.1');              !!!cp ('t88.1');
4046              ## Ignore the token.              ## Ignore the token.
4047              !!!next-token;              !!!next-token;
4048              redo B;              next B;
4049            }            }
4050            unless (length $token->{data}) {            unless (length $token->{data}) {
4051              !!!cp ('t88');              !!!cp ('t88');
4052              !!!next-token;              !!!next-token;
4053              redo B;              next B;
4054            }            }
4055          }          }
4056    
4057          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4058            !!!cp ('t89');            !!!cp ('t89');
4059            ## As if <head>            ## As if <head>
4060            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4061            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4062            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4063                  [$self->{head_element}, $el_category->{head}];
4064    
4065            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4066            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3368  sub _tree_construction_main ($) { Line 4070  sub _tree_construction_main ($) {
4070            !!!cp ('t90');            !!!cp ('t90');
4071            ## As if </noscript>            ## As if </noscript>
4072            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4073            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4074                        
4075            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4076            ## As if </head>            ## As if </head>
# Line 3384  sub _tree_construction_main ($) { Line 4086  sub _tree_construction_main ($) {
4086            !!!cp ('t92');            !!!cp ('t92');
4087          }          }
4088    
4089              ## "after head" insertion mode          ## "after head" insertion mode
4090              ## As if <body>          ## As if <body>
4091              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4092              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4093              ## reprocess          ## reprocess
4094              redo B;          next B;
4095            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4096              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4097                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4098                  !!!cp ('t93');              !!!cp ('t93');
4099                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4100                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4101                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4102                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4103                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4104                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4105                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4106                  !!!cp ('t94');              !!!next-token;
4107                  #              next B;
4108                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4109                  !!!cp ('t95');              !!!cp ('t94');
4110                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
4111                  ## Ignore the token            } else {
4112                  !!!next-token;              !!!cp ('t95');
4113                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4114                }              ## Ignore the token
4115              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4116                !!!cp ('t96');              !!!next-token;
4117                ## As if <head>              next B;
4118                !!!create-element ($self->{head_element}, 'head');            }
4119                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4120                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4121              ## As if <head>
4122              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4123              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4124              push @{$self->{open_elements}},
4125                  [$self->{head_element}, $el_category->{head}];
4126    
4127                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4128                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4129              } else {          } else {
4130                !!!cp ('t97');            !!!cp ('t97');
4131              }          }
4132    
4133              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4134                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4135                  !!!cp ('t98');                  !!!cp ('t98');
4136                  ## As if </noscript>                  ## As if </noscript>
4137                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4138                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4139                                
4140                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4141                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3439  sub _tree_construction_main ($) { Line 4146  sub _tree_construction_main ($) {
4146                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4147                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4148                  !!!cp ('t100');                  !!!cp ('t100');
4149                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4150                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4151                        [$self->{head_element}, $el_category->{head}];
4152                } else {                } else {
4153                  !!!cp ('t101');                  !!!cp ('t101');
4154                }                }
4155                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4156                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4157                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4158                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4159                  !!!nack ('t101.1');
4160                !!!next-token;                !!!next-token;
4161                redo B;                next B;
4162              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4163                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4164                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4165                  !!!cp ('t102');                  !!!cp ('t102');
4166                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4167                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4168                        [$self->{head_element}, $el_category->{head}];
4169                } else {                } else {
4170                  !!!cp ('t103');                  !!!cp ('t103');
4171                }                }
4172                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4173                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4174                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4175                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4176                  !!!ack ('t103.1');
4177                !!!next-token;                !!!next-token;
4178                redo B;                next B;
4179              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4180                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4181                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4182                  !!!cp ('t104');                  !!!cp ('t104');
4183                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4184                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4185                        [$self->{head_element}, $el_category->{head}];
4186                } else {                } else {
4187                  !!!cp ('t105');                  !!!cp ('t105');
4188                }                }
4189                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4190                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.
4191    
4192                unless ($self->{confident}) {                unless ($self->{confident}) {
4193                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4194                    !!!cp ('t106');                    !!!cp ('t106');
4195                      ## NOTE: Whether the encoding is supported or not is handled
4196                      ## in the {change_encoding} callback.
4197                    $self->{change_encoding}                    $self->{change_encoding}
4198                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4199                             $token);
4200                                        
4201                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4202                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4203                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4204                                                 ->{has_reference});                                                 ->{has_reference});
4205                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4206                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4207                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4208                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4209                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4210                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4211                      !!!cp ('t107');                      !!!cp ('t107');
4212                        ## NOTE: Whether the encoding is supported or not is handled
4213                        ## in the {change_encoding} callback.
4214                      $self->{change_encoding}                      $self->{change_encoding}
4215                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4216                               $token);
4217                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4218                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4219                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3524  sub _tree_construction_main ($) { Line 4241  sub _tree_construction_main ($) {
4241    
4242                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4243                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4244                  !!!ack ('t110.1');
4245                !!!next-token;                !!!next-token;
4246                redo B;                next B;
4247              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4248                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4249                  !!!cp ('t111');                  !!!cp ('t111');
4250                  ## As if </noscript>                  ## As if </noscript>
4251                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4252                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4253                                
4254                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4255                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4256                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!cp ('t112');                  !!!cp ('t112');
4258                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                } else {                } else {
4262                  !!!cp ('t113');                  !!!cp ('t113');
4263                }                }
# Line 3549  sub _tree_construction_main ($) { Line 4268  sub _tree_construction_main ($) {
4268                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4269                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4270                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4271                redo B;                next B;
4272              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4273                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4274                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4275                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4276                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4277                  !!!cp ('t114');                  !!!cp ('t114');
4278                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4279                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4280                        [$self->{head_element}, $el_category->{head}];
4281                } else {                } else {
4282                  !!!cp ('t115');                  !!!cp ('t115');
4283                }                }
4284                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4285                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4286                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4287                redo B;                next B;
4288              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4289                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4290                  !!!cp ('t116');                  !!!cp ('t116');
4291                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4292                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4293                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4294                    !!!nack ('t116.1');
4295                  !!!next-token;                  !!!next-token;
4296                  redo B;                  next B;
4297                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4298                  !!!cp ('t117');                  !!!cp ('t117');
4299                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4300                  ## Ignore the token                  ## Ignore the token
4301                    !!!nack ('t117.1');
4302                  !!!next-token;                  !!!next-token;
4303                  redo B;                  next B;
4304                } else {                } else {
4305                  !!!cp ('t118');                  !!!cp ('t118');
4306                  #                  #
# Line 3588  sub _tree_construction_main ($) { Line 4310  sub _tree_construction_main ($) {
4310                  !!!cp ('t119');                  !!!cp ('t119');
4311                  ## As if </noscript>                  ## As if </noscript>
4312                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4313                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4314                                
4315                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4316                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4317                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4318                  !!!cp ('t120');                  !!!cp ('t120');
4319                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4320                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4321                        [$self->{head_element}, $el_category->{head}];
4322                } else {                } else {
4323                  !!!cp ('t121');                  !!!cp ('t121');
4324                }                }
# Line 3604  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                $script_start_tag->();                $script_start_tag->();
4328                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4329                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4330                redo B;                next B;
4331              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4332                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4333                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4334                  !!!cp ('t122');                  !!!cp ('t122');
4335                  ## As if </noscript>                  ## As if </noscript>
4336                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4337                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4338                                    
4339                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4340                  ## As if </head>                  ## As if </head>
# Line 3628  sub _tree_construction_main ($) { Line 4351  sub _tree_construction_main ($) {
4351                }                }
4352    
4353                ## "after head" insertion mode                ## "after head" insertion mode
4354                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4355                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4356                  !!!cp ('t126');                  !!!cp ('t126');
4357                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3638  sub _tree_construction_main ($) { Line 4361  sub _tree_construction_main ($) {
4361                } else {                } else {
4362                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4363                }                }
4364                  !!!nack ('t127.1');
4365                !!!next-token;                !!!next-token;
4366                redo B;                next B;
4367              } else {              } else {
4368                !!!cp ('t128');                !!!cp ('t128');
4369                #                #
# Line 3649  sub _tree_construction_main ($) { Line 4373  sub _tree_construction_main ($) {
4373                !!!cp ('t129');                !!!cp ('t129');
4374                ## As if </noscript>                ## As if </noscript>
4375                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4376                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4377                                
4378                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4379                ## As if </head>                ## As if </head>
# Line 3668  sub _tree_construction_main ($) { Line 4392  sub _tree_construction_main ($) {
4392    
4393              ## "after head" insertion mode              ## "after head" insertion mode
4394              ## As if <body>              ## As if <body>
4395              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4396              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4397              ## reprocess              ## reprocess
4398              redo B;              !!!ack-later;
4399                next B;
4400            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4401              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4402                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4403                  !!!cp ('t132');                  !!!cp ('t132');
4404                  ## As if <head>                  ## As if <head>
4405                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4406                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4407                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4408                        [$self->{head_element}, $el_category->{head}];
4409    
4410                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4411                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4412                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4413                  !!!next-token;                  !!!next-token;
4414                  redo B;                  next B;
4415                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4416                  !!!cp ('t133');                  !!!cp ('t133');
4417                  ## As if </noscript>                  ## As if </noscript>
4418                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4419                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4420                                    
4421                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4423                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4424                  !!!next-token;                  !!!next-token;
4425                  redo B;                  next B;
4426                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4427                  !!!cp ('t134');                  !!!cp ('t134');
4428                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4429                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4430                  !!!next-token;                  !!!next-token;
4431                  redo B;                  next B;
4432                } else {                } else {
4433                  !!!cp ('t135');                  !!!cp ('t135');
4434                  #                  #
# Line 3713  sub _tree_construction_main ($) { Line 4439  sub _tree_construction_main ($) {
4439                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4440                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4441                  !!!next-token;                  !!!next-token;
4442                  redo B;                  next B;
4443                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4444                  !!!cp ('t137');                  !!!cp ('t137');
4445                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4446                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4447                  !!!next-token;                  !!!next-token;
4448                  redo B;                  next B;
4449                } else {                } else {
4450                  !!!cp ('t138');                  !!!cp ('t138');
4451                  #                  #
# Line 3730  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4457                  !!!cp ('t139');                  !!!cp ('t139');
4458                  ## As if <head>                  ## As if <head>
4459                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4460                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4461                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4462                        [$self->{head_element}, $el_category->{head}];
4463    
4464                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4465                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4466                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4467                  !!!cp ('t140');                  !!!cp ('t140');
4468                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4469                  ## Ignore the token                  ## Ignore the token
4470                  !!!next-token;                  !!!next-token;
4471                  redo B;                  next B;
4472                } else {                } else {
4473                  !!!cp ('t141');                  !!!cp ('t141');
4474                }                }
# Line 3753  sub _tree_construction_main ($) { Line 4480  sub _tree_construction_main ($) {
4480                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4481                  !!!cp ('t142');                  !!!cp ('t142');
4482                  ## As if <head>                  ## As if <head>
4483                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4484                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4485                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4486                        [$self->{head_element}, $el_category->{head}];
4487    
4488                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4489                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3770  sub _tree_construction_main ($) { Line 4498  sub _tree_construction_main ($) {
4498                  #                  #
4499                } else {                } else {
4500                  !!!cp ('t145');                  !!!cp ('t145');
4501                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4502                  ## Ignore the token                  ## Ignore the token
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                }                }
4506              }              }
4507    
# Line 3781  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509                !!!cp ('t146');                !!!cp ('t146');
4510                ## As if </noscript>                ## As if </noscript>
4511                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4512                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4513                                
4514                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4515                ## As if </head>                ## As if </head>
# Line 3797  sub _tree_construction_main ($) { Line 4525  sub _tree_construction_main ($) {
4525              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4526  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4527                !!!cp ('t148');                !!!cp ('t148');
4528                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4529                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4530                !!!next-token;                !!!next-token;
4531                redo B;                next B;
4532              } else {              } else {
4533                !!!cp ('t149');                !!!cp ('t149');
4534              }              }
4535    
4536              ## "after head" insertion mode              ## "after head" insertion mode
4537              ## As if <body>              ## As if <body>
4538              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4539              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4540              ## reprocess              ## reprocess
4541              redo B;              next B;
4542        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4543          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544            !!!cp ('t149.1');            !!!cp ('t149.1');
4545    
4546            ## NOTE: As if <head>            ## NOTE: As if <head>
4547            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4548            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4549                ($self->{head_element});                ($self->{head_element});
4550            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4551              #    [$self->{head_element}, $el_category->{head}];
4552            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4553            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4554    
# Line 3841  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4571            !!!cp ('t149.3');            !!!cp ('t149.3');
4572    
4573            !!!parse-error (type => 'in noscript:#eof');            !!!parse-error (type => 'in noscript:#eof', token => $token);
4574    
4575            ## As if </noscript>            ## As if </noscript>
4576            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3860  sub _tree_construction_main ($) { Line 4589  sub _tree_construction_main ($) {
4589          }          }
4590    
4591          ## NOTE: As if <body>          ## NOTE: As if <body>
4592          !!!insert-element ('body');          !!!insert-element ('body',, $token);
4593          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4594          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4595          redo B;          next B;
4596        } else {        } else {
4597          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4598        }        }
# Line 3878  sub _tree_construction_main ($) { Line 4607  sub _tree_construction_main ($) {
4607              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4608    
4609              !!!next-token;              !!!next-token;
4610              redo B;              next B;
4611            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4612              if ({              if ({
4613                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3886  sub _tree_construction_main ($) { Line 4615  sub _tree_construction_main ($) {
4615                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4616                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4617                  ## have an element in table scope                  ## have an element in table scope
4618                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4619                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4620                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4621                      !!!cp ('t151');                      !!!cp ('t151');
4622                      $tn = $node->[1];  
4623                      last INSCOPE;                      ## Close the cell
4624                    } elsif ({                      !!!back-token; # <x>
4625                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4626                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4627                                  line => $token->{line},
4628                                  column => $token->{column}};
4629                        next B;
4630                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4631                      !!!cp ('t152');                      !!!cp ('t152');
4632                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4633                        last;
4634                    }                    }
4635                  } # INSCOPE                  }
4636                    unless (defined $tn) {  
4637                      !!!cp ('t153');                  !!!cp ('t153');
4638  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4639                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4640                      ## Ignore the token                  ## Ignore the token
4641                      !!!next-token;                  !!!nack ('t153.1');
4642                      redo B;                  !!!next-token;
4643                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4644                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4645                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4646                                    
4647                  ## As if </caption>                  ## NOTE: As if </caption>.
4648                  ## have a table element in table scope                  ## have a table element in table scope
4649                  my $i;                  my $i;
4650                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4651                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4652                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4653                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4654                      $i = $_;                        !!!cp ('t155');
4655                      last INSCOPE;                        $i = $_;
4656                    } elsif ({                        last INSCOPE;
4657                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4658                             }->{$node->[1]}) {                        !!!cp ('t156');
4659                      !!!cp ('t156');                        last;
4660                      last INSCOPE;                      }
4661                    }                    }
4662    
4663                      !!!cp ('t157');
4664                      !!!parse-error (type => 'start tag not allowed',
4665                                      value => $token->{tag_name}, token => $token);
4666                      ## Ignore the token
4667                      !!!nack ('t157.1');
4668                      !!!next-token;
4669                      next B;
4670                  } # 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;  
                   }  
4671                                    
4672                  ## generate implied end tags                  ## generate implied end tags
4673                  while ({                  while ($self->{open_elements}->[-1]->[1]
4674                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4675                    !!!cp ('t158');                    !!!cp ('t158');
4676                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4677                  }                  }
4678    
4679                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4680                    !!!cp ('t159');                    !!!cp ('t159');
4681                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4682                                      value => $self->{open_elements}->[-1]->[0]
4683                                          ->manakai_local_name,
4684                                      token => $token);
4685                  } else {                  } else {
4686                    !!!cp ('t160');                    !!!cp ('t160');
4687                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4693  sub _tree_construction_main ($) {
4693                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4694                                    
4695                  ## reprocess                  ## reprocess
4696                  redo B;                  !!!ack-later;
4697                    next B;
4698                } else {                } else {
4699                  !!!cp ('t161');                  !!!cp ('t161');
4700                  #                  #
# Line 3980  sub _tree_construction_main ($) { Line 4710  sub _tree_construction_main ($) {
4710                  my $i;                  my $i;
4711                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4712                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4713                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4714                      !!!cp ('t163');                      !!!cp ('t163');
4715                      $i = $_;                      $i = $_;
4716                      last INSCOPE;                      last INSCOPE;
4717                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4718                      !!!cp ('t164');                      !!!cp ('t164');
4719                      last INSCOPE;                      last INSCOPE;
4720                    }                    }
4721                  } # INSCOPE                  } # INSCOPE
4722                    unless (defined $i) {                    unless (defined $i) {
4723                      !!!cp ('t165');                      !!!cp ('t165');
4724                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4725                      ## Ignore the token                      ## Ignore the token
4726                      !!!next-token;                      !!!next-token;
4727                      redo B;                      next B;
4728                    }                    }
4729                                    
4730                  ## generate implied end tags                  ## generate implied end tags
4731                  while ({                  while ($self->{open_elements}->[-1]->[1]
4732                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4733                    !!!cp ('t166');                    !!!cp ('t166');
4734                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4735                  }                  }
4736    
4737                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4738                            ne $token->{tag_name}) {
4739                    !!!cp ('t167');                    !!!cp ('t167');
4740                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4741                                      value => $self->{open_elements}->[-1]->[0]
4742                                          ->manakai_local_name,
4743                                      token => $token);
4744                  } else {                  } else {
4745                    !!!cp ('t168');                    !!!cp ('t168');
4746                  }                  }
# Line 4021  sub _tree_construction_main ($) { Line 4752  sub _tree_construction_main ($) {
4752                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4753                                    
4754                  !!!next-token;                  !!!next-token;
4755                  redo B;                  next B;
4756                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4757                  !!!cp ('t169');                  !!!cp ('t169');
4758                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4759                  ## Ignore the token                  ## Ignore the token
4760                  !!!next-token;                  !!!next-token;
4761                  redo B;                  next B;
4762                } else {                } else {
4763                  !!!cp ('t170');                  !!!cp ('t170');
4764                  #                  #
# Line 4036  sub _tree_construction_main ($) { Line 4767  sub _tree_construction_main ($) {
4767                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4768                  ## have a table element in table scope                  ## have a table element in table scope
4769                  my $i;                  my $i;
4770                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4771                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4772                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4773                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4774                      $i = $_;                        !!!cp ('t171');
4775                      last INSCOPE;                        $i = $_;
4776                    } elsif ({                        last INSCOPE;
4777                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4778                             }->{$node->[1]}) {                        !!!cp ('t172');
4779                      !!!cp ('t172');                        last;
4780                      last INSCOPE;                      }
4781                    }                    }
4782    
4783                      !!!cp ('t173');
4784                      !!!parse-error (type => 'unmatched end tag',
4785                                      value => $token->{tag_name}, token => $token);
4786                      ## Ignore the token
4787                      !!!next-token;
4788                      next B;
4789                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4790                                    
4791                  ## generate implied end tags                  ## generate implied end tags
4792                  while ({                  while ($self->{open_elements}->[-1]->[1]
4793                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4794                    !!!cp ('t174');                    !!!cp ('t174');
4795                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4796                  }                  }
4797                                    
4798                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4799                    !!!cp ('t175');                    !!!cp ('t175');
4800                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4801                                      value => $self->{open_elements}->[-1]->[0]
4802                                          ->manakai_local_name,
4803                                      token => $token);
4804                  } else {                  } else {
4805                    !!!cp ('t176');                    !!!cp ('t176');
4806                  }                  }
# Line 4079  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4813                                    
4814                  !!!next-token;                  !!!next-token;
4815                  redo B;                  next B;
4816                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4817                  !!!cp ('t177');                  !!!cp ('t177');
4818                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4819                  ## Ignore the token                  ## Ignore the token
4820                  !!!next-token;                  !!!next-token;
4821                  redo B;                  next B;
4822                } else {                } else {
4823                  !!!cp ('t178');                  !!!cp ('t178');
4824                  #                  #
# Line 4098  sub _tree_construction_main ($) { Line 4831  sub _tree_construction_main ($) {
4831                ## have an element in table scope                ## have an element in table scope
4832                my $i;                my $i;
4833                my $tn;                my $tn;
4834                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4835                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4836                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4837                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4838                    $i = $_;                      !!!cp ('t179');
4839                    last INSCOPE;                      $i = $_;
4840                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4841                    !!!cp ('t180');                      ## Close the cell
4842                    $tn = $node->[1];                      !!!back-token; # </x>
4843                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4844                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4845                  } elsif ({                                column => $token->{column}};
4846                            table => 1, html => 1,                      next B;
4847                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4848                    !!!cp ('t181');                      !!!cp ('t180');
4849                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4850                        ## NOTE: There is exactly one |td| or |th| element
4851                        ## in scope in the stack of open elements by definition.
4852                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4853                        ## ISSUE: Can this be reached?
4854                        !!!cp ('t181');
4855                        last;
4856                      }
4857                  }                  }
4858                } # INSCOPE  
               unless (defined $i) {  
4859                  !!!cp ('t182');                  !!!cp ('t182');
4860                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4861                        value => $token->{tag_name}, token => $token);
4862                  ## Ignore the token                  ## Ignore the token
4863                  !!!next-token;                  !!!next-token;
4864                  redo B;                  next B;
4865                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4866              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4867                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4868                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4869    
4870                ## As if </caption>                ## As if </caption>
4871                ## have a table element in table scope                ## have a table element in table scope
4872                my $i;                my $i;
4873                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4874                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4875                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4876                    !!!cp ('t184');                    !!!cp ('t184');
4877                    $i = $_;                    $i = $_;
4878                    last INSCOPE;                    last INSCOPE;
4879                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4880                    !!!cp ('t185');                    !!!cp ('t185');
4881                    last INSCOPE;                    last INSCOPE;
4882                  }                  }
4883                } # INSCOPE                } # INSCOPE
4884                unless (defined $i) {                unless (defined $i) {
4885                  !!!cp ('t186');                  !!!cp ('t186');
4886                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4887                  ## Ignore the token                  ## Ignore the token
4888                  !!!next-token;                  !!!next-token;
4889                  redo B;                  next B;
4890                }                }
4891                                
4892                ## generate implied end tags                ## generate implied end tags
4893                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4894                  !!!cp ('t187');                  !!!cp ('t187');
4895                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4896                }                }
4897    
4898                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4899                  !!!cp ('t188');                  !!!cp ('t188');
4900                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4901                                    value => $self->{open_elements}->[-1]->[0]
4902                                        ->manakai_local_name,
4903                                    token => $token);
4904                } else {                } else {
4905                  !!!cp ('t189');                  !!!cp ('t189');
4906                }                }
# Line 4180  sub _tree_construction_main ($) { Line 4912  sub _tree_construction_main ($) {
4912                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4913    
4914                ## reprocess                ## reprocess
4915                redo B;                next B;
4916              } elsif ({              } elsif ({
4917                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4918                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4919                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4920                  !!!cp ('t190');                  !!!cp ('t190');
4921                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4922                  ## Ignore the token                  ## Ignore the token
4923                  !!!next-token;                  !!!next-token;
4924                  redo B;                  next B;
4925                } else {                } else {
4926                  !!!cp ('t191');                  !!!cp ('t191');
4927                  #                  #
# Line 4200  sub _tree_construction_main ($) { Line 4932  sub _tree_construction_main ($) {
4932                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4933                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4934                !!!cp ('t192');                !!!cp ('t192');
4935                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4936                ## Ignore the token                ## Ignore the token
4937                !!!next-token;                !!!next-token;
4938                redo B;                next B;
4939              } else {              } else {
4940                !!!cp ('t193');                !!!cp ('t193');
4941                #                #
4942              }              }
4943        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4944          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
4945            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
4946              !!!cp ('t75');              !!!cp ('t75');
4947              !!!parse-error (type => 'in body:#eof');              !!!parse-error (type => 'in body:#eof', token => $token);
4948              last;              last;
4949            }            }
4950          }          }
# Line 4237  sub _tree_construction_main ($) { Line 4966  sub _tree_construction_main ($) {
4966            unless (length $token->{data}) {            unless (length $token->{data}) {
4967              !!!cp ('t194');              !!!cp ('t194');
4968              !!!next-token;              !!!next-token;
4969              redo B;              next B;
4970            } else {            } else {
4971              !!!cp ('t195');              !!!cp ('t195');
4972            }            }
4973          }          }
4974    
4975              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4976    
4977              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4978              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4251  sub _tree_construction_main ($) { Line 4980  sub _tree_construction_main ($) {
4980              ## result in a new Text node.              ## result in a new Text node.
4981              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4982                            
4983              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]}) {  
4984                # MUST                # MUST
4985                my $foster_parent_element;                my $foster_parent_element;
4986                my $next_sibling;                my $next_sibling;
4987                my $prev_sibling;                my $prev_sibling;
4988                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4990                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4991                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4992                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4295  sub _tree_construction_main ($) { Line 5021  sub _tree_construction_main ($) {
5021          }          }
5022                            
5023          !!!next-token;          !!!next-token;
5024          redo B;          next B;
5025        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5026              if ({              if ({
5027                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4303  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5030                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5031                  ## Clear back to table context                  ## Clear back to table context
5032                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5033                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5034                    !!!cp ('t201');                    !!!cp ('t201');
5035                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5036                  }                  }
5037                                    
5038                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5039                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5040                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5041                }                }
# Line 4317  sub _tree_construction_main ($) { Line 5043  sub _tree_construction_main ($) {
5043                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5044                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5045                    !!!cp ('t202');                    !!!cp ('t202');
5046                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5047                  }                  }
5048                                    
5049                  ## Clear back to table body context                  ## Clear back to table body context
5050                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5051                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5052                    !!!cp ('t203');                    !!!cp ('t203');
5053                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5054                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4332  sub _tree_construction_main ($) { Line 5057  sub _tree_construction_main ($) {
5057                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5058                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5059                    !!!cp ('t204');                    !!!cp ('t204');
5060                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5061                      !!!nack ('t204');
5062                    !!!next-token;                    !!!next-token;
5063                    redo B;                    next B;
5064                  } else {                  } else {
5065                    !!!cp ('t205');                    !!!cp ('t205');
5066                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5067                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5068                  }                  }
5069                } else {                } else {
# Line 4345  sub _tree_construction_main ($) { Line 5071  sub _tree_construction_main ($) {
5071                }                }
5072    
5073                ## Clear back to table row context                ## Clear back to table row context
5074                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5075                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5076                  !!!cp ('t207');                  !!!cp ('t207');
5077                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5078                }                }
5079                                
5080                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5081                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5082    
5083                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5084                                
5085                  !!!nack ('t207.1');
5086                !!!next-token;                !!!next-token;
5087                redo B;                next B;
5088              } elsif ({              } elsif ({
5089                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5090                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4370  sub _tree_construction_main ($) { Line 5096  sub _tree_construction_main ($) {
5096                  my $i;                  my $i;
5097                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5098                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5099                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5100                      !!!cp ('t208');                      !!!cp ('t208');
5101                      $i = $_;                      $i = $_;
5102                      last INSCOPE;                      last INSCOPE;
5103                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5104                      !!!cp ('t209');                      !!!cp ('t209');
5105                      last INSCOPE;                      last INSCOPE;
5106                    }                    }
5107                  } # INSCOPE                  } # INSCOPE
5108                  unless (defined $i) {                  unless (defined $i) {
5109                   !!!cp ('t210');                    !!!cp ('t210');
5110  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5111                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5112                    ## Ignore the token                    ## Ignore the token
5113                      !!!nack ('t210.1');
5114                    !!!next-token;                    !!!next-token;
5115                    redo B;                    next B;
5116                  }                  }
5117                                    
5118                  ## Clear back to table row context                  ## Clear back to table row context
5119                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5120                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5121                    !!!cp ('t211');                    !!!cp ('t211');
5122                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5123                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4407  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5129                    !!!cp ('t212');                    !!!cp ('t212');
5130                    ## reprocess                    ## reprocess
5131                    redo B;                    !!!ack-later;
5132                      next B;
5133                  } else {                  } else {
5134                    !!!cp ('t213');                    !!!cp ('t213');
5135                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4419  sub _tree_construction_main ($) { Line 5141  sub _tree_construction_main ($) {
5141                  my $i;                  my $i;
5142                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5143                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5144                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5145                      !!!cp ('t214');                      !!!cp ('t214');
5146                      $i = $_;                      $i = $_;
5147                      last INSCOPE;                      last INSCOPE;
5148                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5149                      !!!cp ('t215');                      !!!cp ('t215');
5150                      last INSCOPE;                      last INSCOPE;
5151                    }                    }
# Line 4435  sub _tree_construction_main ($) { Line 5153  sub _tree_construction_main ($) {
5153                  unless (defined $i) {                  unless (defined $i) {
5154                    !!!cp ('t216');                    !!!cp ('t216');
5155  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5156                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5157                    ## Ignore the token                    ## Ignore the token
5158                      !!!nack ('t216.1');
5159                    !!!next-token;                    !!!next-token;
5160                    redo B;                    next B;
5161                  }                  }
5162    
5163                  ## Clear back to table body context                  ## Clear back to table body context
5164                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5165                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5166                    !!!cp ('t217');                    !!!cp ('t217');
5167                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5168                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4466  sub _tree_construction_main ($) { Line 5184  sub _tree_construction_main ($) {
5184    
5185                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5186                  ## Clear back to table context                  ## Clear back to table context
5187                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5188                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5189                    !!!cp ('t219');                    !!!cp ('t219');
5190                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5191                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5192                  }                  }
5193                                    
5194                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5195                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5196                  ## reprocess                  ## reprocess
5197                  redo B;                  !!!ack-later;
5198                    next B;
5199                } elsif ({                } elsif ({
5200                          caption => 1,                          caption => 1,
5201                          colgroup => 1,                          colgroup => 1,
5202                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5203                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5204                  ## Clear back to table context                  ## Clear back to table context
5205                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5206                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5207                    !!!cp ('t220');                    !!!cp ('t220');
5208                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5209                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4493  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5213                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5214                                    
5215                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5216                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5217                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5218                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4502  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5222                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5223                  !!!next-token;                  !!!next-token;
5224                  redo B;                  !!!nack ('t220.1');
5225                    next B;
5226                } else {                } else {
5227                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5228                }                }
5229              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5230                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5231                                  value => $self->{open_elements}->[-1]->[0]
5232                                      ->manakai_local_name,
5233                                  token => $token);
5234    
5235                ## As if </table>                ## As if </table>
5236                ## have a table element in table scope                ## have a table element in table scope
5237                my $i;                my $i;
5238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5239                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5240                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5241                    !!!cp ('t221');                    !!!cp ('t221');
5242                    $i = $_;                    $i = $_;
5243                    last INSCOPE;                    last INSCOPE;
5244                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5245                    !!!cp ('t222');                    !!!cp ('t222');
5246                    last INSCOPE;                    last INSCOPE;
5247                  }                  }
# Line 4529  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249                unless (defined $i) {                unless (defined $i) {
5250                  !!!cp ('t223');                  !!!cp ('t223');
5251  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5252                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5253                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5254                    !!!nack ('t223.1');
5255                  !!!next-token;                  !!!next-token;
5256                  redo B;                  next B;
5257                }                }
5258                                
5259    ## TODO: Followings are removed from the latest spec.
5260                ## generate implied end tags                ## generate implied end tags
5261                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5262                  !!!cp ('t224');                  !!!cp ('t224');
5263                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5264                }                }
5265    
5266                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5267                  !!!cp ('t225');                  !!!cp ('t225');
5268  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5269                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5270                                    value => $self->{open_elements}->[-1]->[0]
5271                                        ->manakai_local_name,
5272                                    token => $token);
5273                } else {                } else {
5274                  !!!cp ('t226');                  !!!cp ('t226');
5275                }                }
# Line 4556  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279    
5280                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5281    
5282                ## reprocess            ## reprocess
5283                redo B;            !!!ack-later;
5284              next B;
5285          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5286            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5287              !!!cp ('t227.8');              !!!cp ('t227.8');
5288              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5289              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5290              redo B;              next B;
5291            } else {            } else {
5292              !!!cp ('t227.7');              !!!cp ('t227.7');
5293              #              #
# Line 4573  sub _tree_construction_main ($) { Line 5297  sub _tree_construction_main ($) {
5297              !!!cp ('t227.6');              !!!cp ('t227.6');
5298              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5299              $script_start_tag->();              $script_start_tag->();
5300              redo B;              next B;
5301            } else {            } else {
5302              !!!cp ('t227.5');              !!!cp ('t227.5');
5303              #              #
# Line 4584  sub _tree_construction_main ($) { Line 5308  sub _tree_construction_main ($) {
5308                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5309                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5310                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5311                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5312    
5313                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5314    
5315                  ## TODO: form element pointer                  ## TODO: form element pointer
5316    
5317                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5318    
5319                  !!!next-token;                  !!!next-token;
5320                  redo B;                  !!!ack ('t227.2.1');
5321                    next B;
5322                } else {                } else {
5323                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5324                  #                  #
# Line 4611  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336            #            #
5337          }          }
5338    
5339          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5340    
5341          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5342          #          #
# Line 4622  sub _tree_construction_main ($) { Line 5347  sub _tree_construction_main ($) {
5347                my $i;                my $i;
5348                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5349                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5350                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5351                    !!!cp ('t228');                    !!!cp ('t228');
5352                    $i = $_;                    $i = $_;
5353                    last INSCOPE;                    last INSCOPE;
5354                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5355                    !!!cp ('t229');                    !!!cp ('t229');
5356                    last INSCOPE;                    last INSCOPE;
5357                  }                  }
5358                } # INSCOPE                } # INSCOPE
5359                unless (defined $i) {                unless (defined $i) {
5360                  !!!cp ('t230');                  !!!cp ('t230');
5361                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5362                  ## Ignore the token                  ## Ignore the token
5363                    !!!nack ('t230.1');
5364                  !!!next-token;                  !!!next-token;
5365                  redo B;                  next B;
5366                } else {                } else {
5367                  !!!cp ('t232');                  !!!cp ('t232');
5368                }                }
5369    
5370                ## Clear back to table row context                ## Clear back to table row context
5371                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5372                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5373                  !!!cp ('t231');                  !!!cp ('t231');
5374  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5375                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4655  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5379                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5380                !!!next-token;                !!!next-token;
5381                redo B;                !!!nack ('t231.1');
5382                  next B;
5383              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5384                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5385                  ## As if </tr>                  ## As if </tr>
# Line 4663  sub _tree_construction_main ($) { Line 5387  sub _tree_construction_main ($) {
5387                  my $i;                  my $i;
5388                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5389                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5390                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5391                      !!!cp ('t233');                      !!!cp ('t233');
5392                      $i = $_;                      $i = $_;
5393                      last INSCOPE;                      last INSCOPE;
5394                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5395                      !!!cp ('t234');                      !!!cp ('t234');
5396                      last INSCOPE;                      last INSCOPE;
5397                    }                    }
# Line 4677  sub _tree_construction_main ($) { Line 5399  sub _tree_construction_main ($) {
5399                  unless (defined $i) {                  unless (defined $i) {
5400                    !!!cp ('t235');                    !!!cp ('t235');
5401  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5402                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5403                    ## Ignore the token                    ## Ignore the token
5404                      !!!nack ('t236.1');
5405                    !!!next-token;                    !!!next-token;
5406                    redo B;                    next B;
5407                  }                  }
5408                                    
5409                  ## Clear back to table row context                  ## Clear back to table row context
5410                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5411                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5412                    !!!cp ('t236');                    !!!cp ('t236');
5413  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5414                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4702  sub _tree_construction_main ($) { Line 5424  sub _tree_construction_main ($) {
5424                  my $i;                  my $i;
5425                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5426                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5427                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5428                      !!!cp ('t237');                      !!!cp ('t237');
5429                      $i = $_;                      $i = $_;
5430                      last INSCOPE;                      last INSCOPE;
5431                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5432                      !!!cp ('t238');                      !!!cp ('t238');
5433                      last INSCOPE;                      last INSCOPE;
5434                    }                    }
5435                  } # INSCOPE                  } # INSCOPE
5436                  unless (defined $i) {                  unless (defined $i) {
5437                    !!!cp ('t239');                    !!!cp ('t239');
5438                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5439                    ## Ignore the token                    ## Ignore the token
5440                      !!!nack ('t239.1');
5441                    !!!next-token;                    !!!next-token;
5442                    redo B;                    next B;
5443                  }                  }
5444                                    
5445                  ## Clear back to table body context                  ## Clear back to table body context
5446                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5447                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5448                    !!!cp ('t240');                    !!!cp ('t240');
5449                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5450                  }                  }
# Line 4752  sub _tree_construction_main ($) { Line 5470  sub _tree_construction_main ($) {
5470                my $i;                my $i;
5471                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5472                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5473                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5474                    !!!cp ('t241');                    !!!cp ('t241');
5475                    $i = $_;                    $i = $_;
5476                    last INSCOPE;                    last INSCOPE;
5477                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5478                    !!!cp ('t242');                    !!!cp ('t242');
5479                    last INSCOPE;                    last INSCOPE;
5480                  }                  }
5481                } # INSCOPE                } # INSCOPE
5482                unless (defined $i) {                unless (defined $i) {
5483                  !!!cp ('t243');                  !!!cp ('t243');
5484                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5485                  ## Ignore the token                  ## Ignore the token
5486                    !!!nack ('t243.1');
5487                  !!!next-token;                  !!!next-token;
5488                  redo B;                  next B;
5489                }                }
5490                                    
5491                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4777  sub _tree_construction_main ($) { Line 5494  sub _tree_construction_main ($) {
5494                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5495                                
5496                !!!next-token;                !!!next-token;
5497                redo B;                next B;
5498              } elsif ({              } elsif ({
5499                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5500                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4787  sub _tree_construction_main ($) { Line 5504  sub _tree_construction_main ($) {
5504                  my $i;                  my $i;
5505                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5506                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5507                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5508                      !!!cp ('t247');                      !!!cp ('t247');
5509                      $i = $_;                      $i = $_;
5510                      last INSCOPE;                      last INSCOPE;
5511                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5512                      !!!cp ('t248');                      !!!cp ('t248');
5513                      last INSCOPE;                      last INSCOPE;
5514                    }                    }
5515                  } # INSCOPE                  } # INSCOPE
5516                    unless (defined $i) {                    unless (defined $i) {
5517                      !!!cp ('t249');                      !!!cp ('t249');
5518                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5519                      ## Ignore the token                      ## Ignore the token
5520                        !!!nack ('t249.1');
5521                      !!!next-token;                      !!!next-token;
5522                      redo B;                      next B;
5523                    }                    }
5524                                    
5525                  ## As if </tr>                  ## As if </tr>
# Line 4811  sub _tree_construction_main ($) { Line 5527  sub _tree_construction_main ($) {
5527                  my $i;                  my $i;
5528                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5529                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5530                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5531                      !!!cp ('t250');                      !!!cp ('t250');
5532                      $i = $_;                      $i = $_;
5533                      last INSCOPE;                      last INSCOPE;
5534                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5535                      !!!cp ('t251');                      !!!cp ('t251');
5536                      last INSCOPE;                      last INSCOPE;
5537                    }                    }
5538                  } # INSCOPE                  } # INSCOPE
5539                    unless (defined $i) {                    unless (defined $i) {
5540                      !!!cp ('t252');                      !!!cp ('t252');
5541                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5542                      ## Ignore the token                      ## Ignore the token
5543                        !!!nack ('t252.1');
5544                      !!!next-token;                      !!!next-token;
5545                      redo B;                      next B;
5546                    }                    }
5547                                    
5548                  ## Clear back to table row context                  ## Clear back to table row context
5549                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5550                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5551                    !!!cp ('t253');                    !!!cp ('t253');
5552  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5553                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4848  sub _tree_construction_main ($) { Line 5562  sub _tree_construction_main ($) {
5562                my $i;                my $i;
5563                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5564                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5565                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5566                    !!!cp ('t254');                    !!!cp ('t254');
5567                    $i = $_;                    $i = $_;
5568                    last INSCOPE;                    last INSCOPE;
5569                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5570                    !!!cp ('t255');                    !!!cp ('t255');
5571                    last INSCOPE;                    last INSCOPE;
5572                  }                  }
5573                } # INSCOPE                } # INSCOPE
5574                unless (defined $i) {                unless (defined $i) {
5575                  !!!cp ('t256');                  !!!cp ('t256');
5576                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5577                  ## Ignore the token                  ## Ignore the token
5578                    !!!nack ('t256.1');
5579                  !!!next-token;                  !!!next-token;
5580                  redo B;                  next B;
5581                }                }
5582    
5583                ## Clear back to table body context                ## Clear back to table body context
5584                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5585                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5586                  !!!cp ('t257');                  !!!cp ('t257');
5587  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5588                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4878  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590    
5591                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5592                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5593                  !!!nack ('t257.1');
5594                !!!next-token;                !!!next-token;
5595                redo B;                next B;
5596              } elsif ({              } elsif ({
5597                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5598                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5599                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5600                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5601                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5602                !!!cp ('t258');            !!!cp ('t258');
5603                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5604                ## Ignore the token            ## Ignore the token
5605                !!!next-token;            !!!nack ('t258.1');
5606                redo B;             !!!next-token;
5607              next B;
5608          } else {          } else {
5609            !!!cp ('t259');            !!!cp ('t259');
5610            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5611    
5612            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5613            #            #
5614          }          }
5615        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5616          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5617                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5618              !!!parse-error (type => 'in body:#eof', token => $token);
5619            !!!cp ('t259.1');            !!!cp ('t259.1');
5620            !!!parse-error (type => 'in body:#eof');            #
5621          } else {          } else {
5622            !!!cp ('t259.2');            !!!cp ('t259.2');
5623              #
5624          }          }
5625    
5626          ## Stop parsing          ## Stop parsing
# Line 4919  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635                unless (length $token->{data}) {                unless (length $token->{data}) {
5636                  !!!cp ('t260');                  !!!cp ('t260');
5637                  !!!next-token;                  !!!next-token;
5638                  redo B;                  next B;
5639                }                }
5640              }              }
5641                            
# Line 4928  sub _tree_construction_main ($) { Line 5644  sub _tree_construction_main ($) {
5644            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5645              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5646                !!!cp ('t262');                !!!cp ('t262');
5647                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5648                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5649                  !!!ack ('t262.1');
5650                !!!next-token;                !!!next-token;
5651                redo B;                next B;
5652              } else {              } else {
5653                !!!cp ('t263');                !!!cp ('t263');
5654                #                #
5655              }              }
5656            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5657              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5658                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5659                  !!!cp ('t264');                  !!!cp ('t264');
5660                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                  !!!next-token;                  !!!next-token;
5663                  redo B;                  next B;
5664                } else {                } else {
5665                  !!!cp ('t265');                  !!!cp ('t265');
5666                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5667                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                              next B;            
5670                }                }
5671              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5672                !!!cp ('t266');                !!!cp ('t266');
5673                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5674                ## Ignore the token                ## Ignore the token
5675                !!!next-token;                !!!next-token;
5676                redo B;                next B;
5677              } else {              } else {
5678                !!!cp ('t267');                !!!cp ('t267');
5679                #                #
5680              }              }
5681        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5682          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5683              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5684            !!!cp ('t270.2');            !!!cp ('t270.2');
5685            ## Stop parsing.            ## Stop parsing.
# Line 4973  sub _tree_construction_main ($) { Line 5690  sub _tree_construction_main ($) {
5690            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5691            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5692            ## Reprocess.            ## Reprocess.
5693            redo B;            next B;
5694          }          }
5695        } else {        } else {
5696          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5697        }        }
5698    
5699            ## As if </colgroup>            ## As if </colgroup>
5700            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5701              !!!cp ('t269');              !!!cp ('t269');
5702  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5703              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5704              ## Ignore the token              ## Ignore the token
5705                !!!nack ('t269.1');
5706              !!!next-token;              !!!next-token;
5707              redo B;              next B;
5708            } else {            } else {
5709              !!!cp ('t270');              !!!cp ('t270');
5710              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5711              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5712                !!!ack-later;
5713              ## reprocess              ## reprocess
5714              redo B;              next B;
5715            }            }
5716      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5717        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5718          !!!cp ('t271');          !!!cp ('t271');
5719          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5720          !!!next-token;          !!!next-token;
5721          redo B;          next B;
5722        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5723              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5724                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5725                  !!!cp ('t272');              !!!cp ('t272');
5726                  ## As if </option>              ## As if </option>
5727                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5728                } else {            } else {
5729                  !!!cp ('t273');              !!!cp ('t273');
5730                }            }
5731    
5732                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5733                !!!next-token;            !!!nack ('t273.1');
5734                redo B;            !!!next-token;
5735              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5736                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5737                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5738                  ## As if </option>              !!!cp ('t274');
5739                  pop @{$self->{open_elements}};              ## As if </option>
5740                } else {              pop @{$self->{open_elements}};
5741                  !!!cp ('t275');            } else {
5742                }              !!!cp ('t275');
5743              }
5744    
5745                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5746                  !!!cp ('t276');              !!!cp ('t276');
5747                  ## As if </optgroup>              ## As if </optgroup>
5748                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5749                } else {            } else {
5750                  !!!cp ('t277');              !!!cp ('t277');
5751                }            }
5752    
5753                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5754                !!!next-token;            !!!nack ('t277.1');
5755                redo B;            !!!next-token;
5756              next B;
5757          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5758                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5759                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5042  sub _tree_construction_main ($) { Line 5763  sub _tree_construction_main ($) {
5763                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
5764                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
5765            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
5766            !!!parse-error (type => 'not closed:select');            !!!parse-error (type => 'not closed:select', token => $token);
5767            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5768            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5769                ## have an element in table scope            ## have an element in table scope
5770                my $i;            my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5774                    !!!cp ('t278');                !!!cp ('t278');
5775                    $i = $_;                $i = $_;
5776                    last INSCOPE;                last INSCOPE;
5777                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                !!!cp ('t279');
5779                           }->{$node->[1]}) {                last INSCOPE;
5780                    !!!cp ('t279');              }
5781                    last INSCOPE;            } # INSCOPE
5782                  }            unless (defined $i) {
5783                } # INSCOPE              !!!cp ('t280');
5784                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5785                  !!!cp ('t280');              ## Ignore the token
5786                  !!!parse-error (type => 'unmatched end tag:select');              !!!nack ('t280.1');
5787                  ## Ignore the token              !!!next-token;
5788                  !!!next-token;              next B;
5789                  redo B;            }
               }  
5790                                
5791                !!!cp ('t281');            !!!cp ('t281');
5792                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5793    
5794                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5795    
5796            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5797              !!!cp ('t281.2');              !!!nack ('t281.2');
5798              !!!next-token;              !!!next-token;
5799              redo B;              next B;
5800            } else {            } else {
5801              !!!cp ('t281.1');              !!!cp ('t281.1');
5802                !!!ack-later;
5803              ## Reprocess the token.              ## Reprocess the token.
5804              redo B;              next B;
5805            }            }
5806          } else {          } else {
5807            !!!cp ('t282');            !!!cp ('t282');
5808            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5809            ## Ignore the token            ## Ignore the token
5810              !!!nack ('t282.1');
5811            !!!next-token;            !!!next-token;
5812            redo B;            next B;
5813          }          }
5814        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5815              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5816                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5817                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5818                  !!!cp ('t283');              !!!cp ('t283');
5819                  ## As if </option>              ## As if </option>
5820                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5821                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5822                  !!!cp ('t284');              !!!cp ('t284');
5823                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5824                } else {            } else {
5825                  !!!cp ('t285');              !!!cp ('t285');
5826                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5827                  ## Ignore the token              ## Ignore the token
5828                }            }
5829                !!!next-token;            !!!nack ('t285.1');
5830                redo B;            !!!next-token;
5831              } elsif ($token->{tag_name} eq 'option') {            next B;
5832                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5833                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5834                  pop @{$self->{open_elements}};              !!!cp ('t286');
5835                } else {              pop @{$self->{open_elements}};
5836                  !!!cp ('t287');            } else {
5837                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5838                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5839                }              ## Ignore the token
5840                !!!next-token;            }
5841                redo B;            !!!nack ('t287.1');
5842              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5843                ## have an element in table scope            next B;
5844                my $i;          } elsif ($token->{tag_name} eq 'select') {
5845                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5846                  my $node = $self->{open_elements}->[$_];            my $i;
5847                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5848                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5849                    $i = $_;              if ($node->[1] & SELECT_EL) {
5850                    last INSCOPE;                !!!cp ('t288');
5851                  } elsif ({                $i = $_;
5852                            table => 1, html => 1,                last INSCOPE;
5853                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5854                    !!!cp ('t289');                !!!cp ('t289');
5855                    last INSCOPE;                last INSCOPE;
5856                  }              }
5857                } # INSCOPE            } # INSCOPE
5858                unless (defined $i) {            unless (defined $i) {
5859                  !!!cp ('t290');              !!!cp ('t290');
5860                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5861                  ## Ignore the token              ## Ignore the token
5862                  !!!next-token;              !!!nack ('t290.1');
5863                  redo B;              !!!next-token;
5864                }              next B;
5865              }
5866                                
5867                !!!cp ('t291');            !!!cp ('t291');
5868                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5869    
5870                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5871    
5872                !!!next-token;            !!!nack ('t291.1');
5873                redo B;            !!!next-token;
5874              next B;
5875          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5876                   {                   {
5877                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5878                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5879                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5880  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5881                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5882                                
5883                ## have an element in table scope            ## have an element in table scope
5884                my $i;            my $i;
5885                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5887                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5888                    !!!cp ('t292');                !!!cp ('t292');
5889                    $i = $_;                $i = $_;
5890                    last INSCOPE;                last INSCOPE;
5891                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892                            table => 1, html => 1,                !!!cp ('t293');
5893                           }->{$node->[1]}) {                last INSCOPE;
5894                    !!!cp ('t293');              }
5895                    last INSCOPE;            } # INSCOPE
5896                  }            unless (defined $i) {
5897                } # INSCOPE              !!!cp ('t294');
5898                unless (defined $i) {              ## Ignore the token
5899                  !!!cp ('t294');              !!!nack ('t294.1');
5900                  ## Ignore the token              !!!next-token;
5901                  !!!next-token;              next B;
5902                  redo B;            }
               }  
5903                                
5904                ## As if </select>            ## As if </select>
5905                ## have an element in table scope            ## have an element in table scope
5906                undef $i;            undef $i;
5907                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5908                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5909                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5910                    !!!cp ('t295');                !!!cp ('t295');
5911                    $i = $_;                $i = $_;
5912                    last INSCOPE;                last INSCOPE;
5913                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5914  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5915                    !!!cp ('t296');                !!!cp ('t296');
5916                    last INSCOPE;                last INSCOPE;
5917                  }              }
5918                } # INSCOPE            } # INSCOPE
5919                unless (defined $i) {            unless (defined $i) {
5920                  !!!cp ('t297');              !!!cp ('t297');
5921  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5922                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5923                  ## Ignore the </select> token              ## Ignore the </select> token
5924                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5925                  redo B;              !!!next-token; ## TODO: ok?
5926                }              next B;
5927              }
5928                                
5929                !!!cp ('t298');            !!!cp ('t298');
5930                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5931    
5932                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5933    
5934                ## reprocess            !!!ack-later;
5935                redo B;            ## reprocess
5936              next B;
5937          } else {          } else {
5938            !!!cp ('t299');            !!!cp ('t299');
5939            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5940            ## Ignore the token            ## Ignore the token
5941              !!!nack ('t299.3');
5942            !!!next-token;            !!!next-token;
5943            redo B;            next B;
5944          }          }
5945        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5946          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5947                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5948            !!!cp ('t299.1');            !!!cp ('t299.1');
5949            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
5950          } else {          } else {
5951            !!!cp ('t299.2');            !!!cp ('t299.2');
5952          }          }
# Line 5244  sub _tree_construction_main ($) { Line 5968  sub _tree_construction_main ($) {
5968            unless (length $token->{data}) {            unless (length $token->{data}) {
5969              !!!cp ('t300');              !!!cp ('t300');
5970              !!!next-token;              !!!next-token;
5971              redo B;              next B;
5972            }            }
5973          }          }
5974                    
5975          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5976            !!!cp ('t301');            !!!cp ('t301');
5977            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5978    
5979            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5980          } else {          } else {
# Line 5258  sub _tree_construction_main ($) { Line 5982  sub _tree_construction_main ($) {
5982          }          }
5983                    
5984          ## "after body" insertion mode          ## "after body" insertion mode
5985          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5986    
5987          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5988          ## reprocess          ## reprocess
5989          redo B;          next B;
5990        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5991          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5992            !!!cp ('t303');            !!!cp ('t303');
5993            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5994                        
5995            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5996          } else {          } else {
# Line 5274  sub _tree_construction_main ($) { Line 5998  sub _tree_construction_main ($) {
5998          }          }
5999    
6000          ## "after body" insertion mode          ## "after body" insertion mode
6001          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6002    
6003          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6004            !!!ack-later;
6005          ## reprocess          ## reprocess
6006          redo B;          next B;
6007        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6008          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6009            !!!cp ('t305');            !!!cp ('t305');
6010            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6011                        
6012            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6013            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5294  sub _tree_construction_main ($) { Line 6019  sub _tree_construction_main ($) {
6019          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6020            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6021              !!!cp ('t307');              !!!cp ('t307');
6022              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6023              ## Ignore the token              ## Ignore the token
6024              !!!next-token;              !!!next-token;
6025              redo B;              next B;
6026            } else {            } else {
6027              !!!cp ('t308');              !!!cp ('t308');
6028              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6029              !!!next-token;              !!!next-token;
6030              redo B;              next B;
6031            }            }
6032          } else {          } else {
6033            !!!cp ('t309');            !!!cp ('t309');
6034            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6035    
6036            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6037            ## reprocess            ## reprocess
6038            redo B;            next B;
6039          }          }
6040        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6041          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5327  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            unless (length $token->{data}) {            unless (length $token->{data}) {
6053              !!!cp ('t310');              !!!cp ('t310');
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            }            }
6057          }          }
6058                    
6059          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6060            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6061              !!!cp ('t311');              !!!cp ('t311');
6062              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6063            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6064              !!!cp ('t312');              !!!cp ('t312');
6065              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6066            } else { # "after html frameset"            } else { # "after html frameset"
6067              !!!cp ('t313');              !!!cp ('t313');
6068              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6069    
6070              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6071              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6072              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6073            }            }
6074                        
6075            ## Ignore the token.            ## Ignore the token.
# Line 5355  sub _tree_construction_main ($) { Line 6080  sub _tree_construction_main ($) {
6080              !!!cp ('t315');              !!!cp ('t315');
6081              !!!next-token;              !!!next-token;
6082            }            }
6083            redo B;            next B;
6084          }          }
6085                    
6086          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6087        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6088          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6089            !!!cp ('t316');            !!!cp ('t316');
6090            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6091    
6092            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6093            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5373  sub _tree_construction_main ($) { Line 6098  sub _tree_construction_main ($) {
6098          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6099              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6100            !!!cp ('t318');            !!!cp ('t318');
6101            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6102              !!!nack ('t318.1');
6103            !!!next-token;            !!!next-token;
6104            redo B;            next B;
6105          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6106                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6107            !!!cp ('t319');            !!!cp ('t319');
6108            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6109            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6110              !!!ack ('t319.1');
6111            !!!next-token;            !!!next-token;
6112            redo B;            next B;
6113          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6114            !!!cp ('t320');            !!!cp ('t320');
6115            ## NOTE: As if in body.            ## NOTE: As if in body.
6116            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6117            redo B;            next B;
6118          } else {          } else {
6119            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6120              !!!cp ('t321');              !!!cp ('t321');
6121              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6122            } else {            } else {
6123              !!!cp ('t322');              !!!cp ('t322');
6124              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6125            }            }
6126            ## Ignore the token            ## Ignore the token
6127              !!!nack ('t322.1');
6128            !!!next-token;            !!!next-token;
6129            redo B;            next B;
6130          }          }
6131        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6132          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6133            !!!cp ('t323');            !!!cp ('t323');
6134            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6135    
6136            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6137            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5413  sub _tree_construction_main ($) { Line 6141  sub _tree_construction_main ($) {
6141    
6142          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6143              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6144            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6145                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6146              !!!cp ('t325');              !!!cp ('t325');
6147              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6148              ## Ignore the token              ## Ignore the token
6149              !!!next-token;              !!!next-token;
6150            } else {            } else {
# Line 5426  sub _tree_construction_main ($) { Line 6154  sub _tree_construction_main ($) {
6154            }            }
6155    
6156            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6157                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6158              !!!cp ('t327');              !!!cp ('t327');
6159              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6160            } else {            } else {
6161              !!!cp ('t328');              !!!cp ('t328');
6162            }            }
6163            redo B;            next B;
6164          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6165                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6166            !!!cp ('t329');            !!!cp ('t329');
6167            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6168            !!!next-token;            !!!next-token;
6169            redo B;            next B;
6170          } else {          } else {
6171            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6172              !!!cp ('t330');              !!!cp ('t330');
6173              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6174            } else {            } else {
6175              !!!cp ('t331');              !!!cp ('t331');
6176              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6177            }            }
6178            ## Ignore the token            ## Ignore the token
6179            !!!next-token;            !!!next-token;
6180            redo B;            next B;
6181          }          }
6182        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6183          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6184                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6185            !!!cp ('t331.1');            !!!cp ('t331.1');
6186            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6187          } else {          } else {
6188            !!!cp ('t331.2');            !!!cp ('t331.2');
6189          }          }
# Line 5477  sub _tree_construction_main ($) { Line 6205  sub _tree_construction_main ($) {
6205          !!!cp ('t332');          !!!cp ('t332');
6206          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6207          $script_start_tag->();          $script_start_tag->();
6208          redo B;          next B;
6209        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6210          !!!cp ('t333');          !!!cp ('t333');
6211          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6212          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6213          redo B;          next B;
6214        } elsif ({        } elsif ({
6215                  base => 1, link => 1,                  base => 1, link => 1,
6216                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6217          !!!cp ('t334');          !!!cp ('t334');
6218          ## 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
6219          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6220          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6221            !!!ack ('t334.1');
6222          !!!next-token;          !!!next-token;
6223          redo B;          next B;
6224        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6225          ## 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
6226          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6227          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.
6228    
6229          unless ($self->{confident}) {          unless ($self->{confident}) {
6230            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6231              !!!cp ('t335');              !!!cp ('t335');
6232                ## NOTE: Whether the encoding is supported or not is handled
6233                ## in the {change_encoding} callback.
6234              $self->{change_encoding}              $self->{change_encoding}
6235                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6236                            
6237              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6238                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6239                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6240                                           ->{has_reference});                                           ->{has_reference});
6241            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6242              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6243                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6244                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6245                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6246                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6247                !!!cp ('t336');                !!!cp ('t336');
6248                  ## NOTE: Whether the encoding is supported or not is handled
6249                  ## in the {change_encoding} callback.
6250                $self->{change_encoding}                $self->{change_encoding}
6251                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6252                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6253                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6254                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5540  sub _tree_construction_main ($) { Line 6272  sub _tree_construction_main ($) {
6272            }            }
6273          }          }
6274    
6275            !!!ack ('t338.1');
6276          !!!next-token;          !!!next-token;
6277          redo B;          next B;
6278        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6279          !!!cp ('t341');          !!!cp ('t341');
6280          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6281          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6282          redo B;          next B;
6283        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6284          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6285                                
6286          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6287              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6288            !!!cp ('t342');            !!!cp ('t342');
6289            ## Ignore the token            ## Ignore the token
6290          } else {          } else {
# Line 5565  sub _tree_construction_main ($) { Line 6298  sub _tree_construction_main ($) {
6298              }              }
6299            }            }
6300          }          }
6301            !!!nack ('t343.1');
6302          !!!next-token;          !!!next-token;
6303          redo B;          next B;
6304        } elsif ({        } elsif ({
6305                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6306                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6307                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6308                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6309                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6310                    form => 1,
6311                    table => 1,
6312                    hr => 1,
6313                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6314            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6315              !!!cp ('t350');
6316              !!!parse-error (type => 'in form:form', token => $token);
6317              ## Ignore the token
6318              !!!nack ('t350.1');
6319              !!!next-token;
6320              next B;
6321            }
6322    
6323          ## has a p element in scope          ## has a p element in scope
6324          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6325            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6326              !!!cp ('t344');              !!!cp ('t344');
6327              !!!back-token;              !!!back-token; # <form>
6328              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6329              redo B;                        line => $token->{line}, column => $token->{column}};
6330            } elsif ({              next B;
6331                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6332              !!!cp ('t345');              !!!cp ('t345');
6333              last INSCOPE;              last INSCOPE;
6334            }            }
6335          } # INSCOPE          } # INSCOPE
6336                        
6337          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6339              !!!nack ('t346.1');
6340            !!!next-token;            !!!next-token;
6341            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6342              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5604  sub _tree_construction_main ($) { Line 6349  sub _tree_construction_main ($) {
6349            } else {            } else {
6350              !!!cp ('t348');              !!!cp ('t348');
6351            }            }
6352          } else {          } elsif ($token->{tag_name} eq 'form') {
6353            !!!cp ('t347');            !!!cp ('t347.1');
6354              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6355    
6356              !!!nack ('t347.2');
6357            !!!next-token;            !!!next-token;
6358          }          } elsif ($token->{tag_name} eq 'table') {
6359          redo B;            !!!cp ('t382');
6360        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6361          if (defined $self->{form_element}) {            
6362            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6363            !!!parse-error (type => 'in form:form');  
6364            ## Ignore the token            !!!nack ('t382.1');
6365              !!!next-token;
6366            } elsif ($token->{tag_name} eq 'hr') {
6367              !!!cp ('t386');
6368              pop @{$self->{open_elements}};
6369            
6370              !!!nack ('t386.1');
6371            !!!next-token;            !!!next-token;
           redo B;  
6372          } else {          } else {
6373            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6374            !!!next-token;            !!!next-token;
           redo B;  
6375          }          }
6376        } elsif ($token->{tag_name} eq 'li') {          next B;
6377          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6378          ## has a p element in scope          ## has a p element in scope
6379          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6380            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6381              !!!cp ('t353');              !!!cp ('t353');
6382              !!!back-token;              !!!back-token; # <x>
6383              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6384              redo B;                        line => $token->{line}, column => $token->{column}};
6385            } elsif ({              next B;
6386                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6387              !!!cp ('t354');              !!!cp ('t354');
6388              last INSCOPE;              last INSCOPE;
6389            }            }
# Line 5658  sub _tree_construction_main ($) { Line 6392  sub _tree_construction_main ($) {
6392          ## Step 1          ## Step 1
6393          my $i = -1;          my $i = -1;
6394          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6395            my $li_or_dtdd = {li => {li => 1},
6396                              dt => {dt => 1, dd => 1},
6397                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6398          LI: {          LI: {
6399            ## Step 2            ## Step 2
6400            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6401              if ($i != -1) {              if ($i != -1) {
6402                !!!cp ('t355');                !!!cp ('t355');
6403                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6404                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6405                                      ->manakai_local_name,
6406                                  token => $token);
6407              } else {              } else {
6408                !!!cp ('t356');                !!!cp ('t356');
6409              }              }
# Line 5675  sub _tree_construction_main ($) { Line 6414  sub _tree_construction_main ($) {
6414            }            }
6415                        
6416            ## Step 3            ## Step 3
6417            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6418                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6419                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6420                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6421                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6422                  not ($node->[1] & DIV_EL)) {
6423              !!!cp ('t358');              !!!cp ('t358');
6424              last LI;              last LI;
6425            }            }
# Line 5691  sub _tree_construction_main ($) { Line 6431  sub _tree_construction_main ($) {
6431            redo LI;            redo LI;
6432          } # LI          } # LI
6433                        
6434          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6435            !!!nack ('t359.1');
6436          !!!next-token;          !!!next-token;
6437          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6438        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6439          ## has a p element in scope          ## has a p element in scope
6440          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6441            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6442              !!!cp ('t367');              !!!cp ('t367');
6443              !!!back-token;              !!!back-token; # <plaintext>
6444              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6445              redo B;                        line => $token->{line}, column => $token->{column}};
6446            } elsif ({              next B;
6447                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6448              !!!cp ('t368');              !!!cp ('t368');
6449              last INSCOPE;              last INSCOPE;
6450            }            }
6451          } # INSCOPE          } # INSCOPE
6452                        
6453          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6454                        
6455          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6456                        
6457            !!!nack ('t368.1');
6458          !!!next-token;          !!!next-token;
6459          redo B;          next B;
6460        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6461          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6462            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6463            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6464              !!!cp ('t371');              !!!cp ('t371');
6465              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6466                            
6467              !!!back-token;              !!!back-token; # <a>
6468              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6469              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6470                $formatting_end_tag->($token);
6471                            
6472              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6473                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5807  sub _tree_construction_main ($) { Line 6492  sub _tree_construction_main ($) {
6492                        
6493          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6494    
6495          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6496          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6497    
6498            !!!nack ('t374.1');
6499          !!!next-token;          !!!next-token;
6500          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;  
6501        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6502          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6503    
6504          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6505          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6506            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6507            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6508              !!!cp ('t376');              !!!cp ('t376');
6509              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6510              !!!back-token;              !!!back-token; # <nobr>
6511              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6512              redo B;                        line => $token->{line}, column => $token->{column}};
6513            } elsif ({              next B;
6514                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6515              !!!cp ('t377');              !!!cp ('t377');
6516              last INSCOPE;              last INSCOPE;
6517            }            }
6518          } # INSCOPE          } # INSCOPE
6519                    
6520          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6521          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6522                    
6523            !!!nack ('t377.1');
6524          !!!next-token;          !!!next-token;
6525          redo B;          next B;
6526        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6527          ## has a button element in scope          ## has a button element in scope
6528          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6529            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6530            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6531              !!!cp ('t378');              !!!cp ('t378');
6532              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6533              !!!back-token;              !!!back-token; # <button>
6534              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6535              redo B;                        line => $token->{line}, column => $token->{column}};
6536            } elsif ({              next B;
6537                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6538              !!!cp ('t379');              !!!cp ('t379');
6539              last INSCOPE;              last INSCOPE;
6540            }            }
# Line 5872  sub _tree_construction_main ($) { Line 6542  sub _tree_construction_main ($) {
6542                        
6543          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6544                        
6545          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6546    
6547          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6548    
6549          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6550    
6551            !!!nack ('t379.1');
6552          !!!next-token;          !!!next-token;
6553          redo B;          next B;
       } elsif ({  
                 applet => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6554        } elsif ({        } elsif ({
6555                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6556                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6557                  image => 1,                  noembed => 1,
6558                    noframes => 1,
6559                    noscript => 0, ## TODO: 1 if scripting is enabled
6560                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6561          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6562            !!!cp ('t384');            !!!cp ('t381');
6563            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6564          } else {          } else {
6565            !!!cp ('t385');            !!!cp ('t399');
6566          }          }
6567            ## NOTE: There is an "as if in body" code clone.
6568          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6569          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6570        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6571          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6572                    
6573          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6574            !!!cp ('t389');            !!!cp ('t389');
6575            ## Ignore the token            ## Ignore the token
6576              !!!nack ('t389'); ## NOTE: Not acknowledged.
6577            !!!next-token;            !!!next-token;
6578            redo B;            next B;
6579          } else {          } else {
6580            my $at = $token->{attributes};            my $at = $token->{attributes};
6581            my $form_attrs;            my $form_attrs;
# Line 5991  sub _tree_construction_main ($) { Line 6586  sub _tree_construction_main ($) {
6586            delete $at->{prompt};            delete $at->{prompt};
6587            my @tokens = (            my @tokens = (
6588                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6589                           attributes => $form_attrs},                           attributes => $form_attrs,
6590                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6591                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6592                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6593                            {type => START_TAG_TOKEN, tag_name => 'p',
6594                             line => $token->{line}, column => $token->{column}},
6595                            {type => START_TAG_TOKEN, tag_name => 'label',
6596                             line => $token->{line}, column => $token->{column}},
6597                         );                         );
6598            if ($prompt_attr) {            if ($prompt_attr) {
6599              !!!cp ('t390');              !!!cp ('t390');
6600              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6601                               #line => $token->{line}, column => $token->{column},
6602                              };
6603            } else {            } else {
6604              !!!cp ('t391');              !!!cp ('t391');
6605              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6606                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6607                               #line => $token->{line}, column => $token->{column},
6608                              }; # SHOULD
6609              ## TODO: make this configurable              ## TODO: make this configurable
6610            }            }
6611            push @tokens,            push @tokens,
6612                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6613                             line => $token->{line}, column => $token->{column}},
6614                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6615                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6616                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6617                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6618                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6619            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6620                             line => $token->{line}, column => $token->{column}},
6621                            {type => END_TAG_TOKEN, tag_name => 'form',
6622                             line => $token->{line}, column => $token->{column}};
6623              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6624            !!!back-token (@tokens);            !!!back-token (@tokens);
6625            redo B;            !!!next-token;
6626              next B;
6627          }          }
6628        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6629          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6630          my $el;          my $el;
6631          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6632                    
6633          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6634          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 6028  sub _tree_construction_main ($) { Line 6637  sub _tree_construction_main ($) {
6637          $insert->($el);          $insert->($el);
6638                    
6639          my $text = '';          my $text = '';
6640            !!!nack ('t392.1');
6641          !!!next-token;          !!!next-token;
6642          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6643            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6058  sub _tree_construction_main ($) { Line 6668  sub _tree_construction_main ($) {
6668            ## Ignore the token            ## Ignore the token
6669          } else {          } else {
6670            !!!cp ('t398');            !!!cp ('t398');
6671            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6672          }          }
6673          !!!next-token;          !!!next-token;
6674          redo B;          next B;
6675        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6676                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6677          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6678    
6679          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6680    
6681            ## "adjust foreign attributes" - done in insert-element-f
6682                    
6683          if ($self->{insertion_mode} & TABLE_IMS or          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6684              $self->{insertion_mode} & BODY_TABLE_IMS or          
6685              $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {          if ($self->{self_closing}) {
6686            !!!cp ('t400.1');            pop @{$self->{open_elements}};
6687            $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;            !!!ack ('t398.1');
6688          } else {          } else {
6689            !!!cp ('t400.2');            !!!cp ('t398.2');
6690            $self->{insertion_mode} = IN_SELECT_IM;            $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6691              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6692              ## mode, "in body" (not "in foreign content") secondary insertion
6693              ## mode, maybe.
6694          }          }
6695    
6696          !!!next-token;          !!!next-token;
6697          redo B;          next B;
6698        } elsif ({        } elsif ({
6699                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6700                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6098  sub _tree_construction_main ($) { Line 6702  sub _tree_construction_main ($) {
6702                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6703                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6704          !!!cp ('t401');          !!!cp ('t401');
6705          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6706          ## Ignore the token          ## Ignore the token
6707            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6708          !!!next-token;          !!!next-token;
6709          redo B;          next B;
6710                    
6711          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6712        } else {        } else {
6713          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6714              !!!cp ('t384');
6715              !!!parse-error (type => 'image', token => $token);
6716              $token->{tag_name} = 'img';
6717            } else {
6718              !!!cp ('t385');
6719            }
6720    
6721            ## NOTE: There is an "as if <br>" code clone.
6722          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6723                    
6724          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6725    
6726            if ({
6727                 applet => 1, marquee => 1, object => 1,
6728                }->{$token->{tag_name}}) {
6729              !!!cp ('t380');
6730              push @$active_formatting_elements, ['#marker', ''];
6731              !!!nack ('t380.1');
6732            } elsif ({
6733                      b => 1, big => 1, em => 1, font => 1, i => 1,
6734                      s => 1, small => 1, strile => 1,
6735                      strong => 1, tt => 1, u => 1,
6736                     }->{$token->{tag_name}}) {
6737              !!!cp ('t375');
6738              push @$active_formatting_elements, $self->{open_elements}->[-1];
6739              !!!nack ('t375.1');
6740            } elsif ($token->{tag_name} eq 'input') {
6741              !!!cp ('t388');
6742              ## TODO: associate with $self->{form_element} if defined
6743              pop @{$self->{open_elements}};
6744              !!!ack ('t388.2');
6745            } elsif ({
6746                      area => 1, basefont => 1, bgsound => 1, br => 1,
6747                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6748                      #image => 1,
6749                     }->{$token->{tag_name}}) {
6750              !!!cp ('t388.1');
6751              pop @{$self->{open_elements}};
6752              !!!ack ('t388.3');
6753            } elsif ($token->{tag_name} eq 'select') {
6754              ## TODO: associate with $self->{form_element} if defined
6755            
6756              if ($self->{insertion_mode} & TABLE_IMS or
6757                  $self->{insertion_mode} & BODY_TABLE_IMS or
6758                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6759                !!!cp ('t400.1');
6760                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6761              } else {
6762                !!!cp ('t400.2');
6763                $self->{insertion_mode} = IN_SELECT_IM;
6764              }
6765              !!!nack ('t400.3');
6766            } else {
6767              !!!nack ('t402');
6768            }
6769                    
6770          !!!next-token;          !!!next-token;
6771          redo B;          next B;
6772        }        }
6773      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6774        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6775          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6776              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6777            for (@{$self->{open_elements}}) {          INSCOPE: {
6778              unless ({            for (reverse @{$self->{open_elements}}) {
6779                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6780                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6781                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6782                      }->{$_->[1]}) {                last INSCOPE;
6783                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6784                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6785              } else {                last;
               !!!cp ('t404');  
6786              }              }
6787            }            }
6788    
6789            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6790                              value => $token->{tag_name}, token => $token);
6791              ## NOTE: Ignore the token.
6792            !!!next-token;            !!!next-token;
6793            redo B;            next B;
6794          } else {          } # INSCOPE
6795            !!!cp ('t405');  
6796            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
6797            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6798            !!!next-token;              !!!cp ('t403');
6799            redo B;              !!!parse-error (type => 'not closed',
6800                                value => $_->[0]->manakai_local_name,
6801                                token => $token);
6802                last;
6803              } else {
6804                !!!cp ('t404');
6805              }
6806          }          }
6807    
6808            $self->{insertion_mode} = AFTER_BODY_IM;
6809            !!!next-token;
6810            next B;
6811        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6812          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
6813            ## up-to-date, though it has same effect as speced.
6814            if (@{$self->{open_elements}} > 1 and
6815                $self->{open_elements}->[1]->[1] & BODY_EL) {
6816            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6817            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6818              !!!cp ('t406');              !!!cp ('t406');
6819              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6820                                value => $self->{open_elements}->[1]->[0]
6821                                    ->manakai_local_name,
6822                                token => $token);
6823            } else {            } else {
6824              !!!cp ('t407');              !!!cp ('t407');
6825            }            }
6826            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6827            ## reprocess            ## reprocess
6828            redo B;            next B;
6829          } else {          } else {
6830            !!!cp ('t408');            !!!cp ('t408');
6831            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6832            ## Ignore the token            ## Ignore the token
6833            !!!next-token;            !!!next-token;
6834            redo B;            next B;
6835          }          }
6836        } elsif ({        } elsif ({
6837                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6170  sub _tree_construction_main ($) { Line 6844  sub _tree_construction_main ($) {
6844          my $i;          my $i;
6845          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6846            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6847            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6848              !!!cp ('t410');              !!!cp ('t410');
6849              $i = $_;              $i = $_;
6850              last INSCOPE;              last INSCOPE;
6851            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6852              !!!cp ('t411');              !!!cp ('t411');
6853              last INSCOPE;              last INSCOPE;
6854            }            }
# Line 6185  sub _tree_construction_main ($) { Line 6856  sub _tree_construction_main ($) {
6856    
6857          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6858            !!!cp ('t413');            !!!cp ('t413');
6859            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6860          } else {          } else {
6861            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6862            while ({            while ({
# Line 6193  sub _tree_construction_main ($) { Line 6864  sub _tree_construction_main ($) {
6864                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6865                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6866                    p => 1,                    p => 1,
6867                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6868              !!!cp ('t409');              !!!cp ('t409');
6869              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6870            }            }
6871    
6872            ## Step 2.            ## Step 2.
6873            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6874                      ne $token->{tag_name}) {
6875              !!!cp ('t412');              !!!cp ('t412');
6876              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6877                                value => $self->{open_elements}->[-1]->[0]
6878                                    ->manakai_local_name,
6879                                token => $token);
6880            } else {            } else {
6881              !!!cp ('t414');              !!!cp ('t414');
6882            }            }
# Line 6216  sub _tree_construction_main ($) { Line 6891  sub _tree_construction_main ($) {
6891                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6892          }          }
6893          !!!next-token;          !!!next-token;
6894          redo B;          next B;
6895        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6896          undef $self->{form_element};          undef $self->{form_element};
6897    
# Line 6224  sub _tree_construction_main ($) { Line 6899  sub _tree_construction_main ($) {
6899          my $i;          my $i;
6900          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6901            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6902            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6903              !!!cp ('t418');              !!!cp ('t418');
6904              $i = $_;              $i = $_;
6905              last INSCOPE;              last INSCOPE;
6906            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6907              !!!cp ('t419');              !!!cp ('t419');
6908              last INSCOPE;              last INSCOPE;
6909            }            }
# Line 6239  sub _tree_construction_main ($) { Line 6911  sub _tree_construction_main ($) {
6911    
6912          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6913            !!!cp ('t421');            !!!cp ('t421');
6914            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6915          } else {          } else {
6916            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6917            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6918              !!!cp ('t417');              !!!cp ('t417');
6919              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6920            }            }
6921                        
6922            ## Step 2.            ## Step 2.
6923            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6924                      ne $token->{tag_name}) {
6925              !!!cp ('t417.1');              !!!cp ('t417.1');
6926              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6927                                value => $self->{open_elements}->[-1]->[0]
6928                                    ->manakai_local_name,
6929                                token => $token);
6930            } else {            } else {
6931              !!!cp ('t420');              !!!cp ('t420');
6932            }              }  
# Line 6262  sub _tree_construction_main ($) { Line 6936  sub _tree_construction_main ($) {
6936          }          }
6937    
6938          !!!next-token;          !!!next-token;
6939          redo B;          next B;
6940        } elsif ({        } elsif ({
6941                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6942                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6270  sub _tree_construction_main ($) { Line 6944  sub _tree_construction_main ($) {
6944          my $i;          my $i;
6945          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6947            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6948              !!!cp ('t423');              !!!cp ('t423');
6949              $i = $_;              $i = $_;
6950              last INSCOPE;              last INSCOPE;
6951            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6952              !!!cp ('t424');              !!!cp ('t424');
6953              last INSCOPE;              last INSCOPE;
6954            }            }
# Line 6287  sub _tree_construction_main ($) { Line 6956  sub _tree_construction_main ($) {
6956    
6957          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6958            !!!cp ('t425.1');            !!!cp ('t425.1');
6959            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6960          } else {          } else {
6961            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6962            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6963              !!!cp ('t422');              !!!cp ('t422');
6964              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6965            }            }
6966                        
6967            ## Step 2.            ## Step 2.
6968            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6969                      ne $token->{tag_name}) {
6970              !!!cp ('t425');              !!!cp ('t425');
6971              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6972            } else {            } else {
6973              !!!cp ('t426');              !!!cp ('t426');
6974            }            }
# Line 6310  sub _tree_construction_main ($) { Line 6978  sub _tree_construction_main ($) {
6978          }          }
6979                    
6980          !!!next-token;          !!!next-token;
6981          redo B;          next B;
6982        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
6983          ## has an element in scope          ## has an element in scope
6984          my $i;          my $i;
6985          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6986            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6987            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6988              !!!cp ('t410.1');              !!!cp ('t410.1');
6989              $i = $_;              $i = $_;
6990              last INSCOPE;              last INSCOPE;
6991            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6992              !!!cp ('t411.1');              !!!cp ('t411.1');
6993              last INSCOPE;              last INSCOPE;
6994            }            }
6995          } # INSCOPE          } # INSCOPE
6996    
6997          if (defined $i) {          if (defined $i) {
6998            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6999                      ne $token->{tag_name}) {
7000              !!!cp ('t412.1');              !!!cp ('t412.1');
7001              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7002                                value => $self->{open_elements}->[-1]->[0]
7003                                    ->manakai_local_name,
7004                                token => $token);
7005            } else {            } else {
7006              !!!cp ('t414.1');              !!!cp ('t414.1');
7007            }            }
# Line 6340  sub _tree_construction_main ($) { Line 7009  sub _tree_construction_main ($) {
7009            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7010          } else {          } else {
7011            !!!cp ('t413.1');            !!!cp ('t413.1');
7012            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7013    
7014            !!!cp ('t415.1');            !!!cp ('t415.1');
7015            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7016            my $el;            my $el;
7017            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7018            $insert->($el);            $insert->($el);
7019            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7020          }          }
7021    
7022          !!!next-token;          !!!next-token;
7023          redo B;          next B;
7024        } elsif ({        } elsif ({
7025                  a => 1,                  a => 1,
7026                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6359  sub _tree_construction_main ($) { Line 7028  sub _tree_construction_main ($) {
7028                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7029                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7030          !!!cp ('t427');          !!!cp ('t427');
7031          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7032          redo B;          next B;
7033        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7034          !!!cp ('t428');          !!!cp ('t428');
7035          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7036    
7037          ## As if <br>          ## As if <br>
7038          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7039                    
7040          my $el;          my $el;
7041          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7042          $insert->($el);          $insert->($el);
7043                    
7044          ## Ignore the token.          ## Ignore the token.
7045          !!!next-token;          !!!next-token;
7046          redo B;          next B;
7047        } elsif ({        } elsif ({
7048                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7049                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6388  sub _tree_construction_main ($) { Line 7057  sub _tree_construction_main ($) {
7057                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7058                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7059          !!!cp ('t429');          !!!cp ('t429');
7060          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7061          ## Ignore the token          ## Ignore the token
7062          !!!next-token;          !!!next-token;
7063          redo B;          next B;
7064                    
7065          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7066                    
# Line 6402  sub _tree_construction_main ($) { Line 7071  sub _tree_construction_main ($) {
7071    
7072          ## Step 2          ## Step 2
7073          S2: {          S2: {
7074            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7075              ## Step 1              ## Step 1
7076              ## generate implied end tags              ## generate implied end tags
7077              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7078                !!!cp ('t430');                !!!cp ('t430');
7079                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7080                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7081              }              }
7082                    
7083              ## Step 2              ## Step 2
7084              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7085                        ne $token->{tag_name}) {
7086                !!!cp ('t431');                !!!cp ('t431');
7087                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7088                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7089                                  value => $self->{open_elements}->[-1]->[0]
7090                                      ->manakai_local_name,
7091                                  token => $token);
7092              } else {              } else {
7093                !!!cp ('t432');                !!!cp ('t432');
7094              }              }
# Line 6429  sub _tree_construction_main ($) { Line 7100  sub _tree_construction_main ($) {
7100              last S2;              last S2;
7101            } else {            } else {
7102              ## Step 3              ## Step 3
7103              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7104                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7105                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7106                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7107                !!!cp ('t433');                !!!cp ('t433');
7108                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7109                ## Ignore the token                ## Ignore the token
7110                !!!next-token;                !!!next-token;
7111                last S2;                last S2;
# Line 6450  sub _tree_construction_main ($) { Line 7121  sub _tree_construction_main ($) {
7121            ## Step 5;            ## Step 5;
7122            redo S2;            redo S2;
7123          } # S2          } # S2
7124          redo B;          next B;
7125        }        }
7126      }      }
7127      redo B;      next B;
7128      } continue { # B
7129        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7130          ## NOTE: The code below is executed in cases where it does not have
7131          ## to be, but it it is harmless even in those cases.
7132          ## has an element in scope
7133          INSCOPE: {
7134            for (reverse 0..$#{$self->{open_elements}}) {
7135              my $node = $self->{open_elements}->[$_];
7136              if ($node->[1] & FOREIGN_EL) {
7137                last INSCOPE;
7138              } elsif ($node->[1] & SCOPING_EL) {
7139                last;
7140              }
7141            }
7142            
7143            ## NOTE: No foreign element in scope.
7144            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7145          } # INSCOPE
7146        }
7147    } # B    } # B
7148    
7149    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6499  sub set_inner_html ($$$) { Line 7189  sub set_inner_html ($$$) {
7189    
7190      ## Step 8 # MUST      ## Step 8 # MUST
7191      my $i = 0;      my $i = 0;
7192      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7193      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7194      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7195        my $self = shift;        my $self = shift;
7196    
# Line 6509  sub set_inner_html ($$$) { Line 7199  sub set_inner_html ($$$) {
7199    
7200        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7201        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7202        $column++;  
7203          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7204          $p->{column}++;
7205    
7206        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7207          $line++;          $p->{line}++;
7208          $column = 0;          $p->{column} = 0;
7209          !!!cp ('i1');          !!!cp ('i1');
7210        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7211          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7212          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7213          $line++;          $p->{line}++;
7214          $column = 0;          $p->{column} = 0;
7215          !!!cp ('i2');          !!!cp ('i2');
7216        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7217          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6528  sub set_inner_html ($$$) { Line 7220  sub set_inner_html ($$$) {
7220          !!!cp ('i4');          !!!cp ('i4');
7221          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7222          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7223          } elsif ($self->{next_char} <= 0x0008 or
7224                   (0x000E <= $self->{next_char} and
7225                    $self->{next_char} <= 0x001F) or
7226                   (0x007F <= $self->{next_char} and
7227                    $self->{next_char} <= 0x009F) or
7228                   (0xD800 <= $self->{next_char} and
7229                    $self->{next_char} <= 0xDFFF) or
7230                   (0xFDD0 <= $self->{next_char} and
7231                    $self->{next_char} <= 0xFDDF) or
7232                   {
7233                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7234                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7235                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7236                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7237                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7238                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7239                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7240                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7241                    0x10FFFE => 1, 0x10FFFF => 1,
7242                   }->{$self->{next_char}}) {
7243            !!!cp ('i4.1');
7244            !!!parse-error (type => 'control char', level => $self->{must_level});
7245    ## TODO: error type documentation
7246        }        }
7247      };      };
7248      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6535  sub set_inner_html ($$$) { Line 7250  sub set_inner_html ($$$) {
7250            
7251      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7252        my (%opt) = @_;        my (%opt) = @_;
7253        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7254          my $column = $opt{column};
7255          if (defined $opt{token} and defined $opt{token}->{line}) {
7256            $line = $opt{token}->{line};
7257            $column = $opt{token}->{column};
7258          }
7259          warn "Parse error ($opt{type}) at line $line column $column\n";
7260      };      };
7261      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7262        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7263      };      };
7264            
7265      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6562  sub set_inner_html ($$$) { Line 7283  sub set_inner_html ($$$) {
7283          unless defined $p->{content_model};          unless defined $p->{content_model};
7284          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7285    
7286      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7287          ## TODO: Foreign element OK?
7288    
7289      ## Step 3      ## Step 3
7290      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6572  sub set_inner_html ($$$) { Line 7294  sub set_inner_html ($$$) {
7294      $doc->append_child ($root);      $doc->append_child ($root);
7295    
7296      ## Step 5 # MUST      ## Step 5 # MUST
7297      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7298    
7299      undef $p->{head_element};      undef $p->{head_element};
7300    
# Line 6618  sub set_inner_html ($$$) { Line 7340  sub set_inner_html ($$$) {
7340      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7341    
7342      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7343    
7344        delete $p->{parse_error}; # delete loop
7345    } else {    } else {
7346      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";
7347    }    }

Legend:
Removed from v.1.104  
changed lines
  Added in v.1.135

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24