/[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.100 by wakaba, Sun Mar 9 04:08:41 2008 UTC revision 1.133 by wakaba, Sat May 17 04:54:11 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset_name = shift;
337    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    my $s;    my $s;
339      
340    if (defined $charset) {    ## HTML5 encoding sniffing algorithm
341      require Encode; ## TODO: decode(utf8) don't delete BOM    require Message::Charset::Info;
342      $s = \ (Encode::decode ($charset, $$bytes_s));    my $charset;
343      $self->{input_encoding} = lc $charset; ## TODO: normalize name    my ($e, $e_status);
344      $self->{confident} = 1;  
345    } else {    SNIFFING: {
346      ## TODO: Implement HTML5 detection algorithm  
347        ## Step 1
348        if (defined $charset_name) {
349          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
350    
351          ## ISSUE: Unsupported encoding is not ignored according to the spec.
352          ($e, $e_status) = $charset->get_perl_encoding
353              (allow_error_reporting => 1,
354               allow_fallback => 1);
355          if ($e) {
356            $self->{confident} = 1;
357            last SNIFFING;
358          }
359        }
360    
361        ## Step 2
362        # wait
363    
364        ## Step 3
365        my $head = substr ($$bytes_s, 0, 3);
366        if ($head =~ /^\xFE\xFF/) {
367          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
368          ($e, $e_status) = $charset->get_perl_encoding
369              (allow_error_reporting => 1,
370               allow_fallback => 1);
371          $self->{confident} = 1;
372          last SNIFFING;
373        } elsif ($head =~ /^\xFF\xFE/) {
374          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
375          ($e, $e_status) = $charset->get_perl_encoding
376              (allow_error_reporting => 1,
377               allow_fallback => 1);
378          $self->{confident} = 1;
379          last SNIFFING;
380        } elsif ($head eq "\xEF\xBB\xBF") {
381          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
382          ($e, $e_status) = $charset->get_perl_encoding
383              (allow_error_reporting => 1,
384               allow_fallback => 1);
385          $self->{confident} = 1;
386          last SNIFFING;
387        }
388    
389        ## Step 4
390        ## TODO: <meta charset>
391    
392        ## Step 5
393        ## TODO: from history
394    
395        ## Step 6
396      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
397      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
398          (substr ($$bytes_s, 0, 1024));          (substr ($$bytes_s, 0, 1024));
399      $charset ||= 'windows-1252';      if (defined $charset_name) {
400      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
401      $self->{input_encoding} = $charset;  
402          ## ISSUE: Unsupported encoding is not ignored according to the spec.
403          ($e, $e_status) = $charset->get_perl_encoding
404              (allow_error_reporting => 1,
405               allow_fallback => 1);
406          if ($e) {
407            $self->{confident} = 0;
408            last SNIFFING;
409          }
410        }
411    
412        ## Step 7: default
413        ## TODO: Make this configurable.
414        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
415            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
416            ## detectable in the step 6.
417        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
418                                                       allow_fallback => 1);
419      $self->{confident} = 0;      $self->{confident} = 0;
420      } # SNIFFING
421    
422      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
423        
424      } elsif (not ($e_status &
425                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
426        
427    }    }
428      $s = \ $e->decode ($$bytes_s);
429      $self->{input_encoding} = $charset->get_iana_name;
430    
431    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
432      my $self = shift;      my $self = shift;
433      my $charset = lc shift;      my $charset_name = lc shift;
434      ## TODO: if $charset is supported      my $token = shift;
435        ## TODO: if $charset_name is supported
436      ## TODO: normalize charset name      ## TODO: normalize charset name
437    
438      ## "Change the encoding" algorithm:      ## "Change the encoding" algorithm:
439    
440      ## Step 1          ## Step 1    
441      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if ($charset_name eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
442        $charset = 'utf-8';        $charset_name = 'utf-8';
443      }      }
444    
445      ## Step 2      ## Step 2
446      if (defined $self->{input_encoding} and      if (defined $self->{input_encoding} and
447          $self->{input_encoding} eq $charset) {          $self->{input_encoding} eq $charset_name) {
448        $self->{confident} = 1;        $self->{confident} = 1;
449        return;        return;
450      }      }
451    
452      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
453          ':'.$charset, level => 'w');          ':'.$charset_name, level => 'w', token => $token);
454    
455      ## Step 3      ## Step 3
456      # if (can) {      # if (can) {
# Line 136  sub parse_byte_string ($$$$;$) { Line 460  sub parse_byte_string ($$$$;$) {
460      # }      # }
461    
462      ## Step 4      ## Step 4
463      throw Whatpm::HTML::RestartParser (charset => $charset);      throw Whatpm::HTML::RestartParser (charset => $charset_name);
464    }; # $self->{change_encoding}    }; # $self->{change_encoding}
465    
466    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
# Line 144  sub parse_byte_string ($$$$;$) { Line 468  sub parse_byte_string ($$$$;$) {
468    try {    try {
469      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_string ($s, @args);  
470    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
471      my $charset = shift->{charset};      my $charset_name = shift->{charset};
472      $s = \ (Encode::decode ($charset, $$bytes_s));          $s = \ (Encode::decode ($charset_name, $$bytes_s));    
473      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset_name; ## TODO: normalize
474      $self->{confident} = 1;      $self->{confident} = 1;
475      $return = $self->parse_char_string ($s, @args);      $return = $self->parse_char_string ($s, @args);
476    };    };
# Line 177  sub parse_string ($$$;$) { Line 501  sub parse_string ($$$;$) {
501        if defined $self->{input_encoding};        if defined $self->{input_encoding};
502    
503    my $i = 0;    my $i = 0;
504    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
505    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
506    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
507      my $self = shift;      my $self = shift;
508    
# Line 187  sub parse_string ($$$;$) { Line 511  sub parse_string ($$$;$) {
511    
512      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
513      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
514      $column++;  
515        ($self->{line_prev}, $self->{column_prev})
516            = ($self->{line}, $self->{column});
517        $self->{column}++;
518            
519      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
520        $line++;        !!!cp ('j1');
521        $column = 0;        $self->{line}++;
522          $self->{column} = 0;
523      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
524          !!!cp ('j2');
525        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
526        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
527        $line++;        $self->{line}++;
528        $column = 0;        $self->{column} = 0;
529      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
530          !!!cp ('j3');
531        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
532      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
533          !!!cp ('j4');
534        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
535        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
536        } elsif ($self->{next_char} <= 0x0008 or
537                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
538                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
539                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
540                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
541                 {
542                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
543                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
544                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
545                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
546                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
547                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
548                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
549                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
550                  0x10FFFE => 1, 0x10FFFF => 1,
551                 }->{$self->{next_char}}) {
552          !!!cp ('j5');
553          !!!parse-error (type => 'control char', level => $self->{must_level});
554    ## TODO: error type documentation
555      }      }
556    };    };
557    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 559  sub parse_string ($$$;$) {
559    
560    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
561      my (%opt) = @_;      my (%opt) = @_;
562      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
563        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
564        warn "Parse error ($opt{type}) at line $line column $column\n";
565    };    };
566    $self->{parse_error} = sub {    $self->{parse_error} = sub {
567      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
568    };    };
569    
570    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 572  sub parse_string ($$$;$) {
572    $self->_construct_tree;    $self->_construct_tree;
573    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
574    
575      delete $self->{parse_error}; # remove loop
576    
577    return $self->{document};    return $self->{document};
578  } # parse_string  } # parse_string
579    
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 641  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
641  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
642  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
643  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
644    sub SELF_CLOSING_START_TAG_STATE () { 34 }
645    sub CDATA_BLOCK_STATE () { 35 }
646    
647  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
648  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 659  sub TABLE_IMS ()      { 0b1000000 }
659  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
660  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
661  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
662    sub SELECT_IMS ()     { 0b10000000000 }
663    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
664        ## NOTE: "in foreign content" insertion mode is special; it is combined
665        ## with the secondary insertion mode.  In this parser, they are stored
666        ## together in the bit-or'ed form.
667    
668  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
669    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 686  sub IN_TABLE_IM () { TABLE_IMS }
686  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
687  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
688  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
689  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
690    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
691  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
692    
693  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 700  sub _initialize_tokenizer ($) {
700    undef $self->{current_attribute};    undef $self->{current_attribute};
701    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
702    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
703      delete $self->{self_closing};
704    $self->{char} = [];    $self->{char} = [];
705    # $self->{next_char}    # $self->{next_char}
706    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 721  sub _initialize_tokenizer ($) {
721  ##        ->{value}  ##        ->{value}
722  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
723  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
724    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
725    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
726    ##     while the token is pushed back to the stack.
727    
728    ## ISSUE: "When a DOCTYPE token is created, its
729    ## <i>self-closing flag</i> must be unset (its other state is that it
730    ## be set), and its attributes list must be empty.": Wrong subject?
731    
732  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
733    
# Line 384  sub _initialize_tokenizer ($) { Line 754  sub _initialize_tokenizer ($) {
754    
755  sub _get_next_token ($) {  sub _get_next_token ($) {
756    my $self = shift;    my $self = shift;
757    
758      if ($self->{self_closing}) {
759        !!!parse-error (type => 'nestc', token => $self->{current_token});
760        ## NOTE: The |self_closing| flag is only set by start tag token.
761        ## In addition, when a start tag token is emitted, it is always set to
762        ## |current_token|.
763        delete $self->{self_closing};
764      }
765    
766    if (@{$self->{token}}) {    if (@{$self->{token}}) {
767        $self->{self_closing} = $self->{token}->[0]->{self_closing};
768      return shift @{$self->{token}};      return shift @{$self->{token}};
769    }    }
770    
# Line 447  sub _get_next_token ($) { Line 827  sub _get_next_token ($) {
827          #          #
828        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
829          !!!cp (11);          !!!cp (11);
830          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
831                      line => $self->{line}, column => $self->{column}});
832          last A; ## TODO: ok?          last A; ## TODO: ok?
833        } else {        } else {
834          !!!cp (12);          !!!cp (12);
835        }        }
836        # Anything else        # Anything else
837        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
838                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
839                       line => $self->{line}, column => $self->{column},
840                      };
841        ## Stay in the data state        ## Stay in the data state
842        !!!next-input-character;        !!!next-input-character;
843    
# Line 463  sub _get_next_token ($) { Line 846  sub _get_next_token ($) {
846        redo A;        redo A;
847      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
848        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
849    
850          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
851                
852        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
853    
# Line 471  sub _get_next_token ($) { Line 856  sub _get_next_token ($) {
856    
857        unless (defined $token) {        unless (defined $token) {
858          !!!cp (13);          !!!cp (13);
859          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
860                      line => $l, column => $c,
861                     });
862        } else {        } else {
863          !!!cp (14);          !!!cp (14);
864          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 877  sub _get_next_token ($) {
877            ## reconsume            ## reconsume
878            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
879    
880            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
881                        line => $self->{line_prev},
882                        column => $self->{column_prev},
883                       });
884    
885            redo A;            redo A;
886          }          }
# Line 510  sub _get_next_token ($) { Line 900  sub _get_next_token ($) {
900            !!!cp (19);            !!!cp (19);
901            $self->{current_token}            $self->{current_token}
902              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
903                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
904                   line => $self->{line_prev},
905                   column => $self->{column_prev}};
906            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
907            !!!next-input-character;            !!!next-input-character;
908            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 910  sub _get_next_token ($) {
910                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
911            !!!cp (20);            !!!cp (20);
912            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
913                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
914                                        line => $self->{line_prev},
915                                        column => $self->{column_prev}};
916            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
917            !!!next-input-character;            !!!next-input-character;
918            redo A;            redo A;
919          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
920            !!!cp (21);            !!!cp (21);
921            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
922                              line => $self->{line_prev},
923                              column => $self->{column_prev});
924            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
925            !!!next-input-character;            !!!next-input-character;
926    
927            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
928                        line => $self->{line_prev},
929                        column => $self->{column_prev},
930                       });
931    
932            redo A;            redo A;
933          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
934            !!!cp (22);            !!!cp (22);
935            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
936                              line => $self->{line_prev},
937                              column => $self->{column_prev});
938            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
939              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
940                                        line => $self->{line_prev},
941                                        column => $self->{column_prev},
942                                       };
943            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
944            redo A;            redo A;
945          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 948  sub _get_next_token ($) {
948            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
949            ## reconsume            ## reconsume
950    
951            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
952                        line => $self->{line_prev},
953                        column => $self->{column_prev},
954                       });
955    
956            redo A;            redo A;
957          }          }
# Line 551  sub _get_next_token ($) { Line 959  sub _get_next_token ($) {
959          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
960        }        }
961      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
962          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
963        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
964          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
965    
966            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
967            my @next_char;            my @next_char;
968            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 979  sub _get_next_token ($) {
979                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
980                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
981    
982                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
983                            line => $l, column => $c,
984                           });
985        
986                redo A;                redo A;
987              }              }
# Line 588  sub _get_next_token ($) { Line 1000  sub _get_next_token ($) {
1000              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1001              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1002              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1003              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1004                          line => $l, column => $c,
1005                         });
1006              redo A;              redo A;
1007            } else {            } else {
1008              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1015  sub _get_next_token ($) {
1015            !!!cp (28);            !!!cp (28);
1016            # next-input-character is already done            # next-input-character is already done
1017            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1018            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1019                        line => $l, column => $c,
1020                       });
1021            redo A;            redo A;
1022          }          }
1023        }        }
# Line 609  sub _get_next_token ($) { Line 1025  sub _get_next_token ($) {
1025        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1026            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1027          !!!cp (29);          !!!cp (29);
1028          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1029                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1030                   tag_name => chr ($self->{next_char} + 0x0020),
1031                   line => $l, column => $c};
1032          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1033          !!!next-input-character;          !!!next-input-character;
1034          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1036  sub _get_next_token ($) {
1036                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1037          !!!cp (30);          !!!cp (30);
1038          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1039                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1040                                      line => $l, column => $c};
1041          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1042          !!!next-input-character;          !!!next-input-character;
1043          redo A;          redo A;
1044        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1045          !!!cp (31);          !!!cp (31);
1046          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1047                            line => $self->{line_prev}, ## "<" in "</>"
1048                            column => $self->{column_prev} - 1);
1049          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1055  sub _get_next_token ($) {
1055          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1056          # reconsume          # reconsume
1057    
1058          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1059                      line => $l, column => $c,
1060                     });
1061    
1062          redo A;          redo A;
1063        } else {        } else {
1064          !!!cp (33);          !!!cp (33);
1065          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1066          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1067            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                      line => $self->{line_prev}, # "<" of "</"
1069                                      column => $self->{column_prev} - 1,
1070                                     };
1071          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1072          redo A;          redo A;
1073        }        }
# Line 657  sub _get_next_token ($) { Line 1084  sub _get_next_token ($) {
1084        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1085          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1086            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1087            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1088          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1089            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1115  sub _get_next_token ($) {
1115          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1116          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1117            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1118            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1119          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1120            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1135  sub _get_next_token ($) {
1135    
1136          redo A;          redo A;
1137        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1138            !!!cp (42);
1139            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1140          !!!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  
1141          redo A;          redo A;
1142        } else {        } else {
1143          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1160  sub _get_next_token ($) {
1160        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1161          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1162            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1163            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1164          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1165            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1182                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1183          !!!cp (49);          !!!cp (49);
1184          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1185                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1186                   value => '',
1187                   line => $self->{line}, column => $self->{column}};
1188          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1189          !!!next-input-character;          !!!next-input-character;
1190          redo A;          redo A;
1191        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1192            !!!cp (50);
1193            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1194          !!!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  
1195          redo A;          redo A;
1196        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1197          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1198          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1199            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1200            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1201          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1202            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1226  sub _get_next_token ($) {
1226          } else {          } else {
1227            !!!cp (56);            !!!cp (56);
1228          }          }
1229          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1230                                value => ''};              = {name => chr ($self->{next_char}),
1231                   value => '',
1232                   line => $self->{line}, column => $self->{column}};
1233          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1234          !!!next-input-character;          !!!next-input-character;
1235          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1239  sub _get_next_token ($) {
1239          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1240              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1241            !!!cp (57);            !!!cp (57);
1242            !!!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});
1243            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1244          } else {          } else {
1245            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1268  sub _get_next_token ($) {
1268          $before_leave->();          $before_leave->();
1269          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1270            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1271            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1272          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1273            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1292  sub _get_next_token ($) {
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (64);
1296          $before_leave->();          $before_leave->();
1297            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1298          !!!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  
1299          redo A;          redo A;
1300        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1301          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1302          $before_leave->();          $before_leave->();
1303          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1304            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1305            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1306          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1307            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1352  sub _get_next_token ($) {
1352        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1374  sub _get_next_token ($) {
1374        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1375                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1376          !!!cp (76);          !!!cp (76);
1377          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1378                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1379                   value => '',
1380                   line => $self->{line}, column => $self->{column}};
1381          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1385            !!!cp (77);
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!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  
1388          redo A;          redo A;
1389        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1391          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1411  sub _get_next_token ($) {
1411          redo A;          redo A;
1412        } else {        } else {
1413          !!!cp (82);          !!!cp (82);
1414          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1415                                value => ''};              = {name => chr ($self->{next_char}),
1416                   value => '',
1417                   line => $self->{line}, column => $self->{column}};
1418          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1447  sub _get_next_token ($) {
1447        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1448          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1449            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1450            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1451          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1452            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1470  sub _get_next_token ($) {
1470          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1471          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1472            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1473            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1474          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1475            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1517  sub _get_next_token ($) {
1517          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1518          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1519            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1520            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1521          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1522            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1560          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1604  sub _get_next_token ($) {
1604        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1605          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1606            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1608          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1609            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1627  sub _get_next_token ($) {
1627          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1628          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1629            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1630            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1631          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1632            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1697  sub _get_next_token ($) {
1697        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1698          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1699            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1700            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1701          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1702            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1717  sub _get_next_token ($) {
1717    
1718          redo A;          redo A;
1719        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1720            !!!cp (122);
1721            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1722          !!!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  
1723          redo A;          redo A;
1724        } else {        } else {
1725          !!!cp (124);          !!!cp ('124.1');
1726          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1727          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1728          ## reconsume          ## reconsume
1729          redo A;          redo A;
1730        }        }
1731        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1732          if ($self->{next_char} == 0x003E) { # >
1733            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1734              !!!cp ('124.2');
1735              !!!parse-error (type => 'nestc', token => $self->{current_token});
1736              ## TODO: Different type than slash in start tag
1737              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1738              if ($self->{current_token}->{attributes}) {
1739                !!!cp ('124.4');
1740                !!!parse-error (type => 'end tag attribute');
1741              } else {
1742                !!!cp ('124.5');
1743              }
1744              ## TODO: Test |<title></title/>|
1745            } else {
1746              !!!cp ('124.3');
1747              $self->{self_closing} = 1;
1748            }
1749    
1750            $self->{state} = DATA_STATE;
1751            !!!next-input-character;
1752    
1753            !!!emit ($self->{current_token}); # start tag or end tag
1754    
1755            redo A;
1756          } else {
1757            !!!cp ('124.4');
1758            !!!parse-error (type => 'nestc');
1759            ## TODO: This error type is wrong.
1760            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1761            ## Reconsume.
1762            redo A;
1763          }
1764      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1765        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1766                
1767        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1768          #my $token = {type => COMMENT_TOKEN, data => ''};
1769    
1770        BC: {        BC: {
1771          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1773  sub _get_next_token ($) {
1773            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1774            !!!next-input-character;            !!!next-input-character;
1775    
1776            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1777    
1778            redo A;            redo A;
1779          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1781  sub _get_next_token ($) {
1781            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1782            ## reconsume            ## reconsume
1783    
1784            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1785    
1786            redo A;            redo A;
1787          } else {          } else {
1788            !!!cp (126);            !!!cp (126);
1789            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1790            !!!next-input-character;            !!!next-input-character;
1791            redo BC;            redo BC;
1792          }          }
# Line 1408  sub _get_next_token ($) { Line 1796  sub _get_next_token ($) {
1796      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1797        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1798    
1799          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1800    
1801        my @next_char;        my @next_char;
1802        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1803                
# Line 1416  sub _get_next_token ($) { Line 1806  sub _get_next_token ($) {
1806          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1807          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1808            !!!cp (127);            !!!cp (127);
1809            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1810                                        line => $l, column => $c,
1811                                       };
1812            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1813            !!!next-input-character;            !!!next-input-character;
1814            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1844  sub _get_next_token ($) {
1844                      !!!cp (129);                      !!!cp (129);
1845                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1846                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1847                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1848                                                  quirks => 1,
1849                                                  line => $l, column => $c,
1850                                                 };
1851                      !!!next-input-character;                      !!!next-input-character;
1852                      redo A;                      redo A;
1853                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1868  sub _get_next_token ($) {
1868          } else {          } else {
1869            !!!cp (135);            !!!cp (135);
1870          }          }
1871          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1872                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1873                   $self->{next_char} == 0x005B) { # [
1874            !!!next-input-character;
1875            push @next_char, $self->{next_char};
1876            if ($self->{next_char} == 0x0043) { # C
1877              !!!next-input-character;
1878              push @next_char, $self->{next_char};
1879              if ($self->{next_char} == 0x0044) { # D
1880                !!!next-input-character;
1881                push @next_char, $self->{next_char};
1882                if ($self->{next_char} == 0x0041) { # A
1883                  !!!next-input-character;
1884                  push @next_char, $self->{next_char};
1885                  if ($self->{next_char} == 0x0054) { # T
1886                    !!!next-input-character;
1887                    push @next_char, $self->{next_char};
1888                    if ($self->{next_char} == 0x0041) { # A
1889                      !!!next-input-character;
1890                      push @next_char, $self->{next_char};
1891                      if ($self->{next_char} == 0x005B) { # [
1892                        !!!cp (135.1);
1893                        $self->{state} = CDATA_BLOCK_STATE;
1894                        !!!next-input-character;
1895                        redo A;
1896                      } else {
1897                        !!!cp (135.2);
1898                      }
1899                    } else {
1900                      !!!cp (135.3);
1901                    }
1902                  } else {
1903                    !!!cp (135.4);                
1904                  }
1905                } else {
1906                  !!!cp (135.5);
1907                }
1908              } else {
1909                !!!cp (135.6);
1910              }
1911            } else {
1912              !!!cp (135.7);
1913            }
1914        } else {        } else {
1915          !!!cp (136);          !!!cp (136);
1916        }        }
# Line 1480  sub _get_next_token ($) { Line 1919  sub _get_next_token ($) {
1919        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1920        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1921        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1922          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1923                                    line => $l, column => $c,
1924                                   };
1925        redo A;        redo A;
1926                
1927        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2045  sub _get_next_token ($) {
2045          redo A;          redo A;
2046        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2047          !!!cp (152);          !!!cp (152);
2048          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2049                            line => $self->{line_prev},
2050                            column => $self->{column_prev});
2051          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2052          ## Stay in the state          ## Stay in the state
2053          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2063  sub _get_next_token ($) {
2063          redo A;          redo A;
2064        } else {        } else {
2065          !!!cp (154);          !!!cp (154);
2066          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2067                            line => $self->{line_prev},
2068                            column => $self->{column_prev});
2069          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2070          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2071          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2104  sub _get_next_token ($) {
2104          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2105          !!!next-input-character;          !!!next-input-character;
2106    
2107          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2108    
2109          redo A;          redo A;
2110        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2113  sub _get_next_token ($) {
2113          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2114          ## reconsume          ## reconsume
2115    
2116          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2117    
2118          redo A;          redo A;
2119        } else {        } else {
2120          !!!cp (160);          !!!cp (160);
2121          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2122              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2123  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2124          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2125          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2635  sub _get_next_token ($) {
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        }        }
2638        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2639          my $s = '';
2640          
2641          my ($l, $c) = ($self->{line}, $self->{column});
2642    
2643          CS: while ($self->{next_char} != -1) {
2644            if ($self->{next_char} == 0x005D) { # ]
2645              !!!next-input-character;
2646              if ($self->{next_char} == 0x005D) { # ]
2647                !!!next-input-character;
2648                MDC: {
2649                  if ($self->{next_char} == 0x003E) { # >
2650                    !!!cp (221.1);
2651                    !!!next-input-character;
2652                    last CS;
2653                  } elsif ($self->{next_char} == 0x005D) { # ]
2654                    !!!cp (221.2);
2655                    $s .= ']';
2656                    !!!next-input-character;
2657                    redo MDC;
2658                  } else {
2659                    !!!cp (221.3);
2660                    $s .= ']]';
2661                    #
2662                  }
2663                } # MDC
2664              } else {
2665                !!!cp (221.4);
2666                $s .= ']';
2667                #
2668              }
2669            } else {
2670              !!!cp (221.5);
2671              #
2672            }
2673            $s .= chr $self->{next_char};
2674            !!!next-input-character;
2675          } # CS
2676    
2677          $self->{state} = DATA_STATE;
2678          ## next-input-character done or EOF, which is reconsumed.
2679    
2680          if (length $s) {
2681            !!!cp (221.6);
2682            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2683                      line => $l, column => $c});
2684          } else {
2685            !!!cp (221.7);
2686          }
2687    
2688          redo A;
2689    
2690          ## ISSUE: "text tokens" in spec.
2691          ## TODO: Streaming support
2692      } else {      } else {
2693        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2694      }      }
# Line 2203  sub _get_next_token ($) { Line 2700  sub _get_next_token ($) {
2700  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2701    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2702    
2703      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2704    
2705    if ({    if ({
2706         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2707         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2742  sub _tokenize_attempt_to_consume_an_enti
2742            redo X;            redo X;
2743          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2744            !!!cp (1005);            !!!cp (1005);
2745            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2746            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2747            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2748            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2751  sub _tokenize_attempt_to_consume_an_enti
2751            !!!next-input-character;            !!!next-input-character;
2752          } else {          } else {
2753            !!!cp (1007);            !!!cp (1007);
2754            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2755          }          }
2756    
2757          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2758            !!!cp (1008);            !!!cp (1008);
2759            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2760            $code = 0xFFFD;            $code = 0xFFFD;
2761          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2762            !!!cp (1009);            !!!cp (1009);
2763            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2764            $code = 0xFFFD;            $code = 0xFFFD;
2765          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2766            !!!cp (1010);            !!!cp (1010);
2767            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2768            $code = 0x000A;            $code = 0x000A;
2769          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2770            !!!cp (1011);            !!!cp (1011);
2771            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2772            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2773          }          }
2774    
2775          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2776                  has_reference => 1};                  has_reference => 1,
2777                    line => $l, column => $c,
2778                   };
2779        } # X        } # X
2780      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2781               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2796  sub _tokenize_attempt_to_consume_an_enti
2796          !!!next-input-character;          !!!next-input-character;
2797        } else {        } else {
2798          !!!cp (1014);          !!!cp (1014);
2799          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2800        }        }
2801    
2802        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2803          !!!cp (1015);          !!!cp (1015);
2804          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2805          $code = 0xFFFD;          $code = 0xFFFD;
2806        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2807          !!!cp (1016);          !!!cp (1016);
2808          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2809          $code = 0xFFFD;          $code = 0xFFFD;
2810        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2811          !!!cp (1017);          !!!cp (1017);
2812          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2813          $code = 0x000A;          $code = 0x000A;
2814        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2815          !!!cp (1018);          !!!cp (1018);
2816          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2817          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2818        }        }
2819                
2820        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2821                  line => $l, column => $c,
2822                 };
2823      } else {      } else {
2824        !!!cp (1019);        !!!cp (1019);
2825        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2826        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2827        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2828        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2839  sub _tokenize_attempt_to_consume_an_enti
2839      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2840      our $EntityChar;      our $EntityChar;
2841    
2842      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2843             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2844             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2845               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2872  sub _tokenize_attempt_to_consume_an_enti
2872            
2873      if ($match > 0) {      if ($match > 0) {
2874        !!!cp (1023);        !!!cp (1023);
2875        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2876                  line => $l, column => $c,
2877                 };
2878      } elsif ($match < 0) {      } elsif ($match < 0) {
2879        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2880        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2881          !!!cp (1024);          !!!cp (1024);
2882          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2883                    line => $l, column => $c,
2884                   };
2885        } else {        } else {
2886          !!!cp (1025);          !!!cp (1025);
2887          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2888                    line => $l, column => $c,
2889                   };
2890        }        }
2891      } else {      } else {
2892        !!!cp (1026);        !!!cp (1026);
2893        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2894        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2895        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2896                  line => $l, column => $c,
2897                 };
2898      }      }
2899    } else {    } else {
2900      !!!cp (1027);      !!!cp (1027);
2901      ## no characters are consumed      ## no characters are consumed
2902      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2903      return undef;      return undef;
2904    }    }
2905  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2970  sub _tree_construction_initial ($) {
2970            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2971            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2972          !!!cp ('t1');          !!!cp ('t1');
2973          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2974        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2975          !!!cp ('t2');          !!!cp ('t2');
2976          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2977          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2978        } else {        } else {
2979          !!!cp ('t3');          !!!cp ('t3');
2980        }        }
2981                
2982        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2983          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2984          ## NOTE: Default value for both |public_id| and |system_id| attributes
2985          ## are empty strings, so that we don't set any value in missing cases.
2986        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2987            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2988        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3115  sub _tree_construction_initial ($) {
3115                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3116               }->{$token->{type}}) {               }->{$token->{type}}) {
3117        !!!cp ('t14');        !!!cp ('t14');
3118        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3119        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3120        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3121        ## reprocess        ## reprocess
3122          !!!ack-later;
3123        return;        return;
3124      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3125        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3137  sub _tree_construction_initial ($) {
3137          !!!cp ('t17');          !!!cp ('t17');
3138        }        }
3139    
3140        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3141        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3142        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3143        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3166  sub _tree_construction_root_element ($)
3166    B: {    B: {
3167        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3168          !!!cp ('t19');          !!!cp ('t19');
3169          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3170          ## Ignore the token          ## Ignore the token
3171          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3172          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3200  sub _tree_construction_root_element ($)
3200        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3201          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3202            my $root_element;            my $root_element;
3203            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3204            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3205            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3206                  [$root_element, $el_category->{html}];
3207    
3208            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3209              !!!cp ('t24');              !!!cp ('t24');
3210              $self->{application_cache_selection}              $self->{application_cache_selection}
3211                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3212              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3213                ## According to Hixie (#whatwg 2008-03-19), it should be
3214                ## resolved against the base URI of the document in HTML
3215                ## or xml:base of the element in XHTML.
3216            } else {            } else {
3217              !!!cp ('t25');              !!!cp ('t25');
3218              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3219            }            }
3220    
3221              !!!nack ('t25c');
3222    
3223            !!!next-token;            !!!next-token;
3224            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3225          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3236  sub _tree_construction_root_element ($)
3236          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3237        }        }
3238    
3239      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3240        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3241      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3242      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3243    
3244      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3245    
3246      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3247        !!!ack-later;
3248      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3249    
3250      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2746  sub _reset_insertion_mode ($) { Line 3268  sub _reset_insertion_mode ($) {
3268        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3269          $last = 1;          $last = 1;
3270          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3271            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') {  
3272              !!!cp ('t27');              !!!cp ('t27');
3273              #              #
3274            } else {            } else {
# Line 2757  sub _reset_insertion_mode ($) { Line 3278  sub _reset_insertion_mode ($) {
3278          }          }
3279        }        }
3280            
3281        ## Step 4..13      ## Step 4..14
3282        my $new_mode = {      my $new_mode;
3283        if ($node->[1] & FOREIGN_EL) {
3284          ## NOTE: Strictly spaking, the line below only applies to MathML and
3285          ## SVG elements.  Currently the HTML syntax supports only MathML and
3286          ## SVG elements as foreigners.
3287          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3288          ## ISSUE: What is set as the secondary insertion mode?
3289        } else {
3290          $new_mode = {
3291                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3292                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3293                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2774  sub _reset_insertion_mode ($) { Line 3303  sub _reset_insertion_mode ($) {
3303                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3304                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3305                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3306                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3307        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3308        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3309                
3310        ## Step 14        ## Step 15
3311        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3312          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3313            !!!cp ('t29');            !!!cp ('t29');
3314            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2792  sub _reset_insertion_mode ($) { Line 3322  sub _reset_insertion_mode ($) {
3322          !!!cp ('t31');          !!!cp ('t31');
3323        }        }
3324                
3325        ## Step 15        ## Step 16
3326        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3327                
3328        ## Step 16        ## Step 17
3329        $i--;        $i--;
3330        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3331                
3332        ## Step 17        ## Step 18
3333        redo S3;        redo S3;
3334      } # S3      } # S3
3335    
# Line 2911  sub _tree_construction_main ($) { Line 3441  sub _tree_construction_main ($) {
3441      ## Step 1      ## Step 1
3442      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3443      my $el;      my $el;
3444      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3445    
3446      ## Step 2      ## Step 2
3447      $insert->($el);      $insert->($el);
# Line 2922  sub _tree_construction_main ($) { Line 3452  sub _tree_construction_main ($) {
3452    
3453      ## Step 4      ## Step 4
3454      my $text = '';      my $text = '';
3455        !!!nack ('t40.1');
3456      !!!next-token;      !!!next-token;
3457      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3458        !!!cp ('t40');        !!!cp ('t40');
# Line 2948  sub _tree_construction_main ($) { Line 3479  sub _tree_construction_main ($) {
3479        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3480        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3481          !!!cp ('t43');          !!!cp ('t43');
3482          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3483        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3484          !!!cp ('t44');          !!!cp ('t44');
3485          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3486        } else {        } else {
3487          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3488        }        }
# Line 2961  sub _tree_construction_main ($) { Line 3492  sub _tree_construction_main ($) {
3492    
3493    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3494      my $script_el;      my $script_el;
3495      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3496      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3497    
3498      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3499      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3500            
3501      my $text = '';      my $text = '';
3502        !!!nack ('t45.1');
3503      !!!next-token;      !!!next-token;
3504      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3505        !!!cp ('t45');        !!!cp ('t45');
# Line 2987  sub _tree_construction_main ($) { Line 3519  sub _tree_construction_main ($) {
3519        ## Ignore the token        ## Ignore the token
3520      } else {      } else {
3521        !!!cp ('t48');        !!!cp ('t48');
3522        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3523        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3524        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3525      }      }
# Line 3010  sub _tree_construction_main ($) { Line 3542  sub _tree_construction_main ($) {
3542      !!!next-token;      !!!next-token;
3543    }; # $script_start_tag    }; # $script_start_tag
3544    
3545      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3546      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3547      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3548    
3549    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3550      my $tag_name = shift;      my $end_tag_token = shift;
3551        my $tag_name = $end_tag_token->{tag_name};
3552    
3553        ## NOTE: The adoption agency algorithm (AAA).
3554    
3555      FET: {      FET: {
3556        ## Step 1        ## Step 1
3557        my $formatting_element;        my $formatting_element;
3558        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3559        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3560          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3561              !!!cp ('t52');
3562              last AFE;
3563            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3564                         eq $tag_name) {
3565            !!!cp ('t51');            !!!cp ('t51');
3566            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3567            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3568            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3569          }          }
3570        } # AFE        } # AFE
3571        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3572          !!!cp ('t53');          !!!cp ('t53');
3573          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3574          ## Ignore the token          ## Ignore the token
3575          !!!next-token;          !!!next-token;
3576          return;          return;
# Line 3047  sub _tree_construction_main ($) { Line 3587  sub _tree_construction_main ($) {
3587              last INSCOPE;              last INSCOPE;
3588            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3589              !!!cp ('t55');              !!!cp ('t55');
3590              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3591                                token => $end_tag_token);
3592              ## Ignore the token              ## Ignore the token
3593              !!!next-token;              !!!next-token;
3594              return;              return;
3595            }            }
3596          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3597            !!!cp ('t56');            !!!cp ('t56');
3598            $in_scope = 0;            $in_scope = 0;
3599          }          }
3600        } # INSCOPE        } # INSCOPE
3601        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3602          !!!cp ('t57');          !!!cp ('t57');
3603          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3604                            token => $end_tag_token);
3605          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3606          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3607          return;          return;
3608        }        }
3609        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3610          !!!cp ('t58');          !!!cp ('t58');
3611          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3612                            value => $self->{open_elements}->[-1]->[0]
3613                                ->manakai_local_name,
3614                            token => $end_tag_token);
3615        }        }
3616                
3617        ## Step 2        ## Step 2
# Line 3077  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3620        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3621          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3622          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3623              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3624              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3625               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3626            !!!cp ('t59');            !!!cp ('t59');
3627            $furthest_block = $node;            $furthest_block = $node;
3628            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3166  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        } # S7          } # S7  
3709                
3710        ## Step 8        ## Step 8
3711        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3712            my $foster_parent_element;
3713            my $next_sibling;
3714            OE: for (reverse 0..$#{$self->{open_elements}}) {
3715              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3716                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3717                                 if (defined $parent and $parent->node_type == 1) {
3718                                   !!!cp ('t65.1');
3719                                   $foster_parent_element = $parent;
3720                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3721                                 } else {
3722                                   !!!cp ('t65.2');
3723                                   $foster_parent_element
3724                                     = $self->{open_elements}->[$_ - 1]->[0];
3725                                 }
3726                                 last OE;
3727                               }
3728                             } # OE
3729                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3730                               unless defined $foster_parent_element;
3731            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3732            $open_tables->[-1]->[1] = 1; # tainted
3733          } else {
3734            !!!cp ('t65.3');
3735            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3736          }
3737                
3738        ## Step 9        ## Step 9
3739        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3212  sub _tree_construction_main ($) { Line 3779  sub _tree_construction_main ($) {
3779      } # FET      } # FET
3780    }; # $formatting_end_tag    }; # $formatting_end_tag
3781    
   ## NOTE: $open_tables->[-1]->[0] is the "current table".  
   ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.  
   my $open_tables = [[$self->{open_elements}->[0]->[0]]];  
   
