/[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.88 by wakaba, Sat Mar 8 02:35:05 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 2648  sub _reset_insertion_mode ($) { Line 2753  sub _reset_insertion_mode ($) {
2753          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2754            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2755                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2756                !!!cp ('t27');
2757              #              #
2758            } else {            } else {
2759                !!!cp ('t28');
2760              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2761            }            }
2762          }          }
# Line 2658  sub _reset_insertion_mode ($) { Line 2765  sub _reset_insertion_mode ($) {
2765        ## Step 4..13        ## Step 4..13
2766        my $new_mode = {        my $new_mode = {
2767                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2768                          ## NOTE: |option| and |optgroup| do not set
2769                          ## insertion mode to "in select" by themselves.
2770                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2771                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2772                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2676  sub _reset_insertion_mode ($) { Line 2785  sub _reset_insertion_mode ($) {
2785        ## Step 14        ## Step 14
2786        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2787          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2788              !!!cp ('t29');
2789            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2790          } else {          } else {
2791              ## ISSUE: Can this state be reached?
2792              !!!cp ('t30');
2793            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2794          }          }
2795          return;          return;
2796          } else {
2797            !!!cp ('t31');
2798        }        }
2799                
2800        ## Step 15        ## Step 15
# Line 2693  sub _reset_insertion_mode ($) { Line 2807  sub _reset_insertion_mode ($) {
2807        ## Step 17        ## Step 17
2808        redo S3;        redo S3;
2809      } # S3      } # S3
2810    
2811      die "$0: _reset_insertion_mode: This line should never be reached";
2812  } # _reset_insertion_mode  } # _reset_insertion_mode
2813    
2814  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2714  sub _tree_construction_main ($) { Line 2830  sub _tree_construction_main ($) {
2830      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2831      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2832        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2833            !!!cp ('t32');
2834          return;          return;
2835        }        }
2836      }      }
# Line 2728  sub _tree_construction_main ($) { Line 2845  sub _tree_construction_main ($) {
2845    
2846        ## Step 6        ## Step 6
2847        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2848            !!!cp ('t33_1');
2849          #          #
2850        } else {        } else {
2851          my $in_open_elements;          my $in_open_elements;
2852          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2853            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2854                !!!cp ('t33');
2855              $in_open_elements = 1;              $in_open_elements = 1;
2856              last OE;              last OE;
2857            }            }
2858          }          }
2859          if ($in_open_elements) {          if ($in_open_elements) {
2860              !!!cp ('t34');
2861            #            #
2862          } else {          } else {
2863              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2864              !!!cp ('t35');
2865            redo S4;            redo S4;
2866          }          }
2867        }        }
# Line 2762  sub _tree_construction_main ($) { Line 2884  sub _tree_construction_main ($) {
2884    
2885        ## Step 11        ## Step 11
2886        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2887            !!!cp ('t36');
2888          ## Step 7'          ## Step 7'
2889          $i++;          $i++;
2890          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2891                    
2892          redo S7;          redo S7;
2893        }        }
2894    
2895          !!!cp ('t37');
2896      } # S7      } # S7
2897    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2898    
2899    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2900      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2901        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2902            !!!cp ('t38');
2903          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2904          return;          return;
2905        }        }
2906      }      }
2907    
2908        !!!cp ('t39');
2909    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2910    
2911    my $parse_rcdata = sub ($$) {    my $parse_rcdata = sub ($$) {
# Line 2799  sub _tree_construction_main ($) { Line 2927  sub _tree_construction_main ($) {
2927      my $text = '';      my $text = '';
2928      !!!next-token;      !!!next-token;
2929      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2930          !!!cp ('t40');
2931        $text .= $token->{data};        $text .= $token->{data};
2932        !!!next-token;        !!!next-token;
2933      }      }
2934    
2935      ## Step 5      ## Step 5
2936      if (length $text) {      if (length $text) {
2937          !!!cp ('t41');
2938        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2939        $el->append_child ($text);        $el->append_child ($text);
2940      }      }
# Line 2813  sub _tree_construction_main ($) { Line 2943  sub _tree_construction_main ($) {
2943      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2944    
2945      ## Step 7      ## Step 7
2946      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2947            $token->{tag_name} eq $start_tag_name) {
2948          !!!cp ('t42');
2949        ## Ignore the token        ## Ignore the token
2950      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2951          !!!cp ('t43');
2952        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2953      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2954          !!!cp ('t44');
2955        !!!parse-error (type => 'in RCDATA:#'.$token->{type});        !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2956      } else {      } else {
2957        die "$0: $content_model_flag in parse_rcdata";        die "$0: $content_model_flag in parse_rcdata";
# Line 2837  sub _tree_construction_main ($) { Line 2971  sub _tree_construction_main ($) {
2971      my $text = '';      my $text = '';
2972      !!!next-token;      !!!next-token;
2973      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
2974          !!!cp ('t45');
2975        $text .= $token->{data};        $text .= $token->{data};
2976        !!!next-token;        !!!next-token;
2977      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2978      if (length $text) {      if (length $text) {
2979          !!!cp ('t46');
2980        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2981      }      }
2982                                
# Line 2848  sub _tree_construction_main ($) { Line 2984  sub _tree_construction_main ($) {
2984    
2985      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
2986          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2987          !!!cp ('t47');
2988        ## Ignore the token        ## Ignore the token
2989      } else {      } else {
2990          !!!cp ('t48');
2991        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2992        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2993        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2994      }      }
2995            
2996      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2997          !!!cp ('t49');
2998        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2999      } else {      } else {
3000          !!!cp ('t50');
3001        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3002        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3003    
# Line 2880  sub _tree_construction_main ($) { Line 3020  sub _tree_construction_main ($) {
3020        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3021        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3022          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3023              !!!cp ('t51');
3024            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3025            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3026            last AFE;            last AFE;
3027          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3028              !!!cp ('t52');
3029            last AFE;            last AFE;
3030          }          }
3031        } # AFE        } # AFE
3032        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3033            !!!cp ('t53');
3034          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3035          ## Ignore the token          ## Ignore the token
3036          !!!next-token;          !!!next-token;
# Line 2900  sub _tree_construction_main ($) { Line 3043  sub _tree_construction_main ($) {
3043          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3044          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3045            if ($in_scope) {            if ($in_scope) {
3046                !!!cp ('t54');
3047              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3048              last INSCOPE;              last INSCOPE;
3049            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3050                !!!cp ('t55');
3051              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3052              ## Ignore the token              ## Ignore the token
3053              !!!next-token;              !!!next-token;
# Line 2912  sub _tree_construction_main ($) { Line 3057  sub _tree_construction_main ($) {
3057                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3058                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3059                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3060              !!!cp ('t56');
3061            $in_scope = 0;            $in_scope = 0;
3062          }          }
3063        } # INSCOPE        } # INSCOPE
3064        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3065            !!!cp ('t57');
3066          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3067          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3068          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3069          return;          return;
3070        }        }
3071        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3072            !!!cp ('t58');
3073          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3074        }        }
3075                
# Line 2934  sub _tree_construction_main ($) { Line 3082  sub _tree_construction_main ($) {
3082              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3083              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3084               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3085              !!!cp ('t59');
3086            $furthest_block = $node;            $furthest_block = $node;
3087            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3088          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3089              !!!cp ('t60');
3090            last OE;            last OE;
3091          }          }
3092        } # OE        } # OE
3093                
3094        ## Step 3        ## Step 3
3095        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3096            !!!cp ('t61');
3097          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3098          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3099          !!!next-token;          !!!next-token;
# Line 2955  sub _tree_construction_main ($) { Line 3106  sub _tree_construction_main ($) {
3106        ## Step 5        ## Step 5
3107        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3108        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3109            !!!cp ('t62');
3110          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3111        }        }
3112                
# Line 2977  sub _tree_construction_main ($) { Line 3129  sub _tree_construction_main ($) {
3129          S7S2: {          S7S2: {
3130            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3131              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3132                  !!!cp ('t63');
3133                $node_i_in_active = $_;                $node_i_in_active = $_;
3134                last S7S2;                last S7S2;
3135              }              }
# Line 2990  sub _tree_construction_main ($) { Line 3143  sub _tree_construction_main ($) {
3143                    
3144          ## Step 4          ## Step 4
3145          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3146              !!!cp ('t64');
3147            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3148          }          }
3149                    
3150          ## Step 5          ## Step 5
3151          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3152              !!!cp ('t65');
3153            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3154            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3155            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3029  sub _tree_construction_main ($) { Line 3184  sub _tree_construction_main ($) {
3184        my $i;        my $i;
3185        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3186          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3187              !!!cp ('t66');
3188            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3189            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3190          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3191              !!!cp ('t67');
3192            $i = $_;            $i = $_;
3193          }          }
3194        } # AFE        } # AFE
# Line 3041  sub _tree_construction_main ($) { Line 3198  sub _tree_construction_main ($) {
3198        undef $i;        undef $i;
3199        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3200          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3201              !!!cp ('t68');
3202            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3203            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3204          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3205              !!!cp ('t69');
3206            $i = $_;            $i = $_;
3207          }          }
3208        } # OE        } # OE
# Line 3071  sub _tree_construction_main ($) { Line 3230  sub _tree_construction_main ($) {
3230                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3231                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3232                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3233                                   !!!cp ('t70');
3234                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3235                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3236                               } else {                               } else {
3237                                   !!!cp ('t71');
3238                                 $foster_parent_element                                 $foster_parent_element
3239                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3240                               }                               }
# Line 3085  sub _tree_construction_main ($) { Line 3246  sub _tree_construction_main ($) {
3246                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3247                             ($child, $next_sibling);                             ($child, $next_sibling);
3248                         } else {                         } else {
3249                             !!!cp ('t72');
3250                           $self->{open_elements}->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
3251                         }                         }
3252    }; # $insert_to_foster    }; # $insert_to_foster
# Line 3093  sub _tree_construction_main ($) { Line 3255  sub _tree_construction_main ($) {
3255    
3256    B: {    B: {
3257      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3258          !!!cp ('t73');
3259        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3260        ## Ignore the token        ## Ignore the token
3261        ## Stay in the phase        ## Stay in the phase
# Line 3100  sub _tree_construction_main ($) { Line 3263  sub _tree_construction_main ($) {
3263        redo B;        redo B;
3264      } elsif ($token->{type} == END_OF_FILE_TOKEN) {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3265        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3266            !!!cp ('t74');
3267          #          #
3268        } else {        } else {
3269          ## Generate implied end tags          ## Generate implied end tags
3270          if ({          while ({
3271               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,                  dd => 1, dt => 1, li => 1, p => 1,
3272               tbody => 1, tfoot=> 1, thead => 1,                 }->{$self->{open_elements}->[-1]->[1]}) {
3273              }->{$self->{open_elements}->[-1]->[1]}) {            !!!cp ('t75');
3274            !!!back-token;            pop @{$self->{open_elements}};
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
3275          }          }
3276                    
3277          if (@{$self->{open_elements}} > 2 or          if (@{$self->{open_elements}} > 2 or
3278              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3279              !!!cp ('t76');
3280            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3281          } elsif (defined $self->{inner_html_node} and          } elsif (defined $self->{inner_html_node} and
3282                   @{$self->{open_elements}} > 1 and                   @{$self->{open_elements}} > 1 and
3283                   $self->{open_elements}->[1]->[1] ne 'body') {                   $self->{open_elements}->[1]->[1] ne 'body') {
3284    ## ISSUE: This case is never reached.
3285              !!!cp ('t77');
3286            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3287            } else {
3288              !!!cp ('t78');
3289          }          }
3290    
3291          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
# Line 3129  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3297               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3298        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3299          ## Turn into the main phase          !!!cp ('t79');
3300          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3301          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3302        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3303          ## Turn into the main phase          !!!cp ('t80');
3304          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3305          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3306          } else {
3307            !!!cp ('t81');
3308        }        }
3309    
3310  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3311  ## 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');  
       }  
