/[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.77 by wakaba, Mon Mar 3 10:20:19 2008 UTC revision 1.98 by wakaba, Sun Mar 9 03:23:43 2008 UTC
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 304  sub ROW_IMS ()        { 0b10000000 }
304  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
305  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
306    
307    ## NOTE: "initial" and "before html" insertion modes have no constants.
308    
309    ## NOTE: "after after body" insertion mode.
310  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
311    
312    ## NOTE: "after after frameset" insertion mode.
313  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
314    
315  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
316  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
317  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 656  sub _get_next_token ($) { Line 662  sub _get_next_token ($) {
662            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
663          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
664            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
665            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
666              !!!cp (36);            #  ## NOTE: This should never be reached.
667              !!!parse-error (type => 'end tag attribute');            #  !!! cp (36);
668            } else {            #  !!! parse-error (type => 'end tag attribute');
669              #} else {
670              !!!cp (37);              !!!cp (37);
671            }            #}
672          } else {          } else {
673            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
674          }          }
# Line 688  sub _get_next_token ($) { Line 695  sub _get_next_token ($) {
695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
696          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
697            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
698            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
699              !!!cp (40);            #  ## NOTE: This state should never be reached.
700              !!!parse-error (type => 'end tag attribute');            #  !!! cp (40);
701            } else {            #  !!! parse-error (type => 'end tag attribute');
702              #} else {
703              !!!cp (41);              !!!cp (41);
704            }            #}
705          } else {          } else {
706            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
707          }          }
# Line 912  sub _get_next_token ($) { Line 920  sub _get_next_token ($) {
920              !!!cp (67);              !!!cp (67);
921              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
922            } else {            } else {
923                ## NOTE: This state should never be reached.
924              !!!cp (68);              !!!cp (68);
925            }            }
926          } else {          } else {
# Line 963  sub _get_next_token ($) { Line 972  sub _get_next_token ($) {
972              !!!cp (74);              !!!cp (74);
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
974            } else {            } else {
975                ## NOTE: This state should never be reached.
976              !!!cp (75);              !!!cp (75);
977            }            }
978          } else {          } else {
# Line 1011  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021              !!!cp (80);              !!!cp (80);
1022              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1023            } else {            } else {
1024                ## NOTE: This state should never be reached.
1025              !!!cp (81);              !!!cp (81);
1026            }            }
1027          } else {          } else {
# Line 1067  sub _get_next_token ($) { Line 1078  sub _get_next_token ($) {
1078              !!!cp (88);              !!!cp (88);
1079              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1080            } else {            } else {
1081                ## NOTE: This state should never be reached.
1082              !!!cp (89);              !!!cp (89);
1083            }            }
1084          } else {          } else {
# Line 1091  sub _get_next_token ($) { Line 1103  sub _get_next_token ($) {
1103              !!!cp (91);              !!!cp (91);
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1105            } else {            } else {
1106                ## NOTE: This state should never be reached.
1107              !!!cp (92);              !!!cp (92);
1108            }            }
1109          } else {          } else {
# Line 1139  sub _get_next_token ($) { Line 1152  sub _get_next_token ($) {
1152              !!!cp (98);              !!!cp (98);
1153              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1154            } else {            } else {
1155                ## NOTE: This state should never be reached.
1156              !!!cp (99);              !!!cp (99);
1157            }            }
1158          } else {          } else {
# Line 1182  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196              !!!cp (104);              !!!cp (104);
1197              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1198            } else {            } else {
1199                ## NOTE: This state should never be reached.
1200              !!!cp (105);              !!!cp (105);
1201            }            }
1202          } else {          } else {
# Line 1228  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243              !!!cp (110);              !!!cp (110);
1244              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1245            } else {            } else {
1246                ## NOTE: This state should never be reached.
1247              !!!cp (111);              !!!cp (111);
1248            }            }
1249          } else {          } else {
# Line 1252  sub _get_next_token ($) { Line 1268  sub _get_next_token ($) {
1268              !!!cp (113);              !!!cp (113);
1269              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1270            } else {            } else {
1271                ## NOTE: This state should never be reached.
1272              !!!cp (114);              !!!cp (114);
1273            }            }
1274          } else {          } else {
# Line 1323  sub _get_next_token ($) { Line 1340  sub _get_next_token ($) {
1340              !!!cp (120);              !!!cp (120);
1341              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1342            } else {            } else {
1343                ## NOTE: This state should never be reached.
1344              !!!cp (121);              !!!cp (121);
1345            }            }
1346          } else {          } else {
# Line 2190  sub _tokenize_attempt_to_consume_an_enti Line 2208  sub _tokenize_attempt_to_consume_an_enti
2208         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2209         $additional => 1,         $additional => 1,
2210        }->{$self->{next_char}}) {        }->{$self->{next_char}}) {
2211        !!!cp (1001);
2212      ## Don't consume      ## Don't consume
2213      ## No error      ## No error
2214      return undef;      return undef;
# Line 2203  sub _tokenize_attempt_to_consume_an_enti Line 2222  sub _tokenize_attempt_to_consume_an_enti
2222          !!!next-input-character;          !!!next-input-character;
2223          if (0x0030 <= $self->{next_char} and          if (0x0030 <= $self->{next_char} and
2224              $self->{next_char} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2225              !!!cp (1002);
2226            $code ||= 0;            $code ||= 0;
2227            $code *= 0x10;            $code *= 0x10;
2228            $code += $self->{next_char} - 0x0030;            $code += $self->{next_char} - 0x0030;
2229            redo X;            redo X;
2230          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{next_char} and
2231                   $self->{next_char} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2232              !!!cp (1003);
2233            $code ||= 0;            $code ||= 0;
2234            $code *= 0x10;            $code *= 0x10;
2235            $code += $self->{next_char} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2236            redo X;            redo X;
2237          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{next_char} and
2238                   $self->{next_char} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2239              !!!cp (1004);
2240            $code ||= 0;            $code ||= 0;
2241            $code *= 0x10;            $code *= 0x10;
2242            $code += $self->{next_char} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2243            redo X;            redo X;
2244          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2245              !!!cp (1005);
2246            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2247            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2248            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2249            return undef;            return undef;
2250          } elsif ($self->{next_char} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2251              !!!cp (1006);
2252            !!!next-input-character;            !!!next-input-character;
2253          } else {          } else {
2254              !!!cp (1007);
2255            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2256          }          }
2257    
2258          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2259              !!!cp (1008);
2260            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2261            $code = 0xFFFD;            $code = 0xFFFD;
2262          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2263              !!!cp (1009);
2264            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2265            $code = 0xFFFD;            $code = 0xFFFD;
2266          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2267              !!!cp (1010);
2268            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2269            $code = 0x000A;            $code = 0x000A;
2270          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2271              !!!cp (1011);
2272            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2273            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2274          }          }
# Line 2254  sub _tokenize_attempt_to_consume_an_enti Line 2283  sub _tokenize_attempt_to_consume_an_enti
2283                
2284        while (0x0030 <= $self->{next_char} and        while (0x0030 <= $self->{next_char} and
2285                  $self->{next_char} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2286            !!!cp (1012);
2287          $code *= 10;          $code *= 10;
2288          $code += $self->{next_char} - 0x0030;          $code += $self->{next_char} - 0x0030;
2289                    
# Line 2261  sub _tokenize_attempt_to_consume_an_enti Line 2291  sub _tokenize_attempt_to_consume_an_enti
2291        }        }
2292    
2293        if ($self->{next_char} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2294            !!!cp (1013);
2295          !!!next-input-character;          !!!next-input-character;
2296        } else {        } else {
2297            !!!cp (1014);
2298          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2299        }        }
2300    
2301        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2302            !!!cp (1015);
2303          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2304          $code = 0xFFFD;          $code = 0xFFFD;
2305        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2306            !!!cp (1016);
2307          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2308          $code = 0xFFFD;          $code = 0xFFFD;
2309        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2310            !!!cp (1017);
2311          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2312          $code = 0x000A;          $code = 0x000A;
2313        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2314            !!!cp (1018);
2315          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2316          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2317        }        }
2318                
2319        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2320      } else {      } else {
2321          !!!cp (1019);
2322        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2323        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2324        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
# Line 2311  sub _tokenize_attempt_to_consume_an_enti Line 2348  sub _tokenize_attempt_to_consume_an_enti
2348        $entity_name .= chr $self->{next_char};        $entity_name .= chr $self->{next_char};
2349        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2350          if ($self->{next_char} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2351              !!!cp (1020);
2352            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2353            $match = 1;            $match = 1;
2354            !!!next-input-character;            !!!next-input-character;
2355            last;            last;
2356          } else {          } else {
2357              !!!cp (1021);
2358            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2359            $match = -1;            $match = -1;
2360            !!!next-input-character;            !!!next-input-character;
2361          }          }
2362        } else {        } else {
2363            !!!cp (1022);
2364          $value .= chr $self->{next_char};          $value .= chr $self->{next_char};
2365          $match *= 2;          $match *= 2;
2366          !!!next-input-character;          !!!next-input-character;
# Line 2328  sub _tokenize_attempt_to_consume_an_enti Line 2368  sub _tokenize_attempt_to_consume_an_enti
2368      }      }
2369            
2370      if ($match > 0) {      if ($match > 0) {
2371          !!!cp (1023);
2372        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2373      } elsif ($match < 0) {      } elsif ($match < 0) {
2374        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2375        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2376            !!!cp (1024);
2377          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2378        } else {        } else {
2379            !!!cp (1025);
2380          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2381        }        }
2382      } else {      } else {
2383          !!!cp (1026);
2384        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2385        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2386        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2387      }      }
2388    } else {    } else {
2389        !!!cp (1027);
2390      ## no characters are consumed      ## no characters are consumed
2391      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2392      return undef;      return undef;
# Line 2381  sub _construct_tree ($) { Line 2426  sub _construct_tree ($) {
2426        
2427    !!!next-token;    !!!next-token;
2428    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2429    undef $self->{form_element};    undef $self->{form_element};
2430    undef $self->{head_element};    undef $self->{head_element};
2431    $self->{open_elements} = [];    $self->{open_elements} = [];
2432    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2433    
2434      ## NOTE: The "initial" insertion mode.
2435    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2436    
2437      ## NOTE: The "before html" insertion mode.
2438    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2439      $self->{insertion_mode} = BEFORE_HEAD_IM;
2440    
2441      ## NOTE: The "before head" insertion mode and so on.
2442    $self->_tree_construction_main;    $self->_tree_construction_main;
2443  } # _construct_tree  } # _construct_tree
2444    
2445  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2446    my $self = shift;    my $self = shift;
2447    
2448      ## NOTE: "initial" insertion mode
2449    
2450    INITIAL: {    INITIAL: {
2451      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2452        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2405  sub _tree_construction_initial ($) { Line 2458  sub _tree_construction_initial ($) {
2458        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2459            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2460            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2461            !!!cp ('t1');
2462          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2463        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2464            !!!cp ('t2');
2465          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2466          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2467          } else {
2468            !!!cp ('t3');
2469        }        }
2470                
2471        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 2422  sub _tree_construction_initial ($) { Line 2479  sub _tree_construction_initial ($) {
2479        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2480                
2481        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2482            !!!cp ('t4');
2483          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2484        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2485          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2500  sub _tree_construction_initial ($) { Line 2558  sub _tree_construction_initial ($) {
2558            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2559            "HTML" => 1,            "HTML" => 1,
2560          }->{$pubid}) {          }->{$pubid}) {
2561              !!!cp ('t5');
2562            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2563          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2564                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2565            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2566                !!!cp ('t6');
2567              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2568            } else {            } else {
2569                !!!cp ('t7');
2570              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2571            }            }
2572          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2573                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2574              !!!cp ('t8');
2575            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2576            } else {
2577              !!!cp ('t9');
2578          }          }
2579          } else {
2580            !!!cp ('t10');
2581        }        }
2582        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2583          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2584          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2585          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2586              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2588              !!!cp ('t11');
2589            } else {
2590              !!!cp ('t12');
2591          }          }
2592          } else {
2593            !!!cp ('t13');
2594        }        }
2595                
2596        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2597        !!!next-token;        !!!next-token;
2598        return;        return;
2599      } elsif ({      } elsif ({
# Line 2529  sub _tree_construction_initial ($) { Line 2601  sub _tree_construction_initial ($) {
2601                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2602                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2603               }->{$token->{type}}) {               }->{$token->{type}}) {
2604          !!!cp ('t14');
2605        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2606        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2607        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2608        ## reprocess        ## reprocess
2609        return;        return;
2610      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2539  sub _tree_construction_initial ($) { Line 2612  sub _tree_construction_initial ($) {
2612          ## Ignore the token          ## Ignore the token
2613    
2614          unless (length $token->{data}) {          unless (length $token->{data}) {
2615            ## Stay in the phase            !!!cp ('t15');
2616              ## Stay in the insertion mode.
2617            !!!next-token;            !!!next-token;
2618            redo INITIAL;            redo INITIAL;
2619            } else {
2620              !!!cp ('t16');
2621          }          }
2622          } else {
2623            !!!cp ('t17');
2624        }        }
2625    
2626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2628        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2629        ## reprocess        ## reprocess
2630        return;        return;
2631      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2632          !!!cp ('t18');
2633        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2634        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2635                
2636        ## Stay in the phase.        ## Stay in the insertion mode.
2637        !!!next-token;        !!!next-token;
2638        redo INITIAL;        redo INITIAL;
2639      } else {      } else {
2640        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2641      }      }
2642    } # INITIAL    } # INITIAL
2643    
2644      die "$0: _tree_construction_initial: This should be never reached";
2645  } # _tree_construction_initial  } # _tree_construction_initial
2646    
2647  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2648    my $self = shift;    my $self = shift;
2649    
2650      ## NOTE: "before html" insertion mode.
2651        
2652    B: {    B: {
2653        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2654            !!!cp ('t19');
2655          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2656          ## Ignore the token          ## Ignore the token
2657          ## Stay in the phase          ## Stay in the insertion mode.
2658          !!!next-token;          !!!next-token;
2659          redo B;          redo B;
2660        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2661            !!!cp ('t20');
2662          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2663          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2664          ## Stay in the phase          ## Stay in the insertion mode.
2665          !!!next-token;          !!!next-token;
2666          redo B;          redo B;
2667        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2584  sub _tree_construction_root_element ($) Line 2669  sub _tree_construction_root_element ($)
2669            ## Ignore the token.            ## Ignore the token.
2670    
2671            unless (length $token->{data}) {            unless (length $token->{data}) {
2672              ## Stay in the phase              !!!cp ('t21');
2673                ## Stay in the insertion mode.
2674              !!!next-token;              !!!next-token;
2675              redo B;              redo B;
2676              } else {
2677                !!!cp ('t22');
2678            }            }
2679            } else {
2680              !!!cp ('t23');
2681          }          }
2682    
2683          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2684    
2685          #          #
2686        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2687          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2688              $token->{attributes}->{manifest}) {            my $root_element;
2689            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2690                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2691            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}}, [$root_element, 'html'];
2692    
2693              if ($token->{attributes}->{manifest}) {
2694                !!!cp ('t24');
2695                $self->{application_cache_selection}
2696                    ->($token->{attributes}->{manifest}->{value});
2697                ## ISSUE: No relative reference resolution?
2698              } else {
2699                !!!cp ('t25');
2700                $self->{application_cache_selection}->(undef);
2701              }
2702    
2703              !!!next-token;
2704              return; ## Go to the "before head" insertion mode.
2705          } else {          } else {
2706            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2707              #
2708          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2709        } elsif ({        } elsif ({
2710                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2711                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2712                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2713          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2714          #          #
2715        } else {        } else {
2716          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2717        }        }
2718    
2719        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html');
2720        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2721        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2722        ## reprocess  
2723        #redo B;      $self->{application_cache_selection}->(undef);
2724        return; ## Go to the main phase.  
2725        ## NOTE: Reprocess the token.
2726        return; ## Go to the "before head" insertion mode.
2727    
2728        ## ISSUE: There is an issue in the spec
2729    } # B    } # B
2730    
2731      die "$0: _tree_construction_root_element: This should never be reached";
2732  } # _tree_construction_root_element  } # _tree_construction_root_element
2733    
2734  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2638  sub _reset_insertion_mode ($) { Line 2743  sub _reset_insertion_mode ($) {
2743            
2744      ## Step 3      ## Step 3
2745      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
2746        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2747          $last = 1;          $last = 1;
2748          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2749            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2750                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2751                !!!cp ('t27');
2752              #              #
2753            } else {            } else {
2754                !!!cp ('t28');
2755              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2756            }            }
2757          }          }
# Line 2658  sub _reset_insertion_mode ($) { Line 2760  sub _reset_insertion_mode ($) {
2760        ## Step 4..13        ## Step 4..13
2761        my $new_mode = {        my $new_mode = {
2762                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2763                          ## NOTE: |option| and |optgroup| do not set
2764                          ## insertion mode to "in select" by themselves.
2765                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2766                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2767                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2676  sub _reset_insertion_mode ($) { Line 2780  sub _reset_insertion_mode ($) {
2780        ## Step 14        ## Step 14
2781        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2782          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2783              !!!cp ('t29');
2784            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2785          } else {          } else {
2786              ## ISSUE: Can this state be reached?
2787              !!!cp ('t30');
2788            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2789          }          }
2790          return;          return;
2791          } else {
2792            !!!cp ('t31');
2793        }        }
2794                
2795        ## Step 15        ## Step 15
# Line 2693  sub _reset_insertion_mode ($) { Line 2802  sub _reset_insertion_mode ($) {
2802        ## Step 17        ## Step 17
2803        redo S3;        redo S3;
2804      } # S3      } # S3
2805    
2806      die "$0: _reset_insertion_mode: This line should never be reached";
2807  } # _reset_insertion_mode  } # _reset_insertion_mode
2808    
2809  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2714  sub _tree_construction_main ($) { Line 2825  sub _tree_construction_main ($) {
2825      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2826      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2827        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2828            !!!cp ('t32');
2829          return;          return;
2830        }        }
2831      }      }
# Line 2728  sub _tree_construction_main ($) { Line 2840  sub _tree_construction_main ($) {
2840    
2841        ## Step 6        ## Step 6
2842        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2843            !!!cp ('t33_1');
2844          #          #
2845        } else {        } else {
2846          my $in_open_elements;          my $in_open_elements;
2847          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2848            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2849                !!!cp ('t33');
2850              $in_open_elements = 1;              $in_open_elements = 1;
2851              last OE;              last OE;
2852            }            }
2853          }          }
2854          if ($in_open_elements) {          if ($in_open_elements) {
2855              !!!cp ('t34');
2856            #            #
2857          } else {          } else {
2858              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2859              !!!cp ('t35');
2860            redo S4;            redo S4;
2861          }          }
2862        }        }
# Line 2762  sub _tree_construction_main ($) { Line 2879  sub _tree_construction_main ($) {
2879    
2880        ## Step 11        ## Step 11
2881        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2882            !!!cp ('t36');
2883          ## Step 7'          ## Step 7'
2884          $i++;          $i++;
2885          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2886                    
2887          redo S7;          redo S7;
2888        }        }
2889    
2890          !!!cp ('t37');
2891      } # S7      } # S7
2892    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2893    
2894    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2895      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2896        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2897            !!!cp ('t38');
2898          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2899          return;          return;
2900        }        }
2901      }      }
2902    
2903        !!!cp ('t39');
2904    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2905    
2906    my $parse_rcdata = sub ($$) {    my $insert;
2907      my ($content_model_flag, $insert) = @_;  
2908      my $parse_rcdata = sub ($) {
2909        my ($content_model_flag) = @_;
2910    
2911      ## Step 1      ## Step 1
2912      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
# Line 2789  sub _tree_construction_main ($) { Line 2914  sub _tree_construction_main ($) {
2914      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes});
2915    
2916      ## Step 2      ## Step 2
2917      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2918    
2919      ## Step 3      ## Step 3
2920      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2799  sub _tree_construction_main ($) { Line 2924  sub _tree_construction_main ($) {
2924      my $text = '';      my $text = '';
2925      !!!next-token;      !!!next-token;
2926      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2927          !!!cp ('t40');
2928        $text .= $token->{data};        $text .= $token->{data};
2929        !!!next-token;        !!!next-token;
2930      }      }
2931    
2932      ## Step 5      ## Step 5
2933      if (length $text) {      if (length $text) {
2934          !!!cp ('t41');
2935        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2936        $el->append_child ($text);        $el->append_child ($text);
2937      }      }
# Line 2813  sub _tree_construction_main ($) { Line 2940  sub _tree_construction_main ($) {
2940      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2941    
2942      ## Step 7      ## Step 7
2943      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2944            $token->{tag_name} eq $start_tag_name) {
2945          !!!cp ('t42');
2946        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
2947      } else {      } else {
2948        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
2949          if ($content_model_flag == CDATA_CONTENT_MODEL) {
2950            !!!cp ('t43');
2951            !!!parse-error (type => 'in CDATA:#'.$token->{type});
2952          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2953            !!!cp ('t44');
2954            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2955          } else {
2956            die "$0: $content_model_flag in parse_rcdata";
2957          }
2958      }      }
2959      !!!next-token;      !!!next-token;
2960    }; # $parse_rcdata    }; # $parse_rcdata
2961    
2962    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
2963      my $script_el;      my $script_el;
2964      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2965      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
# Line 2837  sub _tree_construction_main ($) { Line 2970  sub _tree_construction_main ($) {
2970      my $text = '';      my $text = '';
2971      !!!next-token;      !!!next-token;
2972      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
2973          !!!cp ('t45');
2974        $text .= $token->{data};        $text .= $token->{data};
2975        !!!next-token;        !!!next-token;
2976      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2977      if (length $text) {      if (length $text) {
2978          !!!cp ('t46');
2979        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2980      }      }
2981                                
# Line 2848  sub _tree_construction_main ($) { Line 2983  sub _tree_construction_main ($) {
2983    
2984      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
2985          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2986          !!!cp ('t47');
2987        ## Ignore the token        ## Ignore the token
2988      } else {      } else {
2989          !!!cp ('t48');
2990        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2991        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2992        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2993      }      }
2994            
2995      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2996          !!!cp ('t49');
2997        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2998      } else {      } else {
2999          !!!cp ('t50');
3000        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3001        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3002    
# Line 2880  sub _tree_construction_main ($) { Line 3019  sub _tree_construction_main ($) {
3019        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3020        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3021          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3022              !!!cp ('t51');
3023            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3024            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3025            last AFE;            last AFE;
3026          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3027              !!!cp ('t52');
3028            last AFE;            last AFE;
3029          }          }
3030        } # AFE        } # AFE
3031        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3032            !!!cp ('t53');
3033          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3034          ## Ignore the token          ## Ignore the token
3035          !!!next-token;          !!!next-token;
# Line 2900  sub _tree_construction_main ($) { Line 3042  sub _tree_construction_main ($) {
3042          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3043          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3044            if ($in_scope) {            if ($in_scope) {
3045                !!!cp ('t54');
3046              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3047              last INSCOPE;              last INSCOPE;
3048            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3049                !!!cp ('t55');
3050              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3051              ## Ignore the token              ## Ignore the token
3052              !!!next-token;              !!!next-token;
# Line 2912  sub _tree_construction_main ($) { Line 3056  sub _tree_construction_main ($) {
3056                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3057                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3058                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3059              !!!cp ('t56');
3060            $in_scope = 0;            $in_scope = 0;
3061          }          }
3062        } # INSCOPE        } # INSCOPE
3063        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3064            !!!cp ('t57');
3065          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3066          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3067          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3068          return;          return;
3069        }        }
3070        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3071            !!!cp ('t58');
3072          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3073        }        }
3074                
# Line 2934  sub _tree_construction_main ($) { Line 3081  sub _tree_construction_main ($) {
3081              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3082              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3083               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3084              !!!cp ('t59');
3085            $furthest_block = $node;            $furthest_block = $node;
3086            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3087          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3088              !!!cp ('t60');
3089            last OE;            last OE;
3090          }          }
3091        } # OE        } # OE
3092                
3093        ## Step 3        ## Step 3
3094        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3095            !!!cp ('t61');
3096          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3097          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3098          !!!next-token;          !!!next-token;
# Line 2955  sub _tree_construction_main ($) { Line 3105  sub _tree_construction_main ($) {
3105        ## Step 5        ## Step 5
3106        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3107        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3108            !!!cp ('t62');
3109          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3110        }        }
3111                
# Line 2977  sub _tree_construction_main ($) { Line 3128  sub _tree_construction_main ($) {
3128          S7S2: {          S7S2: {
3129            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3130              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3131                  !!!cp ('t63');
3132                $node_i_in_active = $_;                $node_i_in_active = $_;
3133                last S7S2;                last S7S2;
3134              }              }
# Line 2990  sub _tree_construction_main ($) { Line 3142  sub _tree_construction_main ($) {
3142                    
3143          ## Step 4          ## Step 4
3144          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3145              !!!cp ('t64');
3146            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3147          }          }
3148                    
3149          ## Step 5          ## Step 5
3150          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3151              !!!cp ('t65');
3152            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3153            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3154            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3029  sub _tree_construction_main ($) { Line 3183  sub _tree_construction_main ($) {
3183        my $i;        my $i;
3184        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3185          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3186              !!!cp ('t66');
3187            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3188            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3189          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3190              !!!cp ('t67');
3191            $i = $_;            $i = $_;
3192          }          }
3193        } # AFE        } # AFE
# Line 3041  sub _tree_construction_main ($) { Line 3197  sub _tree_construction_main ($) {
3197        undef $i;        undef $i;
3198        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3199          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3200              !!!cp ('t68');
3201            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3202            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3203          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3204              !!!cp ('t69');
3205            $i = $_;            $i = $_;
3206          }          }
3207        } # OE        } # OE
# Line 3054  sub _tree_construction_main ($) { Line 3212  sub _tree_construction_main ($) {
3212      } # FET      } # FET
3213    }; # $formatting_end_tag    }; # $formatting_end_tag
3214    
3215    my $insert_to_current = sub {    ## NOTE: $open_tables->[-1]->[0] is the "current table".
3216      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3217      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3218    
3219      $insert = my $insert_to_current = sub {
3220      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3221    }; # $insert_to_current    }; # $insert_to_current
3222    
3223    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3224                         my $child = shift;      my $child = shift;
3225                         if ({      if ({
3226                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3227                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3228                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3229                           # MUST        my $foster_parent_element;
3230                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3231                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3232                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3233                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3234                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3235                                   !!!cp ('t70');
3236                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3237                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3238                               } else {                               } else {
3239                                   !!!cp ('t71');
3240                                 $foster_parent_element                                 $foster_parent_element
3241                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3242                               }                               }
# Line 3084  sub _tree_construction_main ($) { Line 3247  sub _tree_construction_main ($) {
3247                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3248                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3249                             ($child, $next_sibling);                             ($child, $next_sibling);
3250                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3251                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3252                         }        !!!cp ('t72');
3253          $self->{open_elements}->[-1]->[0]->append_child ($child);
3254        }
3255    }; # $insert_to_foster    }; # $insert_to_foster
3256    
   my $insert;  
   
