/[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.106 by wakaba, Sun Mar 9 10:31:19 2008 UTC
# Line 74  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 303  sub TABLE_IMS ()      { 0b1000000 }
303  sub ROW_IMS ()        { 0b10000000 }  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    sub SELECT_IMS ()     { 0b10000000000 }
307    
308    ## NOTE: "initial" and "before html" insertion modes have no constants.
309    
310    ## NOTE: "after after body" insertion mode.
311  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
312    
313    ## NOTE: "after after frameset" insertion mode.
314  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
315    
316  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
317  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
318  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 326  sub IN_TABLE_IM () { TABLE_IMS }
326  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
327  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
328  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
329  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
330    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
331  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
332    
333  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 656  sub _get_next_token ($) { Line 664  sub _get_next_token ($) {
664            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
665          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
666            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
667            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
668              !!!cp (36);            #  ## NOTE: This should never be reached.
669              !!!parse-error (type => 'end tag attribute');            #  !!! cp (36);
670            } else {            #  !!! parse-error (type => 'end tag attribute');
671              #} else {
672              !!!cp (37);              !!!cp (37);
673            }            #}
674          } else {          } else {
675            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
676          }          }
# Line 688  sub _get_next_token ($) { Line 697  sub _get_next_token ($) {
697            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
698          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
699            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
700            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
701              !!!cp (40);            #  ## NOTE: This state should never be reached.
702              !!!parse-error (type => 'end tag attribute');            #  !!! cp (40);
703            } else {            #  !!! parse-error (type => 'end tag attribute');
704              #} else {
705              !!!cp (41);              !!!cp (41);
706            }            #}
707          } else {          } else {
708            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
709          }          }
# Line 912  sub _get_next_token ($) { Line 922  sub _get_next_token ($) {
922              !!!cp (67);              !!!cp (67);
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            } else {            } else {
925                ## NOTE: This state should never be reached.
926              !!!cp (68);              !!!cp (68);
927            }            }
928          } else {          } else {
# Line 963  sub _get_next_token ($) { Line 974  sub _get_next_token ($) {
974              !!!cp (74);              !!!cp (74);
975              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
976            } else {            } else {
977                ## NOTE: This state should never be reached.
978              !!!cp (75);              !!!cp (75);
979            }            }
980          } else {          } else {
# Line 1011  sub _get_next_token ($) { Line 1023  sub _get_next_token ($) {
1023              !!!cp (80);              !!!cp (80);
1024              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1025            } else {            } else {
1026                ## NOTE: This state should never be reached.
1027              !!!cp (81);              !!!cp (81);
1028            }            }
1029          } else {          } else {
# Line 1067  sub _get_next_token ($) { Line 1080  sub _get_next_token ($) {
1080              !!!cp (88);              !!!cp (88);
1081              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1082            } else {            } else {
1083                ## NOTE: This state should never be reached.
1084              !!!cp (89);              !!!cp (89);
1085            }            }
1086          } else {          } else {
# Line 1091  sub _get_next_token ($) { Line 1105  sub _get_next_token ($) {
1105              !!!cp (91);              !!!cp (91);
1106              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1107            } else {            } else {
1108                ## NOTE: This state should never be reached.
1109              !!!cp (92);              !!!cp (92);
1110            }            }
1111          } else {          } else {
# Line 1139  sub _get_next_token ($) { Line 1154  sub _get_next_token ($) {
1154              !!!cp (98);              !!!cp (98);
1155              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1156            } else {            } else {
1157                ## NOTE: This state should never be reached.
1158              !!!cp (99);              !!!cp (99);
1159            }            }
1160          } else {          } else {
# Line 1182  sub _get_next_token ($) { Line 1198  sub _get_next_token ($) {
1198              !!!cp (104);              !!!cp (104);
1199              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1200            } else {            } else {
1201                ## NOTE: This state should never be reached.
1202              !!!cp (105);              !!!cp (105);
1203            }            }
1204          } else {          } else {
# Line 1228  sub _get_next_token ($) { Line 1245  sub _get_next_token ($) {
1245              !!!cp (110);              !!!cp (110);
1246              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1247            } else {            } else {
1248                ## NOTE: This state should never be reached.
1249              !!!cp (111);              !!!cp (111);
1250            }            }
1251          } else {          } else {
# Line 1252  sub _get_next_token ($) { Line 1270  sub _get_next_token ($) {
1270              !!!cp (113);              !!!cp (113);
1271              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1272            } else {            } else {
1273                ## NOTE: This state should never be reached.
1274              !!!cp (114);              !!!cp (114);
1275            }            }
1276          } else {          } else {
# Line 1323  sub _get_next_token ($) { Line 1342  sub _get_next_token ($) {
1342              !!!cp (120);              !!!cp (120);
1343              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1344            } else {            } else {
1345                ## NOTE: This state should never be reached.
1346              !!!cp (121);              !!!cp (121);
1347            }            }
1348          } else {          } else {
# Line 2190  sub _tokenize_attempt_to_consume_an_enti Line 2210  sub _tokenize_attempt_to_consume_an_enti
2210         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2211         $additional => 1,         $additional => 1,
2212        }->{$self->{next_char}}) {        }->{$self->{next_char}}) {
2213        !!!cp (1001);
2214      ## Don't consume      ## Don't consume
2215      ## No error      ## No error
2216      return undef;      return undef;
# Line 2203  sub _tokenize_attempt_to_consume_an_enti Line 2224  sub _tokenize_attempt_to_consume_an_enti
2224          !!!next-input-character;          !!!next-input-character;
2225          if (0x0030 <= $self->{next_char} and          if (0x0030 <= $self->{next_char} and
2226              $self->{next_char} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2227              !!!cp (1002);
2228            $code ||= 0;            $code ||= 0;
2229            $code *= 0x10;            $code *= 0x10;
2230            $code += $self->{next_char} - 0x0030;            $code += $self->{next_char} - 0x0030;
2231            redo X;            redo X;
2232          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{next_char} and
2233                   $self->{next_char} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2234              !!!cp (1003);
2235            $code ||= 0;            $code ||= 0;
2236            $code *= 0x10;            $code *= 0x10;
2237            $code += $self->{next_char} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2238            redo X;            redo X;
2239          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{next_char} and
2240                   $self->{next_char} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2241              !!!cp (1004);
2242            $code ||= 0;            $code ||= 0;
2243            $code *= 0x10;            $code *= 0x10;
2244            $code += $self->{next_char} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2245            redo X;            redo X;
2246          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2247              !!!cp (1005);
2248            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2249            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2250            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2251            return undef;            return undef;
2252          } elsif ($self->{next_char} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2253              !!!cp (1006);
2254            !!!next-input-character;            !!!next-input-character;
2255          } else {          } else {
2256              !!!cp (1007);
2257            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2258          }          }
2259    
2260          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2261              !!!cp (1008);
2262            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2263            $code = 0xFFFD;            $code = 0xFFFD;
2264          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2265              !!!cp (1009);
2266            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2267            $code = 0xFFFD;            $code = 0xFFFD;
2268          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2269              !!!cp (1010);
2270            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2271            $code = 0x000A;            $code = 0x000A;
2272          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2273              !!!cp (1011);
2274            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2275            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2276          }          }
# Line 2254  sub _tokenize_attempt_to_consume_an_enti Line 2285  sub _tokenize_attempt_to_consume_an_enti
2285                
2286        while (0x0030 <= $self->{next_char} and        while (0x0030 <= $self->{next_char} and
2287                  $self->{next_char} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2288            !!!cp (1012);
2289          $code *= 10;          $code *= 10;
2290          $code += $self->{next_char} - 0x0030;          $code += $self->{next_char} - 0x0030;
2291                    
# Line 2261  sub _tokenize_attempt_to_consume_an_enti Line 2293  sub _tokenize_attempt_to_consume_an_enti
2293        }        }
2294    
2295        if ($self->{next_char} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2296            !!!cp (1013);
2297          !!!next-input-character;          !!!next-input-character;
2298        } else {        } else {
2299            !!!cp (1014);
2300          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2301        }        }
2302    
2303        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2304            !!!cp (1015);
2305          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2306          $code = 0xFFFD;          $code = 0xFFFD;
2307        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2308            !!!cp (1016);
2309          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2310          $code = 0xFFFD;          $code = 0xFFFD;
2311        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2312            !!!cp (1017);
2313          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2314          $code = 0x000A;          $code = 0x000A;
2315        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2316            !!!cp (1018);
2317          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2318          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2319        }        }
2320                
2321        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2322      } else {      } else {
2323          !!!cp (1019);
2324        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2325        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2326        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
# Line 2311  sub _tokenize_attempt_to_consume_an_enti Line 2350  sub _tokenize_attempt_to_consume_an_enti
2350        $entity_name .= chr $self->{next_char};        $entity_name .= chr $self->{next_char};
2351        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2352          if ($self->{next_char} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2353              !!!cp (1020);
2354            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2355            $match = 1;            $match = 1;
2356            !!!next-input-character;            !!!next-input-character;
2357            last;            last;
2358          } else {          } else {
2359              !!!cp (1021);
2360            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2361            $match = -1;            $match = -1;
2362            !!!next-input-character;            !!!next-input-character;
2363          }          }
2364        } else {        } else {
2365            !!!cp (1022);
2366          $value .= chr $self->{next_char};          $value .= chr $self->{next_char};
2367          $match *= 2;          $match *= 2;
2368          !!!next-input-character;          !!!next-input-character;
# Line 2328  sub _tokenize_attempt_to_consume_an_enti Line 2370  sub _tokenize_attempt_to_consume_an_enti
2370      }      }
2371            
2372      if ($match > 0) {      if ($match > 0) {
2373          !!!cp (1023);
2374        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2375      } elsif ($match < 0) {      } elsif ($match < 0) {
2376        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2377        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2378            !!!cp (1024);
2379          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2380        } else {        } else {
2381            !!!cp (1025);
2382          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2383        }        }
2384      } else {      } else {
2385          !!!cp (1026);
2386        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2387        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2388        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2389      }      }
2390    } else {    } else {
2391        !!!cp (1027);
2392      ## no characters are consumed      ## no characters are consumed
2393      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2394      return undef;      return undef;
# Line 2381  sub _construct_tree ($) { Line 2428  sub _construct_tree ($) {
2428        
2429    !!!next-token;    !!!next-token;
2430    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2431    undef $self->{form_element};    undef $self->{form_element};
2432    undef $self->{head_element};    undef $self->{head_element};
2433    $self->{open_elements} = [];    $self->{open_elements} = [];
2434    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2435    
2436      ## NOTE: The "initial" insertion mode.
2437    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2438    
2439      ## NOTE: The "before html" insertion mode.
2440    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2441      $self->{insertion_mode} = BEFORE_HEAD_IM;
2442    
2443      ## NOTE: The "before head" insertion mode and so on.
2444    $self->_tree_construction_main;    $self->_tree_construction_main;
2445  } # _construct_tree  } # _construct_tree
2446    
2447  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2448    my $self = shift;    my $self = shift;
2449    
2450      ## NOTE: "initial" insertion mode
2451    
2452    INITIAL: {    INITIAL: {
2453      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2454        ## 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 2460  sub _tree_construction_initial ($) {
2460        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2461            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2462            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2463            !!!cp ('t1');
2464          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2465        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2466            !!!cp ('t2');
2467          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2468          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2469          } else {
2470            !!!cp ('t3');
2471        }        }
2472                
2473        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 2422  sub _tree_construction_initial ($) { Line 2481  sub _tree_construction_initial ($) {
2481        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2482                
2483        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2484            !!!cp ('t4');
2485          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2486        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2487          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2500  sub _tree_construction_initial ($) { Line 2560  sub _tree_construction_initial ($) {
2560            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2561            "HTML" => 1,            "HTML" => 1,
2562          }->{$pubid}) {          }->{$pubid}) {
2563              !!!cp ('t5');
2564            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2565          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2566                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2567            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2568                !!!cp ('t6');
2569              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2570            } else {            } else {
2571                !!!cp ('t7');
2572              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2573            }            }
2574          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2575                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2576              !!!cp ('t8');
2577            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2578            } else {
2579              !!!cp ('t9');
2580          }          }
2581          } else {
2582            !!!cp ('t10');
2583        }        }
2584        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2585          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2586          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2587          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") {
2588              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2589            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2590              !!!cp ('t11');
2591            } else {
2592              !!!cp ('t12');
2593          }          }
2594          } else {
2595            !!!cp ('t13');
2596        }        }
2597                
2598        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2599        !!!next-token;        !!!next-token;
2600        return;        return;
2601      } elsif ({      } elsif ({
# Line 2529  sub _tree_construction_initial ($) { Line 2603  sub _tree_construction_initial ($) {
2603                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2604                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2605               }->{$token->{type}}) {               }->{$token->{type}}) {
2606          !!!cp ('t14');
2607        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2608        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2609        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2610        ## reprocess        ## reprocess
2611        return;        return;
2612      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2539  sub _tree_construction_initial ($) { Line 2614  sub _tree_construction_initial ($) {
2614          ## Ignore the token          ## Ignore the token
2615    
2616          unless (length $token->{data}) {          unless (length $token->{data}) {
2617            ## Stay in the phase            !!!cp ('t15');
2618              ## Stay in the insertion mode.
2619            !!!next-token;            !!!next-token;
2620            redo INITIAL;            redo INITIAL;
2621            } else {
2622              !!!cp ('t16');
2623          }          }
2624          } else {
2625            !!!cp ('t17');
2626        }        }
2627    
2628        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2629        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2630        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2631        ## reprocess        ## reprocess
2632        return;        return;
2633      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2634          !!!cp ('t18');
2635        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2636        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2637                
2638        ## Stay in the phase.        ## Stay in the insertion mode.
2639        !!!next-token;        !!!next-token;
2640        redo INITIAL;        redo INITIAL;
2641      } else {      } else {
2642        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2643      }      }
2644    } # INITIAL    } # INITIAL
2645    
2646      die "$0: _tree_construction_initial: This should be never reached";
2647  } # _tree_construction_initial  } # _tree_construction_initial
2648    
2649  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2650    my $self = shift;    my $self = shift;
2651    
2652      ## NOTE: "before html" insertion mode.
2653        
2654    B: {    B: {
2655        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2656            !!!cp ('t19');
2657          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2658          ## Ignore the token          ## Ignore the token
2659          ## Stay in the phase          ## Stay in the insertion mode.
2660          !!!next-token;          !!!next-token;
2661          redo B;          redo B;
2662        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2663            !!!cp ('t20');
2664          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2665          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2666          ## Stay in the phase          ## Stay in the insertion mode.
2667          !!!next-token;          !!!next-token;
2668          redo B;          redo B;
2669        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2584  sub _tree_construction_root_element ($) Line 2671  sub _tree_construction_root_element ($)
2671            ## Ignore the token.            ## Ignore the token.
2672    
2673            unless (length $token->{data}) {            unless (length $token->{data}) {
2674              ## Stay in the phase              !!!cp ('t21');
2675                ## Stay in the insertion mode.
2676              !!!next-token;              !!!next-token;
2677              redo B;              redo B;
2678              } else {
2679                !!!cp ('t22');
2680            }            }
2681            } else {
2682              !!!cp ('t23');
2683          }          }
2684    
2685          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2686    
2687          #          #
2688        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2689          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2690              $token->{attributes}->{manifest}) {            my $root_element;
2691            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2692                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2693            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}}, [$root_element, 'html'];
2694    
2695              if ($token->{attributes}->{manifest}) {
2696                !!!cp ('t24');
2697                $self->{application_cache_selection}
2698                    ->($token->{attributes}->{manifest}->{value});
2699                ## ISSUE: No relative reference resolution?
2700              } else {
2701                !!!cp ('t25');
2702                $self->{application_cache_selection}->(undef);
2703              }
2704    
2705              !!!next-token;
2706              return; ## Go to the "before head" insertion mode.
2707          } else {          } else {
2708            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2709              #
2710          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2711        } elsif ({        } elsif ({
2712                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2713                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2714                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2715          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2716          #          #
2717        } else {        } else {
2718          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2719        }        }
2720    
2721        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html');
2722        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2723        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2724        ## reprocess  
2725        #redo B;      $self->{application_cache_selection}->(undef);
2726        return; ## Go to the main phase.  
2727        ## NOTE: Reprocess the token.
2728        return; ## Go to the "before head" insertion mode.
2729    
2730        ## ISSUE: There is an issue in the spec
2731    } # B    } # B
2732    
2733      die "$0: _tree_construction_root_element: This should never be reached";
2734  } # _tree_construction_root_element  } # _tree_construction_root_element
2735    
2736  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2638  sub _reset_insertion_mode ($) { Line 2745  sub _reset_insertion_mode ($) {
2745            
2746      ## Step 3      ## Step 3
2747      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
2748        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2749          $last = 1;          $last = 1;
2750          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2751            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2752                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2753                !!!cp ('t27');
2754              #              #
2755            } else {            } else {
2756                !!!cp ('t28');
2757              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2758            }            }
2759          }          }
# Line 2658  sub _reset_insertion_mode ($) { Line 2762  sub _reset_insertion_mode ($) {
2762        ## Step 4..13        ## Step 4..13
2763        my $new_mode = {        my $new_mode = {
2764                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2765                          ## NOTE: |option| and |optgroup| do not set
2766                          ## insertion mode to "in select" by themselves.
2767                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2768                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2769                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2676  sub _reset_insertion_mode ($) { Line 2782  sub _reset_insertion_mode ($) {
2782        ## Step 14        ## Step 14
2783        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2784          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2785              !!!cp ('t29');
2786            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2787          } else {          } else {
2788              ## ISSUE: Can this state be reached?
2789              !!!cp ('t30');
2790            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2791          }          }
2792          return;          return;
2793          } else {
2794            !!!cp ('t31');
2795        }        }
2796                
2797        ## Step 15        ## Step 15
# Line 2693  sub _reset_insertion_mode ($) { Line 2804  sub _reset_insertion_mode ($) {
2804        ## Step 17        ## Step 17
2805        redo S3;        redo S3;
2806      } # S3      } # S3
2807    
2808      die "$0: _reset_insertion_mode: This line should never be reached";
2809  } # _reset_insertion_mode  } # _reset_insertion_mode
2810    
2811  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2714  sub _tree_construction_main ($) { Line 2827  sub _tree_construction_main ($) {
2827      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2828      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2829        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2830            !!!cp ('t32');
2831          return;          return;
2832        }        }
2833      }      }
# Line 2728  sub _tree_construction_main ($) { Line 2842  sub _tree_construction_main ($) {
2842    
2843        ## Step 6        ## Step 6
2844        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2845            !!!cp ('t33_1');
2846          #          #
2847        } else {        } else {
2848          my $in_open_elements;          my $in_open_elements;
2849          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2850            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2851                !!!cp ('t33');
2852              $in_open_elements = 1;              $in_open_elements = 1;
2853              last OE;              last OE;
2854            }            }
2855          }          }
2856          if ($in_open_elements) {          if ($in_open_elements) {
2857              !!!cp ('t34');
2858            #            #
2859          } else {          } else {
2860              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2861              !!!cp ('t35');
2862            redo S4;            redo S4;
2863          }          }
2864        }        }
# Line 2762  sub _tree_construction_main ($) { Line 2881  sub _tree_construction_main ($) {
2881    
2882        ## Step 11        ## Step 11
2883        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2884            !!!cp ('t36');
2885          ## Step 7'          ## Step 7'
2886          $i++;          $i++;
2887          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2888                    
2889          redo S7;          redo S7;
2890        }        }
2891    
2892          !!!cp ('t37');
2893      } # S7      } # S7
2894    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2895    
2896    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2897      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2898        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2899            !!!cp ('t38');
2900          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2901          return;          return;
2902        }        }
2903      }      }
2904    
2905        !!!cp ('t39');
2906    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2907    
2908    my $parse_rcdata = sub ($$) {    my $insert;
2909      my ($content_model_flag, $insert) = @_;  
2910      my $parse_rcdata = sub ($) {
2911        my ($content_model_flag) = @_;
2912    
2913      ## Step 1      ## Step 1
2914      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
# Line 2789  sub _tree_construction_main ($) { Line 2916  sub _tree_construction_main ($) {
2916      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes});
2917    
2918      ## Step 2      ## Step 2
2919      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2920    
2921      ## Step 3      ## Step 3
2922      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2799  sub _tree_construction_main ($) { Line 2926  sub _tree_construction_main ($) {
2926      my $text = '';      my $text = '';
2927      !!!next-token;      !!!next-token;
2928      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2929          !!!cp ('t40');
2930        $text .= $token->{data};        $text .= $token->{data};
2931        !!!next-token;        !!!next-token;
2932      }      }
2933    
2934      ## Step 5      ## Step 5
2935      if (length $text) {      if (length $text) {
2936          !!!cp ('t41');
2937        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2938        $el->append_child ($text);        $el->append_child ($text);
2939      }      }
# Line 2813  sub _tree_construction_main ($) { Line 2942  sub _tree_construction_main ($) {
2942      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2943    
2944      ## Step 7      ## Step 7
2945      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2946            $token->{tag_name} eq $start_tag_name) {
2947          !!!cp ('t42');
2948        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
2949      } else {      } else {
2950        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
2951          if ($content_model_flag == CDATA_CONTENT_MODEL) {
2952            !!!cp ('t43');
2953            !!!parse-error (type => 'in CDATA:#'.$token->{type});
2954          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2955            !!!cp ('t44');
2956            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2957          } else {
2958            die "$0: $content_model_flag in parse_rcdata";
2959          }
2960      }      }
2961      !!!next-token;      !!!next-token;
2962    }; # $parse_rcdata    }; # $parse_rcdata
2963    
2964    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
2965      my $script_el;      my $script_el;
2966      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2967      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
# Line 2837  sub _tree_construction_main ($) { Line 2972  sub _tree_construction_main ($) {
2972      my $text = '';      my $text = '';
2973      !!!next-token;      !!!next-token;
2974      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
2975          !!!cp ('t45');
2976        $text .= $token->{data};        $text .= $token->{data};
2977        !!!next-token;        !!!next-token;
2978      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2979      if (length $text) {      if (length $text) {
2980          !!!cp ('t46');
2981        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2982      }      }
2983                                
# Line 2848  sub _tree_construction_main ($) { Line 2985  sub _tree_construction_main ($) {
2985    
2986      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
2987          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2988          !!!cp ('t47');
2989        ## Ignore the token        ## Ignore the token
2990      } else {      } else {
2991          !!!cp ('t48');
2992        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2993        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2994        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2995      }      }
2996            
2997      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2998          !!!cp ('t49');
2999        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3000      } else {      } else {
3001          !!!cp ('t50');
3002        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3003        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3004    
# Line 2871  sub _tree_construction_main ($) { Line 3012  sub _tree_construction_main ($) {
3012      !!!next-token;      !!!next-token;
3013    }; # $script_start_tag    }; # $script_start_tag
3014    
3015      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3016      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3017      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3018    
3019    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3020      my $tag_name = shift;      my $tag_name = shift;
3021    
3022        ## NOTE: The adoption agency algorithm (AAA).
3023    
3024      FET: {      FET: {
3025        ## Step 1        ## Step 1
3026        my $formatting_element;        my $formatting_element;
3027        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3028        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3029          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3030              !!!cp ('t51');
3031            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3032            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3033            last AFE;            last AFE;
3034          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3035              !!!cp ('t52');
3036            last AFE;            last AFE;
3037          }          }
3038        } # AFE        } # AFE
3039        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3040            !!!cp ('t53');
3041          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3042          ## Ignore the token          ## Ignore the token
3043          !!!next-token;          !!!next-token;
# Line 2900  sub _tree_construction_main ($) { Line 3050  sub _tree_construction_main ($) {
3050          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3051          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3052            if ($in_scope) {            if ($in_scope) {
3053                !!!cp ('t54');
3054              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3055              last INSCOPE;              last INSCOPE;
3056            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3057                !!!cp ('t55');
3058              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3059              ## Ignore the token              ## Ignore the token
3060              !!!next-token;              !!!next-token;
3061              return;              return;
3062            }            }
3063          } elsif ({          } elsif ({
3064                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3065                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3066                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3067              !!!cp ('t56');
3068            $in_scope = 0;            $in_scope = 0;
3069          }          }
3070        } # INSCOPE        } # INSCOPE
3071        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3072            !!!cp ('t57');
3073          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3074          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3075          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3076          return;          return;
3077        }        }
3078        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3079            !!!cp ('t58');
3080          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3081        }        }
3082                
# Line 2933  sub _tree_construction_main ($) { Line 3088  sub _tree_construction_main ($) {
3088          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3089              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3090              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3091               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3092              !!!cp ('t59');
3093            $furthest_block = $node;            $furthest_block = $node;
3094            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3095          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3096              !!!cp ('t60');
3097            last OE;            last OE;
3098          }          }
3099        } # OE        } # OE
3100                
3101        ## Step 3        ## Step 3
3102        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3103            !!!cp ('t61');
3104          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3105          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3106          !!!next-token;          !!!next-token;
# Line 2955  sub _tree_construction_main ($) { Line 3113  sub _tree_construction_main ($) {
3113        ## Step 5        ## Step 5
3114        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3115        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3116            !!!cp ('t62');
3117          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3118        }        }
3119                
# Line 2977  sub _tree_construction_main ($) { Line 3136  sub _tree_construction_main ($) {
3136          S7S2: {          S7S2: {
3137            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3138              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3139                  !!!cp ('t63');
3140                $node_i_in_active = $_;                $node_i_in_active = $_;
3141                last S7S2;                last S7S2;
3142              }              }
# Line 2990  sub _tree_construction_main ($) { Line 3150  sub _tree_construction_main ($) {
3150                    
3151          ## Step 4          ## Step 4
3152          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3153              !!!cp ('t64');
3154            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3155          }          }
3156                    
3157          ## Step 5          ## Step 5
3158          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3159              !!!cp ('t65');
3160            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3161            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3162            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3012  sub _tree_construction_main ($) { Line 3174  sub _tree_construction_main ($) {
3174        } # S7          } # S7  
3175                
3176        ## Step 8        ## Step 8
3177        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3178               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3179              }->{$common_ancestor_node->[1]}) {
3180            my $foster_parent_element;
3181            my $next_sibling;
3182                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3183                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3184                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3185                                 if (defined $parent and $parent->node_type == 1) {
3186                                   !!!cp ('t65.1');
3187                                   $foster_parent_element = $parent;
3188                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3189                                 } else {
3190                                   !!!cp ('t65.2');
3191                                   $foster_parent_element
3192                                     = $self->{open_elements}->[$_ - 1]->[0];
3193                                 }
3194                                 last OE;
3195                               }
3196                             } # OE
3197                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3198                               unless defined $foster_parent_element;
3199            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3200            $open_tables->[-1]->[1] = 1; # tainted
3201          } else {
3202            !!!cp ('t65.3');
3203            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3204          }
3205                
3206        ## Step 9        ## Step 9
3207        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3029  sub _tree_construction_main ($) { Line 3218  sub _tree_construction_main ($) {
3218        my $i;        my $i;
3219        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3220          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3221              !!!cp ('t66');
3222            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3223            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3224          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3225              !!!cp ('t67');
3226            $i = $_;            $i = $_;
3227          }          }
3228        } # AFE        } # AFE
# Line 3041  sub _tree_construction_main ($) { Line 3232  sub _tree_construction_main ($) {
3232        undef $i;        undef $i;
3233        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3234          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3235              !!!cp ('t68');
3236            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3237            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3238          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3239              !!!cp ('t69');
3240            $i = $_;            $i = $_;
3241          }          }
3242        } # OE        } # OE
# Line 3054  sub _tree_construction_main ($) { Line 3247  sub _tree_construction_main ($) {
3247      } # FET      } # FET
3248    }; # $formatting_end_tag    }; # $formatting_end_tag
3249    
3250    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3251      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3252    }; # $insert_to_current    }; # $insert_to_current
3253    
3254    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3255                         my $child = shift;      my $child = shift;
3256                         if ({      if ({
3257                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3258                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3259                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3260                           # MUST        my $foster_parent_element;
3261                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3262                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3263                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3264                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3265                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3266                                   !!!cp ('t70');
3267                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3268                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3269                               } else {                               } else {
3270                                   !!!cp ('t71');
3271                                 $foster_parent_element                                 $foster_parent_element
3272                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3273                               }                               }
# Line 3084  sub _tree_construction_main ($) { Line 3278  sub _tree_construction_main ($) {
3278                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3279                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3280                             ($child, $next_sibling);                             ($child, $next_sibling);
3281                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3282                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3283                         }        !!!cp ('t72');
3284          $self->{open_elements}->[-1]->[0]->append_child ($child);
3285        }
3286    }; # $insert_to_foster    }; # $insert_to_foster
3287    
   my $insert;  
   