3312        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3313        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3314          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3315              !!!cp ('t84');
3316            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3317              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3318               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3156  sub _tree_construction_main ($) { Line 3323  sub _tree_construction_main ($) {
3323      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3324        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3325        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3326            !!!cp ('t85');
3327          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3328        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3329            !!!cp ('t86');
3330          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3331        } else {        } else {
3332            !!!cp ('t87');
3333          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3334        }        }
3335        !!!next-token;        !!!next-token;
# Line 3169  sub _tree_construction_main ($) { Line 3339  sub _tree_construction_main ($) {
3339          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3340            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3341            unless (length $token->{data}) {            unless (length $token->{data}) {
3342                !!!cp ('t88');
3343              !!!next-token;              !!!next-token;
3344              redo B;              redo B;
3345            }            }
3346          }          }
3347    
3348          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3349              !!!cp ('t89');
3350            ## As if <head>            ## As if <head>
3351            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
3352            $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 3357  sub _tree_construction_main ($) {
3357    
3358            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3359          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3360              !!!cp ('t90');
3361            ## As if </noscript>            ## As if </noscript>
3362            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3363            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 3195  sub _tree_construction_main ($) { Line 3368  sub _tree_construction_main ($) {
3368    
3369            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3370          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3371              !!!cp ('t91');
3372            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3373    
3374            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3375            } else {
3376              !!!cp ('t92');
3377          }          }
3378    
3379              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3209  sub _tree_construction_main ($) { Line 3385  sub _tree_construction_main ($) {
3385            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3386              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3387                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3388                    !!!cp ('t93');
3389                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3390                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3391                  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 3393  sub _tree_construction_main ($) {
3393                  !!!next-token;                  !!!next-token;
3394                  redo B;                  redo B;
3395                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3396                    !!!cp ('t94');
3397                  #                  #
3398                } else {                } else {
3399                    !!!cp ('t95');
3400                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3401                  ## Ignore the token                  ## Ignore the token
3402                  !!!next-token;                  !!!next-token;
3403                  redo B;                  redo B;
3404                }                }
3405              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3406                  !!!cp ('t96');
3407                ## As if <head>                ## As if <head>
3408                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3409                $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 3411  sub _tree_construction_main ($) {
3411    
3412                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3413                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3414                } else {
3415                  !!!cp ('t97');
3416              }              }
3417    
3418              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3419                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3420                    !!!cp ('t98');
3421                  ## As if </noscript>                  ## As if </noscript>
3422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3423                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
3424                                
3425                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3426                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3427                  } else {
3428                    !!!cp ('t99');
3429                }                }
3430    
3431                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3432                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3433                    !!!cp ('t100');
3434                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3435                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3436                  } else {
3437                    !!!cp ('t101');
3438                }                }
3439                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3440                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 3445  sub _tree_construction_main ($) {
3445              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3446                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3447                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3448                    !!!cp ('t102');
3449                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3450                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3451                  } else {
3452                    !!!cp ('t103');
3453                }                }
3454                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3455                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 3460  sub _tree_construction_main ($) {
3460              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3461                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3462                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3463                    !!!cp ('t104');
3464                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3465                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3466                  } else {
3467                    !!!cp ('t105');
3468                }                }
3469                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3470                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.
3471    
3472                unless ($self->{confident}) {                unless ($self->{confident}) {
3473                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3474                      !!!cp ('t106');
3475                    $self->{change_encoding}                    $self->{change_encoding}
3476                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value});
3477                                        
# Line 3291  sub _tree_construction_main ($) { Line 3486  sub _tree_construction_main ($) {
3486                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
3487                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3488                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3489                        !!!cp ('t107');
3490                      $self->{change_encoding}                      $self->{change_encoding}
3491                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3492                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3493                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3494                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
3495                                                     ->{has_reference});                                                     ->{has_reference});
3496                      } else {
3497                        !!!cp ('t108');
3498                    }                    }
3499                  }                  }
3500                } else {                } else {
3501                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
3502                      !!!cp ('t109');
3503                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3504                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3505                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
3506                                                 ->{has_reference});                                                 ->{has_reference});
3507                  }                  }
3508                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
3509                      !!!cp ('t110');
3510                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3511                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3512                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3320  sub _tree_construction_main ($) { Line 3520  sub _tree_construction_main ($) {
3520                redo B;                redo B;
3521              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3522                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3523                    !!!cp ('t111');
3524                  ## As if </noscript>                  ## As if </noscript>
3525                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3526                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
# Line 3327  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3529                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3530                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3531                    !!!cp ('t112');
3532                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3533                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3534                  } else {
3535                    !!!cp ('t113');
3536                }                }
3537    
3538                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
# 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, $insert_to_current);
3558                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
# Line 3353  sub _tree_construction_main ($) { Line 3560  sub _tree_construction_main ($) {
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 3888  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4161                                
4162                unless (length $token->{data}) {                unless (length $token->{data}) {
4163                    !!!cp ('t194');
4164                  !!!next-token;                  !!!next-token;
4165                  redo B;                  redo B;
4166                  } else {
4167                    !!!cp ('t195');
4168                }                }
4169              }              }
4170    
# Line 3913  sub _tree_construction_main ($) { Line 4188  sub _tree_construction_main ($) {
4188                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4189                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4190                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4191                        !!!cp ('t196');
4192                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4193                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4194                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4195                    } else {                    } else {
4196                        !!!cp ('t197');
4197                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4198                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4199                    }                    }
# Line 3928  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4206                if (defined $prev_sibling and                if (defined $prev_sibling and
4207                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4208                    !!!cp ('t198');
4209                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4210                } else {                } else {
4211                    !!!cp ('t199');
4212                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4213                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4214                     $next_sibling);                     $next_sibling);
4215                }                }
4216              } else {              } else {
4217                  !!!cp ('t200');
4218                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4219              }              }
4220                            
# Line 3949  sub _tree_construction_main ($) { Line 4229  sub _tree_construction_main ($) {
4229                  ## Clear back to table context                  ## Clear back to table context
4230                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4231                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4232                      !!!cp ('t201');
4233                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4234                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4235                  }                  }
# Line 3960  sub _tree_construction_main ($) { Line 4241  sub _tree_construction_main ($) {
4241    
4242                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4243                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4244                      !!!cp ('t202');
4245                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
4246                  }                  }
4247                                    
# Line 3967  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249                  while (not {                  while (not {
4250                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4251                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4252                      !!!cp ('t203');
4253                      ## ISSUE: Can this case be reached?
4254                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
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                    !!!cp ('t207');
4278                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4279                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4280                }                }
# Line 4009  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4299                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4300                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4301                        !!!cp ('t208');
4302                      $i = $_;                      $i = $_;
4303                      last INSCOPE;                      last INSCOPE;
4304                    } elsif ({                    } elsif ({
4305                              table => 1, html => 1,                              html => 1,
4306    
4307                                ## NOTE: This element does not appear here, maybe.
4308                                table => 1,
4309                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4310                        !!!cp ('t209');
4311                      last INSCOPE;                      last INSCOPE;
4312                    }                    }
4313                  } # INSCOPE                  } # INSCOPE
4314                  unless (defined $i) {                  unless (defined $i) {
4315                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4316    ## TODO: This type is wrong.
4317                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4318                    ## Ignore the token                    ## Ignore the token
4319                    !!!next-token;                    !!!next-token;
4320                    redo B;                    redo B;
# Line 4028  sub _tree_construction_main ($) { Line 4324  sub _tree_construction_main ($) {
4324                  while (not {                  while (not {
4325                    tr => 1, html => 1,                    tr => 1, html => 1,
4326                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4327                      !!!cp ('t211');
4328                      ## ISSUE: Can this case be reached?
4329                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4330                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4331                  }                  }
# Line 4035  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4334                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4335                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4336                      !!!cp ('t212');
4337                    ## reprocess                    ## reprocess
4338                    redo B;                    redo B;
4339                  } else {                  } else {
4340                      !!!cp ('t213');
4341                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4342                  }                  }
4343                }                }
# Line 4050  sub _tree_construction_main ($) { Line 4350  sub _tree_construction_main ($) {
4350                    if ({                    if ({
4351                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4352                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4353                        !!!cp ('t214');
4354                      $i = $_;                      $i = $_;
4355                      last INSCOPE;                      last INSCOPE;
4356                    } elsif ({                    } elsif ({
4357                              table => 1, html => 1,                              table => 1, html => 1,
4358                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4359                        !!!cp ('t215');
4360                      last INSCOPE;                      last INSCOPE;
4361                    }                    }
4362                  } # INSCOPE                  } # INSCOPE
4363                  unless (defined $i) {                  unless (defined $i) {
4364                      !!!cp ('t216');
4365    ## TODO: This erorr type ios wrong.
4366                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4367                    ## Ignore the token                    ## Ignore the token
4368                    !!!next-token;                    !!!next-token;
# Line 4069  sub _tree_construction_main ($) { Line 4373  sub _tree_construction_main ($) {
4373                  while (not {                  while (not {
4374                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4375                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4376                      !!!cp ('t217');
4377                      ## ISSUE: Can this state be reached?
4378                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4379                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4380                  }                  }
# Line 4083  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
4389                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4390                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4391                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4392                  } else {
4393                    !!!cp ('t218');
4394                }                }
4395    
4396                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4397                  ## Clear back to table context                  ## Clear back to table context
4398                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4399                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4400                      !!!cp ('t219');
4401                      ## ISSUE: Can this state be reached?
4402                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4403                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4404                  }                  }
# Line 4105  sub _tree_construction_main ($) { Line 4415  sub _tree_construction_main ($) {
4415                  ## Clear back to table context                  ## Clear back to table context
4416                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4417                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4418                      !!!cp ('t220');
4419                      ## ISSUE: Can this state be reached?
4420                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4421                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4422                  }                  }
# Line 4134  sub _tree_construction_main ($) { Line 4446  sub _tree_construction_main ($) {
4446                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4447                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4448                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4449                      !!!cp ('t221');
4450                    $i = $_;                    $i = $_;
4451                    last INSCOPE;                    last INSCOPE;
4452                  } elsif ({                  } elsif ({
4453                            table => 1, html => 1,                            #table => 1,
4454                              html => 1,
4455                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4456                      !!!cp ('t222');
4457                    last INSCOPE;                    last INSCOPE;
4458                  }                  }
4459                } # INSCOPE                } # INSCOPE
4460                unless (defined $i) {                unless (defined $i) {
4461                    !!!cp ('t223');
4462    ## TODO: The following is wrong, maybe.
4463                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4464                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4465                  !!!next-token;                  !!!next-token;
# Line 4150  sub _tree_construction_main ($) { Line 4467  sub _tree_construction_main ($) {
4467                }                }
4468                                
4469                ## generate implied end tags                ## generate implied end tags
4470                if ({                while ({
4471                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4472                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4473                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4474                    }->{$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;  
4475                }                }
4476    
4477                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4478                    !!!cp ('t225');
4479    ## ISSUE: Can this case be reached?
4480                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4481                  } else {
4482                    !!!cp ('t226');
4483                }                }
4484    
4485                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4174  sub _tree_construction_main ($) { Line 4489  sub _tree_construction_main ($) {
4489                ## reprocess                ## reprocess
4490                redo B;                redo B;
4491          } else {          } else {
4492              !!!cp ('t227');
4493            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4494    
4495            $insert = $insert_to_foster;            $insert = $insert_to_foster;
# Line 4187  sub _tree_construction_main ($) { Line 4503  sub _tree_construction_main ($) {
4503                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4504                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4505                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4506                      !!!cp ('t228');
4507                    $i = $_;                    $i = $_;
4508                    last INSCOPE;                    last INSCOPE;
4509                  } elsif ({                  } elsif ({
4510                            table => 1, html => 1,                            table => 1, html => 1,
4511                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4512                      !!!cp ('t229');
4513                    last INSCOPE;                    last INSCOPE;
4514                  }                  }
4515                } # INSCOPE                } # INSCOPE
4516                unless (defined $i) {                unless (defined $i) {
4517                    !!!cp ('t230');
4518                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4519                  ## Ignore the token                  ## Ignore the token
4520                  !!!next-token;                  !!!next-token;
4521                  redo B;                  redo B;
4522                  } else {
4523                    !!!cp ('t232');
4524                }                }
4525    
4526                ## Clear back to table row context                ## Clear back to table row context
4527                while (not {                while (not {
4528                  tr => 1, html => 1,                  tr => 1, html => 1,
4529                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4530                    !!!cp ('t231');
4531    ## ISSUE: Can this state be reached?
4532                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4533                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4534                }                }
# Line 4222  sub _tree_construction_main ($) { Line 4545  sub _tree_construction_main ($) {
4545                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4546                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4547                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4548                        !!!cp ('t233');
4549                      $i = $_;                      $i = $_;
4550                      last INSCOPE;                      last INSCOPE;
4551                    } elsif ({                    } elsif ({
4552                              table => 1, html => 1,                              table => 1, html => 1,
4553                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4554                        !!!cp ('t234');
4555                      last INSCOPE;                      last INSCOPE;
4556                    }                    }
4557                  } # INSCOPE                  } # INSCOPE
4558                  unless (defined $i) {                  unless (defined $i) {
4559                      !!!cp ('t235');
4560    ## TODO: The following is wrong.
4561                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4562                    ## Ignore the token                    ## Ignore the token
4563                    !!!next-token;                    !!!next-token;
# Line 4241  sub _tree_construction_main ($) { Line 4568  sub _tree_construction_main ($) {
4568                  while (not {                  while (not {
4569                    tr => 1, html => 1,                    tr => 1, html => 1,
4570                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4571                      !!!cp ('t236');
4572    ## ISSUE: Can this state be reached?
4573                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4574                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4575                  }                  }
# Line 4258  sub _tree_construction_main ($) { Line 4587  sub _tree_construction_main ($) {
4587                    if ({                    if ({
4588                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4589                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4590                        !!!cp ('t237');
4591                      $i = $_;                      $i = $_;
4592                      last INSCOPE;                      last INSCOPE;
4593                    } elsif ({                    } elsif ({
4594                              table => 1, html => 1,                              table => 1, html => 1,
4595                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4596                        !!!cp ('t238');
4597                      last INSCOPE;                      last INSCOPE;
4598                    }                    }
4599                  } # INSCOPE                  } # INSCOPE
4600                  unless (defined $i) {                  unless (defined $i) {
4601                      !!!cp ('t239');
4602                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4603                    ## Ignore the token                    ## Ignore the token
4604                    !!!next-token;                    !!!next-token;
# Line 4277  sub _tree_construction_main ($) { Line 4609  sub _tree_construction_main ($) {
4609                  while (not {                  while (not {
4610                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4611                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4612                      !!!cp ('t240');
4613                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4614                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4615                  }                  }
# Line 4298  sub _tree_construction_main ($) { Line 4631  sub _tree_construction_main ($) {
4631                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4632                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4633                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4634                      !!!cp ('t241');
4635                    $i = $_;                    $i = $_;
4636                    last INSCOPE;                    last INSCOPE;
4637                  } elsif ({                  } elsif ({
4638                            table => 1, html => 1,                            table => 1, html => 1,
4639                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4640                      !!!cp ('t242');
4641                    last INSCOPE;                    last INSCOPE;
4642                  }                  }
4643                } # INSCOPE                } # INSCOPE
4644                unless (defined $i) {                unless (defined $i) {
4645                    !!!cp ('t243');
4646                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4647                  ## Ignore the token                  ## Ignore the token
4648                  !!!next-token;                  !!!next-token;
# Line 4314  sub _tree_construction_main ($) { Line 4650  sub _tree_construction_main ($) {
4650                }                }
4651    
4652                ## generate implied end tags                ## generate implied end tags
4653                if ({                while ({
4654                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4655                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4656                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t244');
4657                    }->{$self->{open_elements}->[-1]->[1]}) {  ## ISSUE: Can this case be reached?
4658                  !!!back-token;                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4659                }                }
4660                                
4661                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4662                    !!!cp ('t245');
4663                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4664                  } else {
4665                    !!!cp ('t246');
4666                }                }
4667                                    
4668                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4345  sub _tree_construction_main ($) { Line 4681  sub _tree_construction_main ($) {
4681                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4682                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4683                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4684                        !!!cp ('t247');
4685                      $i = $_;                      $i = $_;
4686                      last INSCOPE;                      last INSCOPE;
4687                    } elsif ({                    } elsif ({
4688                              table => 1, html => 1,                              table => 1, html => 1,
4689                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4690                        !!!cp ('t248');
4691                      last INSCOPE;                      last INSCOPE;
4692                    }                    }
4693                  } # INSCOPE                  } # INSCOPE
4694                    unless (defined $i) {                    unless (defined $i) {
4695                        !!!cp ('t249');
4696                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4697                      ## Ignore the token                      ## Ignore the token
4698                      !!!next-token;                      !!!next-token;
# Line 4366  sub _tree_construction_main ($) { Line 4705  sub _tree_construction_main ($) {
4705                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4706                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4707                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4708                        !!!cp ('t250');
4709                      $i = $_;                      $i = $_;
4710                      last INSCOPE;                      last INSCOPE;
4711                    } elsif ({                    } elsif ({
4712                              table => 1, html => 1,                              table => 1, html => 1,
4713                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4714                        !!!cp ('t251');
4715                      last INSCOPE;                      last INSCOPE;
4716                    }                    }
4717                  } # INSCOPE                  } # INSCOPE
4718                    unless (defined $i) {                    unless (defined $i) {
4719                        !!!cp ('t252');
4720                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr');
4721                      ## Ignore the token                      ## Ignore the token
4722                      !!!next-token;                      !!!next-token;
# Line 4385  sub _tree_construction_main ($) { Line 4727  sub _tree_construction_main ($) {
4727                  while (not {                  while (not {
4728                    tr => 1, html => 1,                    tr => 1, html => 1,
4729                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4730                      !!!cp ('t253');
4731    ## ISSUE: Can this case be reached?
4732                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4733                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4734                  }                  }
# Line 4399  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4744                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4745                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4746                      !!!cp ('t254');
4747                    $i = $_;                    $i = $_;
4748                    last INSCOPE;                    last INSCOPE;
4749                  } elsif ({                  } elsif ({
4750                            table => 1, html => 1,                            table => 1, html => 1,
4751                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4752                      !!!cp ('t255');
4753                    last INSCOPE;                    last INSCOPE;
4754                  }                  }
4755                } # INSCOPE                } # INSCOPE
4756                unless (defined $i) {                unless (defined $i) {
4757                    !!!cp ('t256');
4758                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4759                  ## Ignore the token                  ## Ignore the token
4760                  !!!next-token;                  !!!next-token;
# Line 4418  sub _tree_construction_main ($) { Line 4765  sub _tree_construction_main ($) {
4765                while (not {                while (not {
4766                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4767                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4768                    !!!cp ('t257');
4769    ## ISSUE: Can this case be reached?
4770                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4771                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4772                }                }
# Line 4432  sub _tree_construction_main ($) { Line 4781  sub _tree_construction_main ($) {
4781                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4782                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4783                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4784                  !!!cp ('t258');
4785                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4786                ## Ignore the token                ## Ignore the token
4787                !!!next-token;                !!!next-token;
4788                redo B;                redo B;
4789          } else {          } else {
4790              !!!cp ('t259');
4791            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name});
4792    
4793            $insert = $insert_to_foster;            $insert = $insert_to_foster;
# Line 4450  sub _tree_construction_main ($) { Line 4801  sub _tree_construction_main ($) {
4801              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4802                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4803                unless (length $token->{data}) {                unless (length $token->{data}) {
4804                    !!!cp ('t260');
4805                  !!!next-token;                  !!!next-token;
4806                  redo B;                  redo B;
4807                }                }
4808              }              }
4809                            
4810                !!!cp ('t261');
4811              #              #
4812            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4813              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4814                  !!!cp ('t262');
4815                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4816                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4817                !!!next-token;                !!!next-token;
4818                redo B;                redo B;
4819              } else {              } else {
4820                  !!!cp ('t263');
4821                #                #
4822              }              }
4823            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4824              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4825                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4826                    !!!cp ('t264');
4827                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
4828                  ## Ignore the token                  ## Ignore the token
4829                  !!!next-token;                  !!!next-token;
4830                  redo B;                  redo B;
4831                } else {                } else {
4832                    !!!cp ('t265');
4833                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4834                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4835                  !!!next-token;                  !!!next-token;
4836                  redo B;                              redo B;            
4837                }                }
4838              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4839                  !!!cp ('t266');
4840                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col');
4841                ## Ignore the token                ## Ignore the token
4842                !!!next-token;                !!!next-token;
4843                redo B;                redo B;
4844              } else {              } else {
4845                  !!!cp ('t267');
4846                #                #
4847              }              }
4848            } else {            } else {
4849              #              die "$0: $token->{type}: Unknown token type";
4850            }            }
4851    
4852            ## As if </colgroup>            ## As if </colgroup>
4853            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4854                !!!cp ('t269');
4855              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4856              ## Ignore the token              ## Ignore the token
4857              !!!next-token;              !!!next-token;
4858              redo B;              redo B;
4859            } else {            } else {
4860                !!!cp ('t270');
4861              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4862              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
4863              ## reprocess              ## reprocess
# Line 4504  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865            }            }
4866      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4867        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4868            !!!cp ('t271');
4869          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4870          !!!next-token;          !!!next-token;
4871          redo B;          redo B;
4872        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4873              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4874                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4875                    !!!cp ('t272');
4876                  ## As if </option>                  ## As if </option>
4877                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4878                  } else {
4879                    !!!cp ('t273');
4880                }                }
4881    
4882                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4519  sub _tree_construction_main ($) { Line 4884  sub _tree_construction_main ($) {
4884                redo B;                redo B;
4885              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4886                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4887                    !!!cp ('t274');
4888                  ## As if </option>                  ## As if </option>
4889                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4890                  } else {
4891                    !!!cp ('t275');
4892                }                }
4893    
4894                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4895                    !!!cp ('t276');
4896                  ## As if </optgroup>                  ## As if </optgroup>
4897                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4898                  } else {
4899                    !!!cp ('t277');
4900                }                }
4901    
4902                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4903                !!!next-token;                !!!next-token;
4904                redo B;                redo B;
4905              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4906    ## TODO: The type below is not good - <select> is replaced by </select>
4907                !!!parse-error (type => 'not closed:select');                !!!parse-error (type => 'not closed:select');
4908                ## As if </select> instead                ## As if </select> instead
4909                ## have an element in table scope                ## have an element in table scope
# Line 4539  sub _tree_construction_main ($) { Line 4911  sub _tree_construction_main ($) {
4911                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4912                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4913                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4914                      !!!cp ('t278');
4915                    $i = $_;                    $i = $_;
4916                    last INSCOPE;                    last INSCOPE;
4917                  } elsif ({                  } elsif ({
4918                            table => 1, html => 1,                            table => 1, html => 1,
4919                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4920                      !!!cp ('t279');
4921                    last INSCOPE;                    last INSCOPE;
4922                  }                  }
4923                } # INSCOPE                } # INSCOPE
4924                unless (defined $i) {                unless (defined $i) {
4925                    !!!cp ('t280');
4926                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
4927                  ## Ignore the token                  ## Ignore the token
4928                  !!!next-token;                  !!!next-token;
4929                  redo B;                  redo B;
4930                }                }
4931                                
4932                  !!!cp ('t281');
4933                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4934    
4935                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4561  sub _tree_construction_main ($) { Line 4937  sub _tree_construction_main ($) {
4937                !!!next-token;                !!!next-token;
4938                redo B;                redo B;
4939          } else {          } else {
4940              !!!cp ('t282');
4941            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name});
4942            ## Ignore the token            ## Ignore the token
4943            !!!next-token;            !!!next-token;
# Line 4570  sub _tree_construction_main ($) { Line 4947  sub _tree_construction_main ($) {
4947              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4948                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4949                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4950                    !!!cp ('t283');
4951                  ## As if </option>                  ## As if </option>
4952                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
4953                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4954                    !!!cp ('t284');
4955                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4956                } else {                } else {
4957                    !!!cp ('t285');
4958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4959                  ## Ignore the token                  ## Ignore the token
4960                }                }
# Line 4582  sub _tree_construction_main ($) { Line 4962  sub _tree_construction_main ($) {
4962                redo B;                redo B;
4963              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4964                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4965                    !!!cp ('t286');
4966                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4967                } else {                } else {
4968                    !!!cp ('t287');
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 4595  sub _tree_construction_main ($) { Line 4977  sub _tree_construction_main ($) {
4977                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4978                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4979                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4980                      !!!cp ('t288');
4981                    $i = $_;                    $i = $_;
4982                    last INSCOPE;                    last INSCOPE;
4983                  } elsif ({                  } elsif ({
4984                            table => 1, html => 1,                            table => 1, html => 1,
4985                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4986                      !!!cp ('t289');
4987                    last INSCOPE;                    last INSCOPE;
4988                  }                  }
4989                } # INSCOPE                } # INSCOPE
4990                unless (defined $i) {                unless (defined $i) {
4991                    !!!cp ('t290');
4992                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4993                  ## Ignore the token                  ## Ignore the token
4994                  !!!next-token;                  !!!next-token;
4995                  redo B;                  redo B;
4996                }                }
4997                                
4998                  !!!cp ('t291');
4999                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5000    
5001                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4620  sub _tree_construction_main ($) { Line 5006  sub _tree_construction_main ($) {
5006                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
5007                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5008                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5009    ## TODO: The following is wrong?
5010                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5011                                
5012                ## have an element in table scope                ## have an element in table scope
# Line 4627  sub _tree_construction_main ($) { Line 5014  sub _tree_construction_main ($) {
5014                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5016                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5017                      !!!cp ('t292');
5018                    $i = $_;                    $i = $_;
5019                    last INSCOPE;                    last INSCOPE;
5020                  } elsif ({                  } elsif ({
5021                            table => 1, html => 1,                            table => 1, html => 1,
5022                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5023                      !!!cp ('t293');
5024                    last INSCOPE;                    last INSCOPE;
5025                  }                  }
5026                } # INSCOPE                } # INSCOPE
5027                unless (defined $i) {                unless (defined $i) {
5028                    !!!cp ('t294');
5029                  ## Ignore the token                  ## Ignore the token
5030                  !!!next-token;                  !!!next-token;
5031                  redo B;                  redo B;
# Line 4647  sub _tree_construction_main ($) { Line 5037  sub _tree_construction_main ($) {
5037                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5038                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5039                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5040                      !!!cp ('t295');
5041                    $i = $_;                    $i = $_;
5042                    last INSCOPE;                    last INSCOPE;
5043                  } elsif ({                  } elsif ({
5044                            table => 1, html => 1,                            table => 1, html => 1,
5045                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5046    ## ISSUE: Can this state be reached?
5047                      !!!cp ('t296');
5048                    last INSCOPE;                    last INSCOPE;
5049                  }                  }
5050                } # INSCOPE                } # INSCOPE
5051                unless (defined $i) {                unless (defined $i) {
5052                    !!!cp ('t297');
5053    ## TODO: The following error type is correct?
5054                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5055                  ## Ignore the </select> token                  ## Ignore the </select> token
5056                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5057                  redo B;                  redo B;
5058                }                }
5059                                
5060                  !!!cp ('t298');
5061                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5062    
5063                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4669  sub _tree_construction_main ($) { Line 5065  sub _tree_construction_main ($) {
5065                ## reprocess                ## reprocess
5066                redo B;                redo B;
5067          } else {          } else {
5068              !!!cp ('t299');
5069            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name});
5070            ## Ignore the token            ## Ignore the token
5071            !!!next-token;            !!!next-token;
# Line 4687  sub _tree_construction_main ($) { Line 5084  sub _tree_construction_main ($) {
5084            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5085                        
5086            unless (length $token->{data}) {            unless (length $token->{data}) {
5087                !!!cp ('t300');
5088              !!!next-token;              !!!next-token;
5089              redo B;              redo B;
5090            }            }
5091          }          }
5092                    
5093          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5094              !!!cp ('t301');
5095            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5096    
5097            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5098            } else {
5099              !!!cp ('t302');
5100          }          }
5101                    
5102          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4706  sub _tree_construction_main ($) { Line 5107  sub _tree_construction_main ($) {
5107          redo B;          redo B;
5108        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5109          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5110              !!!cp ('t303');
5111            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5112                        
5113            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5114            } else {
5115              !!!cp ('t304');
5116          }          }
5117    
5118          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4719  sub _tree_construction_main ($) { Line 5123  sub _tree_construction_main ($) {
5123          redo B;          redo B;
5124        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5125          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5126              !!!cp ('t305');
5127            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5128                        
5129            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5130            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5131            } else {
5132              !!!cp ('t306');
5133          }          }
5134    
5135          ## "after body" insertion mode          ## "after body" insertion mode
5136          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5137            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5138                !!!cp ('t307');
5139              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html');
5140              ## Ignore the token              ## Ignore the token
5141              !!!next-token;              !!!next-token;
5142              redo B;              redo B;
5143            } else {            } else {
5144                !!!cp ('t308');
5145              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5146              !!!next-token;              !!!next-token;
5147              redo B;              redo B;
5148            }            }
5149          } else {          } else {
5150              !!!cp ('t309');
5151            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
5152    
5153            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
# Line 4753  sub _tree_construction_main ($) { Line 5163  sub _tree_construction_main ($) {
5163            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5164                        
5165            unless (length $token->{data}) {            unless (length $token->{data}) {
5166                !!!cp ('t310');
5167              !!!next-token;              !!!next-token;
5168              redo B;              redo B;
5169            }            }
# Line 4760  sub _tree_construction_main ($) { Line 5171  sub _tree_construction_main ($) {
5171                    
5172          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5173            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5174                !!!cp ('t311');
5175              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
5176            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5177                !!!cp ('t312');
5178              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5179            } else { # "after html frameset"            } else { # "after html frameset"
5180                !!!cp ('t313');
5181              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5182    
5183              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5184              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5185              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5186            }            }
5187                        
5188            ## Ignore the token.            ## Ignore the token.
5189            if (length $token->{data}) {            if (length $token->{data}) {
5190                !!!cp ('t314');
5191              ## reprocess the rest of characters              ## reprocess the rest of characters
5192            } else {            } else {
5193                !!!cp ('t315');
5194              !!!next-token;              !!!next-token;
5195            }            }
5196            redo B;            redo B;
# Line 4783  sub _tree_construction_main ($) { Line 5199  sub _tree_construction_main ($) {
5199          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5200        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5201          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5202              !!!cp ('t316');
5203            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5204    
5205            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5206            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5207          }          } else {
5208              !!!cp ('t317');
5209            }
5210    
5211          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5212              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5213              !!!cp ('t318');
5214            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5215            !!!next-token;            !!!next-token;
5216            redo B;            redo B;
5217          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5218                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5219              !!!cp ('t319');
5220            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5221            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5222            !!!next-token;            !!!next-token;
5223            redo B;            redo B;
5224          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5225              !!!cp ('t320');
5226            ## NOTE: As if in body.            ## NOTE: As if in body.
5227            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5228            redo B;            redo B;
5229          } else {          } else {
5230            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5231                !!!cp ('t321');
5232              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5233            } else {            } else {
5234                !!!cp ('t322');
5235              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5236            }            }
5237            ## Ignore the token            ## Ignore the token
# Line 4816  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240          }          }
5241        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5242          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5243              !!!cp ('t323');
5244            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5245    
5246            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5247            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5248            } else {
5249              !!!cp ('t324');
5250          }          }
5251    
5252          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5253              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5254            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5255                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5256                !!!cp ('t325');
5257              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5258              ## Ignore the token              ## Ignore the token
5259              !!!next-token;              !!!next-token;
5260            } else {            } else {
5261                !!!cp ('t326');
5262              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5263              !!!next-token;              !!!next-token;
5264            }            }
5265    
5266            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5267                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5268                !!!cp ('t327');
5269              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5270              } else {
5271                !!!cp ('t328');
5272            }            }
5273            redo B;            redo B;
5274          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5275                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5276              !!!cp ('t329');
5277            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5278            !!!next-token;            !!!next-token;
5279            redo B;            redo B;
5280          } else {          } else {
5281            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5282                !!!cp ('t330');
5283              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5284            } else {            } else {
5285                !!!cp ('t331');
5286              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5287            }            }
5288            ## Ignore the token            ## Ignore the token
# Line 4866  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301      ## "in body" insertion mode      ## "in body" insertion mode
5302      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5303        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5304            !!!cp ('t332');
5305          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5306          $script_start_tag->($insert);          $script_start_tag->($insert);
5307          redo B;          redo B;
5308        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5309            !!!cp ('t333');
5310          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5311          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5312          redo B;          redo B;
5313        } elsif ({        } elsif ({
5314                  base => 1, link => 1,                  base => 1, link => 1,
5315                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5316            !!!cp ('t334');
5317          ## 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
5318          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5319          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 5326  sub _tree_construction_main ($) {
5326    
5327          unless ($self->{confident}) {          unless ($self->{confident}) {
5328            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5329                !!!cp ('t335');
5330              $self->{change_encoding}              $self->{change_encoding}
5331                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value});
5332                            
# Line 4902  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
5341                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
5342                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5343                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5344                  !!!cp ('t336');
5345                $self->{change_encoding}                $self->{change_encoding}
5346                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5347                $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 5352  sub _tree_construction_main ($) {
5352            }            }
5353          } else {          } else {
5354            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
5355                !!!cp ('t337');
5356              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5357                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5358                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
5359                                           ->{has_reference});                                           ->{has_reference});
5360            }            }
5361            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
5362                !!!cp ('t338');
5363              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5364                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5365                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4928  sub _tree_construction_main ($) { Line 5370  sub _tree_construction_main ($) {
5370          !!!next-token;          !!!next-token;
5371          redo B;          redo B;
5372        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5373            !!!cp ('t341');
5374          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
5375          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5376          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
5377            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
5378                !!!cp ('t339');
5379              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
5380            } else {            } else {
5381                !!!cp ('t340');
5382              $insert->($_[0]);              $insert->($_[0]);
5383            }            }
5384          });          });
# Line 4943  sub _tree_construction_main ($) { Line 5388  sub _tree_construction_main ($) {
5388                                
5389          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5390              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5391              !!!cp ('t342');
5392            ## Ignore the token            ## Ignore the token
5393          } else {          } else {
5394            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5395            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5396              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5397                  !!!cp ('t343');
5398                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5399                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5400                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4958  sub _tree_construction_main ($) { Line 5405  sub _tree_construction_main ($) {
5405          redo B;          redo B;
5406        } elsif ({        } elsif ({
5407                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5408                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5409                  menu => 1, ol => 1, p => 1, ul => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5410                    listing => 1, menu => 1, ol => 1, p => 1, ul => 1,
5411                  pre => 1,                  pre => 1,
5412                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5413          ## has a p element in scope          ## has a p element in scope
5414          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5415            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5416                !!!cp ('t344');
5417              !!!back-token;              !!!back-token;
5418              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5419              redo B;              redo B;
# Line 4972  sub _tree_construction_main ($) { Line 5421  sub _tree_construction_main ($) {
5421                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5422                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5423                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5424                !!!cp ('t345');
5425              last INSCOPE;              last INSCOPE;
5426            }            }
5427          } # INSCOPE          } # INSCOPE
# Line 4982  sub _tree_construction_main ($) { Line 5432  sub _tree_construction_main ($) {
5432            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5433              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5434              unless (length $token->{data}) {              unless (length $token->{data}) {
5435                  !!!cp ('t346');
5436                !!!next-token;                !!!next-token;
5437                } else {
5438                  !!!cp ('t349');
5439              }              }
5440              } else {
5441                !!!cp ('t348');
5442            }            }
5443          } else {          } else {
5444              !!!cp ('t347');
5445            !!!next-token;            !!!next-token;
5446          }          }
5447          redo B;          redo B;
5448        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5449          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5450              !!!cp ('t350');
5451            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5452            ## Ignore the token            ## Ignore the token
5453            !!!next-token;            !!!next-token;
# Line 4999  sub _tree_construction_main ($) { Line 5456  sub _tree_construction_main ($) {
5456            ## has a p element in scope            ## has a p element in scope
5457            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5458              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5459                  !!!cp ('t351');
5460                !!!back-token;                !!!back-token;
5461                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5462                redo B;                redo B;
# Line 5006  sub _tree_construction_main ($) { Line 5464  sub _tree_construction_main ($) {
5464                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5465                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5466                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5467                  !!!cp ('t352');
5468                last INSCOPE;                last INSCOPE;
5469              }              }
5470            } # INSCOPE            } # INSCOPE
# Line 5019  sub _tree_construction_main ($) { Line 5478  sub _tree_construction_main ($) {
5478          ## has a p element in scope          ## has a p element in scope
5479          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5480            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5481                !!!cp ('t353');
5482              !!!back-token;              !!!back-token;
5483              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5484              redo B;              redo B;
# Line 5026  sub _tree_construction_main ($) { Line 5486  sub _tree_construction_main ($) {
5486                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5487                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5488                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5489                !!!cp ('t354');
5490              last INSCOPE;              last INSCOPE;
5491            }            }
5492          } # INSCOPE          } # INSCOPE
# Line 5037  sub _tree_construction_main ($) { Line 5498  sub _tree_construction_main ($) {
5498            ## Step 2            ## Step 2
5499            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5500              if ($i != -1) {              if ($i != -1) {
5501                  !!!cp ('t355');
5502                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5503                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5504                } else {
5505                  !!!cp ('t356');
5506              }              }
5507              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5508              last LI;              last LI;
5509              } else {
5510                !!!cp ('t357');
5511            }            }
5512                        
5513            ## Step 3            ## Step 3
# Line 5050  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5517                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5518                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5519                !!!cp ('t358');
5520              last LI;              last LI;
5521            }            }
5522                        
5523              !!!cp ('t359');
5524            ## Step 4            ## Step 4
5525            $i--;            $i--;
5526            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5066  sub _tree_construction_main ($) { Line 5534  sub _tree_construction_main ($) {
5534          ## has a p element in scope          ## has a p element in scope
5535          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5536            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5537                !!!cp ('t360');
5538              !!!back-token;              !!!back-token;
5539              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5540              redo B;              redo B;
# Line 5073  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5543                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5544                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5545                !!!cp ('t361');
5546              last INSCOPE;              last INSCOPE;
5547            }            }
5548          } # INSCOPE          } # INSCOPE
# Line 5084  sub _tree_construction_main ($) { Line 5554  sub _tree_construction_main ($) {
5554            ## Step 2            ## Step 2
5555            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5556              if ($i != -1) {              if ($i != -1) {
5557                  !!!cp ('t362');
5558                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5559                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5560                } else {
5561                  !!!cp ('t363');
5562              }              }
5563              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5564              last LI;              last LI;
5565              } else {
5566                !!!cp ('t364');
5567            }            }
5568                        
5569            ## Step 3            ## Step 3
# Line 5097  sub _tree_construction_main ($) { Line 5572  sub _tree_construction_main ($) {
5572                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5573                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5574                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5575                !!!cp ('t365');
5576              last LI;              last LI;
5577            }            }
5578                        
5579              !!!cp ('t366');
5580            ## Step 4            ## Step 4
5581            $i--;            $i--;
5582            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5113  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590          ## has a p element in scope          ## has a p element in scope
5591          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5592            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5593                !!!cp ('t367');
5594              !!!back-token;              !!!back-token;
5595              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5596              redo B;              redo B;
# Line 5120  sub _tree_construction_main ($) { Line 5598  sub _tree_construction_main ($) {
5598                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5599                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5600                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5601                !!!cp ('t368');
5602              last INSCOPE;              last INSCOPE;
5603            }            }
5604          } # INSCOPE          } # INSCOPE
# Line 5130  sub _tree_construction_main ($) { Line 5609  sub _tree_construction_main ($) {
5609                        
5610          !!!next-token;          !!!next-token;
5611          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;  
5612        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5613          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5614            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5615            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5616                !!!cp ('t371');
5617              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5618                            
5619              !!!back-token;              !!!back-token;
# Line 5187  sub _tree_construction_main ($) { Line 5622  sub _tree_construction_main ($) {
5622                            
5623              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5624                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5625                    !!!cp ('t372');
5626                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5627                  last AFE2;                  last AFE2;
5628                }                }
5629              } # AFE2              } # AFE2
5630              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5631                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5632                    !!!cp ('t373');
5633                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5634                  last OE;                  last OE;
5635                }                }
5636              } # OE              } # OE
5637              last AFE;              last AFE;
5638            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5639                !!!cp ('t374');
5640              last AFE;              last AFE;
5641            }            }
5642          } # AFE          } # AFE
# Line 5215  sub _tree_construction_main ($) { Line 5653  sub _tree_construction_main ($) {
5653                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5654                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5655                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5656            !!!cp ('t375');
5657          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5658                    
5659          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5229  sub _tree_construction_main ($) { Line 5668  sub _tree_construction_main ($) {
5668          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5669            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5670            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5671                !!!cp ('t376');
5672              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr');
5673              !!!back-token;              !!!back-token;
5674              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
# Line 5237  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5678                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5679                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5680                !!!cp ('t377');
5681              last INSCOPE;              last INSCOPE;
5682            }            }
5683          } # INSCOPE          } # INSCOPE
# Line 5251  sub _tree_construction_main ($) { Line 5692  sub _tree_construction_main ($) {
5692          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5693            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5694            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5695                !!!cp ('t378');
5696              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5697              !!!back-token;              !!!back-token;
5698              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
# Line 5259  sub _tree_construction_main ($) { Line 5701  sub _tree_construction_main ($) {
5701                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5702                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5703                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5704                !!!cp ('t379');
5705              last INSCOPE;              last INSCOPE;
5706            }            }
5707          } # INSCOPE          } # INSCOPE
# Line 5266  sub _tree_construction_main ($) { Line 5709  sub _tree_construction_main ($) {
5709          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5710                        
5711          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5712    
5713            ## TODO: associate with $self->{form_element} if defined
5714    
5715          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5716    
5717          !!!next-token;          !!!next-token;
5718          redo B;          redo B;
5719        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5720                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5721            !!!cp ('t380');
5722          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5723                    
5724          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5280  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727          !!!next-token;          !!!next-token;
5728          redo B;          redo B;
5729        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5730            !!!cp ('t381');
5731          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5732          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5733          redo B;          redo B;
# Line 5287  sub _tree_construction_main ($) { Line 5735  sub _tree_construction_main ($) {
5735          ## has a p element in scope          ## has a p element in scope
5736          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5737            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5738                !!!cp ('t382');
5739              !!!back-token;              !!!back-token;
5740              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5741              redo B;              redo B;
# Line 5294  sub _tree_construction_main ($) { Line 5743  sub _tree_construction_main ($) {
5743                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5744                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5745                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5746                !!!cp ('t383');
5747              last INSCOPE;              last INSCOPE;
5748            }            }
5749          } # INSCOPE          } # INSCOPE
# Line 5310  sub _tree_construction_main ($) { Line 5760  sub _tree_construction_main ($) {
5760                  image => 1,                  image => 1,
5761                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5762          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5763              !!!cp ('t384');
5764            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5765            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5766            } else {
5767              !!!cp ('t385');
5768          }          }
5769    
5770          ## 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 5779  sub _tree_construction_main ($) {
5779          ## has a p element in scope          ## has a p element in scope
5780          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5781            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5782                !!!cp ('t386');
5783              !!!back-token;              !!!back-token;
5784              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5785              redo B;              redo B;
# Line 5333  sub _tree_construction_main ($) { Line 5787  sub _tree_construction_main ($) {
5787                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5788                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5789                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5790                !!!cp ('t387');
5791              last INSCOPE;              last INSCOPE;
5792            }            }
5793          } # INSCOPE          } # INSCOPE
# Line 5343  sub _tree_construction_main ($) { Line 5798  sub _tree_construction_main ($) {
5798          !!!next-token;          !!!next-token;
5799          redo B;          redo B;
5800        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5801            !!!cp ('t388');
5802          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5803                    
5804          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5355  sub _tree_construction_main ($) { Line 5811  sub _tree_construction_main ($) {
5811          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5812                    
5813          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5814              !!!cp ('t389');
5815            ## Ignore the token            ## Ignore the token
5816            !!!next-token;            !!!next-token;
5817            redo B;            redo B;
# Line 5374  sub _tree_construction_main ($) { Line 5831  sub _tree_construction_main ($) {
5831                          {type => START_TAG_TOKEN, tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5832                         );                         );
5833            if ($prompt_attr) {            if ($prompt_attr) {
5834                !!!cp ('t390');
5835              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5836            } else {            } else {
5837                !!!cp ('t391');
5838              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5839                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5840              ## TODO: make this configurable              ## TODO: make this configurable
# Line 5407  sub _tree_construction_main ($) { Line 5866  sub _tree_construction_main ($) {
5866          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5867            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5868            unless (length $token->{data}) {            unless (length $token->{data}) {
5869                !!!cp ('t392');
5870              !!!next-token;              !!!next-token;
5871              } else {
5872                !!!cp ('t393');
5873            }            }
5874            } else {
5875              !!!cp ('t394');
5876          }          }
5877          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5878              !!!cp ('t395');
5879            $text .= $token->{data};            $text .= $token->{data};
5880            !!!next-token;            !!!next-token;
5881          }          }
5882          if (length $text) {          if (length $text) {
5883              !!!cp ('t396');
5884            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5885          }          }
5886                    
# Line 5422  sub _tree_construction_main ($) { Line 5888  sub _tree_construction_main ($) {
5888                    
5889          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
5890              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5891              !!!cp ('t397');
5892            ## Ignore the token            ## Ignore the token
5893          } else {          } else {
5894              !!!cp ('t398');
5895            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5896          }          }
5897          !!!next-token;          !!!next-token;
# Line 5434  sub _tree_construction_main ($) { Line 5902  sub _tree_construction_main ($) {
5902                  noframes => 1,                  noframes => 1,
5903                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5904                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5905            !!!cp ('t399');
5906          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
5907          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5908          redo B;          redo B;
5909        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5910            !!!cp ('t400');
5911          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5912                    
5913          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5914    
5915            ## TODO: associate with $self->{form_element} if defined
5916                    
5917          $self->{insertion_mode} = IN_SELECT_IM;          $self->{insertion_mode} = IN_SELECT_IM;
5918          !!!next-token;          !!!next-token;
# Line 5451  sub _tree_construction_main ($) { Line 5923  sub _tree_construction_main ($) {
5923                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5924                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5925                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5926            !!!cp ('t401');
5927          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5928          ## Ignore the token          ## Ignore the token
5929          !!!next-token;          !!!next-token;
# Line 5458  sub _tree_construction_main ($) { Line 5931  sub _tree_construction_main ($) {
5931                    
5932          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5933        } else {        } else {
5934            !!!cp ('t402');
5935          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5936                    
5937          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5475  sub _tree_construction_main ($) { Line 5949  sub _tree_construction_main ($) {
5949                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5950                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
5951                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5952                  !!!cp ('t403');
5953                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5954                } else {
5955                  !!!cp ('t404');
5956              }              }
5957            }            }
5958    
# Line 5483  sub _tree_construction_main ($) { Line 5960  sub _tree_construction_main ($) {
5960            !!!next-token;            !!!next-token;
5961            redo B;            redo B;
5962          } else {          } else {
5963              !!!cp ('t405');
5964            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5965            ## Ignore the token            ## Ignore the token
5966            !!!next-token;            !!!next-token;
# Line 5492  sub _tree_construction_main ($) { Line 5970  sub _tree_construction_main ($) {
5970          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') {
5971            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
5972            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5973                !!!cp ('t406');
5974              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5975              } else {
5976                !!!cp ('t407');
5977            }            }
5978            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5979            ## reprocess            ## reprocess
5980            redo B;            redo B;
5981          } else {          } else {
5982              !!!cp ('t408');
5983            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5984            ## Ignore the token            ## Ignore the token
5985            !!!next-token;            !!!next-token;
# Line 5507  sub _tree_construction_main ($) { Line 5989  sub _tree_construction_main ($) {
5989                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5990                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
5991                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
5992                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
5993                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
5994                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5517  sub _tree_construction_main ($) { Line 5998  sub _tree_construction_main ($) {
5998            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5999            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6000              ## generate implied end tags              ## generate implied end tags
6001              if ({              while ({
6002                   dd => ($token->{tag_name} ne 'dd'),                      dd => ($token->{tag_name} ne 'dd'),
6003                   dt => ($token->{tag_name} ne 'dt'),                      dt => ($token->{tag_name} ne 'dt'),
6004                   li => ($token->{tag_name} ne 'li'),                      li => ($token->{tag_name} ne 'li'),
6005                   p => ($token->{tag_name} ne 'p'),                      p => 1,
6006                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6007                   tbody => 1, tfoot=> 1, thead => 1,                !!!cp ('t409');
6008                  }->{$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;  
6009              }              }
6010                
6011                !!!cp ('t410');
6012              $i = $_;              $i = $_;
6013              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6014            } elsif ({            } elsif ({
6015                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6016                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6017                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6018                !!!cp ('t411');
6019              last INSCOPE;              last INSCOPE;
6020            }            }
6021          } # INSCOPE          } # INSCOPE
6022                    
6023          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6024            if (defined $i) {            if (defined $i) {
6025                !!!cp ('t412');
6026              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6027            } else {            } else {
6028                !!!cp ('t413');
6029              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6030            }            }
6031          }          }
6032                    
6033          if (defined $i) {          if (defined $i) {
6034              !!!cp ('t414');
6035            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6036          } elsif ($token->{tag_name} eq 'p') {          } else {
6037            ## As if <p>, then reprocess the current token            !!!cp ('t416');
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
6038          }          }
6039          $clear_up_to_marker->()          $clear_up_to_marker->()
6040            if {            if {
# Line 5568  sub _tree_construction_main ($) { Line 6048  sub _tree_construction_main ($) {
6048            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6049            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6050              ## generate implied end tags              ## generate implied end tags
6051              if ({              while ({
6052                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6053                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6054                   tbody => 1, tfoot=> 1, thead => 1,                !!!cp ('t417');
6055                  }->{$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;  
6056              }              }
6057    
6058                !!!cp ('t418');
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}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
6070              !!!cp ('t420');
6071            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6072          } else {          } else {
6073              !!!cp ('t421');
6074            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6075          }          }
6076    
# Line 5607  sub _tree_construction_main ($) { Line 6088  sub _tree_construction_main ($) {
6088                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6089                }->{$node->[1]}) {                }->{$node->[1]}) {
6090              ## generate implied end tags              ## generate implied end tags
6091              if ({              while ({
6092                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6093                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6094                   tbody => 1, tfoot=> 1, thead => 1,                !!!cp ('t422');
6095                  }->{$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;  
6096              }              }
6097    
6098                !!!cp ('t423');
6099              $i = $_;              $i = $_;
6100              last INSCOPE;              last INSCOPE;
6101            } elsif ({            } elsif ({
6102                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6103                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6104                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6105                !!!cp ('t424');
6106              last INSCOPE;              last INSCOPE;
6107            }            }
6108          } # INSCOPE          } # INSCOPE
6109                    
6110          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6111              !!!cp ('t425');
6112            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6113            } else {
6114              !!!cp ('t426');
6115          }          }
6116                    
6117          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
6118          !!!next-token;          !!!next-token;
6119          redo B;          redo B;
6120          } elsif ($token->{tag_name} eq 'p') {
6121            ## has an element in scope
6122            my $i;
6123            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6124              my $node = $self->{open_elements}->[$_];
6125              if ($node->[1] eq $token->{tag_name}) {
6126                ## generate implied end tags
6127                while ({
6128                        dd => 1, dt => 1, li => 1, p => 0,
6129                       }->{$self->{open_elements}->[-1]->[1]}) {
6130                  !!!cp ('t409.1');
6131                  pop @{$self->{open_elements}};
6132                }
6133                
6134                !!!cp ('t410.1');
6135                $i = $_;
6136                last INSCOPE;
6137              } elsif ({
6138                        table => 1, caption => 1, td => 1, th => 1,
6139                        button => 1, marquee => 1, object => 1, html => 1,
6140                       }->{$node->[1]}) {
6141                !!!cp ('t411.1');
6142                last INSCOPE;
6143              }
6144            } # INSCOPE
6145            
6146            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6147              if (defined $i) {
6148                !!!cp ('t412.1');
6149                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6150              } else {
6151                !!!cp ('t413.1');
6152                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6153              }
6154            }
6155            
6156            if (defined $i) {
6157              !!!cp ('t414.1');
6158              splice @{$self->{open_elements}}, $i;
6159            } else {
6160              !!!cp ('t415.1');
6161              ## As if <p>, then reprocess the current token
6162              my $el;
6163              !!!create-element ($el, 'p');
6164              $insert->($el);
6165            }
6166            !!!next-token;
6167            redo B;
6168        } elsif ({        } elsif ({
6169                  a => 1,                  a => 1,
6170                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
6171                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6172                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6173                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6174            !!!cp ('t427');
6175          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6176          redo B;          redo B;
6177        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6178            !!!cp ('t428');
6179          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6180    
6181          ## As if <br>          ## As if <br>
# Line 5667  sub _tree_construction_main ($) { Line 6200  sub _tree_construction_main ($) {
6200                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6201                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6202                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6203            !!!cp ('t429');
6204          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6205          ## Ignore the token          ## Ignore the token
6206          !!!next-token;          !!!next-token;
# Line 5684  sub _tree_construction_main ($) { Line 6218  sub _tree_construction_main ($) {
6218            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6219              ## Step 1              ## Step 1
6220              ## generate implied end tags              ## generate implied end tags
6221              if ({              while ({
6222                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6223                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6224                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6225                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6226                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6227              }              }
6228                    
6229              ## Step 2              ## Step 2
6230              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6231                  !!!cp ('t431');
6232                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6233                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6234                } else {
6235                  !!!cp ('t432');
6236              }              }
6237                            
6238              ## Step 3              ## Step 3
# Line 5712  sub _tree_construction_main ($) { Line 6246  sub _tree_construction_main ($) {
6246                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6247                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6248                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6249                  !!!cp ('t433');
6250                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6251                ## Ignore the token                ## Ignore the token
6252                !!!next-token;                !!!next-token;
6253                last S2;                last S2;
6254              }              }
6255    
6256                !!!cp ('t434');
6257            }            }
6258                        
6259            ## Step 4            ## Step 4
# Line 5732  sub _tree_construction_main ($) { Line 6269  sub _tree_construction_main ($) {
6269      redo B;      redo B;
6270    } # B    } # B
6271    
   ## 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  
   