3782    $insert = my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3783      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3784    }; # $insert_to_current    }; # $insert_to_current
3785    
3786    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3787      my $child = shift;      my $child = shift;
3788      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]}) {  
3789        # MUST        # MUST
3790        my $foster_parent_element;        my $foster_parent_element;
3791        my $next_sibling;        my $next_sibling;
3792                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3793                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3794                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3795                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3796                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3254  sub _tree_construction_main ($) { Line 3815  sub _tree_construction_main ($) {
3815      }      }
3816    }; # $insert_to_foster    }; # $insert_to_foster
3817    
3818    B: {    B: while (1) {
3819      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3820        !!!cp ('t73');        !!!cp ('t73');
3821        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3822        ## Ignore the token        ## Ignore the token
3823        ## Stay in the phase        ## Stay in the phase
3824        !!!next-token;        !!!next-token;
3825        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3826      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3827               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3828        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3829          !!!cp ('t79');          !!!cp ('t79');
3830          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3831          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3832        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3833          !!!cp ('t80');          !!!cp ('t80');
3834          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3835          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3836        } else {        } else {
3837          !!!cp ('t81');          !!!cp ('t81');
3838        }        }
3839    
3840        !!!cp ('t82');        !!!cp ('t82');
3841        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3842        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3843        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3844          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3319  sub _tree_construction_main ($) { Line 3848  sub _tree_construction_main ($) {
3848               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3849          }          }
3850        }        }
3851          !!!nack ('t84.1');
3852        !!!next-token;        !!!next-token;
3853        redo B;        next B;
3854      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3855        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3856        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3334  sub _tree_construction_main ($) { Line 3864  sub _tree_construction_main ($) {
3864          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3865        }        }
3866        !!!next-token;        !!!next-token;
3867        redo B;        next B;
3868      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3869          if ($token->{type} == CHARACTER_TOKEN) {
3870            !!!cp ('t87.1');
3871            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3872            !!!next-token;
3873            next B;
3874          } elsif ($token->{type} == START_TAG_TOKEN) {
3875            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3876                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3877                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3878                ($token->{tag_name} eq 'svg' and
3879                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3880              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3881              !!!cp ('t87.2');
3882              #
3883            } elsif ({
3884                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3885                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3886                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3887                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3888                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3889                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3890                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3891                      var => 1,
3892                     }->{$token->{tag_name}}) {
3893              !!!cp ('t87.2');
3894              !!!parse-error (type => 'not closed',
3895                              value => $self->{open_elements}->[-1]->[0]
3896                                  ->manakai_local_name,
3897                              token => $token);
3898    
3899              pop @{$self->{open_elements}}
3900                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3901    
3902              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3903              ## Reprocess.
3904              next B;
3905            } else {
3906              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3907              my $tag_name = $token->{tag_name};
3908              if ($nsuri eq $SVG_NS) {
3909                $tag_name = {
3910                   altglyph => 'altGlyph',
3911                   altglyphdef => 'altGlyphDef',
3912                   altglyphitem => 'altGlyphItem',
3913                   animatecolor => 'animateColor',
3914                   animatemotion => 'animateMotion',
3915                   animatetransform => 'animateTransform',
3916                   clippath => 'clipPath',
3917                   feblend => 'feBlend',
3918                   fecolormatrix => 'feColorMatrix',
3919                   fecomponenttransfer => 'feComponentTransfer',
3920                   fecomposite => 'feComposite',
3921                   feconvolvematrix => 'feConvolveMatrix',
3922                   fediffuselighting => 'feDiffuseLighting',
3923                   fedisplacementmap => 'feDisplacementMap',
3924                   fedistantlight => 'feDistantLight',
3925                   feflood => 'feFlood',
3926                   fefunca => 'feFuncA',
3927                   fefuncb => 'feFuncB',
3928                   fefuncg => 'feFuncG',
3929                   fefuncr => 'feFuncR',
3930                   fegaussianblur => 'feGaussianBlur',
3931                   feimage => 'feImage',
3932                   femerge => 'feMerge',
3933                   femergenode => 'feMergeNode',
3934                   femorphology => 'feMorphology',
3935                   feoffset => 'feOffset',
3936                   fepointlight => 'fePointLight',
3937                   fespecularlighting => 'feSpecularLighting',
3938                   fespotlight => 'feSpotLight',
3939                   fetile => 'feTile',
3940                   feturbulence => 'feTurbulence',
3941                   foreignobject => 'foreignObject',
3942                   glyphref => 'glyphRef',
3943                   lineargradient => 'linearGradient',
3944                   radialgradient => 'radialGradient',
3945                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3946                   textpath => 'textPath',  
3947                }->{$tag_name} || $tag_name;
3948              }
3949    
3950              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
3951    
3952              ## "adjust foreign attributes" - done in insert-element-f
3953    
3954              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3955    
3956              if ($self->{self_closing}) {
3957                pop @{$self->{open_elements}};
3958                !!!ack ('t87.3');
3959              } else {
3960                !!!cp ('t87.4');
3961              }
3962    
3963              !!!next-token;
3964              next B;
3965            }
3966          } elsif ($token->{type} == END_TAG_TOKEN) {
3967            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3968            !!!cp ('t87.5');
3969            #
3970          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3971            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3972            !!!cp ('t87.6');
3973            #
3974            ## TODO: ...
3975          } else {
3976            die "$0: $token->{type}: Unknown token type";        
3977          }
3978        }
3979    
3980        if ($self->{insertion_mode} & HEAD_IMS) {
3981        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3982          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3983            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3345  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987              !!!cp ('t88.1');              !!!cp ('t88.1');
3988              ## Ignore the token.              ## Ignore the token.
3989              !!!next-token;              !!!next-token;
3990              redo B;              next B;
3991            }            }
3992            unless (length $token->{data}) {            unless (length $token->{data}) {
3993              !!!cp ('t88');              !!!cp ('t88');
3994              !!!next-token;              !!!next-token;
3995              redo B;              next B;
3996            }            }
3997          }          }
3998    
3999          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4000            !!!cp ('t89');            !!!cp ('t89');
4001            ## As if <head>            ## As if <head>
4002            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4003            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4004            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4005                  [$self->{head_element}, $el_category->{head}];
4006    
4007            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4008            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3369  sub _tree_construction_main ($) { Line 4012  sub _tree_construction_main ($) {
4012            !!!cp ('t90');            !!!cp ('t90');
4013            ## As if </noscript>            ## As if </noscript>
4014            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4015            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4016                        
4017            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4018            ## As if </head>            ## As if </head>
# Line 3385  sub _tree_construction_main ($) { Line 4028  sub _tree_construction_main ($) {
4028            !!!cp ('t92');            !!!cp ('t92');
4029          }          }
4030    
4031              ## "after head" insertion mode          ## "after head" insertion mode
4032              ## As if <body>          ## As if <body>
4033              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4034              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4035              ## reprocess          ## reprocess
4036              redo B;          next B;
4037            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4038              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4039                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4040                  !!!cp ('t93');              !!!cp ('t93');
4041                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4042                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4043                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4044                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4045                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4046                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4047                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4048                  !!!cp ('t94');              !!!next-token;
4049                  #              next B;
4050                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4051                  !!!cp ('t95');              !!!cp ('t94');
4052                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
4053                  ## Ignore the token            } else {
4054                  !!!next-token;              !!!cp ('t95');
4055                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4056                }              ## Ignore the token
4057              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4058                !!!cp ('t96');              !!!next-token;
4059                ## As if <head>              next B;
4060                !!!create-element ($self->{head_element}, 'head');            }
4061                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4062                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4063              ## As if <head>
4064              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4065              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4066              push @{$self->{open_elements}},
4067                  [$self->{head_element}, $el_category->{head}];
4068    
4069                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4070                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4071              } else {          } else {
4072                !!!cp ('t97');            !!!cp ('t97');
4073              }          }
4074    
4075              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4076                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4077                  !!!cp ('t98');                  !!!cp ('t98');
4078                  ## As if </noscript>                  ## As if </noscript>
4079                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4080                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4081                                
4082                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4083                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3440  sub _tree_construction_main ($) { Line 4088  sub _tree_construction_main ($) {
4088                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4089                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4090                  !!!cp ('t100');                  !!!cp ('t100');
4091                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4092                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4093                        [$self->{head_element}, $el_category->{head}];
4094                } else {                } else {
4095                  !!!cp ('t101');                  !!!cp ('t101');
4096                }                }
4097                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4098                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4099                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4100                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4101                  !!!nack ('t101.1');
4102                !!!next-token;                !!!next-token;
4103                redo B;                next B;
4104              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4105                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4106                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4107                  !!!cp ('t102');                  !!!cp ('t102');
4108                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4109                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4110                        [$self->{head_element}, $el_category->{head}];
4111                } else {                } else {
4112                  !!!cp ('t103');                  !!!cp ('t103');
4113                }                }
4114                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4115                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4116                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4117                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4118                  !!!ack ('t103.1');
4119                !!!next-token;                !!!next-token;
4120                redo B;                next B;
4121              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4122                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4123                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4124                  !!!cp ('t104');                  !!!cp ('t104');
4125                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4126                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4127                        [$self->{head_element}, $el_category->{head}];
4128                } else {                } else {
4129                  !!!cp ('t105');                  !!!cp ('t105');
4130                }                }
4131                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4132                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.
4133    
4134                unless ($self->{confident}) {                unless ($self->{confident}) {
4135                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4136                    !!!cp ('t106');                    !!!cp ('t106');
4137                    $self->{change_encoding}                    $self->{change_encoding}
4138                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4139                             $token);
4140                                        
4141                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4142                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3497  sub _tree_construction_main ($) { Line 4151  sub _tree_construction_main ($) {
4151                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4152                      !!!cp ('t107');                      !!!cp ('t107');
4153                      $self->{change_encoding}                      $self->{change_encoding}
4154                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4155                               $token);
4156                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4157                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4158                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3525  sub _tree_construction_main ($) { Line 4180  sub _tree_construction_main ($) {
4180    
4181                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4182                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4183                  !!!ack ('t110.1');
4184                !!!next-token;                !!!next-token;
4185                redo B;                next B;
4186              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4187                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4188                  !!!cp ('t111');                  !!!cp ('t111');
4189                  ## As if </noscript>                  ## As if </noscript>
4190                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4191                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4192                                
4193                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4194                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4195                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                  !!!cp ('t112');                  !!!cp ('t112');
4197                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4199                        [$self->{head_element}, $el_category->{head}];
4200                } else {                } else {
4201                  !!!cp ('t113');                  !!!cp ('t113');
4202                }                }
# Line 3550  sub _tree_construction_main ($) { Line 4207  sub _tree_construction_main ($) {
4207                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4208                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4209                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4210                redo B;                next B;
4211              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4212                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4213                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4214                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4215                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4216                  !!!cp ('t114');                  !!!cp ('t114');
4217                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4218                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4219                        [$self->{head_element}, $el_category->{head}];
4220                } else {                } else {
4221                  !!!cp ('t115');                  !!!cp ('t115');
4222                }                }
4223                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4224                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4225                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4226                redo B;                next B;
4227              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4228                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4229                  !!!cp ('t116');                  !!!cp ('t116');
4230                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4231                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4232                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4233                    !!!nack ('t116.1');
4234                  !!!next-token;                  !!!next-token;
4235                  redo B;                  next B;
4236                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4237                  !!!cp ('t117');                  !!!cp ('t117');
4238                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4239                  ## Ignore the token                  ## Ignore the token
4240                    !!!nack ('t117.1');
4241                  !!!next-token;                  !!!next-token;
4242                  redo B;                  next B;
4243                } else {                } else {
4244                  !!!cp ('t118');                  !!!cp ('t118');
4245                  #                  #
# Line 3589  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249                  !!!cp ('t119');                  !!!cp ('t119');
4250                  ## As if </noscript>                  ## As if </noscript>
4251                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4252                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', 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 ('t120');                  !!!cp ('t120');
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 ('t121');                  !!!cp ('t121');
4263                }                }
# Line 3605  sub _tree_construction_main ($) { Line 4266  sub _tree_construction_main ($) {
4266                $script_start_tag->();                $script_start_tag->();
4267                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4268                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4269                redo B;                next B;
4270              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4271                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4272                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4273                  !!!cp ('t122');                  !!!cp ('t122');
4274                  ## As if </noscript>                  ## As if </noscript>
4275                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4276                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4277                                    
4278                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4279                  ## As if </head>                  ## As if </head>
# Line 3629  sub _tree_construction_main ($) { Line 4290  sub _tree_construction_main ($) {
4290                }                }
4291    
4292                ## "after head" insertion mode                ## "after head" insertion mode
4293                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4294                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4295                  !!!cp ('t126');                  !!!cp ('t126');
4296                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3639  sub _tree_construction_main ($) { Line 4300  sub _tree_construction_main ($) {
4300                } else {                } else {
4301                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4302                }                }
4303                  !!!nack ('t127.1');
4304                !!!next-token;                !!!next-token;
4305                redo B;                next B;
4306              } else {              } else {
4307                !!!cp ('t128');                !!!cp ('t128');
4308                #                #
# Line 3650  sub _tree_construction_main ($) { Line 4312  sub _tree_construction_main ($) {
4312                !!!cp ('t129');                !!!cp ('t129');
4313                ## As if </noscript>                ## As if </noscript>
4314                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4315                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4316                                
4317                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4318                ## As if </head>                ## As if </head>
# Line 3669  sub _tree_construction_main ($) { Line 4331  sub _tree_construction_main ($) {
4331    
4332              ## "after head" insertion mode              ## "after head" insertion mode
4333              ## As if <body>              ## As if <body>
4334              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4335              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4336              ## reprocess              ## reprocess
4337              redo B;              !!!ack-later;
4338                next B;
4339            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4340              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4341                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4342                  !!!cp ('t132');                  !!!cp ('t132');
4343                  ## As if <head>                  ## As if <head>
4344                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4345                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4346                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4347                        [$self->{head_element}, $el_category->{head}];
4348    
4349                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4350                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4351                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                  !!!cp ('t133');                  !!!cp ('t133');
4356                  ## As if </noscript>                  ## As if </noscript>
4357                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4358                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4359                                    
4360                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4361                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4362                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4363                  !!!next-token;                  !!!next-token;
4364                  redo B;                  next B;
4365                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4366                  !!!cp ('t134');                  !!!cp ('t134');
4367                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4368                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4369                  !!!next-token;                  !!!next-token;
4370                  redo B;                  next B;
4371                } else {                } else {
4372                  !!!cp ('t135');                  !!!cp ('t135');
4373                  #                  #
# Line 3714  sub _tree_construction_main ($) { Line 4378  sub _tree_construction_main ($) {
4378                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4379                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4380                  !!!next-token;                  !!!next-token;
4381                  redo B;                  next B;
4382                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4383                  !!!cp ('t137');                  !!!cp ('t137');
4384                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4385                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  next B;
4388                } else {                } else {
4389                  !!!cp ('t138');                  !!!cp ('t138');
4390                  #                  #
# Line 3731  sub _tree_construction_main ($) { Line 4395  sub _tree_construction_main ($) {
4395                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4396                  !!!cp ('t139');                  !!!cp ('t139');
4397                  ## As if <head>                  ## As if <head>
4398                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4399                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4400                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4401                        [$self->{head_element}, $el_category->{head}];
4402    
4403                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4404                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4405                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4406                  !!!cp ('t140');                  !!!cp ('t140');
4407                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4408                  ## Ignore the token                  ## Ignore the token
4409                  !!!next-token;                  !!!next-token;
4410                  redo B;                  next B;
4411                } else {                } else {
4412                  !!!cp ('t141');                  !!!cp ('t141');
4413                }                }
# Line 3754  sub _tree_construction_main ($) { Line 4419  sub _tree_construction_main ($) {
4419                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4420                  !!!cp ('t142');                  !!!cp ('t142');
4421                  ## As if <head>                  ## As if <head>
4422                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4423                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4424                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4425                        [$self->{head_element}, $el_category->{head}];
4426    
4427                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4428                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3771  sub _tree_construction_main ($) { Line 4437  sub _tree_construction_main ($) {
4437                  #                  #
4438                } else {                } else {
4439                  !!!cp ('t145');                  !!!cp ('t145');
4440                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4441                  ## Ignore the token                  ## Ignore the token
4442                  !!!next-token;                  !!!next-token;
4443                  redo B;                  next B;
4444                }                }
4445              }              }
4446    
# Line 3782  sub _tree_construction_main ($) { Line 4448  sub _tree_construction_main ($) {
4448                !!!cp ('t146');                !!!cp ('t146');
4449                ## As if </noscript>                ## As if </noscript>
4450                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4451                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4452                                
4453                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4454                ## As if </head>                ## As if </head>
# Line 3798  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4465  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4466                !!!cp ('t148');                !!!cp ('t148');
4467                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4468                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4469                !!!next-token;                !!!next-token;
4470                redo B;                next B;
4471              } else {              } else {
4472                !!!cp ('t149');                !!!cp ('t149');
4473              }              }
4474    
4475              ## "after head" insertion mode              ## "after head" insertion mode
4476              ## As if <body>              ## As if <body>
4477              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4478              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4479              ## reprocess              ## reprocess
4480              redo B;              next B;
4481            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4482              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4483            }            !!!cp ('t149.1');
4484    
4485              ## NOTE: As if <head>
4486              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4487              $self->{open_elements}->[-1]->[0]->append_child
4488                  ($self->{head_element});
4489              #push @{$self->{open_elements}},
4490              #    [$self->{head_element}, $el_category->{head}];
4491              #$self->{insertion_mode} = IN_HEAD_IM;
4492              ## NOTE: Reprocess.
4493    
4494              ## NOTE: As if </head>
4495              #pop @{$self->{open_elements}};
4496              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4497              ## NOTE: Reprocess.
4498              
4499              #
4500            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4501              !!!cp ('t149.2');
4502    
4503              ## NOTE: As if </head>
4504              pop @{$self->{open_elements}};
4505              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4506              ## NOTE: Reprocess.
4507    
4508              #
4509            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4510              !!!cp ('t149.3');
4511    
4512              !!!parse-error (type => 'in noscript:#eof', token => $token);
4513    
4514              ## As if </noscript>
4515              pop @{$self->{open_elements}};
4516              #$self->{insertion_mode} = IN_HEAD_IM;
4517              ## NOTE: Reprocess.
4518    
4519              ## NOTE: As if </head>
4520              pop @{$self->{open_elements}};
4521              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4522              ## NOTE: Reprocess.
4523    
4524              #
4525            } else {
4526              !!!cp ('t149.4');
4527              #
4528            }
4529    
4530            ## NOTE: As if <body>
4531            !!!insert-element ('body',, $token);
4532            $self->{insertion_mode} = IN_BODY_IM;
4533            ## NOTE: Reprocess.
4534            next B;
4535          } else {
4536            die "$0: $token->{type}: Unknown token type";
4537          }
4538    
4539            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4540      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 4546  sub _tree_construction_main ($) {
4546              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4547    
4548              !!!next-token;              !!!next-token;
4549              redo B;              next B;
4550            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4551              if ({              if ({
4552                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3834  sub _tree_construction_main ($) { Line 4554  sub _tree_construction_main ($) {
4554                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4555                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4556                  ## have an element in table scope                  ## have an element in table scope
4557                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4558                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4559                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4560                      !!!cp ('t151');                      !!!cp ('t151');
4561                      $tn = $node->[1];  
4562                      last INSCOPE;                      ## Close the cell
4563                    } elsif ({                      !!!back-token; # <x>
4564                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4565                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4566                                  line => $token->{line},
4567                                  column => $token->{column}};
4568                        next B;
4569                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4570                      !!!cp ('t152');                      !!!cp ('t152');
4571                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4572                        last;
4573                    }                    }
4574                  } # INSCOPE                  }
4575                    unless (defined $tn) {  
4576                      !!!cp ('t153');                  !!!cp ('t153');
4577  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4578                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4579                      ## Ignore the token                  ## Ignore the token
4580                      !!!next-token;                  !!!nack ('t153.1');
4581                      redo B;                  !!!next-token;
4582                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4583                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4584                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4585                                    
4586                  ## As if </caption>                  ## NOTE: As if </caption>.
4587                  ## have a table element in table scope                  ## have a table element in table scope
4588                  my $i;                  my $i;
4589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4590                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4591                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4592                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4593                      $i = $_;                        !!!cp ('t155');
4594                      last INSCOPE;                        $i = $_;
4595                    } elsif ({                        last INSCOPE;
4596                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4597                             }->{$node->[1]}) {                        !!!cp ('t156');
4598                      !!!cp ('t156');                        last;
4599                      last INSCOPE;                      }
4600                    }                    }
4601    
4602                      !!!cp ('t157');
4603                      !!!parse-error (type => 'start tag not allowed',
4604                                      value => $token->{tag_name}, token => $token);
4605                      ## Ignore the token
4606                      !!!nack ('t157.1');
4607                      !!!next-token;
4608                      next B;
4609                  } # 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;  
                   }  