3288    B: {    B: {
3289      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3290          !!!cp ('t73');
3291        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3292        ## Ignore the token        ## Ignore the token
3293        ## Stay in the phase        ## Stay in the phase
3294        !!!next-token;        !!!next-token;
3295        redo B;        redo B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
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 3167  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3338        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
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);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3341                !!!cp ('t88.2');
3342                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3343              } else {
3344                !!!cp ('t88.1');
3345                ## Ignore the token.
3346                !!!next-token;
3347                redo B;
3348              }
3349            unless (length $token->{data}) {            unless (length $token->{data}) {
3350                !!!cp ('t88');
3351              !!!next-token;              !!!next-token;
3352              redo B;              redo B;
3353            }            }
3354          }          }
3355    
3356          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3357              !!!cp ('t89');
3358            ## As if <head>            ## As if <head>
3359            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
3360            $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 3365  sub _tree_construction_main ($) {
3365    
3366            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3367          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3368              !!!cp ('t90');
3369            ## As if </noscript>            ## As if </noscript>
3370            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3371            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 3195  sub _tree_construction_main ($) { Line 3376  sub _tree_construction_main ($) {
3376    
3377            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3378          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3379              !!!cp ('t91');
3380            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3381    
3382            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3383            } else {
3384              !!!cp ('t92');
3385          }          }
3386    
3387              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3209  sub _tree_construction_main ($) { Line 3393  sub _tree_construction_main ($) {
3393            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3394              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3395                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3396                    !!!cp ('t93');
3397                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3398                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3399                  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 3401  sub _tree_construction_main ($) {
3401                  !!!next-token;                  !!!next-token;
3402                  redo B;                  redo B;
3403                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3404                    !!!cp ('t94');
3405                  #                  #
3406                } else {                } else {
3407                    !!!cp ('t95');
3408                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3409                  ## Ignore the token                  ## Ignore the token
3410                  !!!next-token;                  !!!next-token;
3411                  redo B;                  redo B;
3412                }                }
3413              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3414                  !!!cp ('t96');
3415                ## As if <head>                ## As if <head>
3416                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3417                $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 3419  sub _tree_construction_main ($) {
3419    
3420                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3421                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3422                } else {
3423                  !!!cp ('t97');
3424              }              }
3425    
3426              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3427                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3428                    !!!cp ('t98');
3429                  ## As if </noscript>                  ## As if </noscript>
3430                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3431                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
3432                                
3433                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3434                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3435                  } else {
3436                    !!!cp ('t99');
3437                }                }
3438    
3439                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3440                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3441                    !!!cp ('t100');
3442                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3443                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3444                  } else {
3445                    !!!cp ('t101');
3446                }                }
3447                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3448                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3449                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3450                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3451                !!!next-token;                !!!next-token;
3452                redo B;                redo B;
3453              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3454                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3455                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3456                    !!!cp ('t102');
3457                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3458                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3459                  } else {
3460                    !!!cp ('t103');
3461                }                }
3462                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3463                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3464                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3465                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3466                !!!next-token;                !!!next-token;
3467                redo B;                redo B;
3468              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3469                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3470                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3471                    !!!cp ('t104');
3472                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3473                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3474                  } else {
3475                    !!!cp ('t105');
3476                }                }
3477                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3478                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.
3479    
3480                unless ($self->{confident}) {                unless ($self->{confident}) {
3481                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3482                      !!!cp ('t106');
3483                    $self->{change_encoding}                    $self->{change_encoding}
3484                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value});
3485                                        
# Line 3291  sub _tree_construction_main ($) { Line 3494  sub _tree_construction_main ($) {
3494                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
3495                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3496                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3497                        !!!cp ('t107');
3498                      $self->{change_encoding}                      $self->{change_encoding}
3499                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3500                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3501                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3502                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
3503                                                     ->{has_reference});                                                     ->{has_reference});
3504                      } else {
3505                        !!!cp ('t108');
3506                    }                    }
3507                  }                  }
3508                } else {                } else {
3509                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
3510                      !!!cp ('t109');
3511                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3512                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3513                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
3514                                                 ->{has_reference});                                                 ->{has_reference});
3515                  }                  }
3516                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
3517                      !!!cp ('t110');
3518                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3519                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3520                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3314  sub _tree_construction_main ($) { Line 3522  sub _tree_construction_main ($) {
3522                  }                  }
3523                }                }
3524    
3525                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3526                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3527                !!!next-token;                !!!next-token;
3528                redo B;                redo B;
3529              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3530                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3531                    !!!cp ('t111');
3532                  ## As if </noscript>                  ## As if </noscript>
3533                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3534                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
# Line 3327  sub _tree_construction_main ($) { Line 3536  sub _tree_construction_main ($) {
3536                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3537                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3538                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3539                    !!!cp ('t112');
3540                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3541                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3542                  } else {
3543                    !!!cp ('t113');
3544                }                }
3545    
3546                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3547                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3548                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3549                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3550                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3551                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3552                redo B;                redo B;
3553              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3344  sub _tree_construction_main ($) { Line 3555  sub _tree_construction_main ($) {
3555                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3556                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3557                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3558                    !!!cp ('t114');
3559                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3560                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3561                  } else {
3562                    !!!cp ('t115');
3563                }                }
3564                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3565                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3566                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3567                redo B;                redo B;
3568              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3569                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3570                    !!!cp ('t116');
3571                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3572                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3573                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3574                  !!!next-token;                  !!!next-token;
3575                  redo B;                  redo B;
3576                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3577                    !!!cp ('t117');
3578                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3579                  ## Ignore the token                  ## Ignore the token
3580                  !!!next-token;                  !!!next-token;
3581                  redo B;                  redo B;
3582                } else {                } else {
3583                    !!!cp ('t118');
3584                  #                  #
3585                }                }
3586              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3587                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3588                    !!!cp ('t119');
3589                  ## As if </noscript>                  ## As if </noscript>
3590                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3591                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
# Line 3375  sub _tree_construction_main ($) { Line 3593  sub _tree_construction_main ($) {
3593                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3594                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3595                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3596                    !!!cp ('t120');
3597                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3598                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3599                  } else {
3600                    !!!cp ('t121');
3601                }                }
3602    
3603                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3604                $script_start_tag->($insert_to_current);                $script_start_tag->();
3605                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3606                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3607                redo B;                redo B;
3608              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3609                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3610                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3611                    !!!cp ('t122');
3612                  ## As if </noscript>                  ## As if </noscript>
3613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3614                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3397  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619                                    
3620                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3621                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3622                    !!!cp ('t124');
3623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3624                                    
3625                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3626                  } else {
3627                    !!!cp ('t125');
3628                }                }
3629    
3630                ## "after head" insertion mode                ## "after head" insertion mode
3631                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3632                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3633                    !!!cp ('t126');
3634                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3635                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3636                    !!!cp ('t127');
3637                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3638                } else {                } else {
3639                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 3414  sub _tree_construction_main ($) { Line 3641  sub _tree_construction_main ($) {
3641                !!!next-token;                !!!next-token;
3642                redo B;                redo B;
3643              } else {              } else {
3644                  !!!cp ('t128');
3645                #                #
3646              }              }
3647    
3648              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3649                  !!!cp ('t129');
3650                ## As if </noscript>                ## As if </noscript>
3651                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3652                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3428  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657    
3658                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3659              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3660                  !!!cp ('t130');
3661                ## As if </head>                ## As if </head>
3662                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3663    
3664                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3665                } else {
3666                  !!!cp ('t131');
3667              }              }
3668    
3669              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3443  sub _tree_construction_main ($) { Line 3675  sub _tree_construction_main ($) {
3675            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3676              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3677                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3678                    !!!cp ('t132');
3679                  ## As if <head>                  ## As if <head>
3680                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3681                  $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 3687  sub _tree_construction_main ($) {
3687                  !!!next-token;                  !!!next-token;
3688                  redo B;                  redo B;
3689                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3690                    !!!cp ('t133');
3691                  ## As if </noscript>                  ## As if </noscript>
3692                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3693                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head');
3694                                    
3695                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3696                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3464  sub _tree_construction_main ($) { Line 3698  sub _tree_construction_main ($) {
3698                  !!!next-token;                  !!!next-token;
3699                  redo B;                  redo B;
3700                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3701                    !!!cp ('t134');
3702                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3703                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3704                  !!!next-token;                  !!!next-token;
3705                  redo B;                  redo B;
3706                } else {                } else {
3707                    !!!cp ('t135');
3708                  #                  #
3709                }                }
3710              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3711                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3712                    !!!cp ('t136');
3713                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3714                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3715                  !!!next-token;                  !!!next-token;
3716                  redo B;                  redo B;
3717                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3718                    !!!cp ('t137');
3719                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3720                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3721                  !!!next-token;                  !!!next-token;
3722                  redo B;                  redo B;
3723                } else {                } else {
3724                    !!!cp ('t138');
3725                  #                  #
3726                }                }
3727              } elsif ({              } elsif ({
3728                        body => 1, html => 1,                        body => 1, html => 1,
3729                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3730                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3731                    !!!cp ('t139');
3732                  ## As if <head>                  ## As if <head>
3733                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3734                  $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 3737  sub _tree_construction_main ($) {
3737                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3738                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3739                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3740                    !!!cp ('t140');
3741                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3742                  ## Ignore the token                  ## Ignore the token
3743                  !!!next-token;                  !!!next-token;
3744                  redo B;                  redo B;
3745                  } else {
3746                    !!!cp ('t141');
3747                }                }
3748                                
3749                #                #
# Line 3508  sub _tree_construction_main ($) { Line 3751  sub _tree_construction_main ($) {
3751                        p => 1, br => 1,                        p => 1, br => 1,
3752                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3753                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3754                    !!!cp ('t142');
3755                  ## As if <head>                  ## As if <head>
3756                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3757                  $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 3759  sub _tree_construction_main ($) {
3759    
3760                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3761                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3762                  } else {
3763                    !!!cp ('t143');
3764                }                }
3765    
3766                #                #
3767              } else {              } else {
3768                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3769                    !!!cp ('t144');
3770                  #                  #
3771                } else {                } else {
3772                    !!!cp ('t145');
3773                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3774                  ## Ignore the token                  ## Ignore the token
3775                  !!!next-token;                  !!!next-token;
# Line 3530  sub _tree_construction_main ($) { Line 3778  sub _tree_construction_main ($) {
3778              }              }
3779    
3780              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3781                  !!!cp ('t146');
3782                ## As if </noscript>                ## As if </noscript>
3783                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3784                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3540  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789    
3790                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3791              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3792                  !!!cp ('t147');
3793                ## As if </head>                ## As if </head>
3794                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3795    
3796                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3797              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3798    ## ISSUE: This case cannot be reached?
3799                  !!!cp ('t148');
3800                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3801                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3802                !!!next-token;                !!!next-token;
3803                redo B;                redo B;
3804                } else {
3805                  !!!cp ('t149');
3806              }              }
3807    
3808              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3557  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3812              ## reprocess              ## reprocess
3813              redo B;              redo B;
3814            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3815              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3816            }            !!!cp ('t149.1');
3817    
3818              ## NOTE: As if <head>
3819              !!!create-element ($self->{head_element}, 'head');
3820              $self->{open_elements}->[-1]->[0]->append_child
3821                  ($self->{head_element});
3822              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3823              #$self->{insertion_mode} = IN_HEAD_IM;
3824              ## NOTE: Reprocess.
3825    
3826              ## NOTE: As if </head>
3827              #pop @{$self->{open_elements}};
3828              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3829              ## NOTE: Reprocess.
3830              
3831              #
3832            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3833              !!!cp ('t149.2');
3834    
3835              ## NOTE: As if </head>
3836              pop @{$self->{open_elements}};
3837              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3838              ## NOTE: Reprocess.
3839    
3840              #
3841            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3842              !!!cp ('t149.3');
3843    
3844              !!!parse-error (type => 'in noscript:#eof');
3845    
3846              ## As if </noscript>
3847              pop @{$self->{open_elements}};
3848              #$self->{insertion_mode} = IN_HEAD_IM;
3849              ## NOTE: Reprocess.
3850    
3851              ## NOTE: As if </head>
3852              pop @{$self->{open_elements}};
3853              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3854              ## NOTE: Reprocess.
3855    
3856              #
3857            } else {
3858              !!!cp ('t149.4');
3859              #
3860            }
3861    
3862            ## NOTE: As if <body>
3863            !!!insert-element ('body');
3864            $self->{insertion_mode} = IN_BODY_IM;
3865            ## NOTE: Reprocess.
3866            redo B;
3867          } else {
3868            die "$0: $token->{type}: Unknown token type";
3869          }
3870    
3871            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3872      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3873            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3874                !!!cp ('t150');
3875              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3876              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3877                            
# Line 3582  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3891                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3892                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3893                        !!!cp ('t151');
3894                      $tn = $node->[1];                      $tn = $node->[1];
3895                      last INSCOPE;                      last INSCOPE;
3896                    } elsif ({                    } elsif ({
3897                              table => 1, html => 1,                              table => 1, html => 1,
3898                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3899                        !!!cp ('t152');
3900                      last INSCOPE;                      last INSCOPE;
3901                    }                    }
3902                  } # INSCOPE                  } # INSCOPE
3903                    unless (defined $tn) {                    unless (defined $tn) {
3904                        !!!cp ('t153');
3905    ## TODO: This error type is wrong.
3906                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3907                      ## Ignore the token                      ## Ignore the token
3908                      !!!next-token;                      !!!next-token;
3909                      redo B;                      redo B;
3910                    }                    }
3911                                    
3912                    !!!cp ('t154');
3913                  ## Close the cell                  ## Close the cell
3914                  !!!back-token; # <?>                  !!!back-token; # <?>
3915                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
# Line 3610  sub _tree_construction_main ($) { Line 3923  sub _tree_construction_main ($) {
3923                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3924                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3925                    if ($node->[1] eq 'caption') {                    if ($node->[1] eq 'caption') {
3926                        !!!cp ('t155');
3927                      $i = $_;                      $i = $_;
3928                      last INSCOPE;                      last INSCOPE;
3929                    } elsif ({                    } elsif ({
3930                              table => 1, html => 1,                              table => 1, html => 1,
3931                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3932                        !!!cp ('t156');
3933                      last INSCOPE;                      last INSCOPE;
3934                    }                    }
3935                  } # INSCOPE                  } # INSCOPE
3936                    unless (defined $i) {                    unless (defined $i) {
3937                        !!!cp ('t157');
3938    ## TODO: this type is wrong.
3939                      !!!parse-error (type => 'unmatched end tag:caption');                      !!!parse-error (type => 'unmatched end tag:caption');
3940                      ## Ignore the token                      ## Ignore the token
3941                      !!!next-token;                      !!!next-token;
# Line 3626  sub _tree_construction_main ($) { Line 3943  sub _tree_construction_main ($) {
3943                    }                    }
3944                                    
3945                  ## generate implied end tags                  ## generate implied end tags
3946                  if ({                  while ({
3947                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3948                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
3949                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
3950                      }->{$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;  
3951                  }                  }
3952    
3953                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3954                      !!!cp ('t159');
3955                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3956                    } else {
3957                      !!!cp ('t160');
3958                  }                  }
3959                                    
3960                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3652  sub _tree_construction_main ($) { Line 3966  sub _tree_construction_main ($) {
3966                  ## reprocess                  ## reprocess
3967                  redo B;                  redo B;
3968                } else {                } else {
3969                    !!!cp ('t161');
3970                  #                  #
3971                }                }
3972              } else {              } else {
3973                  !!!cp ('t162');
3974                #                #
3975              }              }
3976            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3665  sub _tree_construction_main ($) { Line 3981  sub _tree_construction_main ($) {
3981                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3982                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3983                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3984                        !!!cp ('t163');
3985                      $i = $_;                      $i = $_;
3986                      last INSCOPE;                      last INSCOPE;
3987                    } elsif ({                    } elsif ({
3988                              table => 1, html => 1,                              table => 1, html => 1,
3989                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3990                        !!!cp ('t164');
3991                      last INSCOPE;                      last INSCOPE;
3992                    }                    }
3993                  } # INSCOPE                  } # INSCOPE
3994                    unless (defined $i) {                    unless (defined $i) {
3995                        !!!cp ('t165');
3996                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3997                      ## Ignore the token                      ## Ignore the token
3998                      !!!next-token;                      !!!next-token;
# Line 3681  sub _tree_construction_main ($) { Line 4000  sub _tree_construction_main ($) {
4000                    }                    }
4001                                    
4002                  ## generate implied end tags                  ## generate implied end tags
4003                  if ({                  while ({
4004                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4005                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
4006                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
4007                       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;  
4008                  }                  }
4009                    
4010                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4011                      !!!cp ('t167');
4012                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4013                    } else {
4014                      !!!cp ('t168');
4015                  }                  }
4016                                    
4017                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3707  sub _tree_construction_main ($) { Line 4023  sub _tree_construction_main ($) {
4023                  !!!next-token;                  !!!next-token;
4024                  redo B;                  redo B;
4025                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4026                    !!!cp ('t169');
4027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4028                  ## Ignore the token                  ## Ignore the token
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  redo B;
4031                } else {                } else {
4032                    !!!cp ('t170');
4033                  #                  #
4034                }                }
4035              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
# Line 3721  sub _tree_construction_main ($) { Line 4039  sub _tree_construction_main ($) {
4039                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4040                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4041                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4042                        !!!cp ('t171');
4043                      $i = $_;                      $i = $_;
4044                      last INSCOPE;                      last INSCOPE;
4045                    } elsif ({                    } elsif ({
4046                              table => 1, html => 1,                              table => 1, html => 1,
4047                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4048                        !!!cp ('t172');
4049                      last INSCOPE;                      last INSCOPE;
4050                    }                    }
4051                  } # INSCOPE                  } # INSCOPE
4052                    unless (defined $i) {                    unless (defined $i) {
4053                        !!!cp ('t173');
4054                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4055                      ## Ignore the token                      ## Ignore the token
4056                      !!!next-token;                      !!!next-token;
# Line 3737  sub _tree_construction_main ($) { Line 4058  sub _tree_construction_main ($) {
4058                    }                    }
4059                                    
4060                  ## generate implied end tags                  ## generate implied end tags
4061                  if ({                  while ({
4062                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4063                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4064                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4065                      }->{$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;  
4066                  }                  }
4067                                    
4068                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4069                      !!!cp ('t175');
4070                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4071                    } else {
4072                      !!!cp ('t176');
4073                  }                  }
4074                                    
4075                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3761  sub _tree_construction_main ($) { Line 4081  sub _tree_construction_main ($) {
4081                  !!!next-token;                  !!!next-token;
4082                  redo B;                  redo B;
4083                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4084                    !!!cp ('t177');
4085                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4086                  ## Ignore the token                  ## Ignore the token
4087                  !!!next-token;                  !!!next-token;
4088                  redo B;                  redo B;
4089                } else {                } else {
4090                    !!!cp ('t178');
4091                  #                  #
4092                }                }
4093              } elsif ({              } elsif ({
# Line 3779  sub _tree_construction_main ($) { Line 4101  sub _tree_construction_main ($) {
4101                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4102                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4103                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4104                      !!!cp ('t179');
4105                    $i = $_;                    $i = $_;
4106                    last INSCOPE;                    last INSCOPE;
4107                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4108                      !!!cp ('t180');
4109                    $tn = $node->[1];                    $tn = $node->[1];
4110                    ## NOTE: There is exactly one |td| or |th| element                    ## NOTE: There is exactly one |td| or |th| element
4111                    ## in scope in the stack of open elements by definition.                    ## in scope in the stack of open elements by definition.
4112                  } elsif ({                  } elsif ({
4113                            table => 1, html => 1,                            table => 1, html => 1,
4114                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4115                      !!!cp ('t181');
4116                    last INSCOPE;                    last INSCOPE;
4117                  }                  }
4118                } # INSCOPE                } # INSCOPE
4119                unless (defined $i) {                unless (defined $i) {
4120                    !!!cp ('t182');
4121                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4122                  ## Ignore the token                  ## Ignore the token
4123                  !!!next-token;                  !!!next-token;
4124                  redo B;                  redo B;
4125                  } else {
4126                    !!!cp ('t183');
4127                }                }
4128    
4129                ## Close the cell                ## Close the cell
# Line 3812  sub _tree_construction_main ($) { Line 4140  sub _tree_construction_main ($) {
4140                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4141                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4142                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4143                      !!!cp ('t184');
4144                    $i = $_;                    $i = $_;
4145                    last INSCOPE;                    last INSCOPE;
4146                  } elsif ({                  } elsif ({
4147                            table => 1, html => 1,                            table => 1, html => 1,
4148                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4149                      !!!cp ('t185');
4150                    last INSCOPE;                    last INSCOPE;
4151                  }                  }
4152                } # INSCOPE                } # INSCOPE
4153                unless (defined $i) {                unless (defined $i) {
4154                    !!!cp ('t186');
4155                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4156                  ## Ignore the token                  ## Ignore the token
4157                  !!!next-token;                  !!!next-token;
# Line 3828  sub _tree_construction_main ($) { Line 4159  sub _tree_construction_main ($) {
4159                }                }
4160                                
4161                ## generate implied end tags                ## generate implied end tags
4162                if ({                while ({
4163                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4164                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4165                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4166                    }->{$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;  
4167                }                }
4168    
4169                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4170                    !!!cp ('t188');
4171                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4172                  } else {
4173                    !!!cp ('t189');
4174                }                }
4175    
4176                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3857  sub _tree_construction_main ($) { Line 4185  sub _tree_construction_main ($) {
4185                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4186                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4187                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4188                    !!!cp ('t190');
4189                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4190                  ## Ignore the token                  ## Ignore the token
4191                  !!!next-token;                  !!!next-token;
4192                  redo B;                  redo B;
4193                } else {                } else {
4194                    !!!cp ('t191');
4195                  #                  #
4196                }                }
4197              } elsif ({              } elsif ({
# Line 3869  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4200                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4201                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4202                  !!!cp ('t192');
4203                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4204                ## Ignore the token                ## Ignore the token
4205                !!!next-token;                !!!next-token;
4206                redo B;                redo B;
4207              } else {              } else {
4208                  !!!cp ('t193');
4209                #                #
4210              }              }
4211          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4212            for my $entry (@{$self->{open_elements}}) {
4213              if (not {
4214                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4215                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4216              }->{$entry->[1]}) {
4217                !!!cp ('t75');
4218                !!!parse-error (type => 'in body:#eof');
4219                last;
4220              }
4221            }
4222    
4223            ## Stop parsing.
4224            last B;
4225        } else {        } else {
4226          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4227        }        }
# Line 3884  sub _tree_construction_main ($) { Line 4230  sub _tree_construction_main ($) {
4230        #        #
4231      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4232        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4233              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4234                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4235              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4236                                
4237                unless (length $token->{data}) {            unless (length $token->{data}) {
4238                  !!!next-token;              !!!cp ('t194');
4239                  redo B;              !!!next-token;
4240                }              redo B;
4241              }            } else {
4242                !!!cp ('t195');
4243              }
4244            }
4245    
4246              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4247    
# Line 3913  sub _tree_construction_main ($) { Line 4263  sub _tree_construction_main ($) {
4263                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4264                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4265                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4266                        !!!cp ('t196');
4267                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4268                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4269                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4270                    } else {                    } else {
4271                        !!!cp ('t197');
4272                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4273                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4274                    }                    }
# Line 3928  sub _tree_construction_main ($) { Line 4280  sub _tree_construction_main ($) {
4280                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4281                if (defined $prev_sibling and                if (defined $prev_sibling and
4282                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4283                    !!!cp ('t198');
4284                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4285                } else {                } else {
4286                    !!!cp ('t199');
4287                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4288                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4289                     $next_sibling);                     $next_sibling);
4290                }                }
4291              } else {            $open_tables->[-1]->[1] = 1; # tainted
4292                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4293              }            !!!cp ('t200');
4294              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4295            }
4296                            
4297              !!!next-token;          !!!next-token;
4298              redo B;          redo B;
4299        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4300              if ({              if ({
4301                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3949  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305                  ## Clear back to table context                  ## Clear back to table context
4306                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4307                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4308                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4309                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4310                  }                  }
4311                                    
# Line 3960  sub _tree_construction_main ($) { Line 4316  sub _tree_construction_main ($) {
4316    
4317                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4318                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4319                      !!!cp ('t202');
4320                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
4321                  }                  }
4322                                    
# Line 3967  sub _tree_construction_main ($) { Line 4324  sub _tree_construction_main ($) {
4324                  while (not {                  while (not {
4325                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4326                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4327                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4328                      ## ISSUE: Can this case be reached?
4329                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4330                  }                  }
4331                                    
4332                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4333                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4334                      !!!cp ('t204');
4335                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4336                    !!!next-token;                    !!!next-token;
4337                    redo B;                    redo B;
4338                  } else {                  } else {
4339                      !!!cp ('t205');
4340                    !!!insert-element ('tr');                    !!!insert-element ('tr');
4341                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4342                  }                  }
4343                  } else {
4344                    !!!cp ('t206');
4345                }                }
4346    
4347                ## Clear back to table row context                ## Clear back to table row context
4348                while (not {                while (not {
4349                  tr => 1, html => 1,                  tr => 1, html => 1,
4350                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4351                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4352                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4353                }                }
4354                                
# Line 4009  sub _tree_construction_main ($) { Line 4371  sub _tree_construction_main ($) {
4371                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4372                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4373                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4374                        !!!cp ('t208');
4375                      $i = $_;                      $i = $_;
4376                      last INSCOPE;                      last INSCOPE;
4377                    } elsif ({                    } elsif ({
4378                              table => 1, html => 1,                              html => 1,
4379    
4380                                ## NOTE: This element does not appear here, maybe.
4381                                table => 1,
4382                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4383                        !!!cp ('t209');
4384                      last INSCOPE;                      last INSCOPE;
4385                    }                    }
4386                  } # INSCOPE                  } # INSCOPE
4387                  unless (defined $i) {                  unless (defined $i) {
4388                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4389    ## TODO: This type is wrong.
4390                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4391                    ## Ignore the token                    ## Ignore the token
4392                    !!!next-token;                    !!!next-token;
4393                    redo B;                    redo B;
# Line 4028  sub _tree_construction_main ($) { Line 4397  sub _tree_construction_main ($) {
4397                  while (not {                  while (not {
4398                    tr => 1, html => 1,                    tr => 1, html => 1,
4399                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4400                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4401                      ## ISSUE: Can this case be reached?
4402                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4403                  }                  }
4404                                    
4405                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4406                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4407                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4408                      !!!cp ('t212');
4409                    ## reprocess                    ## reprocess
4410                    redo B;                    redo B;
4411                  } else {                  } else {
4412                      !!!cp ('t213');
4413                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4414                  }                  }
4415                }                }
# Line 4050  sub _tree_construction_main ($) { Line 4422  sub _tree_construction_main ($) {
4422                    if ({                    if ({
4423                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4424                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4425                        !!!cp ('t214');
4426                      $i = $_;                      $i = $_;
4427                      last INSCOPE;                      last INSCOPE;
4428                    } elsif ({                    } elsif ({
4429                              table => 1, html => 1,                              table => 1, html => 1,
4430                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4431                        !!!cp ('t215');
4432                      last INSCOPE;                      last INSCOPE;
4433                    }                    }
4434                  } # INSCOPE                  } # INSCOPE
4435                  unless (defined $i) {                  unless (defined $i) {
4436                      !!!cp ('t216');
4437    ## TODO: This erorr type ios wrong.
4438                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4439                    ## Ignore the token                    ## Ignore the token
4440                    !!!next-token;                    !!!next-token;
# Line 4069  sub _tree_construction_main ($) { Line 4445  sub _tree_construction_main ($) {
4445                  while (not {                  while (not {
4446                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4447                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4448                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4449                      ## ISSUE: Can this state be reached?
4450                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4451                  }                  }
4452                                    
# Line 4083  sub _tree_construction_main ($) { Line 4460  sub _tree_construction_main ($) {
4460                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4461                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4462                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4463                  } else {
4464                    !!!cp ('t218');
4465                }                }
4466    
4467                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4468                  ## Clear back to table context                  ## Clear back to table context
4469                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4470                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4471                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4472                      ## ISSUE: Can this state be reached?
4473                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4474                  }                  }
4475                                    
# Line 4105  sub _tree_construction_main ($) { Line 4485  sub _tree_construction_main ($) {
4485                  ## Clear back to table context                  ## Clear back to table context
4486                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4487                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4488                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4489                      ## ISSUE: Can this state be reached?
4490                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4491                  }                  }
4492                                    
# Line 4134  sub _tree_construction_main ($) { Line 4515  sub _tree_construction_main ($) {
4515                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4516                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4517                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4518                      !!!cp ('t221');
4519                    $i = $_;                    $i = $_;
4520                    last INSCOPE;                    last INSCOPE;
4521                  } elsif ({                  } elsif ({
4522                            table => 1, html => 1,                            #table => 1,
4523                              html => 1,
4524                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4525                      !!!cp ('t222');
4526                    last INSCOPE;                    last INSCOPE;
4527                  }                  }
4528                } # INSCOPE                } # INSCOPE
4529                unless (defined $i) {                unless (defined $i) {
4530                    !!!cp ('t223');
4531    ## TODO: The following is wrong, maybe.
4532                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4533                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  redo B;
4536                }                }
4537                                
4538    ## TODO: Followings are removed from the latest spec.
4539                ## generate implied end tags                ## generate implied end tags
4540                if ({                while ({
4541                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4542                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4543                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4544                    }->{$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;  
4545                }                }
4546    
4547                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4548                    !!!cp ('t225');
4549    ## ISSUE: Can this case be reached?
4550                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4551                  } else {
4552                    !!!cp ('t226');
4553                }                }
4554    
4555                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4556                  pop @{$open_tables};
4557    
4558                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4559    
4560                ## reprocess                ## reprocess
4561                redo B;                redo B;
4562          } else {          } elsif ($token->{tag_name} eq 'style') {
4563            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4564                !!!cp ('t227.8');
4565                ## NOTE: This is a "as if in head" code clone.
4566                $parse_rcdata->(CDATA_CONTENT_MODEL);
4567                redo B;
4568              } else {
4569                !!!cp ('t227.7');
4570                #
4571              }
4572            } elsif ($token->{tag_name} eq 'script') {
4573              if (not $open_tables->[-1]->[1]) { # tainted
4574                !!!cp ('t227.6');
4575                ## NOTE: This is a "as if in head" code clone.
4576                $script_start_tag->();
4577                redo B;
4578              } else {
4579                !!!cp ('t227.5');
4580                #
4581              }
4582            } elsif ($token->{tag_name} eq 'input') {
4583              if (not $open_tables->[-1]->[1]) { # tainted
4584                if ($token->{attributes}->{type}) { ## TODO: case
4585                  my $type = lc $token->{attributes}->{type}->{value};
4586                  if ($type eq 'hidden') {
4587                    !!!cp ('t227.3');
4588                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4589    
4590            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4591    
4592                    ## TODO: form element pointer
4593    
4594                    pop @{$self->{open_elements}};
4595    
4596                    !!!next-token;
4597                    redo B;
4598                  } else {
4599                    !!!cp ('t227.2');
4600                    #
4601                  }
4602                } else {
4603                  !!!cp ('t227.1');
4604                  #
4605                }
4606              } else {
4607                !!!cp ('t227.4');
4608                #
4609              }
4610            } else {
4611              !!!cp ('t227');
4612            #            #
4613          }          }
4614    
4615            !!!parse-error (type => 'in table:'.$token->{tag_name});
4616    
4617            $insert = $insert_to_foster;
4618            #
4619        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4620              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4621                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4187  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4625                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4626                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4627                      !!!cp ('t228');
4628                    $i = $_;                    $i = $_;
4629                    last INSCOPE;                    last INSCOPE;
4630                  } elsif ({                  } elsif ({
4631                            table => 1, html => 1,                            table => 1, html => 1,
4632                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4633                      !!!cp ('t229');
4634                    last INSCOPE;                    last INSCOPE;
4635                  }                  }
4636                } # INSCOPE                } # INSCOPE
4637                unless (defined $i) {                unless (defined $i) {
4638                    !!!cp ('t230');
4639                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4640                  ## Ignore the token                  ## Ignore the token
4641                  !!!next-token;                  !!!next-token;
4642                  redo B;                  redo B;
4643                  } else {
4644                    !!!cp ('t232');
4645                }                }
4646    
4647                ## Clear back to table row context                ## Clear back to table row context
4648                while (not {                while (not {
4649                  tr => 1, html => 1,                  tr => 1, html => 1,
4650                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4651                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4652    ## ISSUE: Can this state be reached?
4653                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4654                }                }
4655    
# Line 4222  sub _tree_construction_main ($) { Line 4665  sub _tree_construction_main ($) {
4665                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4666                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4667                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4668                        !!!cp ('t233');
4669                      $i = $_;                      $i = $_;
4670                      last INSCOPE;                      last INSCOPE;
4671                    } elsif ({                    } elsif ({
4672                              table => 1, html => 1,                              table => 1, html => 1,
4673                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4674                        !!!cp ('t234');
4675                      last INSCOPE;                      last INSCOPE;
4676                    }                    }
4677                  } # INSCOPE                  } # INSCOPE
4678                  unless (defined $i) {                  unless (defined $i) {
4679                      !!!cp ('t235');
4680    ## TODO: The following is wrong.
4681                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4682                    ## Ignore the token                    ## Ignore the token
4683                    !!!next-token;                    !!!next-token;
# Line 4241  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688                  while (not {                  while (not {
4689                    tr => 1, html => 1,                    tr => 1, html => 1,
4690                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4691                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4692    ## ISSUE: Can this state be reached?
4693                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4694                  }                  }
4695                                    
# Line 4258  sub _tree_construction_main ($) { Line 4706  sub _tree_construction_main ($) {
4706                    if ({                    if ({
4707                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4708                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4709                        !!!cp ('t237');
4710                      $i = $_;                      $i = $_;
4711                      last INSCOPE;                      last INSCOPE;
4712                    } elsif ({                    } elsif ({
4713                              table => 1, html => 1,                              table => 1, html => 1,
4714                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4715                        !!!cp ('t238');
4716                      last INSCOPE;                      last INSCOPE;
4717                    }                    }
4718                  } # INSCOPE                  } # INSCOPE
4719                  unless (defined $i) {                  unless (defined $i) {
4720                      !!!cp ('t239');
4721                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4722                    ## Ignore the token                    ## Ignore the token
4723                    !!!next-token;                    !!!next-token;
# Line 4277  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728                  while (not {                  while (not {
4729                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4730                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4731                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4732                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4733                  }                  }
4734                                    
# Line 4293  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4745                }                }
4746    
4747                  ## NOTE: </table> in the "in table" insertion mode.
4748                  ## When you edit the code fragment below, please ensure that
4749                  ## the code for <table> in the "in table" insertion mode
4750                  ## is synced with it.
4751    
4752                ## have a table element in table scope                ## have a table element in table scope
4753                my $i;                my $i;
4754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4755                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4756                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4757                      !!!cp ('t241');
4758                    $i = $_;                    $i = $_;
4759                    last INSCOPE;                    last INSCOPE;
4760                  } elsif ({                  } elsif ({
4761                            table => 1, html => 1,                            table => 1, html => 1,
4762                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4763                      !!!cp ('t242');
4764                    last INSCOPE;                    last INSCOPE;
4765                  }                  }
4766                } # INSCOPE                } # INSCOPE
4767                unless (defined $i) {                unless (defined $i) {
4768                    !!!cp ('t243');
4769                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4770                  ## Ignore the token                  ## Ignore the token
4771                  !!!next-token;                  !!!next-token;
4772                  redo B;                  redo B;
4773                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4774                                    
4775                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4776                  pop @{$open_tables};
4777                                
4778                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4779                                
# Line 4345  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4790                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4791                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4792                        !!!cp ('t247');
4793                      $i = $_;                      $i = $_;
4794                      last INSCOPE;                      last INSCOPE;
4795                    } elsif ({                    } elsif ({
4796                              table => 1, html => 1,                              table => 1, html => 1,
4797                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4798                        !!!cp ('t248');
4799                      last INSCOPE;                      last INSCOPE;
4800                    }                    }
4801                  } # INSCOPE                  } # INSCOPE
4802                    unless (defined $i) {                    unless (defined $i) {
4803                        !!!cp ('t249');
4804                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4805                      ## Ignore the token                      ## Ignore the token
4806                      !!!next-token;                      !!!next-token;
# Line 4366  sub _tree_construction_main ($) { Line 4813  sub _tree_construction_main ($) {
4813                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4814                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4815                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4816                        !!!cp ('t250');
4817                      $i = $_;                      $i = $_;
4818                      last INSCOPE;                      last INSCOPE;
4819                    } elsif ({                    } elsif ({
4820                              table => 1, html => 1,                              table => 1, html => 1,
4821                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4822                        !!!cp ('t251');
4823                      last INSCOPE;                      last INSCOPE;
4824                    }                    }
4825                  } # INSCOPE                  } # INSCOPE
4826                    unless (defined $i) {                    unless (defined $i) {
4827                        !!!cp ('t252');
4828                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr');
4829                      ## Ignore the token                      ## Ignore the token
4830                      !!!next-token;                      !!!next-token;
# Line 4385  sub _tree_construction_main ($) { Line 4835  sub _tree_construction_main ($) {
4835                  while (not {                  while (not {
4836                    tr => 1, html => 1,                    tr => 1, html => 1,
4837                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4838                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4839    ## ISSUE: Can this case be reached?
4840                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4841                  }                  }
4842                                    
# Line 4399  sub _tree_construction_main ($) { Line 4850  sub _tree_construction_main ($) {
4850                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4851                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4852                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4853                      !!!cp ('t254');
4854                    $i = $_;                    $i = $_;
4855                    last INSCOPE;                    last INSCOPE;
4856                  } elsif ({                  } elsif ({
4857                            table => 1, html => 1,                            table => 1, html => 1,
4858                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4859                      !!!cp ('t255');
4860                    last INSCOPE;                    last INSCOPE;
4861                  }                  }
4862                } # INSCOPE                } # INSCOPE
4863                unless (defined $i) {                unless (defined $i) {
4864                    !!!cp ('t256');
4865                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4866                  ## Ignore the token                  ## Ignore the token
4867                  !!!next-token;                  !!!next-token;
# Line 4418  sub _tree_construction_main ($) { Line 4872  sub _tree_construction_main ($) {
4872                while (not {                while (not {
4873                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4874                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4875                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4876    ## ISSUE: Can this case be reached?
4877                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4878                }                }
4879    
# Line 4432  sub _tree_construction_main ($) { Line 4887  sub _tree_construction_main ($) {
4887                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4888                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4889                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4890                  !!!cp ('t258');
4891                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4892                ## Ignore the token                ## Ignore the token
4893                !!!next-token;                !!!next-token;
4894                redo B;                redo B;
4895          } else {          } else {
4896              !!!cp ('t259');
4897            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name});
4898    
4899            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4900            #            #
4901          }          }
4902          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4903            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4904                    @{$self->{open_elements}} == 1) { # redundant, maybe
4905              !!!parse-error (type => 'in body:#eof');
4906              !!!cp ('t259.1');
4907              #
4908            } else {
4909              !!!cp ('t259.2');
4910              #
4911            }
4912    
4913            ## Stop parsing
4914            last B;
4915        } else {        } else {
4916          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4917        }        }
# Line 4450  sub _tree_construction_main ($) { Line 4920  sub _tree_construction_main ($) {
4920              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4921                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4922                unless (length $token->{data}) {                unless (length $token->{data}) {
4923                    !!!cp ('t260');
4924                  !!!next-token;                  !!!next-token;
4925                  redo B;                  redo B;
4926                }                }
4927              }              }
4928                            
4929                !!!cp ('t261');
4930              #              #
4931            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4932              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4933                  !!!cp ('t262');
4934                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4935                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4936                !!!next-token;                !!!next-token;
4937                redo B;                redo B;
4938              } else {              } else {
4939                  !!!cp ('t263');
4940                #                #
4941              }              }
4942            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4943              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4944                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4945                    !!!cp ('t264');
4946                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
4947                  ## Ignore the token                  ## Ignore the token
4948                  !!!next-token;                  !!!next-token;
4949                  redo B;                  redo B;
4950                } else {                } else {
4951                    !!!cp ('t265');
4952                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4953                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                              redo B;            
4956                }                }
4957              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4958                  !!!cp ('t266');
4959                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col');
4960                ## Ignore the token                ## Ignore the token
4961                !!!next-token;                !!!next-token;
4962                redo B;                redo B;
4963              } else {              } else {
4964                  !!!cp ('t267');
4965                #                #
4966              }              }
4967            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4968              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
4969            }              @{$self->{open_elements}} == 1) { # redundant, maybe
4970              !!!cp ('t270.2');
4971              ## Stop parsing.
4972              last B;
4973            } else {
4974              ## NOTE: As if </colgroup>.
4975              !!!cp ('t270.1');
4976              pop @{$self->{open_elements}}; # colgroup
4977              $self->{insertion_mode} = IN_TABLE_IM;
4978              ## Reprocess.
4979              redo B;
4980            }
4981          } else {
4982            die "$0: $token->{type}: Unknown token type";
4983          }
4984    
4985            ## As if </colgroup>            ## As if </colgroup>
4986            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4987                !!!cp ('t269');
4988    ## TODO: Wrong error type?
4989              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4990              ## Ignore the token              ## Ignore the token
4991              !!!next-token;              !!!next-token;
4992              redo B;              redo B;
4993            } else {            } else {
4994                !!!cp ('t270');
4995              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4996              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
4997              ## reprocess              ## reprocess
4998              redo B;              redo B;
4999            }            }
5000      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5001        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5002            !!!cp ('t271');
5003          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5004          !!!next-token;          !!!next-token;
5005          redo B;          redo B;
5006        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5007              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5008                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5009                    !!!cp ('t272');
5010                  ## As if </option>                  ## As if </option>
5011                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5012                  } else {
5013                    !!!cp ('t273');
5014                }                }
5015    
5016                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4519  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018                redo B;                redo B;
5019              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5020                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5021                    !!!cp ('t274');
5022                  ## As if </option>                  ## As if </option>
5023                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5024                  } else {
5025                    !!!cp ('t275');
5026                }                }
5027    
5028                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5029                    !!!cp ('t276');
5030                  ## As if </optgroup>                  ## As if </optgroup>
5031                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5032                  } else {
5033                    !!!cp ('t277');
5034                }                }
5035    
5036                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5037                !!!next-token;                !!!next-token;
5038                redo B;                redo B;
5039              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5040                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5041                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5042                      {
5043                       caption => 1, table => 1,
5044                       tbody => 1, tfoot => 1, thead => 1,
5045                       tr => 1, td => 1, th => 1,
5046                      }->{$token->{tag_name}})) {
5047              ## TODO: The type below is not good - <select> is replaced by </select>
5048              !!!parse-error (type => 'not closed:select');
5049              ## NOTE: As if the token were </select> (<select> case) or
5050              ## as if there were </select> (otherwise).
5051                ## have an element in table scope                ## have an element in table scope
5052                my $i;                my $i;
5053                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5054                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5055                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5056                      !!!cp ('t278');
5057                    $i = $_;                    $i = $_;
5058                    last INSCOPE;                    last INSCOPE;
5059                  } elsif ({                  } elsif ({
5060                            table => 1, html => 1,                            table => 1, html => 1,
5061                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5062                      !!!cp ('t279');
5063                    last INSCOPE;                    last INSCOPE;
5064                  }                  }
5065                } # INSCOPE                } # INSCOPE
5066                unless (defined $i) {                unless (defined $i) {
5067                    !!!cp ('t280');
5068                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5069                  ## Ignore the token                  ## Ignore the token
5070                  !!!next-token;                  !!!next-token;
5071                  redo B;                  redo B;
5072                }                }
5073                                
5074                  !!!cp ('t281');
5075                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5076    
5077                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5078    
5079                !!!next-token;            if ($token->{tag_name} eq 'select') {
5080                redo B;              !!!cp ('t281.2');
5081                !!!next-token;
5082                redo B;
5083              } else {
5084                !!!cp ('t281.1');
5085                ## Reprocess the token.
5086                redo B;
5087              }
5088          } else {          } else {
5089              !!!cp ('t282');
5090            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name});
5091            ## Ignore the token            ## Ignore the token
5092            !!!next-token;            !!!next-token;
# Line 4570  sub _tree_construction_main ($) { Line 5096  sub _tree_construction_main ($) {
5096              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5097                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5098                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5099                    !!!cp ('t283');
5100                  ## As if </option>                  ## As if </option>
5101                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5102                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5103                    !!!cp ('t284');
5104                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5105                } else {                } else {
5106                    !!!cp ('t285');
5107                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5108                  ## Ignore the token                  ## Ignore the token
5109                }                }
# Line 4582  sub _tree_construction_main ($) { Line 5111  sub _tree_construction_main ($) {
5111                redo B;                redo B;
5112              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5113                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5114                    !!!cp ('t286');
5115                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5116                } else {                } else {
5117                    !!!cp ('t287');
5118                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5119                  ## Ignore the token                  ## Ignore the token
5120                }                }
# Line 4595  sub _tree_construction_main ($) { Line 5126  sub _tree_construction_main ($) {
5126                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5127                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5128                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5129                      !!!cp ('t288');
5130                    $i = $_;                    $i = $_;
5131                    last INSCOPE;                    last INSCOPE;
5132                  } elsif ({                  } elsif ({
5133                            table => 1, html => 1,                            table => 1, html => 1,
5134                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5135                      !!!cp ('t289');
5136                    last INSCOPE;                    last INSCOPE;
5137                  }                  }
5138                } # INSCOPE                } # INSCOPE
5139                unless (defined $i) {                unless (defined $i) {
5140                    !!!cp ('t290');
5141                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5142                  ## Ignore the token                  ## Ignore the token
5143                  !!!next-token;                  !!!next-token;
5144                  redo B;                  redo B;
5145                }                }
5146                                
5147                  !!!cp ('t291');
5148                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5149    
5150                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5151    
5152                !!!next-token;                !!!next-token;
5153                redo B;                redo B;
5154              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5155                        caption => 1, table => 1, tbody => 1,                   {
5156                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5157                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5158                     }->{$token->{tag_name}}) {
5159    ## TODO: The following is wrong?
5160                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5161                                
5162                ## have an element in table scope                ## have an element in table scope
# Line 4627  sub _tree_construction_main ($) { Line 5164  sub _tree_construction_main ($) {
5164                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5165                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5166                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5167                      !!!cp ('t292');
5168                    $i = $_;                    $i = $_;
5169                    last INSCOPE;                    last INSCOPE;
5170                  } elsif ({                  } elsif ({
5171                            table => 1, html => 1,                            table => 1, html => 1,
5172                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5173                      !!!cp ('t293');
5174                    last INSCOPE;                    last INSCOPE;
5175                  }                  }
5176                } # INSCOPE                } # INSCOPE
5177                unless (defined $i) {                unless (defined $i) {
5178                    !!!cp ('t294');
5179                  ## Ignore the token                  ## Ignore the token
5180                  !!!next-token;                  !!!next-token;
5181                  redo B;                  redo B;
# Line 4647  sub _tree_construction_main ($) { Line 5187  sub _tree_construction_main ($) {
5187                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5188                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5189                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5190                      !!!cp ('t295');
5191                    $i = $_;                    $i = $_;
5192                    last INSCOPE;                    last INSCOPE;
5193                  } elsif ({                  } elsif ({
5194                            table => 1, html => 1,                            table => 1, html => 1,
5195                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5196    ## ISSUE: Can this state be reached?
5197                      !!!cp ('t296');
5198                    last INSCOPE;                    last INSCOPE;
5199                  }                  }
5200                } # INSCOPE                } # INSCOPE
5201                unless (defined $i) {                unless (defined $i) {
5202                    !!!cp ('t297');
5203    ## TODO: The following error type is correct?
5204                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5205                  ## Ignore the </select> token                  ## Ignore the </select> token
5206                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5207                  redo B;                  redo B;
5208                }                }
5209                                
5210                  !!!cp ('t298');
5211                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5212    
5213                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4669  sub _tree_construction_main ($) { Line 5215  sub _tree_construction_main ($) {
5215                ## reprocess                ## reprocess
5216                redo B;                redo B;
5217          } else {          } else {
5218              !!!cp ('t299');
5219            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name});
5220            ## Ignore the token            ## Ignore the token
5221            !!!next-token;            !!!next-token;
5222            redo B;            redo B;
5223          }          }
5224          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5225            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5226                    @{$self->{open_elements}} == 1) { # redundant, maybe
5227              !!!cp ('t299.1');
5228              !!!parse-error (type => 'in body:#eof');
5229            } else {
5230              !!!cp ('t299.2');
5231            }
5232    
5233            ## Stop parsing.
5234            last B;
5235        } else {        } else {
5236          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5237        }        }
# Line 4687  sub _tree_construction_main ($) { Line 5245  sub _tree_construction_main ($) {
5245            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5246                        
5247            unless (length $token->{data}) {            unless (length $token->{data}) {
5248                !!!cp ('t300');
5249              !!!next-token;              !!!next-token;
5250              redo B;              redo B;
5251            }            }
5252          }          }
5253                    
5254          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5255              !!!cp ('t301');
5256            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5257    
5258            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5259            } else {
5260              !!!cp ('t302');
5261          }          }
5262                    
5263          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4706  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268          redo B;          redo B;
5269        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5270          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5271              !!!cp ('t303');
5272            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5273                        
5274            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5275            } else {
5276              !!!cp ('t304');
5277          }          }
5278    
5279          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4719  sub _tree_construction_main ($) { Line 5284  sub _tree_construction_main ($) {
5284          redo B;          redo B;
5285        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5286          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5287              !!!cp ('t305');
5288            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5289                        
5290            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5291            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5292            } else {
5293              !!!cp ('t306');
5294          }          }
5295    
5296          ## "after body" insertion mode          ## "after body" insertion mode
5297          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5298            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5299                !!!cp ('t307');
5300              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html');
5301              ## Ignore the token              ## Ignore the token
5302              !!!next-token;              !!!next-token;
5303              redo B;              redo B;
5304            } else {            } else {
5305                !!!cp ('t308');
5306              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5307              !!!next-token;              !!!next-token;
5308              redo B;              redo B;
5309            }            }
5310          } else {          } else {
5311              !!!cp ('t309');
5312            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
5313    
5314            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5315            ## reprocess            ## reprocess
5316            redo B;            redo B;
5317          }          }
5318          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5319            !!!cp ('t309.2');
5320            ## Stop parsing
5321            last B;
5322        } else {        } else {
5323          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5324        }        }
# Line 4753  sub _tree_construction_main ($) { Line 5328  sub _tree_construction_main ($) {
5328            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5329                        
5330            unless (length $token->{data}) {            unless (length $token->{data}) {
5331                !!!cp ('t310');
5332              !!!next-token;              !!!next-token;
5333              redo B;              redo B;
5334            }            }
# Line 4760  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                    
5337          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5338            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5339                !!!cp ('t311');
5340              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
5341            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5342                !!!cp ('t312');
5343              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5344            } else { # "after html frameset"            } else { # "after html frameset"
5345                !!!cp ('t313');
5346              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5347    
5348              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5349              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5350              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5351            }            }
5352                        
5353            ## Ignore the token.            ## Ignore the token.
5354            if (length $token->{data}) {            if (length $token->{data}) {
5355                !!!cp ('t314');
5356              ## reprocess the rest of characters              ## reprocess the rest of characters
5357            } else {            } else {
5358                !!!cp ('t315');
5359              !!!next-token;              !!!next-token;
5360            }            }
5361            redo B;            redo B;
# Line 4783  sub _tree_construction_main ($) { Line 5364  sub _tree_construction_main ($) {
5364          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5365        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5366          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5367              !!!cp ('t316');
5368            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5369    
5370            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5371            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5372          }          } else {
5373              !!!cp ('t317');
5374            }
5375    
5376          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5377              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5378              !!!cp ('t318');
5379            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5380            !!!next-token;            !!!next-token;
5381            redo B;            redo B;
5382          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5383                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5384              !!!cp ('t319');
5385            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5386            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5387            !!!next-token;            !!!next-token;
5388            redo B;            redo B;
5389          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5390              !!!cp ('t320');
5391            ## NOTE: As if in body.            ## NOTE: As if in body.
5392            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5393            redo B;            redo B;
5394          } else {          } else {
5395            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5396                !!!cp ('t321');
5397              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5398            } else {            } else {
5399                !!!cp ('t322');
5400              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5401            }            }
5402            ## Ignore the token            ## Ignore the token
# Line 4816  sub _tree_construction_main ($) { Line 5405  sub _tree_construction_main ($) {
5405          }          }
5406        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5407          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5408              !!!cp ('t323');
5409            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5410    
5411            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5412            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5413            } else {
5414              !!!cp ('t324');
5415          }          }
5416    
5417          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5418              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5419            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5420                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5421                !!!cp ('t325');
5422              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5423              ## Ignore the token              ## Ignore the token
5424              !!!next-token;              !!!next-token;
5425            } else {            } else {
5426                !!!cp ('t326');
5427              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5428              !!!next-token;              !!!next-token;
5429            }            }
5430    
5431            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5432                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5433                !!!cp ('t327');
5434              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5435              } else {
5436                !!!cp ('t328');
5437            }            }
5438            redo B;            redo B;
5439          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5440                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5441              !!!cp ('t329');
5442            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5443            !!!next-token;            !!!next-token;
5444            redo B;            redo B;
5445          } else {          } else {
5446            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5447                !!!cp ('t330');
5448              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5449            } else {            } else {
5450                !!!cp ('t331');
5451              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5452            }            }
5453            ## Ignore the token            ## Ignore the token
5454            !!!next-token;            !!!next-token;
5455            redo B;            redo B;
5456          }          }
5457          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5458            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5459                    @{$self->{open_elements}} == 1) { # redundant, maybe
5460              !!!cp ('t331.1');
5461              !!!parse-error (type => 'in body:#eof');
5462            } else {
5463              !!!cp ('t331.2');
5464            }
5465            
5466            ## Stop parsing
5467            last B;
5468        } else {        } else {
5469          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5470        }        }
# Line 4866  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477      ## "in body" insertion mode      ## "in body" insertion mode
5478      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5479        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5480            !!!cp ('t332');
5481          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5482          $script_start_tag->($insert);          $script_start_tag->();
5483          redo B;          redo B;
5484        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5485            !!!cp ('t333');
5486          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5487          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5488          redo B;          redo B;
5489        } elsif ({        } elsif ({
5490                  base => 1, link => 1,                  base => 1, link => 1,
5491                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5492            !!!cp ('t334');
5493          ## 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
5494          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5495          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 5502  sub _tree_construction_main ($) {
5502    
5503          unless ($self->{confident}) {          unless ($self->{confident}) {
5504            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5505                !!!cp ('t335');
5506              $self->{change_encoding}              $self->{change_encoding}
5507                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value});
5508                            
# Line 4902  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
5518                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5519                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5520                  !!!cp ('t336');
5521                $self->{change_encoding}                $self->{change_encoding}
5522                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5523                $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 5528  sub _tree_construction_main ($) {
5528            }            }
5529          } else {          } else {
5530            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
5531                !!!cp ('t337');
5532              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5533                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5534                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
5535                                           ->{has_reference});                                           ->{has_reference});
5536            }            }
5537            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
5538                !!!cp ('t338');
5539              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5540                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5541                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4928  sub _tree_construction_main ($) { Line 5546  sub _tree_construction_main ($) {
5546          !!!next-token;          !!!next-token;
5547          redo B;          redo B;
5548        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5549          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5550          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5551          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5552          redo B;          redo B;
5553        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5554          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
5555                                
5556          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5557              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5558              !!!cp ('t342');
5559            ## Ignore the token            ## Ignore the token
5560          } else {          } else {
5561            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5562            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5563              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5564                  !!!cp ('t343');
5565                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5566                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5567                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4958  sub _tree_construction_main ($) { Line 5572  sub _tree_construction_main ($) {
5572          redo B;          redo B;
5573        } elsif ({        } elsif ({
5574                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5575                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5576                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5577                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5578                  pre => 1,                  pre => 1, listing => 1,
5579                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5580          ## has a p element in scope          ## has a p element in scope
5581          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5582            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5583                !!!cp ('t344');
5584              !!!back-token;              !!!back-token;
5585              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5586              redo B;              redo B;
5587            } elsif ({            } elsif ({
5588                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5589                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5590                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5591                !!!cp ('t345');
5592              last INSCOPE;              last INSCOPE;
5593            }            }
5594          } # INSCOPE          } # INSCOPE
5595                        
5596          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5597          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5598            !!!next-token;            !!!next-token;
5599            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5600              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5601              unless (length $token->{data}) {              unless (length $token->{data}) {
5602                  !!!cp ('t346');
5603                !!!next-token;                !!!next-token;
5604                } else {
5605                  !!!cp ('t349');
5606              }              }
5607              } else {
5608                !!!cp ('t348');
5609            }            }
5610          } else {          } else {
5611              !!!cp ('t347');
5612            !!!next-token;            !!!next-token;
5613          }          }
5614          redo B;          redo B;
5615        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5616          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5617              !!!cp ('t350');
5618            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5619            ## Ignore the token            ## Ignore the token
5620            !!!next-token;            !!!next-token;
# Line 4999  sub _tree_construction_main ($) { Line 5623  sub _tree_construction_main ($) {
5623            ## has a p element in scope            ## has a p element in scope
5624            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5625              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5626                  !!!cp ('t351');
5627                !!!back-token;                !!!back-token;
5628                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5629                redo B;                redo B;
5630              } elsif ({              } elsif ({
5631                        table => 1, caption => 1, td => 1, th => 1,                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5632                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5633                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5634                  !!!cp ('t352');
5635                last INSCOPE;                last INSCOPE;
5636              }              }
5637            } # INSCOPE            } # INSCOPE
# Line 5019  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645          ## has a p element in scope          ## has a p element in scope
5646          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5647            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5648                !!!cp ('t353');
5649              !!!back-token;              !!!back-token;
5650              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5651              redo B;              redo B;
5652            } elsif ({            } elsif ({
5653                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5654                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5655                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5656                !!!cp ('t354');
5657              last INSCOPE;              last INSCOPE;
5658            }            }
5659          } # INSCOPE          } # INSCOPE
# Line 5037  sub _tree_construction_main ($) { Line 5665  sub _tree_construction_main ($) {
5665            ## Step 2            ## Step 2
5666            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5667              if ($i != -1) {              if ($i != -1) {
5668                  !!!cp ('t355');
5669                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5670                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5671                } else {
5672                  !!!cp ('t356');
5673              }              }
5674              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5675              last LI;              last LI;
5676              } else {
5677                !!!cp ('t357');
5678            }            }
5679                        
5680            ## Step 3            ## Step 3
# Line 5050  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5684                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5685                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5686                !!!cp ('t358');
5687              last LI;              last LI;
5688            }            }
5689                        
5690              !!!cp ('t359');
5691            ## Step 4            ## Step 4
5692            $i--;            $i--;
5693            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5066  sub _tree_construction_main ($) { Line 5701  sub _tree_construction_main ($) {
5701          ## has a p element in scope          ## has a p element in scope
5702          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5703            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5704                !!!cp ('t360');
5705              !!!back-token;              !!!back-token;
5706              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5707              redo B;              redo B;
5708            } elsif ({            } elsif ({
5709                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5710                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5711                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5712                !!!cp ('t361');
5713              last INSCOPE;              last INSCOPE;
5714            }            }
5715          } # INSCOPE          } # INSCOPE
# Line 5084  sub _tree_construction_main ($) { Line 5721  sub _tree_construction_main ($) {
5721            ## Step 2            ## Step 2
5722            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5723              if ($i != -1) {              if ($i != -1) {
5724                  !!!cp ('t362');
5725                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5726                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5727                } else {
5728                  !!!cp ('t363');
5729              }              }
5730              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5731              last LI;              last LI;
5732              } else {
5733                !!!cp ('t364');
5734            }            }
5735                        
5736            ## Step 3            ## Step 3
# Line 5097  sub _tree_construction_main ($) { Line 5739  sub _tree_construction_main ($) {
5739                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5740                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5741                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5742                !!!cp ('t365');
5743              last LI;              last LI;
5744            }            }
5745                        
5746              !!!cp ('t366');
5747            ## Step 4            ## Step 4
5748            $i--;            $i--;
5749            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 5113  sub _tree_construction_main ($) { Line 5757  sub _tree_construction_main ($) {
5757          ## has a p element in scope          ## has a p element in scope
5758          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5759            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5760                !!!cp ('t367');
5761              !!!back-token;              !!!back-token;
5762              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5763              redo B;              redo B;
5764            } elsif ({            } elsif ({
5765                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5766                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5767                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5768                !!!cp ('t368');
5769              last INSCOPE;              last INSCOPE;
5770            }            }
5771          } # INSCOPE          } # INSCOPE
# Line 5130  sub _tree_construction_main ($) { Line 5776  sub _tree_construction_main ($) {
5776                        
5777          !!!next-token;          !!!next-token;
5778          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;  
5779        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5780          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5781            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5782            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5783                !!!cp ('t371');
5784              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5785                            
5786              !!!back-token;              !!!back-token;
# Line 5187  sub _tree_construction_main ($) { Line 5789  sub _tree_construction_main ($) {
5789                            
5790              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5791                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5792                    !!!cp ('t372');
5793                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5794                  last AFE2;                  last AFE2;
5795                }                }
5796              } # AFE2              } # AFE2
5797              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5798                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5799                    !!!cp ('t373');
5800                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5801                  last OE;                  last OE;
5802                }                }
5803              } # OE              } # OE
5804              last AFE;              last AFE;
5805            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5806                !!!cp ('t374');
5807              last AFE;              last AFE;
5808            }            }
5809          } # AFE          } # AFE
# Line 5215  sub _tree_construction_main ($) { Line 5820  sub _tree_construction_main ($) {
5820                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5821                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5822                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5823            !!!cp ('t375');
5824          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5825                    
5826          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5229  sub _tree_construction_main ($) { Line 5835  sub _tree_construction_main ($) {
5835          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5836            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5837            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5838                !!!cp ('t376');
5839              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr');
5840              !!!back-token;              !!!back-token;
5841              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5842              redo B;              redo B;
5843            } elsif ({            } elsif ({
5844                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5845                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5846                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5847                !!!cp ('t377');
5848              last INSCOPE;              last INSCOPE;
5849            }            }
5850          } # INSCOPE          } # INSCOPE
# Line 5251  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
5859          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5860            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5861            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5862                !!!cp ('t378');
5863              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5864              !!!back-token;              !!!back-token;
5865              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5866              redo B;              redo B;
5867            } elsif ({            } elsif ({
5868                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5869                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5870                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5871                !!!cp ('t379');
5872              last INSCOPE;              last INSCOPE;
5873            }            }
5874          } # INSCOPE          } # INSCOPE
# Line 5266  sub _tree_construction_main ($) { Line 5876  sub _tree_construction_main ($) {
5876          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5877                        
5878          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5879    
5880            ## TODO: associate with $self->{form_element} if defined
5881    
5882          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5883    
5884          !!!next-token;          !!!next-token;
5885          redo B;          redo B;
5886        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ({
5887                 $token->{tag_name} eq 'object') {                  applet => 1, marquee => 1, object => 1,
5888                   }->{$token->{tag_name}}) {
5889            !!!cp ('t380');
5890          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5891                    
5892          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5280  sub _tree_construction_main ($) { Line 5895  sub _tree_construction_main ($) {
5895          !!!next-token;          !!!next-token;
5896          redo B;          redo B;
5897        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5898            !!!cp ('t381');
5899          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5900          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5901          redo B;          redo B;
5902        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5903          ## has a p element in scope          ## has a p element in scope
5904          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5905            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5906                !!!cp ('t382');
5907              !!!back-token;              !!!back-token;
5908              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5909              redo B;              redo B;
5910            } elsif ({            } elsif ({
5911                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5912                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5913                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5914                !!!cp ('t383');
5915              last INSCOPE;              last INSCOPE;
5916            }            }
5917          } # INSCOPE          } # INSCOPE
5918                        
5919          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5920                      push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5921    
5922          $self->{insertion_mode} = IN_TABLE_IM;          $self->{insertion_mode} = IN_TABLE_IM;
5923                        
5924          !!!next-token;          !!!next-token;
# Line 5310  sub _tree_construction_main ($) { Line 5929  sub _tree_construction_main ($) {
5929                  image => 1,                  image => 1,
5930                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5931          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5932              !!!cp ('t384');
5933            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5934            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5935            } else {
5936              !!!cp ('t385');
5937          }          }
5938    
5939          ## 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 5948  sub _tree_construction_main ($) {
5948          ## has a p element in scope          ## has a p element in scope
5949          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5950            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5951                !!!cp ('t386');
5952              !!!back-token;              !!!back-token;
5953              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5954              redo B;              redo B;
5955            } elsif ({            } elsif ({
5956                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5957                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5958                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5959                !!!cp ('t387');
5960              last INSCOPE;              last INSCOPE;
5961            }            }
5962          } # INSCOPE          } # INSCOPE
# Line 5343  sub _tree_construction_main ($) { Line 5967  sub _tree_construction_main ($) {
5967          !!!next-token;          !!!next-token;
5968          redo B;          redo B;
5969        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5970            !!!cp ('t388');
5971          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5972                    
5973          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5355  sub _tree_construction_main ($) { Line 5980  sub _tree_construction_main ($) {
5980          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5981                    
5982          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5983              !!!cp ('t389');
5984            ## Ignore the token            ## Ignore the token
5985            !!!next-token;            !!!next-token;
5986            redo B;            redo B;
# Line 5374  sub _tree_construction_main ($) { Line 6000  sub _tree_construction_main ($) {
6000                          {type => START_TAG_TOKEN, tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
6001                         );                         );
6002            if ($prompt_attr) {            if ($prompt_attr) {
6003                !!!cp ('t390');
6004              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
6005            } else {            } else {
6006                !!!cp ('t391');
6007              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6008                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
6009              ## TODO: make this configurable              ## TODO: make this configurable
# Line 5407  sub _tree_construction_main ($) { Line 6035  sub _tree_construction_main ($) {
6035          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6036            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6037            unless (length $token->{data}) {            unless (length $token->{data}) {
6038                !!!cp ('t392');
6039              !!!next-token;              !!!next-token;
6040              } else {
6041                !!!cp ('t393');
6042            }            }
6043            } else {
6044              !!!cp ('t394');
6045          }          }
6046          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6047              !!!cp ('t395');
6048            $text .= $token->{data};            $text .= $token->{data};
6049            !!!next-token;            !!!next-token;
6050          }          }
6051          if (length $text) {          if (length $text) {
6052              !!!cp ('t396');
6053            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6054          }          }
6055                    
# Line 5422  sub _tree_construction_main ($) { Line 6057  sub _tree_construction_main ($) {
6057                    
6058          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6059              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6060              !!!cp ('t397');
6061            ## Ignore the token            ## Ignore the token
6062          } else {          } else {
6063              !!!cp ('t398');
6064            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
6065          }          }
6066          !!!next-token;          !!!next-token;
# Line 5434  sub _tree_construction_main ($) { Line 6071  sub _tree_construction_main ($) {
6071                  noframes => 1,                  noframes => 1,
6072                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6073                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6074            !!!cp ('t399');
6075          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6076          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6077          redo B;          redo B;
6078        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
6079            !!!cp ('t400');
6080          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6081                    
6082          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6083    
6084            ## TODO: associate with $self->{form_element} if defined
6085                    
6086          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{insertion_mode} & TABLE_IMS or
6087                $self->{insertion_mode} & BODY_TABLE_IMS or
6088                $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6089              !!!cp ('t400.1');
6090              $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6091            } else {
6092              !!!cp ('t400.2');
6093              $self->{insertion_mode} = IN_SELECT_IM;
6094            }
6095          !!!next-token;          !!!next-token;
6096          redo B;          redo B;
6097        } elsif ({        } elsif ({
# Line 5451  sub _tree_construction_main ($) { Line 6100  sub _tree_construction_main ($) {
6100                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6101                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6102                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6103            !!!cp ('t401');
6104          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
6105          ## Ignore the token          ## Ignore the token
6106          !!!next-token;          !!!next-token;
# Line 5458  sub _tree_construction_main ($) { Line 6108  sub _tree_construction_main ($) {
6108                    
6109          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6110        } else {        } else {
6111            !!!cp ('t402');
6112          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6113                    
6114          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5475  sub _tree_construction_main ($) { Line 6126  sub _tree_construction_main ($) {
6126                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
6127                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
6128                      }->{$_->[1]}) {                      }->{$_->[1]}) {
6129                  !!!cp ('t403');
6130                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
6131                } else {
6132                  !!!cp ('t404');
6133              }              }
6134            }            }
6135    
# Line 5483  sub _tree_construction_main ($) { Line 6137  sub _tree_construction_main ($) {
6137            !!!next-token;            !!!next-token;
6138            redo B;            redo B;
6139          } else {          } else {
6140              !!!cp ('t405');
6141            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6142            ## Ignore the token            ## Ignore the token
6143            !!!next-token;            !!!next-token;
# Line 5492  sub _tree_construction_main ($) { Line 6147  sub _tree_construction_main ($) {
6147          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') {
6148            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6149            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6150                !!!cp ('t406');
6151              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
6152              } else {
6153                !!!cp ('t407');
6154            }            }
6155            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6156            ## reprocess            ## reprocess
6157            redo B;            redo B;
6158          } else {          } else {
6159              !!!cp ('t408');
6160            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6161            ## Ignore the token            ## Ignore the token
6162            !!!next-token;            !!!next-token;
# Line 5507  sub _tree_construction_main ($) { Line 6166  sub _tree_construction_main ($) {
6166                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6167                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6168                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6169                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6170                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6171                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6172          ## has an element in scope          ## has an element in scope
6173          my $i;          my $i;
6174          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6176            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6177              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6178              $i = $_;              $i = $_;
6179              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6180            } elsif ({            } elsif ({
6181                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6182                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6183                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6184                !!!cp ('t411');
6185              last INSCOPE;              last INSCOPE;
6186            }            }
6187          } # INSCOPE          } # INSCOPE
6188            
6189          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6190            if (defined $i) {            !!!cp ('t413');
6191              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6192            } else {
6193              ## Step 1. generate implied end tags
6194              while ({
6195                      dd => ($token->{tag_name} ne 'dd'),
6196                      dt => ($token->{tag_name} ne 'dt'),
6197                      li => ($token->{tag_name} ne 'li'),
6198                      p => 1,
6199                     }->{$self->{open_elements}->[-1]->[1]}) {
6200                !!!cp ('t409');
6201                pop @{$self->{open_elements}};
6202              }
6203    
6204              ## Step 2.
6205              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6206                !!!cp ('t412');
6207              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6208            } else {            } else {
6209              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6210            }            }
6211          }  
6212                      ## Step 3.
         if (defined $i) {  
6213            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6214          } elsif ($token->{tag_name} eq 'p') {  
6215            ## As if <p>, then reprocess the current token            ## Step 4.
6216            my $el;            $clear_up_to_marker->()
6217            !!!create-element ($el, 'p');                if {
6218            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6219                  }->{$token->{tag_name}};
6220          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6221          !!!next-token;          !!!next-token;
6222          redo B;          redo B;
6223        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6224            undef $self->{form_element};
6225    
6226          ## has an element in scope          ## has an element in scope
6227            my $i;
6228          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6229            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6230            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6231              ## generate implied end tags              !!!cp ('t418');
6232              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6233              last INSCOPE;              last INSCOPE;
6234            } elsif ({            } elsif ({
6235                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6236                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6237                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6238                !!!cp ('t419');
6239              last INSCOPE;              last INSCOPE;
6240            }            }
6241          } # INSCOPE          } # INSCOPE
6242            
6243          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6244            pop @{$self->{open_elements}};            !!!cp ('t421');
         } else {  
6245            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6246            } else {
6247              ## Step 1. generate implied end tags
6248              while ({
6249                      dd => 1, dt => 1, li => 1, p => 1,
6250                     }->{$self->{open_elements}->[-1]->[1]}) {
6251                !!!cp ('t417');
6252                pop @{$self->{open_elements}};
6253              }
6254              
6255              ## Step 2.
6256              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6257                !!!cp ('t417.1');
6258                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6259              } else {
6260                !!!cp ('t420');
6261              }  
6262              
6263              ## Step 3.
6264              splice @{$self->{open_elements}}, $i;
6265          }          }
6266    
         undef $self->{form_element};  