3257    B: {    B: {
3258      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3259          !!!cp ('t73');
3260        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3261        ## Ignore the token        ## Ignore the token
3262        ## Stay in the phase        ## Stay in the phase
# Line 3100  sub _tree_construction_main ($) { Line 3264  sub _tree_construction_main ($) {
3264        redo B;        redo B;
3265      } elsif ($token->{type} == END_OF_FILE_TOKEN) {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3266        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3267            !!!cp ('t74');
3268          #          #
3269        } else {        } else {
3270          ## Generate implied end tags          ## Generate implied end tags
3271          if ({          while ({
3272               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,                  dd => 1, dt => 1, li => 1, p => 1,
3273               tbody => 1, tfoot=> 1, thead => 1,                 }->{$self->{open_elements}->[-1]->[1]}) {
3274              }->{$self->{open_elements}->[-1]->[1]}) {            !!!cp ('t75');
3275            !!!back-token;            pop @{$self->{open_elements}};
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
3276          }          }
3277                    
3278          if (@{$self->{open_elements}} > 2 or          if (@{$self->{open_elements}} > 2 or
3279              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3280              !!!cp ('t76');
3281            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3282          } elsif (defined $self->{inner_html_node} and          } elsif (defined $self->{inner_html_node} and
3283                   @{$self->{open_elements}} > 1 and                   @{$self->{open_elements}} > 1 and
3284                   $self->{open_elements}->[1]->[1] ne 'body') {                   $self->{open_elements}->[1]->[1] ne 'body') {
3285    ## ISSUE: This case is never reached.
3286              !!!cp ('t77');
3287            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3288            } else {
3289              !!!cp ('t78');
3290          }          }
3291    
3292          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
# Line 3129  sub _tree_construction_main ($) { Line 3297  sub _tree_construction_main ($) {
3297      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3298               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3299        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3300          ## Turn into the main phase          !!!cp ('t79');
3301          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3302          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3303        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3304          ## Turn into the main phase          !!!cp ('t80');
3305          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3306          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3307          } else {
3308            !!!cp ('t81');
3309        }        }
3310    
3311  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3312  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag');
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3313        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3314        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3315          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3316              !!!cp ('t84');
3317            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3318              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3319               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3156  sub _tree_construction_main ($) { Line 3324  sub _tree_construction_main ($) {
3324      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3325        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3326        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3327            !!!cp ('t85');
3328          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3329        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3330            !!!cp ('t86');
3331          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3332        } else {        } else {
3333            !!!cp ('t87');
3334          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3335        }        }
3336        !!!next-token;        !!!next-token;
# Line 3169  sub _tree_construction_main ($) { Line 3340  sub _tree_construction_main ($) {
3340          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3341            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3342            unless (length $token->{data}) {            unless (length $token->{data}) {
3343                !!!cp ('t88');
3344              !!!next-token;              !!!next-token;
3345              redo B;              redo B;
3346            }            }
3347          }          }
3348    
3349          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3350              !!!cp ('t89');
3351            ## As if <head>            ## As if <head>
3352            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
3353            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3185  sub _tree_construction_main ($) { Line 3358  sub _tree_construction_main ($) {
3358    
3359            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3360          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3361              !!!cp ('t90');
3362            ## As if </noscript>            ## As if </noscript>
3363            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3364            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 3195  sub _tree_construction_main ($) { Line 3369  sub _tree_construction_main ($) {
3369    
3370            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3371          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3372              !!!cp ('t91');
3373            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3374    
3375            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3376            } else {
3377              !!!cp ('t92');
3378          }          }
3379    
3380              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3209  sub _tree_construction_main ($) { Line 3386  sub _tree_construction_main ($) {
3386            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3387              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3388                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3389                    !!!cp ('t93');
3390                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3391                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3392                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
# Line 3216  sub _tree_construction_main ($) { Line 3394  sub _tree_construction_main ($) {
3394                  !!!next-token;                  !!!next-token;
3395                  redo B;                  redo B;
3396                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3397                    !!!cp ('t94');
3398                  #                  #
3399                } else {                } else {
3400                    !!!cp ('t95');
3401                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3402                  ## Ignore the token                  ## Ignore the token
3403                  !!!next-token;                  !!!next-token;
3404                  redo B;                  redo B;
3405                }                }
3406              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3407                  !!!cp ('t96');
3408                ## As if <head>                ## As if <head>
3409                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3410                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3231  sub _tree_construction_main ($) { Line 3412  sub _tree_construction_main ($) {
3412    
3413                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3414                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3415                } else {
3416                  !!!cp ('t97');
3417              }              }
3418    
3419              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3420                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3421                    !!!cp ('t98');
3422                  ## As if </noscript>                  ## As if </noscript>
3423                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3424                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
3425                                
3426                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3427                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3428                  } else {
3429                    !!!cp ('t99');
3430                }                }
3431    
3432                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3433                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3434                    !!!cp ('t100');
3435                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3436                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3437                  } else {
3438                    !!!cp ('t101');
3439                }                }
3440                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3441                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 3257  sub _tree_construction_main ($) { Line 3446  sub _tree_construction_main ($) {
3446              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3447                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3448                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3449                    !!!cp ('t102');
3450                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3451                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3452                  } else {
3453                    !!!cp ('t103');
3454                }                }
3455                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3456                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 3269  sub _tree_construction_main ($) { Line 3461  sub _tree_construction_main ($) {
3461              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3462                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3463                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3464                    !!!cp ('t104');
3465                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3466                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3467                  } else {
3468                    !!!cp ('t105');
3469                }                }
3470                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3471                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.
3472    
3473                unless ($self->{confident}) {                unless ($self->{confident}) {
3474                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3475                      !!!cp ('t106');
3476                    $self->{change_encoding}                    $self->{change_encoding}
3477                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value});
3478                                        
# Line 3291  sub _tree_construction_main ($) { Line 3487  sub _tree_construction_main ($) {
3487                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
3488                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3489                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3490                        !!!cp ('t107');
3491                      $self->{change_encoding}                      $self->{change_encoding}
3492                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3493                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3494                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3495                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
3496                                                     ->{has_reference});                                                     ->{has_reference});
3497                      } else {
3498                        !!!cp ('t108');
3499                    }                    }
3500                  }                  }
3501                } else {                } else {
3502                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
3503                      !!!cp ('t109');
3504                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3505                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3506                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
3507                                                 ->{has_reference});                                                 ->{has_reference});
3508                  }                  }
3509                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
3510                      !!!cp ('t110');
3511                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3512                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3513                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3320  sub _tree_construction_main ($) { Line 3521  sub _tree_construction_main ($) {
3521                redo B;                redo B;
3522              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3523                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3524                    !!!cp ('t111');
3525                  ## As if </noscript>                  ## As if </noscript>
3526                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3527                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
# Line 3327  sub _tree_construction_main ($) { Line 3529  sub _tree_construction_main ($) {
3529                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3530                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3531                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3532                    !!!cp ('t112');
3533                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3534                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3535                  } else {
3536                    !!!cp ('t113');
3537                }                }
3538    
3539                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3540                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3541                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3542                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
                               sub { $parent->append_child ($_[0]) });  