4610                                    
4611                  ## generate implied end tags                  ## generate implied end tags
4612                  while ({                  while ($self->{open_elements}->[-1]->[1]
4613                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4614                    !!!cp ('t158');                    !!!cp ('t158');
4615                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4616                  }                  }
4617    
4618                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4619                    !!!cp ('t159');                    !!!cp ('t159');
4620                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4621                                      value => $self->{open_elements}->[-1]->[0]
4622                                          ->manakai_local_name,
4623                                      token => $token);
4624                  } else {                  } else {
4625                    !!!cp ('t160');                    !!!cp ('t160');
4626                  }                  }
# Line 3912  sub _tree_construction_main ($) { Line 4632  sub _tree_construction_main ($) {
4632                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4633                                    
4634                  ## reprocess                  ## reprocess
4635                  redo B;                  !!!ack-later;
4636                    next B;
4637                } else {                } else {
4638                  !!!cp ('t161');                  !!!cp ('t161');
4639                  #                  #
# Line 3928  sub _tree_construction_main ($) { Line 4649  sub _tree_construction_main ($) {
4649                  my $i;                  my $i;
4650                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4651                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4652                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4653                      !!!cp ('t163');                      !!!cp ('t163');
4654                      $i = $_;                      $i = $_;
4655                      last INSCOPE;                      last INSCOPE;
4656                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4657                      !!!cp ('t164');                      !!!cp ('t164');
4658                      last INSCOPE;                      last INSCOPE;
4659                    }                    }
4660                  } # INSCOPE                  } # INSCOPE
4661                    unless (defined $i) {                    unless (defined $i) {
4662                      !!!cp ('t165');                      !!!cp ('t165');
4663                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4664                      ## Ignore the token                      ## Ignore the token
4665                      !!!next-token;                      !!!next-token;
4666                      redo B;                      next B;
4667                    }                    }
4668                                    
4669                  ## generate implied end tags                  ## generate implied end tags
4670                  while ({                  while ($self->{open_elements}->[-1]->[1]
4671                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4672                    !!!cp ('t166');                    !!!cp ('t166');
4673                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4674                  }                  }
4675    
4676                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4677                            ne $token->{tag_name}) {
4678                    !!!cp ('t167');                    !!!cp ('t167');
4679                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4680                                      value => $self->{open_elements}->[-1]->[0]
4681                                          ->manakai_local_name,
4682                                      token => $token);
4683                  } else {                  } else {
4684                    !!!cp ('t168');                    !!!cp ('t168');
4685                  }                  }
# Line 3969  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4692                                    
4693                  !!!next-token;                  !!!next-token;
4694                  redo B;                  next B;
4695                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4696                  !!!cp ('t169');                  !!!cp ('t169');
4697                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4698                  ## Ignore the token                  ## Ignore the token
4699                  !!!next-token;                  !!!next-token;
4700                  redo B;                  next B;
4701                } else {                } else {
4702                  !!!cp ('t170');                  !!!cp ('t170');
4703                  #                  #
# Line 3984  sub _tree_construction_main ($) { Line 4706  sub _tree_construction_main ($) {
4706                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4707                  ## have a table element in table scope                  ## have a table element in table scope
4708                  my $i;                  my $i;
4709                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4710                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4711                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4712                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4713                      $i = $_;                        !!!cp ('t171');
4714                      last INSCOPE;                        $i = $_;
4715                    } elsif ({                        last INSCOPE;
4716                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4717                             }->{$node->[1]}) {                        !!!cp ('t172');
4718                      !!!cp ('t172');                        last;
4719                      last INSCOPE;                      }
4720                    }                    }
4721    
4722                      !!!cp ('t173');
4723                      !!!parse-error (type => 'unmatched end tag',
4724                                      value => $token->{tag_name}, token => $token);
4725                      ## Ignore the token
4726                      !!!next-token;
4727                      next B;
4728                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
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 ('t174');                    !!!cp ('t174');
4734                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4735                  }                  }
4736                                    
4737                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4738                    !!!cp ('t175');                    !!!cp ('t175');
4739                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4740                                      value => $self->{open_elements}->[-1]->[0]
4741                                          ->manakai_local_name,
4742                                      token => $token);
4743                  } else {                  } else {
4744                    !!!cp ('t176');                    !!!cp ('t176');
4745                  }                  }
# Line 4027  sub _tree_construction_main ($) { Line 4751  sub _tree_construction_main ($) {
4751                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4752                                    
4753                  !!!next-token;                  !!!next-token;
4754                  redo B;                  next B;
4755                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4756                  !!!cp ('t177');                  !!!cp ('t177');
4757                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4758                  ## Ignore the token                  ## Ignore the token
4759                  !!!next-token;                  !!!next-token;
4760                  redo B;                  next B;
4761                } else {                } else {
4762                  !!!cp ('t178');                  !!!cp ('t178');
4763                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                ## have an element in table scope                ## have an element in table scope
4771                my $i;                my $i;
4772                my $tn;                my $tn;
4773                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4774                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4775                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4776                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4777                    $i = $_;                      !!!cp ('t179');
4778                    last INSCOPE;                      $i = $_;
4779                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4780                    !!!cp ('t180');                      ## Close the cell
4781                    $tn = $node->[1];                      !!!back-token; # </x>
4782                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4783                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4784                  } elsif ({                                column => $token->{column}};
4785                            table => 1, html => 1,                      next B;
4786                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4787                    !!!cp ('t181');                      !!!cp ('t180');
4788                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4789                        ## NOTE: There is exactly one |td| or |th| element
4790                        ## in scope in the stack of open elements by definition.
4791                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4792                        ## ISSUE: Can this be reached?
4793                        !!!cp ('t181');
4794                        last;
4795                      }
4796                  }                  }
4797                } # INSCOPE  
               unless (defined $i) {  
4798                  !!!cp ('t182');                  !!!cp ('t182');
4799                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4800                        value => $token->{tag_name}, token => $token);
4801                  ## Ignore the token                  ## Ignore the token
4802                  !!!next-token;                  !!!next-token;
4803                  redo B;                  next B;
4804                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4805              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4806                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4807                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4808    
4809                ## As if </caption>                ## As if </caption>
4810                ## have a table element in table scope                ## have a table element in table scope
4811                my $i;                my $i;
4812                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4813                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4814                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4815                    !!!cp ('t184');                    !!!cp ('t184');
4816                    $i = $_;                    $i = $_;
4817                    last INSCOPE;                    last INSCOPE;
4818                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4819                    !!!cp ('t185');                    !!!cp ('t185');
4820                    last INSCOPE;                    last INSCOPE;
4821                  }                  }
4822                } # INSCOPE                } # INSCOPE
4823                unless (defined $i) {                unless (defined $i) {
4824                  !!!cp ('t186');                  !!!cp ('t186');
4825                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4826                  ## Ignore the token                  ## Ignore the token
4827                  !!!next-token;                  !!!next-token;
4828                  redo B;                  next B;
4829                }                }
4830                                
4831                ## generate implied end tags                ## generate implied end tags
4832                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4833                  !!!cp ('t187');                  !!!cp ('t187');
4834                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4835                }                }
4836    
4837                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4838                  !!!cp ('t188');                  !!!cp ('t188');
4839                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4840                                    value => $self->{open_elements}->[-1]->[0]
4841                                        ->manakai_local_name,
4842                                    token => $token);
4843                } else {                } else {
4844                  !!!cp ('t189');                  !!!cp ('t189');
4845                }                }
# Line 4128  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4852    
4853                ## reprocess                ## reprocess
4854                redo B;                next B;
4855              } elsif ({              } elsif ({
4856                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4857                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4858                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4859                  !!!cp ('t190');                  !!!cp ('t190');
4860                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4861                  ## Ignore the token                  ## Ignore the token
4862                  !!!next-token;                  !!!next-token;
4863                  redo B;                  next B;
4864                } else {                } else {
4865                  !!!cp ('t191');                  !!!cp ('t191');
4866                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4872                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4873                !!!cp ('t192');                !!!cp ('t192');
4874                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                ## Ignore the token                ## Ignore the token
4876                !!!next-token;                !!!next-token;
4877                redo B;                next B;
4878              } else {              } else {
4879                !!!cp ('t193');                !!!cp ('t193');
4880                #                #
4881              }              }
4882          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4883            for my $entry (@{$self->{open_elements}}) {
4884              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4885                !!!cp ('t75');
4886                !!!parse-error (type => 'in body:#eof', token => $token);
4887                last;
4888              }
4889            }
4890    
4891            ## Stop parsing.
4892            last B;
4893        } else {        } else {
4894          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4895        }        }
# Line 4171  sub _tree_construction_main ($) { Line 4905  sub _tree_construction_main ($) {
4905            unless (length $token->{data}) {            unless (length $token->{data}) {
4906              !!!cp ('t194');              !!!cp ('t194');
4907              !!!next-token;              !!!next-token;
4908              redo B;              next B;
4909            } else {            } else {
4910              !!!cp ('t195');              !!!cp ('t195');
4911            }            }
4912          }          }
4913    
4914              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4915    
4916              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4917              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4185  sub _tree_construction_main ($) { Line 4919  sub _tree_construction_main ($) {
4919              ## result in a new Text node.              ## result in a new Text node.
4920              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4921                            
4922              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]}) {  
4923                # MUST                # MUST
4924                my $foster_parent_element;                my $foster_parent_element;
4925                my $next_sibling;                my $next_sibling;
4926                my $prev_sibling;                my $prev_sibling;
4927                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4928                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4929                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4930                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4931                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4229  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
4960          }          }
4961                            
4962          !!!next-token;          !!!next-token;
4963          redo B;          next B;
4964        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4965              if ({              if ({
4966                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4237  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4969                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4970                  ## Clear back to table context                  ## Clear back to table context
4971                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4972                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4973                    !!!cp ('t201');                    !!!cp ('t201');
4974                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4975                  }                  }
4976                                    
4977                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4978                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4979                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4980                }                }
# Line 4251  sub _tree_construction_main ($) { Line 4982  sub _tree_construction_main ($) {
4982                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4983                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4984                    !!!cp ('t202');                    !!!cp ('t202');
4985                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4986                  }                  }
4987                                    
4988                  ## Clear back to table body context                  ## Clear back to table body context
4989                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4990                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4991                    !!!cp ('t203');                    !!!cp ('t203');
4992                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
4993                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4266  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4997                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4998                    !!!cp ('t204');                    !!!cp ('t204');
4999                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5000                      !!!nack ('t204');
5001                    !!!next-token;                    !!!next-token;
5002                    redo B;                    next B;
5003                  } else {                  } else {
5004                    !!!cp ('t205');                    !!!cp ('t205');
5005                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5006                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5007                  }                  }
5008                } else {                } else {
# Line 4279  sub _tree_construction_main ($) { Line 5010  sub _tree_construction_main ($) {
5010                }                }
5011    
5012                ## Clear back to table row context                ## Clear back to table row context
5013                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5014                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5015                  !!!cp ('t207');                  !!!cp ('t207');
5016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5017                }                }
5018                                
5019                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5020                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5021    
5022                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5023                                
5024                  !!!nack ('t207.1');
5025                !!!next-token;                !!!next-token;
5026                redo B;                next B;
5027              } elsif ({              } elsif ({
5028                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5029                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5035  sub _tree_construction_main ($) {
5035                  my $i;                  my $i;
5036                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5037                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5038                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5039                      !!!cp ('t208');                      !!!cp ('t208');
5040                      $i = $_;                      $i = $_;
5041                      last INSCOPE;                      last INSCOPE;
5042                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5043                      !!!cp ('t209');                      !!!cp ('t209');
5044                      last INSCOPE;                      last INSCOPE;
5045                    }                    }
5046                  } # INSCOPE                  } # INSCOPE
5047                  unless (defined $i) {                  unless (defined $i) {
5048                   !!!cp ('t210');                    !!!cp ('t210');
5049  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5050                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5051                    ## Ignore the token                    ## Ignore the token
5052                      !!!nack ('t210.1');
5053                    !!!next-token;                    !!!next-token;
5054                    redo B;                    next B;
5055                  }                  }
5056                                    
5057                  ## Clear back to table row context                  ## Clear back to table row context
5058                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5059                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5060                    !!!cp ('t211');                    !!!cp ('t211');
5061                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5062                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4341  sub _tree_construction_main ($) { Line 5067  sub _tree_construction_main ($) {
5067                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5068                    !!!cp ('t212');                    !!!cp ('t212');
5069                    ## reprocess                    ## reprocess
5070                    redo B;                    !!!ack-later;
5071                      next B;
5072                  } else {                  } else {
5073                    !!!cp ('t213');                    !!!cp ('t213');
5074                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4353  sub _tree_construction_main ($) { Line 5080  sub _tree_construction_main ($) {
5080                  my $i;                  my $i;
5081                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5082                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5083                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5084                      !!!cp ('t214');                      !!!cp ('t214');
5085                      $i = $_;                      $i = $_;
5086                      last INSCOPE;                      last INSCOPE;
5087                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5088                      !!!cp ('t215');                      !!!cp ('t215');
5089                      last INSCOPE;                      last INSCOPE;
5090                    }                    }
# Line 4369  sub _tree_construction_main ($) { Line 5092  sub _tree_construction_main ($) {
5092                  unless (defined $i) {                  unless (defined $i) {
5093                    !!!cp ('t216');                    !!!cp ('t216');
5094  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5095                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5096                    ## Ignore the token                    ## Ignore the token
5097                      !!!nack ('t216.1');
5098                    !!!next-token;                    !!!next-token;
5099                    redo B;                    next B;
5100                  }                  }
5101    
5102                  ## Clear back to table body context                  ## Clear back to table body context
5103                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5104                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5105                    !!!cp ('t217');                    !!!cp ('t217');
5106                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5107                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4400  sub _tree_construction_main ($) { Line 5123  sub _tree_construction_main ($) {
5123    
5124                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5125                  ## Clear back to table context                  ## Clear back to table context
5126                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5127                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5128                    !!!cp ('t219');                    !!!cp ('t219');
5129                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5130                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5131                  }                  }
5132                                    
5133                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5134                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5135                  ## reprocess                  ## reprocess
5136                  redo B;                  !!!ack-later;
5137                    next B;
5138                } elsif ({                } elsif ({
5139                          caption => 1,                          caption => 1,
5140                          colgroup => 1,                          colgroup => 1,
5141                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5142                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5143                  ## Clear back to table context                  ## Clear back to table context
5144                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5145                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5146                    !!!cp ('t220');                    !!!cp ('t220');
5147                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5148                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4427  sub _tree_construction_main ($) { Line 5151  sub _tree_construction_main ($) {
5151                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5152                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5153                                    
5154                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5156                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5157                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4436  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5161                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5162                  !!!next-token;                  !!!next-token;
5163                  redo B;                  !!!nack ('t220.1');
5164                    next B;
5165                } else {                } else {
5166                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5167                }                }
5168              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5169                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5170                                  value => $self->{open_elements}->[-1]->[0]
5171                                      ->manakai_local_name,
5172                                  token => $token);
5173    
5174                ## As if </table>                ## As if </table>
5175                ## have a table element in table scope                ## have a table element in table scope
5176                my $i;                my $i;
5177                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5178                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5179                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5180                    !!!cp ('t221');                    !!!cp ('t221');
5181                    $i = $_;                    $i = $_;
5182                    last INSCOPE;                    last INSCOPE;
5183                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5184                    !!!cp ('t222');                    !!!cp ('t222');
5185                    last INSCOPE;                    last INSCOPE;
5186                  }                  }
# Line 4463  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188                unless (defined $i) {                unless (defined $i) {
5189                  !!!cp ('t223');                  !!!cp ('t223');
5190  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5191                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5192                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5193                    !!!nack ('t223.1');
5194                  !!!next-token;                  !!!next-token;
5195                  redo B;                  next B;
5196                }                }
5197                                
5198    ## TODO: Followings are removed from the latest spec.
5199                ## generate implied end tags                ## generate implied end tags
5200                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5201                  !!!cp ('t224');                  !!!cp ('t224');
5202                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5203                }                }
5204    
5205                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5206                  !!!cp ('t225');                  !!!cp ('t225');
5207  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5208                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5209                                    value => $self->{open_elements}->[-1]->[0]
5210                                        ->manakai_local_name,
5211                                    token => $token);
5212                } else {                } else {
5213                  !!!cp ('t226');                  !!!cp ('t226');
5214                }                }
# Line 4490  sub _tree_construction_main ($) { Line 5218  sub _tree_construction_main ($) {
5218    
5219                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5220    
5221                ## reprocess            ## reprocess
5222                redo B;            !!!ack-later;
5223              next B;
5224          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5225            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5226              !!!cp ('t227.8');              !!!cp ('t227.8');
5227              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5228              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5229              redo B;              next B;
5230            } else {            } else {
5231              !!!cp ('t227.7');              !!!cp ('t227.7');
5232              #              #
# Line 4507  sub _tree_construction_main ($) { Line 5236  sub _tree_construction_main ($) {
5236              !!!cp ('t227.6');              !!!cp ('t227.6');
5237              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5238              $script_start_tag->();              $script_start_tag->();
5239              redo B;              next B;
5240            } else {            } else {
5241              !!!cp ('t227.5');              !!!cp ('t227.5');
5242              #              #
# Line 4518  sub _tree_construction_main ($) { Line 5247  sub _tree_construction_main ($) {
5247                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5248                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5249                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5250                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5251    
5252                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253    
5254                  ## TODO: form element pointer                  ## TODO: form element pointer
5255    
5256                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5257    
5258                  !!!next-token;                  !!!next-token;
5259                  redo B;                  !!!ack ('t227.2.1');
5260                    next B;
5261                } else {                } else {
5262                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5263                  #                  #
# Line 4545  sub _tree_construction_main ($) { Line 5275  sub _tree_construction_main ($) {
5275            #            #
5276          }          }
5277    
5278          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5279    
5280          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5281          #          #
# Line 4556  sub _tree_construction_main ($) { Line 5286  sub _tree_construction_main ($) {
5286                my $i;                my $i;
5287                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5288                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5289                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5290                    !!!cp ('t228');                    !!!cp ('t228');
5291                    $i = $_;                    $i = $_;
5292                    last INSCOPE;                    last INSCOPE;
5293                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5294                    !!!cp ('t229');                    !!!cp ('t229');
5295                    last INSCOPE;                    last INSCOPE;
5296                  }                  }
5297                } # INSCOPE                } # INSCOPE
5298                unless (defined $i) {                unless (defined $i) {
5299                  !!!cp ('t230');                  !!!cp ('t230');
5300                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5301                  ## Ignore the token                  ## Ignore the token
5302                    !!!nack ('t230.1');
5303                  !!!next-token;                  !!!next-token;
5304                  redo B;                  next B;
5305                } else {                } else {
5306                  !!!cp ('t232');                  !!!cp ('t232');
5307                }                }
5308    
5309                ## Clear back to table row context                ## Clear back to table row context
5310                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5311                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5312                  !!!cp ('t231');                  !!!cp ('t231');
5313  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5314                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4589  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5318                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5319                !!!next-token;                !!!next-token;
5320                redo B;                !!!nack ('t231.1');
5321                  next B;
5322              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5323                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5324                  ## As if </tr>                  ## As if </tr>
# Line 4597  sub _tree_construction_main ($) { Line 5326  sub _tree_construction_main ($) {
5326                  my $i;                  my $i;
5327                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5328                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5329                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5330                      !!!cp ('t233');                      !!!cp ('t233');
5331                      $i = $_;                      $i = $_;
5332                      last INSCOPE;                      last INSCOPE;
5333                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5334                      !!!cp ('t234');                      !!!cp ('t234');
5335                      last INSCOPE;                      last INSCOPE;
5336                    }                    }
# Line 4611  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  unless (defined $i) {                  unless (defined $i) {
5339                    !!!cp ('t235');                    !!!cp ('t235');
5340  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5341                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5342                    ## Ignore the token                    ## Ignore the token
5343                      !!!nack ('t236.1');
5344                    !!!next-token;                    !!!next-token;
5345                    redo B;                    next B;
5346                  }                  }
5347                                    
5348                  ## Clear back to table row context                  ## Clear back to table row context
5349                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5350                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5351                    !!!cp ('t236');                    !!!cp ('t236');
5352  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5353                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4636  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363                  my $i;                  my $i;
5364                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5365                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5366                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5367                      !!!cp ('t237');                      !!!cp ('t237');
5368                      $i = $_;                      $i = $_;
5369                      last INSCOPE;                      last INSCOPE;
5370                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5371                      !!!cp ('t238');                      !!!cp ('t238');
5372                      last INSCOPE;                      last INSCOPE;
5373                    }                    }
5374                  } # INSCOPE                  } # INSCOPE
5375                  unless (defined $i) {                  unless (defined $i) {
5376                    !!!cp ('t239');                    !!!cp ('t239');
5377                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5378                    ## Ignore the token                    ## Ignore the token
5379                      !!!nack ('t239.1');
5380                    !!!next-token;                    !!!next-token;
5381                    redo B;                    next B;
5382                  }                  }
5383                                    
5384                  ## Clear back to table body context                  ## Clear back to table body context
5385                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5386                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5387                    !!!cp ('t240');                    !!!cp ('t240');
5388                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5389                  }                  }
# Line 4686  sub _tree_construction_main ($) { Line 5409  sub _tree_construction_main ($) {
5409                my $i;                my $i;
5410                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5411                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5412                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5413                    !!!cp ('t241');                    !!!cp ('t241');
5414                    $i = $_;                    $i = $_;
5415                    last INSCOPE;                    last INSCOPE;
5416                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5417                    !!!cp ('t242');                    !!!cp ('t242');
5418                    last INSCOPE;                    last INSCOPE;
5419                  }                  }
5420                } # INSCOPE                } # INSCOPE
5421                unless (defined $i) {                unless (defined $i) {
5422                  !!!cp ('t243');                  !!!cp ('t243');
5423                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5424                  ## Ignore the token                  ## Ignore the token
5425                    !!!nack ('t243.1');
5426                  !!!next-token;                  !!!next-token;
5427                  redo B;                  next B;
5428                }                }
5429                                    
5430                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4711  sub _tree_construction_main ($) { Line 5433  sub _tree_construction_main ($) {
5433                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5434                                
5435                !!!next-token;                !!!next-token;
5436                redo B;                next B;
5437              } elsif ({              } elsif ({
5438                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5439                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4721  sub _tree_construction_main ($) { Line 5443  sub _tree_construction_main ($) {
5443                  my $i;                  my $i;
5444                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5445                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5446                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5447                      !!!cp ('t247');                      !!!cp ('t247');
5448                      $i = $_;                      $i = $_;
5449                      last INSCOPE;                      last INSCOPE;
5450                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5451                      !!!cp ('t248');                      !!!cp ('t248');
5452                      last INSCOPE;                      last INSCOPE;
5453                    }                    }
5454                  } # INSCOPE                  } # INSCOPE
5455                    unless (defined $i) {                    unless (defined $i) {
5456                      !!!cp ('t249');                      !!!cp ('t249');
5457                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5458                      ## Ignore the token                      ## Ignore the token
5459                        !!!nack ('t249.1');
5460                      !!!next-token;                      !!!next-token;
5461                      redo B;                      next B;
5462                    }                    }
5463                                    
5464                  ## As if </tr>                  ## As if </tr>
# Line 4745  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  my $i;                  my $i;
5467                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5468                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5469                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5470                      !!!cp ('t250');                      !!!cp ('t250');
5471                      $i = $_;                      $i = $_;
5472                      last INSCOPE;                      last INSCOPE;
5473                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5474                      !!!cp ('t251');                      !!!cp ('t251');
5475                      last INSCOPE;                      last INSCOPE;
5476                    }                    }
5477                  } # INSCOPE                  } # INSCOPE
5478                    unless (defined $i) {                    unless (defined $i) {
5479                      !!!cp ('t252');                      !!!cp ('t252');
5480                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5481                      ## Ignore the token                      ## Ignore the token
5482                        !!!nack ('t252.1');
5483                      !!!next-token;                      !!!next-token;
5484                      redo B;                      next B;
5485                    }                    }
5486                                    
5487                  ## Clear back to table row context                  ## Clear back to table row context
5488                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5489                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5490                    !!!cp ('t253');                    !!!cp ('t253');
5491  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5492                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4782  sub _tree_construction_main ($) { Line 5501  sub _tree_construction_main ($) {
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5505                    !!!cp ('t254');                    !!!cp ('t254');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5509                    !!!cp ('t255');                    !!!cp ('t255');
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!cp ('t256');                  !!!cp ('t256');
5515                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t256.1');
5518                  !!!next-token;                  !!!next-token;
5519                  redo B;                  next B;
5520                }                }
5521    
5522                ## Clear back to table body context                ## Clear back to table body context
5523                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5524                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5525                  !!!cp ('t257');                  !!!cp ('t257');
5526  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5527                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4812  sub _tree_construction_main ($) { Line 5529  sub _tree_construction_main ($) {
5529    
5530                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5531                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5532                  !!!nack ('t257.1');
5533                !!!next-token;                !!!next-token;
5534                redo B;                next B;
5535              } elsif ({              } elsif ({
5536                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5537                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5538                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5539                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5540                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5541                !!!cp ('t258');            !!!cp ('t258');
5542                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5543                ## Ignore the token            ## Ignore the token
5544                !!!next-token;            !!!nack ('t258.1');
5545                redo B;             !!!next-token;
5546              next B;
5547          } else {          } else {
5548            !!!cp ('t259');            !!!cp ('t259');
5549            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5550    
5551            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5552            #            #
5553          }          }
5554          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5555            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5556                    @{$self->{open_elements}} == 1) { # redundant, maybe
5557              !!!parse-error (type => 'in body:#eof', token => $token);
5558              !!!cp ('t259.1');
5559              #
5560            } else {
5561              !!!cp ('t259.2');
5562              #
5563            }
5564    
5565            ## Stop parsing
5566            last B;
5567        } else {        } else {
5568          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5569        }        }
# Line 4842  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574                unless (length $token->{data}) {                unless (length $token->{data}) {
5575                  !!!cp ('t260');                  !!!cp ('t260');
5576                  !!!next-token;                  !!!next-token;
5577                  redo B;                  next B;
5578                }                }
5579              }              }
5580                            
# Line 4851  sub _tree_construction_main ($) { Line 5583  sub _tree_construction_main ($) {
5583            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5584              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5585                !!!cp ('t262');                !!!cp ('t262');
5586                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5587                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5588                  !!!ack ('t262.1');
5589                !!!next-token;                !!!next-token;
5590                redo B;                next B;
5591              } else {              } else {
5592                !!!cp ('t263');                !!!cp ('t263');
5593                #                #
5594              }              }
5595            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5596              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5597                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5598                  !!!cp ('t264');                  !!!cp ('t264');
5599                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                  !!!next-token;                  !!!next-token;
5602                  redo B;                  next B;
5603                } else {                } else {
5604                  !!!cp ('t265');                  !!!cp ('t265');
5605                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5606                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5607                  !!!next-token;                  !!!next-token;
5608                  redo B;                              next B;            
5609                }                }
5610              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5611                !!!cp ('t266');                !!!cp ('t266');
5612                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5613                ## Ignore the token                ## Ignore the token
5614                !!!next-token;                !!!next-token;
5615                redo B;                next B;
5616              } else {              } else {
5617                !!!cp ('t267');                !!!cp ('t267');
5618                #                #
5619              }              }
5620            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5621              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5622            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5623              !!!cp ('t270.2');
5624              ## Stop parsing.
5625              last B;
5626            } else {
5627              ## NOTE: As if </colgroup>.
5628              !!!cp ('t270.1');
5629              pop @{$self->{open_elements}}; # colgroup
5630              $self->{insertion_mode} = IN_TABLE_IM;
5631              ## Reprocess.
5632              next B;
5633            }
5634          } else {
5635            die "$0: $token->{type}: Unknown token type";
5636          }
5637    
5638            ## As if </colgroup>            ## As if </colgroup>
5639            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5640              !!!cp ('t269');              !!!cp ('t269');
5641              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5642                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5643              ## Ignore the token              ## Ignore the token
5644                !!!nack ('t269.1');
5645              !!!next-token;              !!!next-token;
5646              redo B;              next B;
5647            } else {            } else {
5648              !!!cp ('t270');              !!!cp ('t270');
5649              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5650              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5651                !!!ack-later;
5652              ## reprocess              ## reprocess
5653              redo B;              next B;
5654            }            }
5655      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5656        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5657          !!!cp ('t271');          !!!cp ('t271');
5658          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5659          !!!next-token;          !!!next-token;
5660          redo B;          next B;
5661        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5662              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5663                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5664                  !!!cp ('t272');              !!!cp ('t272');
5665                  ## As if </option>              ## As if </option>
5666                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5667                } else {            } else {
5668                  !!!cp ('t273');              !!!cp ('t273');
5669                }            }
5670    
5671                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5672                !!!next-token;            !!!nack ('t273.1');
5673                redo B;            !!!next-token;
5674              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5675                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5676                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5677                  ## As if </option>              !!!cp ('t274');
5678                  pop @{$self->{open_elements}};              ## As if </option>
5679                } else {              pop @{$self->{open_elements}};
5680                  !!!cp ('t275');            } else {
5681                }              !!!cp ('t275');
5682              }
5683    
5684                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5685                  !!!cp ('t276');              !!!cp ('t276');
5686                  ## As if </optgroup>              ## As if </optgroup>
5687                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5688                } else {            } else {
5689                  !!!cp ('t277');              !!!cp ('t277');
5690                }            }
5691    
5692                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5693                !!!next-token;            !!!nack ('t277.1');
5694                redo B;            !!!next-token;
5695              } elsif ($token->{tag_name} eq 'select') {            next B;
5696  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5697                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5698                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5699                ## have an element in table scope                    {
5700                my $i;                     caption => 1, table => 1,
5701                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5702                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5703                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5704                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5705                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5706                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5707                  } elsif ({            ## as if there were </select> (otherwise).
5708                            table => 1, html => 1,            ## have an element in table scope
5709                           }->{$node->[1]}) {            my $i;
5710                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5711                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5712                  }              if ($node->[1] & SELECT_EL) {
5713                } # INSCOPE                !!!cp ('t278');
5714                unless (defined $i) {                $i = $_;
5715                  !!!cp ('t280');                last INSCOPE;
5716                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5717                  ## Ignore the token                !!!cp ('t279');
5718                  !!!next-token;                last INSCOPE;
5719                  redo B;              }
5720                }            } # INSCOPE
5721              unless (defined $i) {
5722                !!!cp ('t280');
5723                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5724                ## Ignore the token
5725                !!!nack ('t280.1');
5726                !!!next-token;
5727                next B;
5728              }
5729                                
5730                !!!cp ('t281');            !!!cp ('t281');
5731                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5732    
5733                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5734    
5735                !!!next-token;            if ($token->{tag_name} eq 'select') {
5736                redo B;              !!!nack ('t281.2');
5737                !!!next-token;
5738                next B;
5739              } else {
5740                !!!cp ('t281.1');
5741                !!!ack-later;
5742                ## Reprocess the token.
5743                next B;
5744              }
5745          } else {          } else {
5746            !!!cp ('t282');            !!!cp ('t282');
5747            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5748            ## Ignore the token            ## Ignore the token
5749              !!!nack ('t282.1');
5750            !!!next-token;            !!!next-token;
5751            redo B;            next B;
5752          }          }
5753        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5754              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5755                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5756                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5757                  !!!cp ('t283');              !!!cp ('t283');
5758                  ## As if </option>              ## As if </option>
5759                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5760                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5761                  !!!cp ('t284');              !!!cp ('t284');
5762                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5763                } else {            } else {
5764                  !!!cp ('t285');              !!!cp ('t285');
5765                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5766                  ## Ignore the token              ## Ignore the token
5767                }            }
5768                !!!next-token;            !!!nack ('t285.1');
5769                redo B;            !!!next-token;
5770              } elsif ($token->{tag_name} eq 'option') {            next B;
5771                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5772                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5773                  pop @{$self->{open_elements}};              !!!cp ('t286');
5774                } else {              pop @{$self->{open_elements}};
5775                  !!!cp ('t287');            } else {
5776                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5777                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5778                }              ## Ignore the token
5779                !!!next-token;            }
5780                redo B;            !!!nack ('t287.1');
5781              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5782                ## have an element in table scope            next B;
5783                my $i;          } elsif ($token->{tag_name} eq 'select') {
5784                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5785                  my $node = $self->{open_elements}->[$_];            my $i;
5786                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5787                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5788                    $i = $_;              if ($node->[1] & SELECT_EL) {
5789                    last INSCOPE;                !!!cp ('t288');
5790                  } elsif ({                $i = $_;
5791                            table => 1, html => 1,                last INSCOPE;
5792                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5793                    !!!cp ('t289');                !!!cp ('t289');
5794                    last INSCOPE;                last INSCOPE;
5795                  }              }
5796                } # INSCOPE            } # INSCOPE
5797                unless (defined $i) {            unless (defined $i) {
5798                  !!!cp ('t290');              !!!cp ('t290');
5799                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5800                  ## Ignore the token              ## Ignore the token
5801                  !!!next-token;              !!!nack ('t290.1');
5802                  redo B;              !!!next-token;
5803                }              next B;
5804              }
5805                                
5806                !!!cp ('t291');            !!!cp ('t291');
5807                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5808    
5809                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5810    
5811                !!!next-token;            !!!nack ('t291.1');
5812                redo B;            !!!next-token;
5813              } elsif ({            next B;
5814                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5815                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5816                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5817                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5818                     }->{$token->{tag_name}}) {
5819  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5820                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5821                                
5822                ## have an element in table scope            ## have an element in table scope
5823                my $i;            my $i;
5824                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5825                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5826                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5827                    !!!cp ('t292');                !!!cp ('t292');
5828                    $i = $_;                $i = $_;
5829                    last INSCOPE;                last INSCOPE;
5830                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5831                            table => 1, html => 1,                !!!cp ('t293');
5832                           }->{$node->[1]}) {                last INSCOPE;
5833                    !!!cp ('t293');              }
5834                    last INSCOPE;            } # INSCOPE
5835                  }            unless (defined $i) {
5836                } # INSCOPE              !!!cp ('t294');
5837                unless (defined $i) {              ## Ignore the token
5838                  !!!cp ('t294');              !!!nack ('t294.1');
5839                  ## Ignore the token              !!!next-token;
5840                  !!!next-token;              next B;
5841                  redo B;            }
               }  