6267          !!!next-token;          !!!next-token;
6268          redo B;          redo B;
6269        } elsif ({        } elsif ({
# Line 5606  sub _tree_construction_main ($) { Line 6276  sub _tree_construction_main ($) {
6276            if ({            if ({
6277                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6278                }->{$node->[1]}) {                }->{$node->[1]}) {
6279              ## generate implied end tags              !!!cp ('t423');
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6280              $i = $_;              $i = $_;
6281              last INSCOPE;              last INSCOPE;
6282            } elsif ({            } elsif ({
6283                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6284                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6285                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6286                !!!cp ('t424');
6287              last INSCOPE;              last INSCOPE;
6288            }            }
6289          } # INSCOPE          } # INSCOPE
6290            
6291          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6292              !!!cp ('t425.1');
6293            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6294            } else {
6295              ## Step 1. generate implied end tags
6296              while ({
6297                      dd => 1, dt => 1, li => 1, p => 1,
6298                     }->{$self->{open_elements}->[-1]->[1]}) {
6299                !!!cp ('t422');
6300                pop @{$self->{open_elements}};
6301              }
6302              
6303              ## Step 2.
6304              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6305                !!!cp ('t425');
6306                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6307              } else {
6308                !!!cp ('t426');
6309              }
6310    
6311              ## Step 3.
6312              splice @{$self->{open_elements}}, $i;
6313          }          }
6314                    
6315          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6316            redo B;
6317          } elsif ($token->{tag_name} eq 'p') {
6318            ## has an element in scope
6319            my $i;
6320            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6321              my $node = $self->{open_elements}->[$_];
6322              if ($node->[1] eq $token->{tag_name}) {
6323                !!!cp ('t410.1');
6324                $i = $_;
6325                last INSCOPE;
6326              } elsif ({
6327                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6328                        button => 1, marquee => 1, object => 1, html => 1,
6329                       }->{$node->[1]}) {
6330                !!!cp ('t411.1');
6331                last INSCOPE;
6332              }
6333            } # INSCOPE
6334    
6335            if (defined $i) {
6336              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6337                !!!cp ('t412.1');
6338                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6339              } else {
6340                !!!cp ('t414.1');
6341              }
6342    
6343              splice @{$self->{open_elements}}, $i;
6344            } else {
6345              !!!cp ('t413.1');
6346              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6347    
6348              !!!cp ('t415.1');
6349              ## As if <p>, then reprocess the current token
6350              my $el;
6351              !!!create-element ($el, 'p');
6352              $insert->($el);
6353              ## NOTE: Not inserted into |$self->{open_elements}|.
6354            }
6355    
6356          !!!next-token;          !!!next-token;
6357          redo B;          redo B;
6358        } elsif ({        } elsif ({
# Line 5640  sub _tree_construction_main ($) { Line 6361  sub _tree_construction_main ($) {
6361                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6362                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6363                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6364            !!!cp ('t427');
6365          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6366          redo B;          redo B;
6367        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6368            !!!cp ('t428');
6369          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6370    
6371          ## As if <br>          ## As if <br>
# Line 5667  sub _tree_construction_main ($) { Line 6390  sub _tree_construction_main ($) {
6390                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6391                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6392                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6393            !!!cp ('t429');
6394          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6395          ## Ignore the token          ## Ignore the token
6396          !!!next-token;          !!!next-token;
# Line 5684  sub _tree_construction_main ($) { Line 6408  sub _tree_construction_main ($) {
6408            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6409              ## Step 1              ## Step 1
6410              ## generate implied end tags              ## generate implied end tags
6411              if ({              while ({
6412                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6413                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6414                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6415                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6416                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6417              }              }
6418                    
6419              ## Step 2              ## Step 2
6420              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6421                  !!!cp ('t431');
6422                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6423                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6424                } else {
6425                  !!!cp ('t432');
6426              }              }
6427                            
6428              ## Step 3              ## Step 3
# Line 5712  sub _tree_construction_main ($) { Line 6436  sub _tree_construction_main ($) {
6436                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6437                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6438                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6439                  !!!cp ('t433');
6440                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6441                ## Ignore the token                ## Ignore the token
6442                !!!next-token;                !!!next-token;
6443                last S2;                last S2;
6444              }              }
6445    
6446                !!!cp ('t434');
6447            }            }
6448                        
6449            ## Step 4            ## Step 4
# Line 5732  sub _tree_construction_main ($) { Line 6459  sub _tree_construction_main ($) {
6459      redo B;      redo B;
6460    } # B    } # B
6461    
   ## 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  
   