3543                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3544                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3545                redo B;                redo B;
# Line 3344  sub _tree_construction_main ($) { Line 3548  sub _tree_construction_main ($) {
3548                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3549                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3550                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3551                    !!!cp ('t114');
3552                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3553                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3554                  } else {
3555                    !!!cp ('t115');
3556                }                }
3557                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3558                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3559                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3560                redo B;                redo B;
3561              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3562                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3563                    !!!cp ('t116');
3564                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3565                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3566                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3567                  !!!next-token;                  !!!next-token;
3568                  redo B;                  redo B;
3569                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3570                    !!!cp ('t117');
3571                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3572                  ## Ignore the token                  ## Ignore the token
3573                  !!!next-token;                  !!!next-token;
3574                  redo B;                  redo B;
3575                } else {                } else {
3576                    !!!cp ('t118');
3577                  #                  #
3578                }                }
3579              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3580                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3581                    !!!cp ('t119');
3582                  ## As if </noscript>                  ## As if </noscript>
3583                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3584                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
# Line 3375  sub _tree_construction_main ($) { Line 3586  sub _tree_construction_main ($) {
3586                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3587                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3588                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3589                    !!!cp ('t120');
3590                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3591                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3592                  } else {
3593                    !!!cp ('t121');
3594                }                }
3595    
3596                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
# Line 3387  sub _tree_construction_main ($) { Line 3601  sub _tree_construction_main ($) {
3601              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3602                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3603                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3604                    !!!cp ('t122');
3605                  ## As if </noscript>                  ## As if </noscript>
3606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3607                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3397  sub _tree_construction_main ($) { Line 3612  sub _tree_construction_main ($) {
3612                                    
3613                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3614                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3615                    !!!cp ('t124');
3616                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3617                                    
3618                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3619                  } else {
3620                    !!!cp ('t125');
3621                }                }
3622    
3623                ## "after head" insertion mode                ## "after head" insertion mode
3624                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3625                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3626                    !!!cp ('t126');
3627                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3628                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3629                    !!!cp ('t127');
3630                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3631                } else {                } else {
3632                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 3414  sub _tree_construction_main ($) { Line 3634  sub _tree_construction_main ($) {
3634                !!!next-token;                !!!next-token;
3635                redo B;                redo B;
3636              } else {              } else {
3637                  !!!cp ('t128');
3638                #                #
3639              }              }
3640    
3641              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3642                  !!!cp ('t129');
3643                ## As if </noscript>                ## As if </noscript>
3644                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3645                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3428  sub _tree_construction_main ($) { Line 3650  sub _tree_construction_main ($) {
3650    
3651                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3652              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3653                  !!!cp ('t130');
3654                ## As if </head>                ## As if </head>
3655                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3656    
3657                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3658                } else {
3659                  !!!cp ('t131');
3660              }              }
3661    
3662              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3443  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3669              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3670                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3671                    !!!cp ('t132');
3672                  ## As if <head>                  ## As if <head>
3673                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3674                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3454  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680                  !!!next-token;                  !!!next-token;
3681                  redo B;                  redo B;
3682                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3683                    !!!cp ('t133');
3684                  ## As if </noscript>                  ## As if </noscript>
3685                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3686                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head');
3687                                    
3688                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3689                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3464  sub _tree_construction_main ($) { Line 3691  sub _tree_construction_main ($) {
3691                  !!!next-token;                  !!!next-token;
3692                  redo B;                  redo B;
3693                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3694                    !!!cp ('t134');
3695                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3696                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3697                  !!!next-token;                  !!!next-token;
3698                  redo B;                  redo B;
3699                } else {                } else {
3700                    !!!cp ('t135');
3701                  #                  #
3702                }                }
3703              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3704                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3705                    !!!cp ('t136');
3706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3707                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3708                  !!!next-token;                  !!!next-token;
3709                  redo B;                  redo B;
3710                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3711                    !!!cp ('t137');
3712                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3713                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3714                  !!!next-token;                  !!!next-token;
3715                  redo B;                  redo B;
3716                } else {                } else {
3717                    !!!cp ('t138');
3718                  #                  #
3719                }                }
3720              } elsif ({              } elsif ({
3721                        body => 1, html => 1,                        body => 1, html => 1,
3722                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3723                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3724                    !!!cp ('t139');
3725                  ## As if <head>                  ## As if <head>
3726                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3727                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3497  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3731                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3732                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3733                    !!!cp ('t140');
3734                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3735                  ## Ignore the token                  ## Ignore the token
3736                  !!!next-token;                  !!!next-token;
3737                  redo B;                  redo B;
3738                  } else {
3739                    !!!cp ('t141');
3740                }                }
3741                                
3742                #                #
# Line 3508  sub _tree_construction_main ($) { Line 3744  sub _tree_construction_main ($) {
3744                        p => 1, br => 1,                        p => 1, br => 1,
3745                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3746                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3747                    !!!cp ('t142');
3748                  ## As if <head>                  ## As if <head>
3749                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3750                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3515  sub _tree_construction_main ($) { Line 3752  sub _tree_construction_main ($) {
3752    
3753                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3754                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3755                  } else {
3756                    !!!cp ('t143');
3757                }                }
3758    
3759                #                #
3760              } else {              } else {
3761                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3762                    !!!cp ('t144');
3763                  #                  #
3764                } else {                } else {
3765                    !!!cp ('t145');
3766                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3767                  ## Ignore the token                  ## Ignore the token
3768                  !!!next-token;                  !!!next-token;
# Line 3530  sub _tree_construction_main ($) { Line 3771  sub _tree_construction_main ($) {
3771              }              }
3772    
3773              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3774                  !!!cp ('t146');
3775                ## As if </noscript>                ## As if </noscript>
3776                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3777                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3540  sub _tree_construction_main ($) { Line 3782  sub _tree_construction_main ($) {
3782    
3783                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3784              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3785                  !!!cp ('t147');
3786                ## As if </head>                ## As if </head>
3787                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3788    
3789                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3790              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3791    ## ISSUE: This case cannot be reached?
3792                  !!!cp ('t148');
3793                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3794                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3795                !!!next-token;                !!!next-token;
3796                redo B;                redo B;
3797                } else {
3798                  !!!cp ('t149');
3799              }              }
3800    
3801              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3564  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3812      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3813            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3814                !!!cp ('t150');
3815              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3816              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3817                            
# Line 3582  sub _tree_construction_main ($) { Line 3830  sub _tree_construction_main ($) {
3830                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3831                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3832                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3833                        !!!cp ('t151');
3834                      $tn = $node->[1];                      $tn = $node->[1];
3835                      last INSCOPE;                      last INSCOPE;
3836                    } elsif ({                    } elsif ({
3837                              table => 1, html => 1,                              table => 1, html => 1,
3838                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3839                        !!!cp ('t152');
3840                      last INSCOPE;                      last INSCOPE;
3841                    }                    }
3842                  } # INSCOPE                  } # INSCOPE
3843                    unless (defined $tn) {                    unless (defined $tn) {
3844                        !!!cp ('t153');
3845    ## TODO: This error type is wrong.
3846                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3847                      ## Ignore the token                      ## Ignore the token
3848                      !!!next-token;                      !!!next-token;
3849                      redo B;                      redo B;
3850                    }                    }
3851                                    
3852                    !!!cp ('t154');
3853                  ## Close the cell                  ## Close the cell
3854                  !!!back-token; # <?>                  !!!back-token; # <?>
3855                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
# Line 3610  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3864                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3865                    if ($node->[1] eq 'caption') {                    if ($node->[1] eq 'caption') {
3866                        !!!cp ('t155');
3867                      $i = $_;                      $i = $_;
3868                      last INSCOPE;                      last INSCOPE;
3869                    } elsif ({                    } elsif ({
3870                              table => 1, html => 1,                              table => 1, html => 1,
3871                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3872                        !!!cp ('t156');
3873                      last INSCOPE;                      last INSCOPE;
3874                    }                    }
3875                  } # INSCOPE                  } # INSCOPE
3876                    unless (defined $i) {                    unless (defined $i) {
3877                        !!!cp ('t157');
3878    ## TODO: this type is wrong.
3879                      !!!parse-error (type => 'unmatched end tag:caption');                      !!!parse-error (type => 'unmatched end tag:caption');
3880                      ## Ignore the token                      ## Ignore the token
3881                      !!!next-token;                      !!!next-token;
# Line 3626  sub _tree_construction_main ($) { Line 3883  sub _tree_construction_main ($) {
3883                    }                    }
3884                                    
3885                  ## generate implied end tags                  ## generate implied end tags
3886                  if ({                  while ({
3887                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3888                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
3889                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
3890                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3891                  }                  }
3892    
3893                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3894                      !!!cp ('t159');
3895                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3896                    } else {
3897                      !!!cp ('t160');
3898                  }                  }
3899                                    
3900                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3652  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906                  ## reprocess                  ## reprocess
3907                  redo B;                  redo B;
3908                } else {                } else {
3909                    !!!cp ('t161');
3910                  #                  #
3911                }                }
3912              } else {              } else {
3913                  !!!cp ('t162');
3914                #                #
3915              }              }
3916            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3665  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3922                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3923                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3924                        !!!cp ('t163');
3925                      $i = $_;                      $i = $_;
3926                      last INSCOPE;                      last INSCOPE;
3927                    } elsif ({                    } elsif ({
3928                              table => 1, html => 1,                              table => 1, html => 1,
3929                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3930                        !!!cp ('t164');
3931                      last INSCOPE;                      last INSCOPE;
3932                    }                    }
3933                  } # INSCOPE                  } # INSCOPE
3934                    unless (defined $i) {                    unless (defined $i) {
3935                        !!!cp ('t165');
3936                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3937                      ## Ignore the token                      ## Ignore the token
3938                      !!!next-token;                      !!!next-token;
# Line 3681  sub _tree_construction_main ($) { Line 3940  sub _tree_construction_main ($) {
3940                    }                    }
3941                                    
3942                  ## generate implied end tags                  ## generate implied end tags
3943                  if ({                  while ({
3944                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3945                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
3946                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
3947                       tr => 1,                    pop @{$self->{open_elements}};
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3948                  }                  }
3949                    
3950                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3951                      !!!cp ('t167');
3952                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3953                    } else {
3954                      !!!cp ('t168');
3955                  }                  }
3956                                    
3957                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3707  sub _tree_construction_main ($) { Line 3963  sub _tree_construction_main ($) {
3963                  !!!next-token;                  !!!next-token;
3964                  redo B;                  redo B;
3965                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3966                    !!!cp ('t169');
3967                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3968                  ## Ignore the token                  ## Ignore the token
3969                  !!!next-token;                  !!!next-token;
3970                  redo B;                  redo B;
3971                } else {                } else {
3972                    !!!cp ('t170');
3973                  #                  #
3974                }                }
3975              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
# Line 3721  sub _tree_construction_main ($) { Line 3979  sub _tree_construction_main ($) {
3979                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3980                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3981                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3982                        !!!cp ('t171');
3983                      $i = $_;                      $i = $_;
3984                      last INSCOPE;                      last INSCOPE;
3985                    } elsif ({                    } elsif ({
3986                              table => 1, html => 1,                              table => 1, html => 1,
3987                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3988                        !!!cp ('t172');
3989                      last INSCOPE;                      last INSCOPE;
3990                    }                    }
3991                  } # INSCOPE                  } # INSCOPE
3992                    unless (defined $i) {                    unless (defined $i) {
3993                        !!!cp ('t173');
3994                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                      ## Ignore the token                      ## Ignore the token
3996                      !!!next-token;                      !!!next-token;
# Line 3737  sub _tree_construction_main ($) { Line 3998  sub _tree_construction_main ($) {
3998                    }                    }
3999                                    
4000                  ## generate implied end tags                  ## generate implied end tags
4001                  if ({                  while ({
4002                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4003                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4004                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4005                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4006                  }                  }
4007                                    
4008                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4009                      !!!cp ('t175');
4010                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4011                    } else {
4012                      !!!cp ('t176');
4013                  }                  }
4014                                    
4015                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3761  sub _tree_construction_main ($) { Line 4021  sub _tree_construction_main ($) {
4021                  !!!next-token;                  !!!next-token;
4022                  redo B;                  redo B;
4023                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4024                    !!!cp ('t177');
4025                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4026                  ## Ignore the token                  ## Ignore the token
4027                  !!!next-token;                  !!!next-token;
4028                  redo B;                  redo B;
4029                } else {                } else {
4030                    !!!cp ('t178');
4031                  #                  #
4032                }                }
4033              } elsif ({              } elsif ({
# Line 3779  sub _tree_construction_main ($) { Line 4041  sub _tree_construction_main ($) {
4041                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4042                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4043                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4044                      !!!cp ('t179');
4045                    $i = $_;                    $i = $_;
4046                    last INSCOPE;                    last INSCOPE;
4047                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4048                      !!!cp ('t180');
4049                    $tn = $node->[1];                    $tn = $node->[1];
4050                    ## NOTE: There is exactly one |td| or |th| element                    ## NOTE: There is exactly one |td| or |th| element
4051                    ## in scope in the stack of open elements by definition.                    ## in scope in the stack of open elements by definition.
4052                  } elsif ({                  } elsif ({
4053                            table => 1, html => 1,                            table => 1, html => 1,
4054                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4055                      !!!cp ('t181');
4056                    last INSCOPE;                    last INSCOPE;
4057                  }                  }
4058                } # INSCOPE                } # INSCOPE
4059                unless (defined $i) {                unless (defined $i) {
4060                    !!!cp ('t182');
4061                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4062                  ## Ignore the token                  ## Ignore the token
4063                  !!!next-token;                  !!!next-token;
4064                  redo B;                  redo B;
4065                  } else {
4066                    !!!cp ('t183');
4067                }                }
4068    
4069                ## Close the cell                ## Close the cell
# Line 3812  sub _tree_construction_main ($) { Line 4080  sub _tree_construction_main ($) {
4080                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4081                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4082                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4083                      !!!cp ('t184');
4084                    $i = $_;                    $i = $_;
4085                    last INSCOPE;                    last INSCOPE;
4086                  } elsif ({                  } elsif ({
4087                            table => 1, html => 1,                            table => 1, html => 1,
4088                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4089                      !!!cp ('t185');
4090                    last INSCOPE;                    last INSCOPE;
4091                  }                  }
4092                } # INSCOPE                } # INSCOPE
4093                unless (defined $i) {                unless (defined $i) {
4094                    !!!cp ('t186');
4095                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4096                  ## Ignore the token                  ## Ignore the token
4097                  !!!next-token;                  !!!next-token;
# Line 3828  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099                }                }
4100                                
4101                ## generate implied end tags                ## generate implied end tags
4102                if ({                while ({
4103                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4104                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4105                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4106                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4107                }                }
4108    
4109                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4110                    !!!cp ('t188');
4111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4112                  } else {
4113                    !!!cp ('t189');
4114                }                }
4115    
4116                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3857  sub _tree_construction_main ($) { Line 4125  sub _tree_construction_main ($) {
4125                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4126                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4127                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4128                    !!!cp ('t190');
4129                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4130                  ## Ignore the token                  ## Ignore the token
4131                  !!!next-token;                  !!!next-token;
4132                  redo B;                  redo B;
4133                } else {                } else {
4134                    !!!cp ('t191');
4135                  #                  #
4136                }                }
4137              } elsif ({              } elsif ({
# Line 3869  sub _tree_construction_main ($) { Line 4139  sub _tree_construction_main ($) {
4139                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4140                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4141                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4142                  !!!cp ('t192');
4143                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4144                ## Ignore the token                ## Ignore the token
4145                !!!next-token;                !!!next-token;
4146                redo B;                redo B;
4147              } else {              } else {
4148                  !!!cp ('t193');
4149                #                #
4150              }              }
4151        } else {        } else {
# Line 3884  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156        #        #
4157      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4158        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4159              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4160                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4161              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4162                                
4163                unless (length $token->{data}) {            unless (length $token->{data}) {
4164                  !!!next-token;              !!!cp ('t194');
4165                  redo B;              !!!next-token;
4166                }              redo B;
4167              }            } else {
4168                !!!cp ('t195');
4169              }
4170            }
4171    
4172              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4173    
# Line 3913  sub _tree_construction_main ($) { Line 4189  sub _tree_construction_main ($) {
4189                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4190                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4191                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4192                        !!!cp ('t196');
4193                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4194                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4195                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4196                    } else {                    } else {
4197                        !!!cp ('t197');
4198                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4199                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4200                    }                    }
# Line 3928  sub _tree_construction_main ($) { Line 4206  sub _tree_construction_main ($) {
4206                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4207                if (defined $prev_sibling and                if (defined $prev_sibling and
4208                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4209                    !!!cp ('t198');
4210                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4211                } else {                } else {
4212                    !!!cp ('t199');
4213                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4214                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4215                     $next_sibling);                     $next_sibling);
4216                }                }
4217              } else {            $open_tables->[-1]->[1] = 1; # tainted
4218                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4219              }            !!!cp ('t200');
4220              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4221            }
4222                            
4223              !!!next-token;          !!!next-token;
4224              redo B;          redo B;
4225        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4226              if ({              if ({
4227                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3949  sub _tree_construction_main ($) { Line 4231  sub _tree_construction_main ($) {
4231                  ## Clear back to table context                  ## Clear back to table context
4232                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4233                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4234                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4235                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4236                  }                  }
4237                                    
# Line 3960  sub _tree_construction_main ($) { Line 4242  sub _tree_construction_main ($) {
4242    
4243                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4244                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4245                      !!!cp ('t202');
4246                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
4247                  }                  }
4248                                    
# Line 3967  sub _tree_construction_main ($) { Line 4250  sub _tree_construction_main ($) {
4250                  while (not {                  while (not {
4251                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4252                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4253                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4254                      ## ISSUE: Can this case be reached?
4255                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4256                  }                  }
4257                                    
4258                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4259                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4260                      !!!cp ('t204');
4261                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4262                    !!!next-token;                    !!!next-token;
4263                    redo B;                    redo B;
4264                  } else {                  } else {
4265                      !!!cp ('t205');
4266                    !!!insert-element ('tr');                    !!!insert-element ('tr');
4267                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4268                  }                  }
4269                  } else {
4270                    !!!cp ('t206');
4271                }                }
4272    
4273                ## Clear back to table row context                ## Clear back to table row context
4274                while (not {                while (not {
4275                  tr => 1, html => 1,                  tr => 1, html => 1,
4276                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4277                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4278                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4279                }                }
4280                                
# Line 4009  sub _tree_construction_main ($) { Line 4297  sub _tree_construction_main ($) {
4297                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4298                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4299                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4300                        !!!cp ('t208');
4301                      $i = $_;                      $i = $_;
4302                      last INSCOPE;                      last INSCOPE;
4303                    } elsif ({                    } elsif ({
4304                              table => 1, html => 1,                              html => 1,
4305    
4306                                ## NOTE: This element does not appear here, maybe.
4307                                table => 1,
4308                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4309                        !!!cp ('t209');
4310                      last INSCOPE;                      last INSCOPE;
4311                    }                    }
4312                  } # INSCOPE                  } # INSCOPE
4313                  unless (defined $i) {                  unless (defined $i) {
4314                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4315    ## TODO: This type is wrong.
4316                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4317                    ## Ignore the token                    ## Ignore the token
4318                    !!!next-token;                    !!!next-token;
4319                    redo B;                    redo B;
# Line 4028  sub _tree_construction_main ($) { Line 4323  sub _tree_construction_main ($) {
4323                  while (not {                  while (not {
4324                    tr => 1, html => 1,                    tr => 1, html => 1,
4325                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4326                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4327                      ## ISSUE: Can this case be reached?
4328                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4329                  }                  }
4330                                    
4331                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4332                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4333                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4334                      !!!cp ('t212');
4335                    ## reprocess                    ## reprocess
4336                    redo B;                    redo B;
4337                  } else {                  } else {
4338                      !!!cp ('t213');
4339                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4340                  }                  }
4341                }                }
# Line 4050  sub _tree_construction_main ($) { Line 4348  sub _tree_construction_main ($) {
4348                    if ({                    if ({
4349                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4350                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4351                        !!!cp ('t214');
4352                      $i = $_;                      $i = $_;
4353                      last INSCOPE;                      last INSCOPE;
4354                    } elsif ({                    } elsif ({
4355                              table => 1, html => 1,                              table => 1, html => 1,
4356                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4357                        !!!cp ('t215');
4358                      last INSCOPE;                      last INSCOPE;
4359                    }                    }
4360                  } # INSCOPE                  } # INSCOPE
4361                  unless (defined $i) {                  unless (defined $i) {
4362                      !!!cp ('t216');
4363    ## TODO: This erorr type ios wrong.
4364                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4365                    ## Ignore the token                    ## Ignore the token
4366                    !!!next-token;                    !!!next-token;
# Line 4069  sub _tree_construction_main ($) { Line 4371  sub _tree_construction_main ($) {
4371                  while (not {                  while (not {
4372                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4373                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4374                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4375                      ## ISSUE: Can this state be reached?
4376                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4377                  }                  }
4378                                    
# Line 4083  sub _tree_construction_main ($) { Line 4386  sub _tree_construction_main ($) {
4386                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4387                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4388                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4389                  } else {
4390                    !!!cp ('t218');
4391                }                }
4392    
4393                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4394                  ## Clear back to table context                  ## Clear back to table context
4395                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4396                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4397                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4398                      ## ISSUE: Can this state be reached?
4399                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4400                  }                  }
4401                                    
# Line 4105  sub _tree_construction_main ($) { Line 4411  sub _tree_construction_main ($) {
4411                  ## Clear back to table context                  ## Clear back to table context
4412                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4413                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4414                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4415                      ## ISSUE: Can this state be reached?
4416                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4417                  }                  }
4418                                    
# Line 4134  sub _tree_construction_main ($) { Line 4441  sub _tree_construction_main ($) {
4441                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4442                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4443                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4444                      !!!cp ('t221');
4445                    $i = $_;                    $i = $_;
4446                    last INSCOPE;                    last INSCOPE;
4447                  } elsif ({                  } elsif ({
4448                            table => 1, html => 1,                            #table => 1,
4449                              html => 1,
4450                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4451                      !!!cp ('t222');
4452                    last INSCOPE;                    last INSCOPE;
4453                  }                  }
4454                } # INSCOPE                } # INSCOPE
4455                unless (defined $i) {                unless (defined $i) {
4456                    !!!cp ('t223');
4457    ## TODO: The following is wrong, maybe.
4458                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4459                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4460                  !!!next-token;                  !!!next-token;
# Line 4150  sub _tree_construction_main ($) { Line 4462  sub _tree_construction_main ($) {
4462                }                }
4463                                
4464                ## generate implied end tags                ## generate implied end tags
4465                if ({                while ({
4466                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4467                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4468                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4469                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4470                }                }
4471    
4472                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4473                    !!!cp ('t225');
4474    ## ISSUE: Can this case be reached?
4475                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4476                  } else {
4477                    !!!cp ('t226');
4478                }                }
4479    
4480                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4481                  pop @{$open_tables};
4482    
4483                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4484    
4485                ## reprocess                ## reprocess
4486                redo B;                redo B;
4487          } else {          } elsif ($token->{tag_name} eq 'input') {
4488            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4489                if ($token->{attributes}->{type}) { ## TODO: case
4490                  my $type = lc $token->{attributes}->{type}->{value};
4491                  if ($type eq 'hidden') {
4492                    !!!cp ('t227.3');
4493                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4494    
4495            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4496    
4497                    ## TODO: form element pointer
4498    
4499                    pop @{$self->{open_elements}};
4500    
4501                    !!!next-token;
4502                    redo B;
4503                  } else {
4504                    !!!cp ('t227.2');
4505                    #
4506                  }
4507                } else {
4508                  !!!cp ('t227.1');
4509                  #
4510                }
4511              } else {
4512                !!!cp ('t227.4');
4513                #
4514              }
4515            } else {
4516              !!!cp ('t227');
4517            #            #
4518          }          }
4519    
4520            !!!parse-error (type => 'in table:'.$token->{tag_name});
4521    
4522            $insert = $insert_to_foster;
4523            #
4524        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4525              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4526                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4187  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4530                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4531                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4532                      !!!cp ('t228');
4533                    $i = $_;                    $i = $_;
4534                    last INSCOPE;                    last INSCOPE;
4535                  } elsif ({                  } elsif ({
4536                            table => 1, html => 1,                            table => 1, html => 1,
4537                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4538                      !!!cp ('t229');
4539                    last INSCOPE;                    last INSCOPE;
4540                  }                  }
4541                } # INSCOPE                } # INSCOPE
4542                unless (defined $i) {                unless (defined $i) {
4543                    !!!cp ('t230');
4544                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4545                  ## Ignore the token                  ## Ignore the token
4546                  !!!next-token;                  !!!next-token;
4547                  redo B;                  redo B;
4548                  } else {
4549                    !!!cp ('t232');
4550                }                }
4551    
4552                ## Clear back to table row context                ## Clear back to table row context
4553                while (not {                while (not {
4554                  tr => 1, html => 1,                  tr => 1, html => 1,
4555                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4556                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4557    ## ISSUE: Can this state be reached?
4558                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4559                }                }
4560    
# Line 4222  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4571                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4572                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4573                        !!!cp ('t233');
4574                      $i = $_;                      $i = $_;
4575                      last INSCOPE;                      last INSCOPE;
4576                    } elsif ({                    } elsif ({
4577                              table => 1, html => 1,                              table => 1, html => 1,
4578                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4579                        !!!cp ('t234');
4580                      last INSCOPE;                      last INSCOPE;
4581                    }                    }
4582                  } # INSCOPE                  } # INSCOPE
4583                  unless (defined $i) {                  unless (defined $i) {
4584                      !!!cp ('t235');
4585    ## TODO: The following is wrong.
4586                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4587                    ## Ignore the token                    ## Ignore the token
4588                    !!!next-token;                    !!!next-token;
# Line 4241  sub _tree_construction_main ($) { Line 4593  sub _tree_construction_main ($) {
4593                  while (not {                  while (not {
4594                    tr => 1, html => 1,                    tr => 1, html => 1,
4595                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4596                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4597    ## ISSUE: Can this state be reached?
4598                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4599                  }                  }
4600                                    
# Line 4258  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                    if ({                    if ({
4612                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4613                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4614                        !!!cp ('t237');
4615                      $i = $_;                      $i = $_;
4616                      last INSCOPE;                      last INSCOPE;
4617                    } elsif ({                    } elsif ({
4618                              table => 1, html => 1,                              table => 1, html => 1,
4619                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4620                        !!!cp ('t238');
4621                      last INSCOPE;                      last INSCOPE;
4622                    }                    }
4623                  } # INSCOPE                  } # INSCOPE
4624                  unless (defined $i) {                  unless (defined $i) {
4625                      !!!cp ('t239');
4626                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4627                    ## Ignore the token                    ## Ignore the token
4628                    !!!next-token;                    !!!next-token;
# Line 4277  sub _tree_construction_main ($) { Line 4633  sub _tree_construction_main ($) {
4633                  while (not {                  while (not {
4634                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4635                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4636                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4637                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4638                  }                  }
4639                                    
# Line 4293  sub _tree_construction_main ($) { Line 4649  sub _tree_construction_main ($) {
4649                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4650                }                }
4651    
4652                  ## NOTE: </table> in the "in table" insertion mode.
4653                  ## When you edit the code fragment below, please ensure that
4654                  ## the code for <table> in the "in table" insertion mode
4655                  ## is synced with it.
4656    
4657                ## have a table element in table scope                ## have a table element in table scope
4658                my $i;                my $i;
4659                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4660                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4661                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4662                      !!!cp ('t241');
4663                    $i = $_;                    $i = $_;
4664                    last INSCOPE;                    last INSCOPE;
4665                  } elsif ({                  } elsif ({
4666                            table => 1, html => 1,                            table => 1, html => 1,
4667                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4668                      !!!cp ('t242');
4669                    last INSCOPE;                    last INSCOPE;
4670                  }                  }
4671                } # INSCOPE                } # INSCOPE
4672                unless (defined $i) {                unless (defined $i) {
4673                    !!!cp ('t243');
4674                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4675                  ## Ignore the token                  ## Ignore the token
4676                  !!!next-token;                  !!!next-token;
4677                  redo B;                  redo B;
4678                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4679                                    
4680                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4681                  pop @{$open_tables};
4682                                
4683                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4684                                
# Line 4345  sub _tree_construction_main ($) { Line 4694  sub _tree_construction_main ($) {
4694                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4695                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4696                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4697                        !!!cp ('t247');
4698                      $i = $_;                      $i = $_;
4699                      last INSCOPE;                      last INSCOPE;
4700                    } elsif ({                    } elsif ({
4701                              table => 1, html => 1,                              table => 1, html => 1,
4702                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4703                        !!!cp ('t248');
4704                      last INSCOPE;                      last INSCOPE;
4705                    }                    }
4706                  } # INSCOPE                  } # INSCOPE
4707                    unless (defined $i) {                    unless (defined $i) {
4708                        !!!cp ('t249');
4709                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4710                      ## Ignore the token                      ## Ignore the token
4711                      !!!next-token;                      !!!next-token;
# Line 4366  sub _tree_construction_main ($) { Line 4718  sub _tree_construction_main ($) {
4718                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4719                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4720                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4721                        !!!cp ('t250');
4722                      $i = $_;                      $i = $_;
4723                      last INSCOPE;                      last INSCOPE;
4724                    } elsif ({                    } elsif ({
4725                              table => 1, html => 1,                              table => 1, html => 1,
4726                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4727                        !!!cp ('t251');
4728                      last INSCOPE;                      last INSCOPE;
4729                    }                    }
4730                  } # INSCOPE                  } # INSCOPE
4731                    unless (defined $i) {                    unless (defined $i) {
4732                        !!!cp ('t252');
4733                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr');
4734                      ## Ignore the token                      ## Ignore the token
4735                      !!!next-token;                      !!!next-token;
# Line 4385  sub _tree_construction_main ($) { Line 4740  sub _tree_construction_main ($) {
4740                  while (not {                  while (not {
4741                    tr => 1, html => 1,                    tr => 1, html => 1,
4742                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4743                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4744    ## ISSUE: Can this case be reached?
4745                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4746                  }                  }
4747                                    
# Line 4399  sub _tree_construction_main ($) { Line 4755  sub _tree_construction_main ($) {
4755                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4756                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4757                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4758                      !!!cp ('t254');
4759                    $i = $_;                    $i = $_;
4760                    last INSCOPE;                    last INSCOPE;
4761                  } elsif ({                  } elsif ({
4762                            table => 1, html => 1,                            table => 1, html => 1,
4763                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4764                      !!!cp ('t255');
4765                    last INSCOPE;                    last INSCOPE;
4766                  }                  }
4767                } # INSCOPE                } # INSCOPE
4768                unless (defined $i) {                unless (defined $i) {
4769                    !!!cp ('t256');
4770                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4771                  ## Ignore the token                  ## Ignore the token
4772                  !!!next-token;                  !!!next-token;
# Line 4418  sub _tree_construction_main ($) { Line 4777  sub _tree_construction_main ($) {
4777                while (not {                while (not {
4778                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4779                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4780                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4781    ## ISSUE: Can this case be reached?
4782                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4783                }                }
4784    
# Line 4432  sub _tree_construction_main ($) { Line 4792  sub _tree_construction_main ($) {
4792                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4793                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4794                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4795                  !!!cp ('t258');
4796                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4797                ## Ignore the token                ## Ignore the token
4798                !!!next-token;                !!!next-token;
4799                redo B;                redo B;
4800          } else {          } else {
4801              !!!cp ('t259');
4802            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name});
4803    
4804            $insert = $insert_to_foster;            $insert = $insert_to_foster;
# Line 4450  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4813                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4814                unless (length $token->{data}) {                unless (length $token->{data}) {
4815                    !!!cp ('t260');
4816                  !!!next-token;                  !!!next-token;
4817                  redo B;                  redo B;
4818                }                }
4819              }              }
4820                            
4821                !!!cp ('t261');
4822              #              #
4823            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4824              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4825                  !!!cp ('t262');
4826                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4827                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4828                !!!next-token;                !!!next-token;
4829                redo B;                redo B;
4830              } else {              } else {
4831                  !!!cp ('t263');
4832                #                #
4833              }              }
4834            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4835              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4836                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4837                    !!!cp ('t264');
4838                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
4839                  ## Ignore the token                  ## Ignore the token
4840                  !!!next-token;                  !!!next-token;
4841                  redo B;                  redo B;
4842                } else {                } else {
4843                    !!!cp ('t265');
4844                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4845                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4846                  !!!next-token;                  !!!next-token;
4847                  redo B;                              redo B;            
4848                }                }
4849              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4850                  !!!cp ('t266');
4851                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col');
4852                ## Ignore the token                ## Ignore the token
4853                !!!next-token;                !!!next-token;
4854                redo B;                redo B;
4855              } else {              } else {
4856                  !!!cp ('t267');
4857                #                #
4858              }              }
4859            } else {            } else {
4860              #              die "$0: $token->{type}: Unknown token type";
4861            }            }
4862    
4863            ## As if </colgroup>            ## As if </colgroup>
4864            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4865                !!!cp ('t269');
4866              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4867              ## Ignore the token              ## Ignore the token
4868              !!!next-token;              !!!next-token;
4869              redo B;              redo B;
4870            } else {            } else {
4871                !!!cp ('t270');
4872              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4873              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
4874              ## reprocess              ## reprocess
# Line 4504  sub _tree_construction_main ($) { Line 4876  sub _tree_construction_main ($) {
4876            }            }
4877      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4878        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4879            !!!cp ('t271');
4880          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4881          !!!next-token;          !!!next-token;
4882          redo B;          redo B;
4883        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4884              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4885                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4886                    !!!cp ('t272');
4887                  ## As if </option>                  ## As if </option>
4888                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4889                  } else {
4890                    !!!cp ('t273');
4891                }                }
4892    
4893                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4519  sub _tree_construction_main ($) { Line 4895  sub _tree_construction_main ($) {
4895                redo B;                redo B;
4896              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4897                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4898                    !!!cp ('t274');
4899                  ## As if </option>                  ## As if </option>
4900                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4901                  } else {
4902                    !!!cp ('t275');
4903                }                }
4904    
4905                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4906                    !!!cp ('t276');
4907                  ## As if </optgroup>                  ## As if </optgroup>
4908                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4909                  } else {
4910                    !!!cp ('t277');
4911                }                }
4912    
4913                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4914                !!!next-token;                !!!next-token;
4915                redo B;                redo B;
4916              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4917    ## TODO: The type below is not good - <select> is replaced by </select>
4918                !!!parse-error (type => 'not closed:select');                !!!parse-error (type => 'not closed:select');
4919                ## As if </select> instead                ## As if </select> instead
4920                ## have an element in table scope                ## have an element in table scope
# Line 4539  sub _tree_construction_main ($) { Line 4922  sub _tree_construction_main ($) {
4922                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4923                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4924                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4925                      !!!cp ('t278');
4926                    $i = $_;                    $i = $_;
4927                    last INSCOPE;                    last INSCOPE;
4928                  } elsif ({                  } elsif ({
4929                            table => 1, html => 1,                            table => 1, html => 1,
4930                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4931                      !!!cp ('t279');
4932                    last INSCOPE;                    last INSCOPE;
4933                  }                  }
4934                } # INSCOPE                } # INSCOPE
4935                unless (defined $i) {                unless (defined $i) {
4936                    !!!cp ('t280');
4937                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
4938                  ## Ignore the token                  ## Ignore the token
4939                  !!!next-token;                  !!!next-token;
4940                  redo B;                  redo B;
4941                }                }
4942                                
4943                  !!!cp ('t281');
4944                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4945    
4946                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4561  sub _tree_construction_main ($) { Line 4948  sub _tree_construction_main ($) {
4948                !!!next-token;                !!!next-token;
4949                redo B;                redo B;
4950          } else {          } else {
4951              !!!cp ('t282');
4952            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name});
4953            ## Ignore the token            ## Ignore the token
4954            !!!next-token;            !!!next-token;
# Line 4570  sub _tree_construction_main ($) { Line 4958  sub _tree_construction_main ($) {
4958              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4959                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4960                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4961                    !!!cp ('t283');
4962                  ## As if </option>                  ## As if </option>
4963                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
4964                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4965                    !!!cp ('t284');
4966                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4967                } else {                } else {
4968                    !!!cp ('t285');
4969                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4970                  ## Ignore the token                  ## Ignore the token
4971                }                }
# Line 4582  sub _tree_construction_main ($) { Line 4973  sub _tree_construction_main ($) {
4973                redo B;                redo B;
4974              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4975                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4976                    !!!cp ('t286');
4977                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4978                } else {                } else {
4979                    !!!cp ('t287');
4980                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4981                  ## Ignore the token                  ## Ignore the token
4982                }                }
# Line 4595  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4990                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4991                      !!!cp ('t288');
4992                    $i = $_;                    $i = $_;
4993                    last INSCOPE;                    last INSCOPE;
4994                  } elsif ({                  } elsif ({
4995                            table => 1, html => 1,                            table => 1, html => 1,
4996                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4997                      !!!cp ('t289');
4998                    last INSCOPE;                    last INSCOPE;
4999                  }                  }
5000                } # INSCOPE                } # INSCOPE
5001                unless (defined $i) {                unless (defined $i) {
5002                    !!!cp ('t290');
5003                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5004                  ## Ignore the token                  ## Ignore the token
5005                  !!!next-token;                  !!!next-token;
5006                  redo B;                  redo B;
5007                }                }
5008                                
5009                  !!!cp ('t291');
5010                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5011    
5012                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4620  sub _tree_construction_main ($) { Line 5017  sub _tree_construction_main ($) {
5017                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
5018                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5019                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5020    ## TODO: The following is wrong?
5021                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5022                                
5023                ## have an element in table scope                ## have an element in table scope
# Line 4627  sub _tree_construction_main ($) { Line 5025  sub _tree_construction_main ($) {
5025                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5026                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5027                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5028                      !!!cp ('t292');
5029                    $i = $_;                    $i = $_;
5030                    last INSCOPE;                    last INSCOPE;
5031                  } elsif ({                  } elsif ({
5032                            table => 1, html => 1,                            table => 1, html => 1,
5033                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5034                      !!!cp ('t293');
5035                    last INSCOPE;                    last INSCOPE;
5036                  }                  }
5037                } # INSCOPE                } # INSCOPE
5038                unless (defined $i) {                unless (defined $i) {
5039                    !!!cp ('t294');
5040                  ## Ignore the token                  ## Ignore the token
5041                  !!!next-token;                  !!!next-token;
5042                  redo B;                  redo B;
# Line 4647  sub _tree_construction_main ($) { Line 5048  sub _tree_construction_main ($) {
5048                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5049                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5050                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5051                      !!!cp ('t295');
5052                    $i = $_;                    $i = $_;
5053                    last INSCOPE;                    last INSCOPE;
5054                  } elsif ({                  } elsif ({
5055                            table => 1, html => 1,                            table => 1, html => 1,
5056                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5057    ## ISSUE: Can this state be reached?
5058                      !!!cp ('t296');
5059                    last INSCOPE;                    last INSCOPE;
5060                  }                  }
5061                } # INSCOPE                } # INSCOPE
5062                unless (defined $i) {                unless (defined $i) {
5063                    !!!cp ('t297');
5064    ## TODO: The following error type is correct?
5065                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5066                  ## Ignore the </select> token                  ## Ignore the </select> token
5067                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5068                  redo B;                  redo B;
5069                }                }
5070                                
5071                  !!!cp ('t298');
5072                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5073    
5074                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4669  sub _tree_construction_main ($) { Line 5076  sub _tree_construction_main ($) {
5076                ## reprocess                ## reprocess
5077                redo B;                redo B;
5078          } else {          } else {
5079              !!!cp ('t299');
5080            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name});
5081            ## Ignore the token            ## Ignore the token
5082            !!!next-token;            !!!next-token;
# Line 4687  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5096                        
5097            unless (length $token->{data}) {            unless (length $token->{data}) {
5098                !!!cp ('t300');
5099              !!!next-token;              !!!next-token;
5100              redo B;              redo B;
5101            }            }
5102          }          }
5103                    
5104          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5105              !!!cp ('t301');
5106            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5107    
5108            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5109            } else {
5110              !!!cp ('t302');
5111          }          }
5112                    
5113          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4706  sub _tree_construction_main ($) { Line 5118  sub _tree_construction_main ($) {
5118          redo B;          redo B;
5119        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5120          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5121              !!!cp ('t303');
5122            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5123                        
5124            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5125            } else {
5126              !!!cp ('t304');
5127          }          }
5128    
5129          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4719  sub _tree_construction_main ($) { Line 5134  sub _tree_construction_main ($) {
5134          redo B;          redo B;
5135        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5136          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5137              !!!cp ('t305');
5138            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5139                        
5140            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5141            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5142            } else {
5143              !!!cp ('t306');
5144          }          }
5145    
5146          ## "after body" insertion mode          ## "after body" insertion mode
5147          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5148            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5149                !!!cp ('t307');
5150              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html');
5151              ## Ignore the token              ## Ignore the token
5152              !!!next-token;              !!!next-token;
5153              redo B;              redo B;
5154            } else {            } else {
5155                !!!cp ('t308');
5156              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5157              !!!next-token;              !!!next-token;
5158              redo B;              redo B;
5159            }            }
5160          } else {          } else {
5161              !!!cp ('t309');
5162            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
5163    
5164            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
# Line 4753  sub _tree_construction_main ($) { Line 5174  sub _tree_construction_main ($) {
5174            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5175                        
5176            unless (length $token->{data}) {            unless (length $token->{data}) {
5177                !!!cp ('t310');
5178              !!!next-token;              !!!next-token;
5179              redo B;              redo B;
5180            }            }
# Line 4760  sub _tree_construction_main ($) { Line 5182  sub _tree_construction_main ($) {
5182                    
5183          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5184            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5185                !!!cp ('t311');
5186              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
5187            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5188                !!!cp ('t312');
5189              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5190            } else { # "after html frameset"            } else { # "after html frameset"
5191                !!!cp ('t313');
5192              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5193    
5194              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5195              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5196              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5197            }            }
5198                        
5199            ## Ignore the token.            ## Ignore the token.
5200            if (length $token->{data}) {            if (length $token->{data}) {
5201                !!!cp ('t314');
5202              ## reprocess the rest of characters              ## reprocess the rest of characters
5203            } else {            } else {
5204                !!!cp ('t315');
5205              !!!next-token;              !!!next-token;
5206            }            }
5207            redo B;            redo B;
# Line 4783  sub _tree_construction_main ($) { Line 5210  sub _tree_construction_main ($) {
5210          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5211        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5212          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5213              !!!cp ('t316');
5214            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5215    
5216            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5217            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5218          }          } else {
5219              !!!cp ('t317');
5220            }
5221    
5222          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5223              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5224              !!!cp ('t318');
5225            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5226            !!!next-token;            !!!next-token;
5227            redo B;            redo B;
5228          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5229                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5230              !!!cp ('t319');
5231            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5232            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5233            !!!next-token;            !!!next-token;
5234            redo B;            redo B;
5235          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5236              !!!cp ('t320');
5237            ## NOTE: As if in body.            ## NOTE: As if in body.
5238            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5239            redo B;            redo B;
5240          } else {          } else {
5241            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5242                !!!cp ('t321');
5243              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5244            } else {            } else {
5245                !!!cp ('t322');
5246              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5247            }            }
5248            ## Ignore the token            ## Ignore the token
# Line 4816  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251          }          }
5252        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5253          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5254              !!!cp ('t323');
5255            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5256    
5257            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5258            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5259            } else {
5260              !!!cp ('t324');
5261          }          }
5262    
5263          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5264              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5265            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5266                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5267                !!!cp ('t325');
5268              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5269              ## Ignore the token              ## Ignore the token
5270              !!!next-token;              !!!next-token;
5271            } else {            } else {
5272                !!!cp ('t326');
5273              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5274              !!!next-token;              !!!next-token;
5275            }            }
5276    
5277            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5278                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5279                !!!cp ('t327');
5280              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5281              } else {
5282                !!!cp ('t328');
5283            }            }
5284            redo B;            redo B;
5285          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5286                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5287              !!!cp ('t329');
5288            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5289            !!!next-token;            !!!next-token;
5290            redo B;            redo B;
5291          } else {          } else {
5292            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5293                !!!cp ('t330');
5294              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5295            } else {            } else {
5296                !!!cp ('t331');
5297              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5298            }            }
5299            ## Ignore the token            ## Ignore the token
# Line 4866  sub _tree_construction_main ($) { Line 5312  sub _tree_construction_main ($) {
5312      ## "in body" insertion mode      ## "in body" insertion mode
5313      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5314        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5315            !!!cp ('t332');
5316          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5317          $script_start_tag->($insert);          $script_start_tag->($insert);
5318          redo B;          redo B;
5319        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5320            !!!cp ('t333');
5321          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5322          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5323          redo B;          redo B;
5324        } elsif ({        } elsif ({
5325                  base => 1, link => 1,                  base => 1, link => 1,
5326                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5327            !!!cp ('t334');
5328          ## 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
5329          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5330          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 4888  sub _tree_construction_main ($) { Line 5337  sub _tree_construction_main ($) {
5337    
5338          unless ($self->{confident}) {          unless ($self->{confident}) {
5339            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5340                !!!cp ('t335');
5341              $self->{change_encoding}              $self->{change_encoding}
5342                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value});
5343                            
# Line 4902  sub _tree_construction_main ($) { Line 5352  sub _tree_construction_main ($) {
5352                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
5353                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5354                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5355                  !!!cp ('t336');
5356                $self->{change_encoding}                $self->{change_encoding}
5357                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5358                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 4912  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363            }            }
5364          } else {          } else {
5365            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
5366                !!!cp ('t337');
5367              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5368                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5369                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
5370                                           ->{has_reference});                                           ->{has_reference});
5371            }            }
5372            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
5373                !!!cp ('t338');
5374              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5375                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5376                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4928  sub _tree_construction_main ($) { Line 5381  sub _tree_construction_main ($) {
5381          !!!next-token;          !!!next-token;
5382          redo B;          redo B;
5383        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5384          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5385          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5386          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5387          redo B;          redo B;
5388        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5389          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
5390                                
5391          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5392              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5393              !!!cp ('t342');
5394            ## Ignore the token            ## Ignore the token
5395          } else {          } else {
5396            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5397            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5398              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5399                  !!!cp ('t343');
5400                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5401                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5402                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4958  sub _tree_construction_main ($) { Line 5407  sub _tree_construction_main ($) {
5407          redo B;          redo B;
5408        } elsif ({        } elsif ({
5409                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5410                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5411                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5412                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5413                  pre => 1,                  pre => 1, listing => 1,
5414                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5415          ## has a p element in scope          ## has a p element in scope
5416          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5417            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5418                !!!cp ('t344');
5419              !!!back-token;              !!!back-token;
5420              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5421              redo B;              redo B;
# Line 4972  sub _tree_construction_main ($) { Line 5423  sub _tree_construction_main ($) {
5423                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5424                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5425                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5426                !!!cp ('t345');
5427              last INSCOPE;              last INSCOPE;
5428            }            }
5429          } # INSCOPE          } # INSCOPE
5430                        
5431          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5432          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5433            !!!next-token;            !!!next-token;
5434            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5435              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5436              unless (length $token->{data}) {              unless (length $token->{data}) {
5437                  !!!cp ('t346');
5438                !!!next-token;                !!!next-token;
5439                } else {
5440                  !!!cp ('t349');
5441              }              }
5442              } else {
5443                !!!cp ('t348');
5444            }            }
5445          } else {          } else {
5446              !!!cp ('t347');
5447            !!!next-token;            !!!next-token;
5448          }          }
5449          redo B;          redo B;
5450        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5451          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5452              !!!cp ('t350');
5453            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5454            ## Ignore the token            ## Ignore the token
5455            !!!next-token;            !!!next-token;
# Line 4999  sub _tree_construction_main ($) { Line 5458  sub _tree_construction_main ($) {
5458            ## has a p element in scope            ## has a p element in scope
5459            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5460              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5461                  !!!cp ('t351');
5462                !!!back-token;                !!!back-token;
5463                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5464                redo B;                redo B;
# Line 5006  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5467                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5468                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5469                  !!!cp ('t352');
5470                last INSCOPE;                last INSCOPE;
5471              }              }
5472            } # INSCOPE            } # INSCOPE
# Line 5019  sub _tree_construction_main ($) { Line 5480  sub _tree_construction_main ($) {
5480          ## has a p element in scope          ## has a p element in scope
5481          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5482            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5483                !!!cp ('t353');
5484              !!!back-token;              !!!back-token;
5485              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5486              redo B;              redo B;
# Line 5026  sub _tree_construction_main ($) { Line 5488  sub _tree_construction_main ($) {
5488                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5489                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5490                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5491                !!!cp ('t354');
5492              last INSCOPE;              last INSCOPE;
5493            }            }
5494          } # INSCOPE          } # INSCOPE
# Line 5037  sub _tree_construction_main ($) { Line 5500  sub _tree_construction_main ($) {
5500            ## Step 2            ## Step 2
5501            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5502              if ($i != -1) {              if ($i != -1) {
5503                  !!!cp ('t355');
5504                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5505                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5506                } else {
5507                  !!!cp ('t356');
5508              }              }
5509              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5510              last LI;              last LI;
5511              } else {
5512                !!!cp ('t357');
5513            }            }
5514                        
5515            ## Step 3            ## Step 3
# Line 5050  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5519                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5520                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5521                !!!cp ('t358');
5522              last LI;              last LI;
5523            }            }
5524                        
5525              !!!cp ('t359');
5526            ## Step 4            ## Step 4
5527            $i--;            $i--;
5528            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5066  sub _tree_construction_main ($) { Line 5536  sub _tree_construction_main ($) {
5536          ## has a p element in scope          ## has a p element in scope
5537          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5538            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5539                !!!cp ('t360');
5540              !!!back-token;              !!!back-token;
5541              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5542              redo B;              redo B;
# Line 5073  sub _tree_construction_main ($) { Line 5544  sub _tree_construction_main ($) {
5544                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5545                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5546                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5547                !!!cp ('t361');
5548              last INSCOPE;              last INSCOPE;
5549            }            }
5550          } # INSCOPE          } # INSCOPE
# Line 5084  sub _tree_construction_main ($) { Line 5556  sub _tree_construction_main ($) {
5556            ## Step 2            ## Step 2
5557            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5558              if ($i != -1) {              if ($i != -1) {
5559                  !!!cp ('t362');
5560                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5561                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5562                } else {
5563                  !!!cp ('t363');
5564              }              }
5565              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5566              last LI;              last LI;
5567              } else {
5568                !!!cp ('t364');
5569            }            }
5570                        
5571            ## Step 3            ## Step 3
# Line 5097  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5575                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5576                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5577                !!!cp ('t365');
5578              last LI;              last LI;
5579            }            }
5580                        
5581              !!!cp ('t366');
5582            ## Step 4            ## Step 4
5583            $i--;            $i--;
5584            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5113  sub _tree_construction_main ($) { Line 5592  sub _tree_construction_main ($) {
5592          ## has a p element in scope          ## has a p element in scope
5593          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5594            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5595                !!!cp ('t367');
5596              !!!back-token;              !!!back-token;
5597              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5598              redo B;              redo B;
# Line 5120  sub _tree_construction_main ($) { Line 5600  sub _tree_construction_main ($) {
5600                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5601                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5602                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5603                !!!cp ('t368');
5604              last INSCOPE;              last INSCOPE;
5605            }            }
5606          } # INSCOPE          } # INSCOPE
# Line 5130  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                        
5612          !!!next-token;          !!!next-token;
5613          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5614        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5615          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5616            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5617            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5618                !!!cp ('t371');
5619              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5620                            
5621              !!!back-token;              !!!back-token;
# Line 5187  sub _tree_construction_main ($) { Line 5624  sub _tree_construction_main ($) {
5624                            
5625              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5626                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5627                    !!!cp ('t372');
5628                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5629                  last AFE2;                  last AFE2;
5630                }                }
5631              } # AFE2              } # AFE2
5632              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5633                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5634                    !!!cp ('t373');
5635                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5636                  last OE;                  last OE;
5637                }                }
5638              } # OE              } # OE
5639              last AFE;              last AFE;
5640            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5641                !!!cp ('t374');
5642              last AFE;              last AFE;
5643            }            }
5644          } # AFE          } # AFE
# Line 5215  sub _tree_construction_main ($) { Line 5655  sub _tree_construction_main ($) {
5655                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5656                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5657                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5658            !!!cp ('t375');
5659          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5660                    
5661          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5229  sub _tree_construction_main ($) { Line 5670  sub _tree_construction_main ($) {
5670          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5671            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5672            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5673                !!!cp ('t376');
5674              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr');
5675              !!!back-token;              !!!back-token;
5676              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
# Line 5237  sub _tree_construction_main ($) { Line 5679  sub _tree_construction_main ($) {
5679                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5680                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5681                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5682                !!!cp ('t377');
5683              last INSCOPE;              last INSCOPE;
5684            }            }
5685          } # INSCOPE          } # INSCOPE
# Line 5251  sub _tree_construction_main ($) { Line 5694  sub _tree_construction_main ($) {
5694          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5695            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5696            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5697                !!!cp ('t378');
5698              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5699              !!!back-token;              !!!back-token;
5700              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
# Line 5259  sub _tree_construction_main ($) { Line 5703  sub _tree_construction_main ($) {
5703                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5704                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5705                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5706                !!!cp ('t379');
5707              last INSCOPE;              last INSCOPE;
5708            }            }
5709          } # INSCOPE          } # INSCOPE
# Line 5266  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5712                        
5713          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5714    
5715            ## TODO: associate with $self->{form_element} if defined
5716    
5717          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5718    
5719          !!!next-token;          !!!next-token;
5720          redo B;          redo B;
5721        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5722                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5723            !!!cp ('t380');
5724          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5725                    
5726          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5280  sub _tree_construction_main ($) { Line 5729  sub _tree_construction_main ($) {
5729          !!!next-token;          !!!next-token;
5730          redo B;          redo B;
5731        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5732            !!!cp ('t381');
5733          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5734          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5735          redo B;          redo B;
5736        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5737          ## has a p element in scope          ## has a p element in scope
5738          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5739            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5740                !!!cp ('t382');
5741              !!!back-token;              !!!back-token;
5742              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5743              redo B;              redo B;
# Line 5294  sub _tree_construction_main ($) { Line 5745  sub _tree_construction_main ($) {
5745                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5746                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5747                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5748                !!!cp ('t383');
5749              last INSCOPE;              last INSCOPE;
5750            }            }
5751          } # INSCOPE          } # INSCOPE
5752                        
5753          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5754                      push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5755    
5756          $self->{insertion_mode} = IN_TABLE_IM;          $self->{insertion_mode} = IN_TABLE_IM;
5757                        
5758          !!!next-token;          !!!next-token;
# Line 5310  sub _tree_construction_main ($) { Line 5763  sub _tree_construction_main ($) {
5763                  image => 1,                  image => 1,
5764                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5765          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5766              !!!cp ('t384');
5767            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5768            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5769            } else {
5770              !!!cp ('t385');
5771          }          }
5772    
5773          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
# Line 5326  sub _tree_construction_main ($) { Line 5782  sub _tree_construction_main ($) {
5782          ## has a p element in scope          ## has a p element in scope
5783          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5784            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5785                !!!cp ('t386');
5786              !!!back-token;              !!!back-token;
5787              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5788              redo B;              redo B;
# Line 5333  sub _tree_construction_main ($) { Line 5790  sub _tree_construction_main ($) {
5790                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5791                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5792                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5793                !!!cp ('t387');
5794              last INSCOPE;              last INSCOPE;
5795            }            }
5796          } # INSCOPE          } # INSCOPE
# Line 5343  sub _tree_construction_main ($) { Line 5801  sub _tree_construction_main ($) {
5801          !!!next-token;          !!!next-token;
5802          redo B;          redo B;
5803        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5804            !!!cp ('t388');
5805          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5806                    
5807          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5355  sub _tree_construction_main ($) { Line 5814  sub _tree_construction_main ($) {
5814          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5815                    
5816          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5817              !!!cp ('t389');
5818            ## Ignore the token            ## Ignore the token
5819            !!!next-token;            !!!next-token;
5820            redo B;            redo B;
# Line 5374  sub _tree_construction_main ($) { Line 5834  sub _tree_construction_main ($) {
5834                          {type => START_TAG_TOKEN, tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5835                         );                         );
5836            if ($prompt_attr) {            if ($prompt_attr) {
5837                !!!cp ('t390');
5838              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5839            } else {            } else {
5840                !!!cp ('t391');
5841              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5842                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5843              ## TODO: make this configurable              ## TODO: make this configurable
# Line 5407  sub _tree_construction_main ($) { Line 5869  sub _tree_construction_main ($) {
5869          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5870            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5871            unless (length $token->{data}) {            unless (length $token->{data}) {
5872                !!!cp ('t392');
5873              !!!next-token;              !!!next-token;
5874              } else {
5875                !!!cp ('t393');
5876            }            }
5877            } else {
5878              !!!cp ('t394');
5879          }          }
5880          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5881              !!!cp ('t395');
5882            $text .= $token->{data};            $text .= $token->{data};
5883            !!!next-token;            !!!next-token;
5884          }          }
5885          if (length $text) {          if (length $text) {
5886              !!!cp ('t396');
5887            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5888          }          }
5889                    
# Line 5422  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891                    
5892          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
5893              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5894              !!!cp ('t397');
5895            ## Ignore the token            ## Ignore the token
5896          } else {          } else {
5897              !!!cp ('t398');
5898            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5899          }          }
5900          !!!next-token;          !!!next-token;
# Line 5434  sub _tree_construction_main ($) { Line 5905  sub _tree_construction_main ($) {
5905                  noframes => 1,                  noframes => 1,
5906                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5907                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5908            !!!cp ('t399');
5909          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
5910          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5911          redo B;          redo B;
5912        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5913            !!!cp ('t400');
5914          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5915                    
5916          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5917    
5918            ## TODO: associate with $self->{form_element} if defined
5919                    
5920          $self->{insertion_mode} = IN_SELECT_IM;          $self->{insertion_mode} = IN_SELECT_IM;
5921          !!!next-token;          !!!next-token;
# Line 5451  sub _tree_construction_main ($) { Line 5926  sub _tree_construction_main ($) {
5926                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5927                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5928                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5929            !!!cp ('t401');
5930          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5931          ## Ignore the token          ## Ignore the token
5932          !!!next-token;          !!!next-token;
# Line 5458  sub _tree_construction_main ($) { Line 5934  sub _tree_construction_main ($) {
5934                    
5935          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5936        } else {        } else {
5937            !!!cp ('t402');
5938          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5939                    
5940          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5475  sub _tree_construction_main ($) { Line 5952  sub _tree_construction_main ($) {
5952                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5953                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
5954                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5955                  !!!cp ('t403');
5956                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5957                } else {
5958                  !!!cp ('t404');
5959              }              }
5960            }            }
5961    
# Line 5483  sub _tree_construction_main ($) { Line 5963  sub _tree_construction_main ($) {
5963            !!!next-token;            !!!next-token;
5964            redo B;            redo B;
5965          } else {          } else {
5966              !!!cp ('t405');
5967            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5968            ## Ignore the token            ## Ignore the token
5969            !!!next-token;            !!!next-token;
# Line 5492  sub _tree_construction_main ($) { Line 5973  sub _tree_construction_main ($) {
5973          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5974            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
5975            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5976                !!!cp ('t406');
5977              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5978              } else {
5979                !!!cp ('t407');
5980            }            }
5981            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5982            ## reprocess            ## reprocess
5983            redo B;            redo B;
5984          } else {          } else {
5985              !!!cp ('t408');
5986            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5987            ## Ignore the token            ## Ignore the token
5988            !!!next-token;            !!!next-token;
# Line 5507  sub _tree_construction_main ($) { Line 5992  sub _tree_construction_main ($) {
5992                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5993                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
5994                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
5995                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
5996                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
5997                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5516  sub _tree_construction_main ($) { Line 6000  sub _tree_construction_main ($) {
6000          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6001            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6002            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6003              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6004              $i = $_;              $i = $_;
6005              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6006            } elsif ({            } elsif ({
6007                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6008                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6009                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6010                !!!cp ('t411');
6011              last INSCOPE;              last INSCOPE;
6012            }            }
6013          } # INSCOPE          } # INSCOPE
6014            
6015          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6016            if (defined $i) {            !!!cp ('t413');
6017              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6018            } else {
6019              ## Step 1. generate implied end tags
6020              while ({
6021                      dd => ($token->{tag_name} ne 'dd'),
6022                      dt => ($token->{tag_name} ne 'dt'),
6023                      li => ($token->{tag_name} ne 'li'),
6024                      p => 1,
6025                     }->{$self->{open_elements}->[-1]->[1]}) {
6026                !!!cp ('t409');
6027                pop @{$self->{open_elements}};
6028              }
6029    
6030              ## Step 2.
6031              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6032                !!!cp ('t412');
6033              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6034            } else {            } else {
6035              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6036            }            }
6037          }  
6038                      ## Step 3.
         if (defined $i) {  
6039            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6040          } elsif ($token->{tag_name} eq 'p') {  
6041            ## As if <p>, then reprocess the current token            ## Step 4.
6042            my $el;            $clear_up_to_marker->()
6043            !!!create-element ($el, 'p');                if {
6044            $insert->($el);                  button => 1, marquee => 1, object => 1,
6045                  }->{$token->{tag_name}};
6046          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6047          !!!next-token;          !!!next-token;
6048          redo B;          redo B;
6049        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6050            undef $self->{form_element};
6051    
6052          ## has an element in scope          ## has an element in scope
6053            my $i;
6054          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6055            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6056            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6057              ## generate implied end tags              !!!cp ('t418');
6058              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6059              last INSCOPE;              last INSCOPE;
6060            } elsif ({            } elsif ({
6061                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6062                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6063                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6064                !!!cp ('t419');
6065              last INSCOPE;              last INSCOPE;
6066            }            }
6067          } # INSCOPE          } # INSCOPE
6068            
6069          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6070            pop @{$self->{open_elements}};            !!!cp ('t421');
         } else {  
6071            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6072            } else {
6073              ## Step 1. generate implied end tags
6074              while ({
6075                      dd => 1, dt => 1, li => 1, p => 1,
6076                     }->{$self->{open_elements}->[-1]->[1]}) {
6077                !!!cp ('t417');
6078                pop @{$self->{open_elements}};
6079              }
6080              
6081              ## Step 2.
6082              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6083                !!!cp ('t417.1');
6084                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6085              } else {
6086                !!!cp ('t420');
6087              }  
6088              
6089              ## Step 3.
6090              splice @{$self->{open_elements}}, $i;
6091          }          }
6092    
         undef $self->{form_element};  