5842                                
5843                ## As if </select>            ## As if </select>
5844                ## have an element in table scope            ## have an element in table scope
5845                undef $i;            undef $i;
5846                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5847                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5848                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5849                    !!!cp ('t295');                !!!cp ('t295');
5850                    $i = $_;                $i = $_;
5851                    last INSCOPE;                last INSCOPE;
5852                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5853  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5854                    !!!cp ('t296');                !!!cp ('t296');
5855                    last INSCOPE;                last INSCOPE;
5856                  }              }
5857                } # INSCOPE            } # INSCOPE
5858                unless (defined $i) {            unless (defined $i) {
5859                  !!!cp ('t297');              !!!cp ('t297');
5860  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5861                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5862                  ## Ignore the </select> token              ## Ignore the </select> token
5863                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5864                  redo B;              !!!next-token; ## TODO: ok?
5865                }              next B;
5866              }
5867                                
5868                !!!cp ('t298');            !!!cp ('t298');
5869                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5870    
5871                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5872    
5873                ## reprocess            !!!ack-later;
5874                redo B;            ## reprocess
5875              next B;
5876          } else {          } else {
5877            !!!cp ('t299');            !!!cp ('t299');
5878            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5879            ## Ignore the token            ## Ignore the token
5880              !!!nack ('t299.3');
5881            !!!next-token;            !!!next-token;
5882            redo B;            next B;
5883          }          }
5884          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5885            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5886                    @{$self->{open_elements}} == 1) { # redundant, maybe
5887              !!!cp ('t299.1');
5888              !!!parse-error (type => 'in body:#eof', token => $token);
5889            } else {
5890              !!!cp ('t299.2');
5891            }
5892    
5893            ## Stop parsing.
5894            last B;
5895        } else {        } else {
5896          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5897        }        }
# Line 5125  sub _tree_construction_main ($) { Line 5907  sub _tree_construction_main ($) {
5907            unless (length $token->{data}) {            unless (length $token->{data}) {
5908              !!!cp ('t300');              !!!cp ('t300');
5909              !!!next-token;              !!!next-token;
5910              redo B;              next B;
5911            }            }
5912          }          }
5913                    
5914          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5915            !!!cp ('t301');            !!!cp ('t301');
5916            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5917    
5918            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5919          } else {          } else {
# Line 5139  sub _tree_construction_main ($) { Line 5921  sub _tree_construction_main ($) {
5921          }          }
5922                    
5923          ## "after body" insertion mode          ## "after body" insertion mode
5924          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5925    
5926          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5927          ## reprocess          ## reprocess
5928          redo B;          next B;
5929        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5930          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5931            !!!cp ('t303');            !!!cp ('t303');
5932            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5933                        
5934            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5935          } else {          } else {
# Line 5155  sub _tree_construction_main ($) { Line 5937  sub _tree_construction_main ($) {
5937          }          }
5938    
5939          ## "after body" insertion mode          ## "after body" insertion mode
5940          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5941    
5942          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5943            !!!ack-later;
5944          ## reprocess          ## reprocess
5945          redo B;          next B;
5946        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5947          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5948            !!!cp ('t305');            !!!cp ('t305');
5949            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5950                        
5951            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5952            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5175  sub _tree_construction_main ($) { Line 5958  sub _tree_construction_main ($) {
5958          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5959            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5960              !!!cp ('t307');              !!!cp ('t307');
5961              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5962              ## Ignore the token              ## Ignore the token
5963              !!!next-token;              !!!next-token;
5964              redo B;              next B;
5965            } else {            } else {
5966              !!!cp ('t308');              !!!cp ('t308');
5967              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5968              !!!next-token;              !!!next-token;
5969              redo B;              next B;
5970            }            }
5971          } else {          } else {
5972            !!!cp ('t309');            !!!cp ('t309');
5973            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5974    
5975            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5976            ## reprocess            ## reprocess
5977            redo B;            next B;
5978          }          }
5979          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5980            !!!cp ('t309.2');
5981            ## Stop parsing
5982            last B;
5983        } else {        } else {
5984          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5985        }        }
# Line 5204  sub _tree_construction_main ($) { Line 5991  sub _tree_construction_main ($) {
5991            unless (length $token->{data}) {            unless (length $token->{data}) {
5992              !!!cp ('t310');              !!!cp ('t310');
5993              !!!next-token;              !!!next-token;
5994              redo B;              next B;
5995            }            }
5996          }          }
5997                    
5998          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5999            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6000              !!!cp ('t311');              !!!cp ('t311');
6001              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6002            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6003              !!!cp ('t312');              !!!cp ('t312');
6004              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6005            } else { # "after html frameset"            } else { # "after html frameset"
6006              !!!cp ('t313');              !!!cp ('t313');
6007              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6008    
6009              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6010              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6011              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6012            }            }
6013                        
6014            ## Ignore the token.            ## Ignore the token.
# Line 5232  sub _tree_construction_main ($) { Line 6019  sub _tree_construction_main ($) {
6019              !!!cp ('t315');              !!!cp ('t315');
6020              !!!next-token;              !!!next-token;
6021            }            }
6022            redo B;            next B;
6023          }          }
6024                    
6025          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6026        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6027          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6028            !!!cp ('t316');            !!!cp ('t316');
6029            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6030    
6031            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6032            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5250  sub _tree_construction_main ($) { Line 6037  sub _tree_construction_main ($) {
6037          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6038              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6039            !!!cp ('t318');            !!!cp ('t318');
6040            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6041              !!!nack ('t318.1');
6042            !!!next-token;            !!!next-token;
6043            redo B;            next B;
6044          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6045                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6046            !!!cp ('t319');            !!!cp ('t319');
6047            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6048            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6049              !!!ack ('t319.1');
6050            !!!next-token;            !!!next-token;
6051            redo B;            next B;
6052          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6053            !!!cp ('t320');            !!!cp ('t320');
6054            ## NOTE: As if in body.            ## NOTE: As if in body.
6055            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6056            redo B;            next B;
6057          } else {          } else {
6058            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6059              !!!cp ('t321');              !!!cp ('t321');
6060              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6061            } else {            } else {
6062              !!!cp ('t322');              !!!cp ('t322');
6063              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6064            }            }
6065            ## Ignore the token            ## Ignore the token
6066              !!!nack ('t322.1');
6067            !!!next-token;            !!!next-token;
6068            redo B;            next B;
6069          }          }
6070        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6071          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6072            !!!cp ('t323');            !!!cp ('t323');
6073            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6074    
6075            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6076            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5290  sub _tree_construction_main ($) { Line 6080  sub _tree_construction_main ($) {
6080    
6081          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6082              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6083            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6084                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6085              !!!cp ('t325');              !!!cp ('t325');
6086              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6087              ## Ignore the token              ## Ignore the token
6088              !!!next-token;              !!!next-token;
6089            } else {            } else {
# Line 5303  sub _tree_construction_main ($) { Line 6093  sub _tree_construction_main ($) {
6093            }            }
6094    
6095            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6096                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6097              !!!cp ('t327');              !!!cp ('t327');
6098              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6099            } else {            } else {
6100              !!!cp ('t328');              !!!cp ('t328');
6101            }            }
6102            redo B;            next B;
6103          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6104                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6105            !!!cp ('t329');            !!!cp ('t329');
6106            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6107            !!!next-token;            !!!next-token;
6108            redo B;            next B;
6109          } else {          } else {
6110            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6111              !!!cp ('t330');              !!!cp ('t330');
6112              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6113            } else {            } else {
6114              !!!cp ('t331');              !!!cp ('t331');
6115              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6116            }            }
6117            ## Ignore the token            ## Ignore the token
6118            !!!next-token;            !!!next-token;
6119            redo B;            next B;
6120          }          }
6121          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6122            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6123                    @{$self->{open_elements}} == 1) { # redundant, maybe
6124              !!!cp ('t331.1');
6125              !!!parse-error (type => 'in body:#eof', token => $token);
6126            } else {
6127              !!!cp ('t331.2');
6128            }
6129            
6130            ## Stop parsing
6131            last B;
6132        } else {        } else {
6133          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6134        }        }
# Line 5343  sub _tree_construction_main ($) { Line 6144  sub _tree_construction_main ($) {
6144          !!!cp ('t332');          !!!cp ('t332');
6145          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6146          $script_start_tag->();          $script_start_tag->();
6147          redo B;          next B;
6148        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6149          !!!cp ('t333');          !!!cp ('t333');
6150          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6151          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6152          redo B;          next B;
6153        } elsif ({        } elsif ({
6154                  base => 1, link => 1,                  base => 1, link => 1,
6155                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6156          !!!cp ('t334');          !!!cp ('t334');
6157          ## 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
6158          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6159          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6160            !!!ack ('t334.1');
6161          !!!next-token;          !!!next-token;
6162          redo B;          next B;
6163        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6164          ## 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
6165          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6166          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.
6167    
6168          unless ($self->{confident}) {          unless ($self->{confident}) {
6169            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
6170              !!!cp ('t335');              !!!cp ('t335');
6171              $self->{change_encoding}              $self->{change_encoding}
6172                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6173                            
6174              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6175                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5382  sub _tree_construction_main ($) { Line 6184  sub _tree_construction_main ($) {
6184                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6185                !!!cp ('t336');                !!!cp ('t336');
6186                $self->{change_encoding}                $self->{change_encoding}
6187                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6188                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6189                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6190                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5406  sub _tree_construction_main ($) { Line 6208  sub _tree_construction_main ($) {
6208            }            }
6209          }          }
6210    
6211            !!!ack ('t338.1');
6212          !!!next-token;          !!!next-token;
6213          redo B;          next B;
6214        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6215          !!!cp ('t341');          !!!cp ('t341');
6216          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6217          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6218          redo B;          next B;
6219        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6220          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6221                                
6222          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6223              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6224            !!!cp ('t342');            !!!cp ('t342');
6225            ## Ignore the token            ## Ignore the token
6226          } else {          } else {
# Line 5431  sub _tree_construction_main ($) { Line 6234  sub _tree_construction_main ($) {
6234              }              }
6235            }            }
6236          }          }
6237            !!!nack ('t343.1');
6238          !!!next-token;          !!!next-token;
6239          redo B;          next B;
6240        } elsif ({        } elsif ({
6241                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6242                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6243                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6244                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6245                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6246                    form => 1,
6247                    table => 1,
6248                    hr => 1,
6249                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6250            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6251              !!!cp ('t350');
6252              !!!parse-error (type => 'in form:form', token => $token);
6253              ## Ignore the token
6254              !!!nack ('t350.1');
6255              !!!next-token;
6256              next B;
6257            }
6258    
6259          ## has a p element in scope          ## has a p element in scope
6260          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6261            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6262              !!!cp ('t344');              !!!cp ('t344');
6263              !!!back-token;              !!!back-token; # <form>
6264              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6265              redo B;                        line => $token->{line}, column => $token->{column}};
6266            } elsif ({              next B;
6267                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6268              !!!cp ('t345');              !!!cp ('t345');
6269              last INSCOPE;              last INSCOPE;
6270            }            }
6271          } # INSCOPE          } # INSCOPE
6272                        
6273          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6274          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6275              !!!nack ('t346.1');
6276            !!!next-token;            !!!next-token;
6277            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6278              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5470  sub _tree_construction_main ($) { Line 6285  sub _tree_construction_main ($) {
6285            } else {            } else {
6286              !!!cp ('t348');              !!!cp ('t348');
6287            }            }
6288          } else {          } elsif ($token->{tag_name} eq 'form') {
6289            !!!cp ('t347');            !!!cp ('t347.1');
6290              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6291    
6292              !!!nack ('t347.2');
6293            !!!next-token;            !!!next-token;
6294          }          } elsif ($token->{tag_name} eq 'table') {
6295          redo B;            !!!cp ('t382');
6296        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6297          if (defined $self->{form_element}) {            
6298            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6299            !!!parse-error (type => 'in form:form');  
6300            ## Ignore the token            !!!nack ('t382.1');
6301              !!!next-token;
6302            } elsif ($token->{tag_name} eq 'hr') {
6303              !!!cp ('t386');
6304              pop @{$self->{open_elements}};
6305            
6306              !!!nack ('t386.1');
6307            !!!next-token;            !!!next-token;
           redo B;  
6308          } else {          } else {
6309            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6310            !!!next-token;            !!!next-token;
           redo B;  
6311          }          }
6312        } elsif ($token->{tag_name} eq 'li') {          next B;
6313          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6314          ## has a p element in scope          ## has a p element in scope
6315          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6316            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6317              !!!cp ('t353');              !!!cp ('t353');
6318              !!!back-token;              !!!back-token; # <x>
6319              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6320              redo B;                        line => $token->{line}, column => $token->{column}};
6321            } elsif ({              next B;
6322                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6323              !!!cp ('t354');              !!!cp ('t354');
6324              last INSCOPE;              last INSCOPE;
6325            }            }
# Line 5524  sub _tree_construction_main ($) { Line 6328  sub _tree_construction_main ($) {
6328          ## Step 1          ## Step 1
6329          my $i = -1;          my $i = -1;
6330          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6331            my $li_or_dtdd = {li => {li => 1},
6332                              dt => {dt => 1, dd => 1},
6333                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6334          LI: {          LI: {
6335            ## Step 2            ## Step 2
6336            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6337              if ($i != -1) {              if ($i != -1) {
6338                !!!cp ('t355');                !!!cp ('t355');
6339                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6340                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6341                                      ->manakai_local_name,
6342                                  token => $token);
6343              } else {              } else {
6344                !!!cp ('t356');                !!!cp ('t356');
6345              }              }
# Line 5541  sub _tree_construction_main ($) { Line 6350  sub _tree_construction_main ($) {
6350            }            }
6351                        
6352            ## Step 3            ## Step 3
6353            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6354                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6355                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6356                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6357                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6358                  not ($node->[1] & DIV_EL)) {
6359              !!!cp ('t358');              !!!cp ('t358');
6360              last LI;              last LI;
6361            }            }
# Line 5557  sub _tree_construction_main ($) { Line 6367  sub _tree_construction_main ($) {
6367            redo LI;            redo LI;
6368          } # LI          } # LI
6369                        
6370          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6371            !!!nack ('t359.1');
6372          !!!next-token;          !!!next-token;
6373          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6374        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6375          ## has a p element in scope          ## has a p element in scope
6376          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6377            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6378              !!!cp ('t367');              !!!cp ('t367');
6379              !!!back-token;              !!!back-token; # <plaintext>
6380              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6381              redo B;                        line => $token->{line}, column => $token->{column}};
6382            } elsif ({              next B;
6383                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6384              !!!cp ('t368');              !!!cp ('t368');
6385              last INSCOPE;              last INSCOPE;
6386            }            }
6387          } # INSCOPE          } # INSCOPE
6388                        
6389          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6390                        
6391          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6392                        
6393            !!!nack ('t368.1');
6394          !!!next-token;          !!!next-token;
6395          redo B;          next B;
6396        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6397          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6398            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6399            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6400              !!!cp ('t371');              !!!cp ('t371');
6401              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6402                            
6403              !!!back-token;              !!!back-token; # <a>
6404              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6405              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6406                $formatting_end_tag->($token);
6407                            
6408              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6409                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5673  sub _tree_construction_main ($) { Line 6428  sub _tree_construction_main ($) {
6428                        
6429          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6430    
6431          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6432          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6433    
6434            !!!nack ('t374.1');
6435          !!!next-token;          !!!next-token;
6436          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;  
6437        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6438          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6439    
6440          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6441          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6443            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6444              !!!cp ('t376');              !!!cp ('t376');
6445              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6446              !!!back-token;              !!!back-token; # <nobr>
6447              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6448              redo B;                        line => $token->{line}, column => $token->{column}};
6449            } elsif ({              next B;
6450                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6451              !!!cp ('t377');              !!!cp ('t377');
6452              last INSCOPE;              last INSCOPE;
6453            }            }
6454          } # INSCOPE          } # INSCOPE
6455                    
6456          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6457          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6458                    
6459            !!!nack ('t377.1');
6460          !!!next-token;          !!!next-token;
6461          redo B;          next B;
6462        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6463          ## has a button element in scope          ## has a button element in scope
6464          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6465            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6466            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6467              !!!cp ('t378');              !!!cp ('t378');
6468              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6469              !!!back-token;              !!!back-token; # <button>
6470              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6471              redo B;                        line => $token->{line}, column => $token->{column}};
6472            } elsif ({              next B;
6473                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6474              !!!cp ('t379');              !!!cp ('t379');
6475              last INSCOPE;              last INSCOPE;
6476            }            }
# Line 5738  sub _tree_construction_main ($) { Line 6478  sub _tree_construction_main ($) {
6478                        
6479          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6480                        
6481          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6482    
6483          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6484    
6485          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6486    
6487            !!!nack ('t379.1');
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6490        } elsif ({        } elsif ({
6491                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6492                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6493                  image => 1,                  noembed => 1,
6494                    noframes => 1,
6495                    noscript => 0, ## TODO: 1 if scripting is enabled
6496                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6497          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6498            !!!cp ('t384');            !!!cp ('t381');
6499            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6500          } else {          } else {
6501            !!!cp ('t385');            !!!cp ('t399');
6502          }          }
6503            ## NOTE: There is an "as if in body" code clone.
6504          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6505          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6506        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6507          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6508                    
6509          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6510            !!!cp ('t389');            !!!cp ('t389');
6511            ## Ignore the token            ## Ignore the token
6512              !!!nack ('t389'); ## NOTE: Not acknowledged.
6513            !!!next-token;            !!!next-token;
6514            redo B;            next B;
6515          } else {          } else {
6516            my $at = $token->{attributes};            my $at = $token->{attributes};
6517            my $form_attrs;            my $form_attrs;
# Line 5856  sub _tree_construction_main ($) { Line 6522  sub _tree_construction_main ($) {
6522            delete $at->{prompt};            delete $at->{prompt};
6523            my @tokens = (            my @tokens = (
6524                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6525                           attributes => $form_attrs},                           attributes => $form_attrs,
6526                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6527                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6528                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6529                            {type => START_TAG_TOKEN, tag_name => 'p',
6530                             line => $token->{line}, column => $token->{column}},
6531                            {type => START_TAG_TOKEN, tag_name => 'label',
6532                             line => $token->{line}, column => $token->{column}},
6533                         );                         );
6534            if ($prompt_attr) {            if ($prompt_attr) {
6535              !!!cp ('t390');              !!!cp ('t390');
6536              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6537                               #line => $token->{line}, column => $token->{column},
6538                              };
6539            } else {            } else {
6540              !!!cp ('t391');              !!!cp ('t391');
6541              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6542                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6543                               #line => $token->{line}, column => $token->{column},
6544                              }; # SHOULD
6545              ## TODO: make this configurable              ## TODO: make this configurable
6546            }            }
6547            push @tokens,            push @tokens,
6548                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6549                             line => $token->{line}, column => $token->{column}},
6550                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6551                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6552                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6553                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6554                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6555            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6556                             line => $token->{line}, column => $token->{column}},
6557                            {type => END_TAG_TOKEN, tag_name => 'form',
6558                             line => $token->{line}, column => $token->{column}};
6559              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6560            !!!back-token (@tokens);            !!!back-token (@tokens);
6561            redo B;            !!!next-token;
6562              next B;
6563          }          }
6564        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6565          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6566          my $el;          my $el;
6567          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6568                    
6569          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6570          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5893  sub _tree_construction_main ($) { Line 6573  sub _tree_construction_main ($) {
6573          $insert->($el);          $insert->($el);
6574                    
6575          my $text = '';          my $text = '';
6576            !!!nack ('t392.1');
6577          !!!next-token;          !!!next-token;
6578          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6579            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5923  sub _tree_construction_main ($) { Line 6604  sub _tree_construction_main ($) {
6604            ## Ignore the token            ## Ignore the token
6605          } else {          } else {
6606            !!!cp ('t398');            !!!cp ('t398');
6607            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6608          }          }
6609          !!!next-token;          !!!next-token;
6610          redo B;          next B;
6611        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6612                  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');  
6613          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6614    
6615          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6616    
6617            ## "adjust foreign attributes" - done in insert-element-f
6618            
6619            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6620                    
6621          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6622              pop @{$self->{open_elements}};
6623              !!!ack ('t398.1');
6624            } else {
6625              !!!cp ('t398.2');
6626              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6627              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6628              ## mode, "in body" (not "in foreign content") secondary insertion
6629              ## mode, maybe.
6630            }
6631    
6632          !!!next-token;          !!!next-token;
6633          redo B;          next B;
6634        } elsif ({        } elsif ({
6635                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6636                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5955  sub _tree_construction_main ($) { Line 6638  sub _tree_construction_main ($) {
6638                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6639                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6640          !!!cp ('t401');          !!!cp ('t401');
6641          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6642          ## Ignore the token          ## Ignore the token
6643            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6644          !!!next-token;          !!!next-token;
6645          redo B;          next B;
6646                    
6647          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6648        } else {        } else {
6649          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6650              !!!cp ('t384');
6651              !!!parse-error (type => 'image', token => $token);
6652              $token->{tag_name} = 'img';
6653            } else {
6654              !!!cp ('t385');
6655            }
6656    
6657            ## NOTE: There is an "as if <br>" code clone.
6658          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6659                    
6660          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661    
6662            if ({
6663                 applet => 1, marquee => 1, object => 1,
6664                }->{$token->{tag_name}}) {
6665              !!!cp ('t380');
6666              push @$active_formatting_elements, ['#marker', ''];
6667              !!!nack ('t380.1');
6668            } elsif ({
6669                      b => 1, big => 1, em => 1, font => 1, i => 1,
6670                      s => 1, small => 1, strile => 1,
6671                      strong => 1, tt => 1, u => 1,
6672                     }->{$token->{tag_name}}) {
6673              !!!cp ('t375');
6674              push @$active_formatting_elements, $self->{open_elements}->[-1];
6675              !!!nack ('t375.1');
6676            } elsif ($token->{tag_name} eq 'input') {
6677              !!!cp ('t388');
6678              ## TODO: associate with $self->{form_element} if defined
6679              pop @{$self->{open_elements}};
6680              !!!ack ('t388.2');
6681            } elsif ({
6682                      area => 1, basefont => 1, bgsound => 1, br => 1,
6683                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6684                      #image => 1,
6685                     }->{$token->{tag_name}}) {
6686              !!!cp ('t388.1');
6687              pop @{$self->{open_elements}};
6688              !!!ack ('t388.3');
6689            } elsif ($token->{tag_name} eq 'select') {
6690              ## TODO: associate with $self->{form_element} if defined
6691            
6692              if ($self->{insertion_mode} & TABLE_IMS or
6693                  $self->{insertion_mode} & BODY_TABLE_IMS or
6694                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6695                !!!cp ('t400.1');
6696                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6697              } else {
6698                !!!cp ('t400.2');
6699                $self->{insertion_mode} = IN_SELECT_IM;
6700              }
6701              !!!nack ('t400.3');
6702            } else {
6703              !!!nack ('t402');
6704            }
6705                    
6706          !!!next-token;          !!!next-token;
6707          redo B;          next B;
6708        }        }
6709      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6710        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6711          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6712              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6713            for (@{$self->{open_elements}}) {          INSCOPE: {
6714              unless ({            for (reverse @{$self->{open_elements}}) {
6715                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6716                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6717                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6718                      }->{$_->[1]}) {                last INSCOPE;
6719                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6720                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6721              } else {                last;
               !!!cp ('t404');  
6722              }              }
6723            }            }
6724    
6725            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6726            !!!next-token;                            value => $token->{tag_name}, token => $token);
6727            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6728            !!!next-token;            !!!next-token;
6729            redo B;            next B;
6730            } # INSCOPE
6731    
6732            for (@{$self->{open_elements}}) {
6733              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6734                !!!cp ('t403');
6735                !!!parse-error (type => 'not closed',
6736                                value => $_->[0]->manakai_local_name,
6737                                token => $token);
6738                last;
6739              } else {
6740                !!!cp ('t404');
6741              }
6742          }          }
6743    
6744            $self->{insertion_mode} = AFTER_BODY_IM;
6745            !!!next-token;
6746            next B;
6747        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6748          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
6749            ## up-to-date, though it has same effect as speced.
6750            if (@{$self->{open_elements}} > 1 and
6751                $self->{open_elements}->[1]->[1] & BODY_EL) {
6752            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6753            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6754              !!!cp ('t406');              !!!cp ('t406');
6755              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6756                                value => $self->{open_elements}->[1]->[0]
6757                                    ->manakai_local_name,
6758                                token => $token);
6759            } else {            } else {
6760              !!!cp ('t407');              !!!cp ('t407');
6761            }            }
6762            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6763            ## reprocess            ## reprocess
6764            redo B;            next B;
6765          } else {          } else {
6766            !!!cp ('t408');            !!!cp ('t408');
6767            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6768            ## Ignore the token            ## Ignore the token
6769            !!!next-token;            !!!next-token;
6770            redo B;            next B;
6771          }          }
6772        } elsif ({        } elsif ({
6773                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6774                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6775                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6776                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6777                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6778                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6779          ## has an element in scope          ## has an element in scope
6780          my $i;          my $i;
6781          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6782            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6783            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6784              !!!cp ('t410');              !!!cp ('t410');
6785              $i = $_;              $i = $_;
6786              last INSCOPE;              last INSCOPE;
6787            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6788              !!!cp ('t411');              !!!cp ('t411');
6789              last INSCOPE;              last INSCOPE;
6790            }            }
# Line 6042  sub _tree_construction_main ($) { Line 6792  sub _tree_construction_main ($) {
6792    
6793          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6794            !!!cp ('t413');            !!!cp ('t413');
6795            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6796          } else {          } else {
6797            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6798            while ({            while ({
# Line 6050  sub _tree_construction_main ($) { Line 6800  sub _tree_construction_main ($) {
6800                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6801                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6802                    p => 1,                    p => 1,
6803                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6804              !!!cp ('t409');              !!!cp ('t409');
6805              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6806            }            }
6807    
6808            ## Step 2.            ## Step 2.
6809            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6810                      ne $token->{tag_name}) {
6811              !!!cp ('t412');              !!!cp ('t412');
6812              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6813                                value => $self->{open_elements}->[-1]->[0]
6814                                    ->manakai_local_name,
6815                                token => $token);
6816            } else {            } else {
6817              !!!cp ('t414');              !!!cp ('t414');
6818            }            }
# Line 6069  sub _tree_construction_main ($) { Line 6823  sub _tree_construction_main ($) {
6823            ## Step 4.            ## Step 4.
6824            $clear_up_to_marker->()            $clear_up_to_marker->()
6825                if {                if {
6826                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6827                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6828          }          }
6829          !!!next-token;          !!!next-token;
6830          redo B;          next B;
6831        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6832          undef $self->{form_element};          undef $self->{form_element};
6833    
# Line 6081  sub _tree_construction_main ($) { Line 6835  sub _tree_construction_main ($) {
6835          my $i;          my $i;
6836          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6838            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6839              !!!cp ('t418');              !!!cp ('t418');
6840              $i = $_;              $i = $_;
6841              last INSCOPE;              last INSCOPE;
6842            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6843              !!!cp ('t419');              !!!cp ('t419');
6844              last INSCOPE;              last INSCOPE;
6845            }            }
# Line 6096  sub _tree_construction_main ($) { Line 6847  sub _tree_construction_main ($) {
6847    
6848          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6849            !!!cp ('t421');            !!!cp ('t421');
6850            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6851          } else {          } else {
6852            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6853            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6854              !!!cp ('t417');              !!!cp ('t417');
6855              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6856            }            }
6857                        
6858            ## Step 2.            ## Step 2.
6859            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6860                      ne $token->{tag_name}) {
6861              !!!cp ('t417.1');              !!!cp ('t417.1');
6862              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6863                                value => $self->{open_elements}->[-1]->[0]
6864                                    ->manakai_local_name,
6865                                token => $token);
6866            } else {            } else {
6867              !!!cp ('t420');              !!!cp ('t420');
6868            }              }  
# Line 6119  sub _tree_construction_main ($) { Line 6872  sub _tree_construction_main ($) {
6872          }          }
6873    
6874          !!!next-token;          !!!next-token;
6875          redo B;          next B;
6876        } elsif ({        } elsif ({
6877                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6878                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6127  sub _tree_construction_main ($) { Line 6880  sub _tree_construction_main ($) {
6880          my $i;          my $i;
6881          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6882            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6883            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6884              !!!cp ('t423');              !!!cp ('t423');
6885              $i = $_;              $i = $_;
6886              last INSCOPE;              last INSCOPE;
6887            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6888              !!!cp ('t424');              !!!cp ('t424');
6889              last INSCOPE;              last INSCOPE;
6890            }            }
# Line 6144  sub _tree_construction_main ($) { Line 6892  sub _tree_construction_main ($) {
6892    
6893          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6894            !!!cp ('t425.1');            !!!cp ('t425.1');
6895            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6896          } else {          } else {
6897            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6898            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6899              !!!cp ('t422');              !!!cp ('t422');
6900              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6901            }            }
6902                        
6903            ## Step 2.            ## Step 2.
6904            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906              !!!cp ('t425');              !!!cp ('t425');
6907              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6908            } else {            } else {
6909              !!!cp ('t426');              !!!cp ('t426');
6910            }            }
# Line 6167  sub _tree_construction_main ($) { Line 6914  sub _tree_construction_main ($) {
6914          }          }
6915                    
6916          !!!next-token;          !!!next-token;
6917          redo B;          next B;
6918        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
6919          ## has an element in scope          ## has an element in scope
6920          my $i;          my $i;
6921          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6922            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6923            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6924              !!!cp ('t410.1');              !!!cp ('t410.1');
6925              $i = $_;              $i = $_;
6926              last INSCOPE;              last INSCOPE;
6927            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6928              !!!cp ('t411.1');              !!!cp ('t411.1');
6929              last INSCOPE;              last INSCOPE;
6930            }            }
6931          } # INSCOPE          } # INSCOPE
6932    
6933          if (defined $i) {          if (defined $i) {
6934            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6935                      ne $token->{tag_name}) {
6936              !!!cp ('t412.1');              !!!cp ('t412.1');
6937              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6938                                value => $self->{open_elements}->[-1]->[0]
6939                                    ->manakai_local_name,
6940                                token => $token);
6941            } else {            } else {
6942              !!!cp ('t414.1');              !!!cp ('t414.1');
6943            }            }
# Line 6197  sub _tree_construction_main ($) { Line 6945  sub _tree_construction_main ($) {
6945            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6946          } else {          } else {
6947            !!!cp ('t413.1');            !!!cp ('t413.1');
6948            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6949    
6950            !!!cp ('t415.1');            !!!cp ('t415.1');
6951            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6952            my $el;            my $el;
6953            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
6954            $insert->($el);            $insert->($el);
6955            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
6956          }          }
6957    
6958          !!!next-token;          !!!next-token;
6959          redo B;          next B;
6960        } elsif ({        } elsif ({
6961                  a => 1,                  a => 1,
6962                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6216  sub _tree_construction_main ($) { Line 6964  sub _tree_construction_main ($) {
6964                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6965                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6966          !!!cp ('t427');          !!!cp ('t427');
6967          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6968          redo B;          next B;
6969        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6970          !!!cp ('t428');          !!!cp ('t428');
6971          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6972    
6973          ## As if <br>          ## As if <br>
6974          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6975                    
6976          my $el;          my $el;
6977          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6978          $insert->($el);          $insert->($el);
6979                    
6980          ## Ignore the token.          ## Ignore the token.
6981          !!!next-token;          !!!next-token;
6982          redo B;          next B;
6983        } elsif ({        } elsif ({
6984                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6985                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6245  sub _tree_construction_main ($) { Line 6993  sub _tree_construction_main ($) {
6993                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6994                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6995          !!!cp ('t429');          !!!cp ('t429');
6996          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6997          ## Ignore the token          ## Ignore the token
6998          !!!next-token;          !!!next-token;
6999          redo B;          next B;
7000                    
7001          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7002                    
# Line 6259  sub _tree_construction_main ($) { Line 7007  sub _tree_construction_main ($) {
7007    
7008          ## Step 2          ## Step 2
7009          S2: {          S2: {
7010            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7011              ## Step 1              ## Step 1
7012              ## generate implied end tags              ## generate implied end tags
7013              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7014                !!!cp ('t430');                !!!cp ('t430');
7015                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7016                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7017              }              }
7018                    
7019              ## Step 2              ## Step 2
7020              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7021                        ne $token->{tag_name}) {
7022                !!!cp ('t431');                !!!cp ('t431');
7023                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7024                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7025                                  value => $self->{open_elements}->[-1]->[0]
7026                                      ->manakai_local_name,
7027                                  token => $token);
7028              } else {              } else {
7029                !!!cp ('t432');                !!!cp ('t432');
7030              }              }
# Line 6286  sub _tree_construction_main ($) { Line 7036  sub _tree_construction_main ($) {
7036              last S2;              last S2;
7037            } else {            } else {
7038              ## Step 3              ## Step 3
7039              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7040                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7041                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7042                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7043                !!!cp ('t433');                !!!cp ('t433');
7044                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7045                ## Ignore the token                ## Ignore the token
7046                !!!next-token;                !!!next-token;
7047                last S2;                last S2;
# Line 6307  sub _tree_construction_main ($) { Line 7057  sub _tree_construction_main ($) {
7057            ## Step 5;            ## Step 5;
7058            redo S2;            redo S2;
7059          } # S2          } # S2
7060          redo B;          next B;
7061        }        }
7062      }      }
7063      redo B;      next B;
7064      } continue { # B
7065        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7066          ## NOTE: The code below is executed in cases where it does not have
7067          ## to be, but it it is harmless even in those cases.
7068          ## has an element in scope
7069          INSCOPE: {
7070            for (reverse 0..$#{$self->{open_elements}}) {
7071              my $node = $self->{open_elements}->[$_];
7072              if ($node->[1] & FOREIGN_EL) {
7073                last INSCOPE;
7074              } elsif ($node->[1] & SCOPING_EL) {
7075                last;
7076              }
7077            }
7078            
7079            ## NOTE: No foreign element in scope.
7080            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7081          } # INSCOPE
7082        }
7083    } # B    } # B
7084    
7085    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6356  sub set_inner_html ($$$) { Line 7125  sub set_inner_html ($$$) {
7125    
7126      ## Step 8 # MUST      ## Step 8 # MUST
7127      my $i = 0;      my $i = 0;
7128      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7129      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7130      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7131        my $self = shift;        my $self = shift;
7132    
# Line 6366  sub set_inner_html ($$$) { Line 7135  sub set_inner_html ($$$) {
7135    
7136        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7137        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7138        $column++;  
7139          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7140          $p->{column}++;
7141    
7142        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7143          $line++;          $p->{line}++;
7144          $column = 0;          $p->{column} = 0;
7145          !!!cp ('i1');          !!!cp ('i1');
7146        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7147          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7148          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7149          $line++;          $p->{line}++;
7150          $column = 0;          $p->{column} = 0;
7151          !!!cp ('i2');          !!!cp ('i2');
7152        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7153          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6385  sub set_inner_html ($$$) { Line 7156  sub set_inner_html ($$$) {
7156          !!!cp ('i4');          !!!cp ('i4');
7157          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7158          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7159          } elsif ($self->{next_char} <= 0x0008 or
7160                   (0x000E <= $self->{next_char} and
7161                    $self->{next_char} <= 0x001F) or
7162                   (0x007F <= $self->{next_char} and
7163                    $self->{next_char} <= 0x009F) or
7164                   (0xD800 <= $self->{next_char} and
7165                    $self->{next_char} <= 0xDFFF) or
7166                   (0xFDD0 <= $self->{next_char} and
7167                    $self->{next_char} <= 0xFDDF) or
7168                   {
7169                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7170                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7171                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7172                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7173                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7174                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7175                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7176                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7177                    0x10FFFE => 1, 0x10FFFF => 1,
7178                   }->{$self->{next_char}}) {
7179            !!!cp ('i4.1');
7180            !!!parse-error (type => 'control char', level => $self->{must_level});
7181    ## TODO: error type documentation
7182        }        }
7183      };      };
7184      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6392  sub set_inner_html ($$$) { Line 7186  sub set_inner_html ($$$) {
7186            
7187      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7188        my (%opt) = @_;        my (%opt) = @_;
7189        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7190          my $column = $opt{column};
7191          if (defined $opt{token} and defined $opt{token}->{line}) {
7192            $line = $opt{token}->{line};
7193            $column = $opt{token}->{column};
7194          }
7195          warn "Parse error ($opt{type}) at line $line column $column\n";
7196      };      };
7197      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7198        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7199      };      };
7200            
7201      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6419  sub set_inner_html ($$$) { Line 7219  sub set_inner_html ($$$) {
7219          unless defined $p->{content_model};          unless defined $p->{content_model};
7220          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7221    
7222      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7223          ## TODO: Foreign element OK?
7224    
7225      ## Step 3      ## Step 3
7226      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6429  sub set_inner_html ($$$) { Line 7230  sub set_inner_html ($$$) {
7230      $doc->append_child ($root);      $doc->append_child ($root);
7231    
7232      ## Step 5 # MUST      ## Step 5 # MUST
7233      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7234    
7235      undef $p->{head_element};      undef $p->{head_element};
7236    
# Line 6475  sub set_inner_html ($$$) { Line 7276  sub set_inner_html ($$$) {
7276      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7277    
7278      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7279    
7280        delete $p->{parse_error}; # delete loop
7281    } else {    } else {
7282      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";
7283    }    }

Legend:
Removed from v.1.100  
changed lines
  Added in v.1.133

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24