6462    ## Stop parsing # MUST    ## Stop parsing # MUST
6463        
6464    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5778  sub set_inner_html ($$$) { Line 6500  sub set_inner_html ($$$) {
6500      my $p = $class->new;      my $p = $class->new;
6501      $p->{document} = $doc;      $p->{document} = $doc;
6502    
6503      ## Step 9 # MUST      ## Step 8 # MUST
6504      my $i = 0;      my $i = 0;
6505      my $line = 1;      my $line = 1;
6506      my $column = 0;      my $column = 0;
# Line 5795  sub set_inner_html ($$$) { Line 6517  sub set_inner_html ($$$) {
6517        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6518          $line++;          $line++;
6519          $column = 0;          $column = 0;
6520            !!!cp ('i1');
6521        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6522          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6523          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6524          $line++;          $line++;
6525          $column = 0;          $column = 0;
6526            !!!cp ('i2');
6527        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6528          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6529            !!!cp ('i3');
6530        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
6531            !!!cp ('i4');
6532          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6533          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6534        }        }
# Line 5841  sub set_inner_html ($$$) { Line 6567  sub set_inner_html ($$$) {
6567    
6568      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6569    
6570      ## Step 4      ## Step 3
6571      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6572        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6573    
6574      ## Step 5 # MUST      ## Step 4 # MUST
6575      $doc->append_child ($root);      $doc->append_child ($root);
6576    
6577      ## Step 6 # MUST      ## Step 5 # MUST
6578      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6579    
6580      undef $p->{head_element};      undef $p->{head_element};
6581    
6582      ## Step 7 # MUST      ## Step 6 # MUST
6583      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6584    
6585      ## Step 8 # MUST      ## Step 7 # MUST
6586      my $anode = $node;      my $anode = $node;
6587      AN: while (defined $anode) {      AN: while (defined $anode) {
6588        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6589          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6590          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6591            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
6592                !!!cp ('i5');
6593              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6594              last AN;              last AN;
6595            }            }
# Line 5871  sub set_inner_html ($$$) { Line 6598  sub set_inner_html ($$$) {
6598        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6599      } # AN      } # AN
6600            
6601      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6602      {      {
6603        my $self = $p;        my $self = $p;
6604        !!!next-token;        !!!next-token;
6605      }      }
6606      $p->_tree_construction_main;      $p->_tree_construction_main;
6607    
6608      ## Step 11 # MUST      ## Step 10 # MUST
6609      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6610      for (@cn) {      for (@cn) {
6611        $node->remove_child ($_);        $node->remove_child ($_);
6612      }      }
6613      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6614    
6615      ## Step 12 # MUST      ## Step 11 # MUST
6616      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6617      for (@cn) {      for (@cn) {
6618        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24