6272    ## Stop parsing # MUST    ## Stop parsing # MUST
6273        
6274    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5778  sub set_inner_html ($$$) { Line 6310  sub set_inner_html ($$$) {
6310      my $p = $class->new;      my $p = $class->new;
6311      $p->{document} = $doc;      $p->{document} = $doc;
6312    
6313      ## Step 9 # MUST      ## Step 8 # MUST
6314      my $i = 0;      my $i = 0;
6315      my $line = 1;      my $line = 1;
6316      my $column = 0;      my $column = 0;
# Line 5795  sub set_inner_html ($$$) { Line 6327  sub set_inner_html ($$$) {
6327        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6328          $line++;          $line++;
6329          $column = 0;          $column = 0;
6330            !!!cp ('i1');
6331        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6332          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6333          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6334          $line++;          $line++;
6335          $column = 0;          $column = 0;
6336            !!!cp ('i2');
6337        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6338          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6339            !!!cp ('i3');
6340        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
6341            !!!cp ('i4');
6342          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6343          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6344        }        }
# Line 5841  sub set_inner_html ($$$) { Line 6377  sub set_inner_html ($$$) {
6377    
6378      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6379    
6380      ## Step 4      ## Step 3
6381      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6382        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6383    
6384      ## Step 5 # MUST      ## Step 4 # MUST
6385      $doc->append_child ($root);      $doc->append_child ($root);
6386    
6387      ## Step 6 # MUST      ## Step 5 # MUST
6388      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6389    
6390      undef $p->{head_element};      undef $p->{head_element};
6391    
6392      ## Step 7 # MUST      ## Step 6 # MUST
6393      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6394    
6395      ## Step 8 # MUST      ## Step 7 # MUST
6396      my $anode = $node;      my $anode = $node;
6397      AN: while (defined $anode) {      AN: while (defined $anode) {
6398        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6399          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6400          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6401            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
6402                !!!cp ('i5');
6403              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6404              last AN;              last AN;
6405            }            }
# Line 5871  sub set_inner_html ($$$) { Line 6408  sub set_inner_html ($$$) {
6408        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6409      } # AN      } # AN
6410            
6411      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6412      {      {
6413        my $self = $p;        my $self = $p;
6414        !!!next-token;        !!!next-token;
6415      }      }
6416      $p->_tree_construction_main;      $p->_tree_construction_main;
6417    
6418      ## Step 11 # MUST      ## Step 10 # MUST
6419      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6420      for (@cn) {      for (@cn) {
6421        $node->remove_child ($_);        $node->remove_child ($_);
6422      }      }
6423      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6424    
6425      ## Step 12 # MUST      ## Step 11 # MUST
6426      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6427      for (@cn) {      for (@cn) {
6428        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24