6093          !!!next-token;          !!!next-token;
6094          redo B;          redo B;
6095        } elsif ({        } elsif ({
# Line 5606  sub _tree_construction_main ($) { Line 6102  sub _tree_construction_main ($) {
6102            if ({            if ({
6103                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6104                }->{$node->[1]}) {                }->{$node->[1]}) {
6105              ## generate implied end tags              !!!cp ('t423');
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6106              $i = $_;              $i = $_;
6107              last INSCOPE;              last INSCOPE;
6108            } elsif ({            } elsif ({
6109                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6110                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6111                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6112                !!!cp ('t424');
6113              last INSCOPE;              last INSCOPE;
6114            }            }
6115          } # INSCOPE          } # INSCOPE
6116            
6117          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6118              !!!cp ('t425.1');
6119            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6120            } else {
6121              ## Step 1. generate implied end tags
6122              while ({
6123                      dd => 1, dt => 1, li => 1, p => 1,
6124                     }->{$self->{open_elements}->[-1]->[1]}) {
6125                !!!cp ('t422');
6126                pop @{$self->{open_elements}};
6127              }
6128              
6129              ## Step 2.
6130              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6131                !!!cp ('t425');
6132                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6133              } else {
6134                !!!cp ('t426');
6135              }
6136    
6137              ## Step 3.
6138              splice @{$self->{open_elements}}, $i;
6139          }          }
6140                    
6141          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6142            redo B;
6143          } elsif ($token->{tag_name} eq 'p') {
6144            ## has an element in scope
6145            my $i;
6146            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6147              my $node = $self->{open_elements}->[$_];
6148              if ($node->[1] eq $token->{tag_name}) {
6149                !!!cp ('t410.1');
6150                $i = $_;
6151                last INSCOPE;
6152              } elsif ({
6153                        table => 1, caption => 1, td => 1, th => 1,
6154                        button => 1, marquee => 1, object => 1, html => 1,
6155                       }->{$node->[1]}) {
6156                !!!cp ('t411.1');
6157                last INSCOPE;
6158              }
6159            } # INSCOPE
6160    
6161            if (defined $i) {
6162              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6163                !!!cp ('t412.1');
6164                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6165              } else {
6166                !!!cp ('t414.1');
6167              }
6168    
6169              splice @{$self->{open_elements}}, $i;
6170            } else {
6171              !!!cp ('t413.1');
6172              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6173    
6174              !!!cp ('t415.1');
6175              ## As if <p>, then reprocess the current token
6176              my $el;
6177              !!!create-element ($el, 'p');
6178              $insert->($el);
6179              ## NOTE: Not inserted into |$self->{open_elements}|.
6180            }
6181    
6182          !!!next-token;          !!!next-token;
6183          redo B;          redo B;
6184        } elsif ({        } elsif ({
# Line 5640  sub _tree_construction_main ($) { Line 6187  sub _tree_construction_main ($) {
6187                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6188                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6189                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6190            !!!cp ('t427');
6191          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6192          redo B;          redo B;
6193        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6194            !!!cp ('t428');
6195          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6196    
6197          ## As if <br>          ## As if <br>
# Line 5667  sub _tree_construction_main ($) { Line 6216  sub _tree_construction_main ($) {
6216                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6217                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6218                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6219            !!!cp ('t429');
6220          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6221          ## Ignore the token          ## Ignore the token
6222          !!!next-token;          !!!next-token;
# Line 5684  sub _tree_construction_main ($) { Line 6234  sub _tree_construction_main ($) {
6234            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6235              ## Step 1              ## Step 1
6236              ## generate implied end tags              ## generate implied end tags
6237              if ({              while ({
6238                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6239                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6240                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6241                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6242                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6243              }              }
6244                    
6245              ## Step 2              ## Step 2
6246              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6247                  !!!cp ('t431');
6248                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6249                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6250                } else {
6251                  !!!cp ('t432');
6252              }              }
6253                            
6254              ## Step 3              ## Step 3
# Line 5712  sub _tree_construction_main ($) { Line 6262  sub _tree_construction_main ($) {
6262                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6263                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6264                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6265                  !!!cp ('t433');
6266                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6267                ## Ignore the token                ## Ignore the token
6268                !!!next-token;                !!!next-token;
6269                last S2;                last S2;
6270              }              }
6271    
6272                !!!cp ('t434');
6273            }            }
6274                        
6275            ## Step 4            ## Step 4
# Line 5732  sub _tree_construction_main ($) { Line 6285  sub _tree_construction_main ($) {
6285      redo B;      redo B;
6286    } # B    } # B
6287    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6288    ## Stop parsing # MUST    ## Stop parsing # MUST
6289        
6290    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5778  sub set_inner_html ($$$) { Line 6326  sub set_inner_html ($$$) {
6326      my $p = $class->new;      my $p = $class->new;
6327      $p->{document} = $doc;      $p->{document} = $doc;
6328    
6329      ## Step 9 # MUST      ## Step 8 # MUST
6330      my $i = 0;      my $i = 0;
6331      my $line = 1;      my $line = 1;
6332      my $column = 0;      my $column = 0;
# Line 5795  sub set_inner_html ($$$) { Line 6343  sub set_inner_html ($$$) {
6343        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6344          $line++;          $line++;
6345          $column = 0;          $column = 0;
6346            !!!cp ('i1');
6347        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6348          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6349          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6350          $line++;          $line++;
6351          $column = 0;          $column = 0;
6352            !!!cp ('i2');
6353        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6354          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6355            !!!cp ('i3');
6356        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
6357            !!!cp ('i4');
6358          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6359          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6360        }        }
# Line 5841  sub set_inner_html ($$$) { Line 6393  sub set_inner_html ($$$) {
6393    
6394      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6395    
6396      ## Step 4      ## Step 3
6397      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6398        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6399    
6400      ## Step 5 # MUST      ## Step 4 # MUST
6401      $doc->append_child ($root);      $doc->append_child ($root);
6402    
6403      ## Step 6 # MUST      ## Step 5 # MUST
6404      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6405    
6406      undef $p->{head_element};      undef $p->{head_element};
6407    
6408      ## Step 7 # MUST      ## Step 6 # MUST
6409      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6410    
6411      ## Step 8 # MUST      ## Step 7 # MUST
6412      my $anode = $node;      my $anode = $node;
6413      AN: while (defined $anode) {      AN: while (defined $anode) {
6414        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6415          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6416          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6417            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
6418                !!!cp ('i5');
6419              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6420              last AN;              last AN;
6421            }            }
# Line 5871  sub set_inner_html ($$$) { Line 6424  sub set_inner_html ($$$) {
6424        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6425      } # AN      } # AN
6426            
6427      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6428      {      {
6429        my $self = $p;        my $self = $p;
6430        !!!next-token;        !!!next-token;
6431      }      }
6432      $p->_tree_construction_main;      $p->_tree_construction_main;
6433    
6434      ## Step 11 # MUST      ## Step 10 # MUST
6435      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6436      for (@cn) {      for (@cn) {
6437        $node->remove_child ($_);        $node->remove_child ($_);
6438      }      }
6439      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6440    
6441      ## Step 12 # MUST      ## Step 11 # MUST
6442      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6443      for (@cn) {      for (@cn) {
6444        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

Legend:
Removed from v.1.77  
changed lines
  Added in v.1.98

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24