/[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.82 by wakaba, Wed Mar 5 02:55:08 2008 UTC revision 1.105 by wakaba, Sun Mar 9 10:05:21 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 2420  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 2579  sub _tree_construction_initial ($) { Line 2595  sub _tree_construction_initial ($) {
2595          !!!cp ('t13');          !!!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 2590  sub _tree_construction_initial ($) { Line 2606  sub _tree_construction_initial ($) {
2606        !!!cp ('t14');        !!!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 2599  sub _tree_construction_initial ($) { Line 2615  sub _tree_construction_initial ($) {
2615    
2616          unless (length $token->{data}) {          unless (length $token->{data}) {
2617            !!!cp ('t15');            !!!cp ('t15');
2618            ## Stay in the phase            ## Stay in the insertion mode.
2619            !!!next-token;            !!!next-token;
2620            redo INITIAL;            redo INITIAL;
2621          } else {          } else {
# Line 2611  sub _tree_construction_initial ($) { Line 2627  sub _tree_construction_initial ($) {
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) {
# Line 2619  sub _tree_construction_initial ($) { Line 2635  sub _tree_construction_initial ($) {
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 {
# Line 2632  sub _tree_construction_initial ($) { Line 2648  sub _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');          !!!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');          !!!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 2654  sub _tree_construction_root_element ($) Line 2672  sub _tree_construction_root_element ($)
2672    
2673            unless (length $token->{data}) {            unless (length $token->{data}) {
2674              !!!cp ('t21');              !!!cp ('t21');
2675              ## Stay in the phase              ## Stay in the insertion mode.
2676              !!!next-token;              !!!next-token;
2677              redo B;              redo B;
2678            } else {            } else {
# Line 2668  sub _tree_construction_root_element ($) Line 2686  sub _tree_construction_root_element ($)
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            !!!cp ('t24');            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2692            $self->{application_cache_selection}            $self->{document}->append_child ($root_element);
2693                 ->($token->{attributes}->{manifest}->{value});            push @{$self->{open_elements}}, [$root_element, 'html'];
2694            ## ISSUE: No relative reference resolution?  
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            !!!cp ('t25');            !!!cp ('t25.1');
2709            $self->{application_cache_selection}->(undef);            #
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          !!!cp ('t26');          !!!cp ('t26');
         $self->{application_cache_selection}->(undef);  
   
         ## 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";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2717  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}) {
# Line 2739  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 2880  sub _tree_construction_main ($) { Line 2905  sub _tree_construction_main ($) {
2905      !!!cp ('t39');      !!!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 2889  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 2919  sub _tree_construction_main ($) { Line 2946  sub _tree_construction_main ($) {
2946          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
2947        !!!cp ('t42');        !!!cp ('t42');
2948        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!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 2983  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;
# Line 3026  sub _tree_construction_main ($) { Line 3061  sub _tree_construction_main ($) {
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');            !!!cp ('t56');
# Line 3053  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');            !!!cp ('t59');
3093            $furthest_block = $node;            $furthest_block = $node;
3094            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3139  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 3185  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;
# Line 3217  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                           !!!cp ('t72');      } else {
3283                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!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');        !!!cp ('t73');
# Line 3233  sub _tree_construction_main ($) { Line 3293  sub _tree_construction_main ($) {
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) {  
         !!!cp ('t74');  
         #  
       } 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]}) {  
           !!!cp ('t75');  
           !!!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')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
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          !!!cp ('t79');          !!!cp ('t79');
         ## Turn into the main phase  
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          !!!cp ('t80');          !!!cp ('t80');
         ## Turn into the main phase  
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 {        } else {
3307          !!!cp ('t81');          !!!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}) {  
         !!!cp ('t82');  
         !!!parse-error (type => 'not first start tag');  
       } else {  
         !!!cp ('t83');  
       }  
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)) {
# Line 3320  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');              !!!cp ('t88');
3351              !!!next-token;              !!!next-token;
# Line 3421  sub _tree_construction_main ($) { Line 3446  sub _tree_construction_main ($) {
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;
# Line 3436  sub _tree_construction_main ($) { Line 3461  sub _tree_construction_main ($) {
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;
# Line 3497  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;
# Line 3521  sub _tree_construction_main ($) { Line 3546  sub _tree_construction_main ($) {
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 3537  sub _tree_construction_main ($) { Line 3561  sub _tree_construction_main ($) {
3561                } else {                } else {
3562                  !!!cp ('t115');                  !!!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') {
# Line 3577  sub _tree_construction_main ($) { Line 3601  sub _tree_construction_main ($) {
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
# Line 3787  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) {
# Line 3858  sub _tree_construction_main ($) { Line 3935  sub _tree_construction_main ($) {
3935                  } # INSCOPE                  } # INSCOPE
3936                    unless (defined $i) {                    unless (defined $i) {
3937                      !!!cp ('t157');                      !!!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 3865  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]}) {
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
3949                    !!!cp ('t158');                    !!!cp ('t158');
3950                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $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') {
# Line 3929  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]}) {
                      th => ($token->{tag_name} eq 'td'),  
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4006                    !!!cp ('t166');                    !!!cp ('t166');
4007                    !!!back-token;                    pop @{$self->{open_elements}};
                   $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');                    !!!cp ('t167');
4012                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
# Line 3994  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]}) {
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4064                    !!!cp ('t174');                    !!!cp ('t174');
4065                    !!!back-token;                    pop @{$self->{open_elements}};
                   $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') {
# Line 4100  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]}) {
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4165                  !!!cp ('t187');                  !!!cp ('t187');
4166                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $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') {
# Line 4156  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208                !!!cp ('t193');                !!!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 4164  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                  !!!cp ('t194');              !!!cp ('t194');
4239                  !!!next-token;              !!!next-token;
4240                  redo B;              redo B;
4241                } else {            } else {
4242                  !!!cp ('t195');              !!!cp ('t195');
4243                }            }
4244              }          }
4245    
4246              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4247    
# Line 4221  sub _tree_construction_main ($) { Line 4288  sub _tree_construction_main ($) {
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                !!!cp ('t200');          } else {
4293                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!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 4238  sub _tree_construction_main ($) { Line 4306  sub _tree_construction_main ($) {
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                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4309                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4310                  }                  }
4311                                    
# Line 4258  sub _tree_construction_main ($) { Line 4325  sub _tree_construction_main ($) {
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                    !!!cp ('t203');                    !!!cp ('t203');
4328                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4329                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4330                  }                  }
4331                                    
# Line 4282  sub _tree_construction_main ($) { Line 4349  sub _tree_construction_main ($) {
4349                  tr => 1, html => 1,                  tr => 1, html => 1,
4350                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4351                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4352                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4353                }                }
4354                                
# Line 4309  sub _tree_construction_main ($) { Line 4375  sub _tree_construction_main ($) {
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');                      !!!cp ('t209');
4384                      last INSCOPE;                      last INSCOPE;
# Line 4317  sub _tree_construction_main ($) { Line 4386  sub _tree_construction_main ($) {
4386                  } # INSCOPE                  } # INSCOPE
4387                  unless (defined $i) {                  unless (defined $i) {
4388                   !!!cp ('t210');                   !!!cp ('t210');
4389    ## TODO: This type is wrong.
4390                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4391                    ## Ignore the token                    ## Ignore the token
4392                    !!!next-token;                    !!!next-token;
# Line 4328  sub _tree_construction_main ($) { Line 4398  sub _tree_construction_main ($) {
4398                    tr => 1, html => 1,                    tr => 1, html => 1,
4399                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4400                    !!!cp ('t211');                    !!!cp ('t211');
4401                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4402                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4403                  }                  }
4404                                    
# Line 4376  sub _tree_construction_main ($) { Line 4446  sub _tree_construction_main ($) {
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                    !!!cp ('t217');                    !!!cp ('t217');
4449                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4450                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4451                  }                  }
4452                                    
# Line 4399  sub _tree_construction_main ($) { Line 4469  sub _tree_construction_main ($) {
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                    !!!cp ('t219');                    !!!cp ('t219');
4472                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4473                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4474                  }                  }
4475                                    
# Line 4416  sub _tree_construction_main ($) { Line 4486  sub _tree_construction_main ($) {
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                    !!!cp ('t220');                    !!!cp ('t220');
4489                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4490                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4491                  }                  }
4492                                    
# Line 4449  sub _tree_construction_main ($) { Line 4519  sub _tree_construction_main ($) {
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');                    !!!cp ('t222');
4526                    last INSCOPE;                    last INSCOPE;
# Line 4457  sub _tree_construction_main ($) { Line 4528  sub _tree_construction_main ($) {
4528                } # INSCOPE                } # INSCOPE
4529                unless (defined $i) {                unless (defined $i) {
4530                  !!!cp ('t223');                  !!!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;
# Line 4464  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                }                }
4537                                
4538                ## generate implied end tags                ## generate implied end tags
4539                if ({                while ({
4540                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4541                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4542                  !!!cp ('t224');                  !!!cp ('t224');
4543                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $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;  
4544                }                }
4545    
4546                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4547                  !!!cp ('t225');                  !!!cp ('t225');
4548    ## ISSUE: Can this case be reached?
4549                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4550                } else {                } else {
4551                  !!!cp ('t226');                  !!!cp ('t226');
4552                }                }
4553    
4554                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4555                  pop @{$open_tables};
4556    
4557                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4558    
4559                ## reprocess                ## reprocess
4560                redo B;                redo B;
4561            } elsif ($token->{tag_name} eq 'style') {
4562              if (not $open_tables->[-1]->[1]) { # tainted
4563                !!!cp ('t227.8');
4564                ## NOTE: This is a "as if in head" code clone.
4565                $parse_rcdata->(CDATA_CONTENT_MODEL);
4566                redo B;
4567              } else {
4568                !!!cp ('t227.7');
4569                #
4570              }
4571            } elsif ($token->{tag_name} eq 'script') {
4572              if (not $open_tables->[-1]->[1]) { # tainted
4573                !!!cp ('t227.6');
4574                ## NOTE: This is a "as if in head" code clone.
4575                $script_start_tag->();
4576                redo B;
4577              } else {
4578                !!!cp ('t227.5');
4579                #
4580              }
4581            } elsif ($token->{tag_name} eq 'input') {
4582              if (not $open_tables->[-1]->[1]) { # tainted
4583                if ($token->{attributes}->{type}) { ## TODO: case
4584                  my $type = lc $token->{attributes}->{type}->{value};
4585                  if ($type eq 'hidden') {
4586                    !!!cp ('t227.3');
4587                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4588    
4589                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4590    
4591                    ## TODO: form element pointer
4592    
4593                    pop @{$self->{open_elements}};
4594    
4595                    !!!next-token;
4596                    redo B;
4597                  } else {
4598                    !!!cp ('t227.2');
4599                    #
4600                  }
4601                } else {
4602                  !!!cp ('t227.1');
4603                  #
4604                }
4605              } else {
4606                !!!cp ('t227.4');
4607                #
4608              }
4609          } else {          } else {
4610            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4611            #            #
4612          }          }
4613    
4614            !!!parse-error (type => 'in table:'.$token->{tag_name});
4615    
4616            $insert = $insert_to_foster;
4617            #
4618        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4619              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4620                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4531  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648                  tr => 1, html => 1,                  tr => 1, html => 1,
4649                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4650                  !!!cp ('t231');                  !!!cp ('t231');
4651                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4652                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4653                }                }
4654    
# Line 4559  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676                  } # INSCOPE                  } # INSCOPE
4677                  unless (defined $i) {                  unless (defined $i) {
4678                    !!!cp ('t235');                    !!!cp ('t235');
4679    ## TODO: The following is wrong.
4680                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4681                    ## Ignore the token                    ## Ignore the token
4682                    !!!next-token;                    !!!next-token;
# Line 4570  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688                    tr => 1, html => 1,                    tr => 1, html => 1,
4689                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4690                    !!!cp ('t236');                    !!!cp ('t236');
4691                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4692                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4693                  }                  }
4694                                    
# Line 4610  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4729                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4730                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4731                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4732                  }                  }
4733                                    
# Line 4626  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4744                }                }
4745    
4746                  ## NOTE: </table> in the "in table" insertion mode.
4747                  ## When you edit the code fragment below, please ensure that
4748                  ## the code for <table> in the "in table" insertion mode
4749                  ## is synced with it.
4750    
4751                ## have a table element in table scope                ## have a table element in table scope
4752                my $i;                my $i;
4753                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4648  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                  !!!next-token;                  !!!next-token;
4771                  redo B;                  redo B;
4772                }                }
   
               ## 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]}) {  
                 !!!cp ('t244');  
                 !!!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') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
               }  
4773                                    
4774                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4775                  pop @{$open_tables};
4776                                
4777                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4778                                
# Line 4732  sub _tree_construction_main ($) { Line 4835  sub _tree_construction_main ($) {
4835                    tr => 1, html => 1,                    tr => 1, html => 1,
4836                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4837                    !!!cp ('t253');                    !!!cp ('t253');
4838                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
4839                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4840                  }                  }
4841                                    
# Line 4769  sub _tree_construction_main ($) { Line 4872  sub _tree_construction_main ($) {
4872                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4873                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4874                  !!!cp ('t257');                  !!!cp ('t257');
4875                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
4876                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4877                }                }
4878    
# Line 4795  sub _tree_construction_main ($) { Line 4898  sub _tree_construction_main ($) {
4898            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4899            #            #
4900          }          }
4901          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4902            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4903                    @{$self->{open_elements}} == 1) { # redundant, maybe
4904              !!!parse-error (type => 'in body:#eof');
4905              !!!cp ('t259.1');
4906              #
4907            } else {
4908              !!!cp ('t259.2');
4909              #
4910            }
4911    
4912            ## Stop parsing
4913            last B;
4914        } else {        } else {
4915          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4916        }        }
# Line 4847  sub _tree_construction_main ($) { Line 4963  sub _tree_construction_main ($) {
4963                !!!cp ('t267');                !!!cp ('t267');
4964                #                #
4965              }              }
4966            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4967              !!!cp ('t268');          if ($self->{open_elements}->[-1]->[1] eq 'html' or
4968              #              @{$self->{open_elements}} == 1) { # redundant, maybe
4969            }            !!!cp ('t270.2');
4970              ## Stop parsing.
4971              last B;
4972            } else {
4973              ## NOTE: As if </colgroup>.
4974              !!!cp ('t270.1');
4975              pop @{$self->{open_elements}}; # colgroup
4976              $self->{insertion_mode} = IN_TABLE_IM;
4977              ## Reprocess.
4978              redo B;
4979            }
4980          } else {
4981            die "$0: $token->{type}: Unknown token type";
4982          }
4983    
4984            ## As if </colgroup>            ## As if </colgroup>
4985            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4986              !!!cp ('t269');              !!!cp ('t269');
4987    ## TODO: Wrong error type?
4988              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4989              ## Ignore the token              ## Ignore the token
4990              !!!next-token;              !!!next-token;
# Line 4866  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996              ## reprocess              ## reprocess
4997              redo B;              redo B;
4998            }            }
4999      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5000        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5001          !!!cp ('t271');          !!!cp ('t271');
5002          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
# Line 4905  sub _tree_construction_main ($) { Line 5035  sub _tree_construction_main ($) {
5035                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5036                !!!next-token;                !!!next-token;
5037                redo B;                redo B;
5038              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5039                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5040                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5041                      {
5042                       caption => 1, table => 1,
5043                       tbody => 1, tfoot => 1, thead => 1,
5044                       tr => 1, td => 1, th => 1,
5045                      }->{$token->{tag_name}})) {
5046              ## TODO: The type below is not good - <select> is replaced by </select>
5047              !!!parse-error (type => 'not closed:select');
5048              ## NOTE: As if the token were </select> (<select> case) or
5049              ## as if there were </select> (otherwise).
5050                ## have an element in table scope                ## have an element in table scope
5051                my $i;                my $i;
5052                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5053                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5054                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5055                    !!!cp ('t278');                    !!!cp ('t278');
5056                    $i = $_;                    $i = $_;
5057                    last INSCOPE;                    last INSCOPE;
# Line 4936  sub _tree_construction_main ($) { Line 5075  sub _tree_construction_main ($) {
5075    
5076                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5077    
5078                !!!next-token;            if ($token->{tag_name} eq 'select') {
5079                redo B;              !!!cp ('t281.2');
5080                !!!next-token;
5081                redo B;
5082              } else {
5083                !!!cp ('t281.1');
5084                ## Reprocess the token.
5085                redo B;
5086              }
5087          } else {          } else {
5088            !!!cp ('t282');            !!!cp ('t282');
5089            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name});
# Line 5004  sub _tree_construction_main ($) { Line 5150  sub _tree_construction_main ($) {
5150    
5151                !!!next-token;                !!!next-token;
5152                redo B;                redo B;
5153              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5154                        caption => 1, table => 1, tbody => 1,                   {
5155                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5156                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5157                     }->{$token->{tag_name}}) {
5158    ## TODO: The following is wrong?
5159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5160                                
5161                ## have an element in table scope                ## have an element in table scope
# Line 5044  sub _tree_construction_main ($) { Line 5192  sub _tree_construction_main ($) {
5192                  } elsif ({                  } elsif ({
5193                            table => 1, html => 1,                            table => 1, html => 1,
5194                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5195    ## ISSUE: Can this state be reached?
5196                    !!!cp ('t296');                    !!!cp ('t296');
5197                    last INSCOPE;                    last INSCOPE;
5198                  }                  }
5199                } # INSCOPE                } # INSCOPE
5200                unless (defined $i) {                unless (defined $i) {
5201                  !!!cp ('t297');                  !!!cp ('t297');
5202    ## TODO: The following error type is correct?
5203                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5204                  ## Ignore the </select> token                  ## Ignore the </select> token
5205                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
# Line 5070  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220            !!!next-token;            !!!next-token;
5221            redo B;            redo B;
5222          }          }
5223          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5224            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5225                    @{$self->{open_elements}} == 1) { # redundant, maybe
5226              !!!cp ('t299.1');
5227              !!!parse-error (type => 'in body:#eof');
5228            } else {
5229              !!!cp ('t299.2');
5230            }
5231    
5232            ## Stop parsing.
5233            last B;
5234        } else {        } else {
5235          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5236        }        }
# Line 5093  sub _tree_construction_main ($) { Line 5254  sub _tree_construction_main ($) {
5254            !!!cp ('t301');            !!!cp ('t301');
5255            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5256    
5257            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5258          } else {          } else {
5259            !!!cp ('t302');            !!!cp ('t302');
5260          }          }
# Line 5109  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270            !!!cp ('t303');            !!!cp ('t303');
5271            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5272                        
5273            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5274          } else {          } else {
5275            !!!cp ('t304');            !!!cp ('t304');
5276          }          }
# Line 5126  sub _tree_construction_main ($) { Line 5287  sub _tree_construction_main ($) {
5287            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5288                        
5289            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5290            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5291          } else {          } else {
5292            !!!cp ('t306');            !!!cp ('t306');
5293          }          }
# Line 5153  sub _tree_construction_main ($) { Line 5314  sub _tree_construction_main ($) {
5314            ## reprocess            ## reprocess
5315            redo B;            redo B;
5316          }          }
5317          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5318            !!!cp ('t309.2');
5319            ## Stop parsing
5320            last B;
5321        } else {        } else {
5322          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5323        }        }
# Line 5180  sub _tree_construction_main ($) { Line 5345  sub _tree_construction_main ($) {
5345              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5346    
5347              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5348              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5349              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5350            }            }
5351                        
# Line 5202  sub _tree_construction_main ($) { Line 5367  sub _tree_construction_main ($) {
5367            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5368    
5369            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5370            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5371          } else {          } else {
5372            !!!cp ('t317');            !!!cp ('t317');
5373          }          }
# Line 5223  sub _tree_construction_main ($) { Line 5388  sub _tree_construction_main ($) {
5388          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5389            !!!cp ('t320');            !!!cp ('t320');
5390            ## NOTE: As if in body.            ## NOTE: As if in body.
5391            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5392            redo B;            redo B;
5393          } else {          } else {
5394            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
# Line 5243  sub _tree_construction_main ($) { Line 5408  sub _tree_construction_main ($) {
5408            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5409    
5410            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5411            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5412          } else {          } else {
5413            !!!cp ('t324');            !!!cp ('t324');
5414          }          }
# Line 5288  sub _tree_construction_main ($) { Line 5453  sub _tree_construction_main ($) {
5453            !!!next-token;            !!!next-token;
5454            redo B;            redo B;
5455          }          }
5456          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5457            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5458                    @{$self->{open_elements}} == 1) { # redundant, maybe
5459              !!!cp ('t331.1');
5460              !!!parse-error (type => 'in body:#eof');
5461            } else {
5462              !!!cp ('t331.2');
5463            }
5464            
5465            ## Stop parsing
5466            last B;
5467        } else {        } else {
5468          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5469        }        }
# Line 5302  sub _tree_construction_main ($) { Line 5478  sub _tree_construction_main ($) {
5478        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5479          !!!cp ('t332');          !!!cp ('t332');
5480          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5481          $script_start_tag->($insert);          $script_start_tag->();
5482          redo B;          redo B;
5483        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5484          !!!cp ('t333');          !!!cp ('t333');
5485          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5486          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5487          redo B;          redo B;
5488        } elsif ({        } elsif ({
5489                  base => 1, link => 1,                  base => 1, link => 1,
# Line 5370  sub _tree_construction_main ($) { Line 5546  sub _tree_construction_main ($) {
5546          redo B;          redo B;
5547        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5548          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5549          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5550          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
5551          redo B;          redo B;
5552        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5553          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 5404  sub _tree_construction_main ($) { Line 5571  sub _tree_construction_main ($) {
5571          redo B;          redo B;
5572        } elsif ({        } elsif ({
5573                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5574                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5575                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5576                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5577                  pre => 1,                  pre => 1, listing => 1,
5578                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5579          ## has a p element in scope          ## has a p element in scope
5580          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
# Line 5416  sub _tree_construction_main ($) { Line 5584  sub _tree_construction_main ($) {
5584              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5585              redo B;              redo B;
5586            } elsif ({            } elsif ({
5587                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5588                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5589                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5590              !!!cp ('t345');              !!!cp ('t345');
# Line 5425  sub _tree_construction_main ($) { Line 5593  sub _tree_construction_main ($) {
5593          } # INSCOPE          } # INSCOPE
5594                        
5595          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5596          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5597            !!!next-token;            !!!next-token;
5598            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5599              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5459  sub _tree_construction_main ($) { Line 5627  sub _tree_construction_main ($) {
5627                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5628                redo B;                redo B;
5629              } elsif ({              } elsif ({
5630                        table => 1, caption => 1, td => 1, th => 1,                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5631                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5632                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5633                !!!cp ('t352');                !!!cp ('t352');
# Line 5481  sub _tree_construction_main ($) { Line 5649  sub _tree_construction_main ($) {
5649              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5650              redo B;              redo B;
5651            } elsif ({            } elsif ({
5652                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5653                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5654                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5655              !!!cp ('t354');              !!!cp ('t354');
# Line 5537  sub _tree_construction_main ($) { Line 5705  sub _tree_construction_main ($) {
5705              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5706              redo B;              redo B;
5707            } elsif ({            } elsif ({
5708                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5709                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5710                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5711              !!!cp ('t361');              !!!cp ('t361');
# Line 5593  sub _tree_construction_main ($) { Line 5761  sub _tree_construction_main ($) {
5761              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5762              redo B;              redo B;
5763            } elsif ({            } elsif ({
5764                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5765                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5766                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5767              !!!cp ('t368');              !!!cp ('t368');
# Line 5607  sub _tree_construction_main ($) { Line 5775  sub _tree_construction_main ($) {
5775                        
5776          !!!next-token;          !!!next-token;
5777          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') {  
             !!!cp ('t369');  
             !!!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]}) {  
             !!!cp ('t370');  
             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;  
5778        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5779          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5780            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 5719  sub _tree_construction_main ($) { Line 5840  sub _tree_construction_main ($) {
5840              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5841              redo B;              redo B;
5842            } elsif ({            } elsif ({
5843                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5844                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5845                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5846              !!!cp ('t377');              !!!cp ('t377');
# Line 5743  sub _tree_construction_main ($) { Line 5864  sub _tree_construction_main ($) {
5864              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5865              redo B;              redo B;
5866            } elsif ({            } elsif ({
5867                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5868                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5869                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5870              !!!cp ('t379');              !!!cp ('t379');
# Line 5754  sub _tree_construction_main ($) { Line 5875  sub _tree_construction_main ($) {
5875          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5876                        
5877          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5878    
5879            ## TODO: associate with $self->{form_element} if defined
5880    
5881          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5882    
5883          !!!next-token;          !!!next-token;
5884          redo B;          redo B;
5885        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ({
5886                 $token->{tag_name} eq 'object') {                  applet => 1, marquee => 1, object => 1,
5887                   }->{$token->{tag_name}}) {
5888          !!!cp ('t380');          !!!cp ('t380');
5889          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5890                    
# Line 5771  sub _tree_construction_main ($) { Line 5896  sub _tree_construction_main ($) {
5896        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5897          !!!cp ('t381');          !!!cp ('t381');
5898          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5899          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5900          redo B;          redo B;
5901        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5902          ## has a p element in scope          ## has a p element in scope
# Line 5782  sub _tree_construction_main ($) { Line 5907  sub _tree_construction_main ($) {
5907              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5908              redo B;              redo B;
5909            } elsif ({            } elsif ({
5910                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5911                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5912                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5913              !!!cp ('t383');              !!!cp ('t383');
# Line 5791  sub _tree_construction_main ($) { Line 5916  sub _tree_construction_main ($) {
5916          } # INSCOPE          } # INSCOPE
5917                        
5918          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5919                      push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5920    
5921          $self->{insertion_mode} = IN_TABLE_IM;          $self->{insertion_mode} = IN_TABLE_IM;
5922                        
5923          !!!next-token;          !!!next-token;
# Line 5826  sub _tree_construction_main ($) { Line 5952  sub _tree_construction_main ($) {
5952              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5953              redo B;              redo B;
5954            } elsif ({            } elsif ({
5955                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5956                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5957                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5958              !!!cp ('t387');              !!!cp ('t387');
# Line 5946  sub _tree_construction_main ($) { Line 6072  sub _tree_construction_main ($) {
6072                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6073          !!!cp ('t399');          !!!cp ('t399');
6074          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6075          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6076          redo B;          redo B;
6077        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
6078          !!!cp ('t400');          !!!cp ('t400');
6079          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6080                    
6081          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6082    
6083            ## TODO: associate with $self->{form_element} if defined
6084                    
6085          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{insertion_mode} & TABLE_IMS or
6086                $self->{insertion_mode} & BODY_TABLE_IMS or
6087                $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6088              !!!cp ('t400.1');
6089              $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6090            } else {
6091              !!!cp ('t400.2');
6092              $self->{insertion_mode} = IN_SELECT_IM;
6093            }
6094          !!!next-token;          !!!next-token;
6095          redo B;          redo B;
6096        } elsif ({        } elsif ({
# Line 6029  sub _tree_construction_main ($) { Line 6165  sub _tree_construction_main ($) {
6165                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6166                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6167                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6168                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6169                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6170                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6171          ## has an element in scope          ## has an element in scope
6172          my $i;          my $i;
6173          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6174            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6175            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## generate implied end tags  
             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]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6176              !!!cp ('t410');              !!!cp ('t410');
6177              $i = $_;              $i = $_;
6178              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6179            } elsif ({            } elsif ({
6180                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6181                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6182                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6183              !!!cp ('t411');              !!!cp ('t411');
6184              last INSCOPE;              last INSCOPE;
6185            }            }
6186          } # INSCOPE          } # INSCOPE
6187            
6188          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6189            if (defined $i) {            !!!cp ('t413');
6190              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6191            } else {
6192              ## Step 1. generate implied end tags
6193              while ({
6194                      dd => ($token->{tag_name} ne 'dd'),
6195                      dt => ($token->{tag_name} ne 'dt'),
6196                      li => ($token->{tag_name} ne 'li'),
6197                      p => 1,
6198                     }->{$self->{open_elements}->[-1]->[1]}) {
6199                !!!cp ('t409');
6200                pop @{$self->{open_elements}};
6201              }
6202    
6203              ## Step 2.
6204              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6205              !!!cp ('t412');              !!!cp ('t412');
6206              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6207            } else {            } else {
6208              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6209            }            }
6210          }  
6211                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6212            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6213          } elsif ($token->{tag_name} eq 'p') {  
6214            !!!cp ('t415');            ## Step 4.
6215            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6216            my $el;                if {
6217            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6218            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6219          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6220          !!!next-token;          !!!next-token;
6221          redo B;          redo B;
6222        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6223            undef $self->{form_element};
6224    
6225          ## has an element in scope          ## has an element in scope
6226            my $i;
6227          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6228            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6229            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## 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]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6230              !!!cp ('t418');              !!!cp ('t418');
6231                $i = $_;
6232              last INSCOPE;              last INSCOPE;
6233            } elsif ({            } elsif ({
6234                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6235                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6236                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6237              !!!cp ('t419');              !!!cp ('t419');
6238              last INSCOPE;              last INSCOPE;
6239            }            }
6240          } # INSCOPE          } # INSCOPE
6241            
6242          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6243            !!!cp ('t421');            !!!cp ('t421');
6244            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6245            } else {
6246              ## Step 1. generate implied end tags
6247              while ({
6248                      dd => 1, dt => 1, li => 1, p => 1,
6249                     }->{$self->{open_elements}->[-1]->[1]}) {
6250                !!!cp ('t417');
6251                pop @{$self->{open_elements}};
6252              }
6253              
6254              ## Step 2.
6255              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6256                !!!cp ('t417.1');
6257                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6258              } else {
6259                !!!cp ('t420');
6260              }  
6261              
6262              ## Step 3.
6263              splice @{$self->{open_elements}}, $i;
6264          }          }
6265    
         undef $self->{form_element};  
6266          !!!next-token;          !!!next-token;
6267          redo B;          redo B;
6268        } elsif ({        } elsif ({
# Line 6144  sub _tree_construction_main ($) { Line 6275  sub _tree_construction_main ($) {
6275            if ({            if ({
6276                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6277                }->{$node->[1]}) {                }->{$node->[1]}) {
             ## 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]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
6278              !!!cp ('t423');              !!!cp ('t423');
6279              $i = $_;              $i = $_;
6280              last INSCOPE;              last INSCOPE;
6281            } elsif ({            } elsif ({
6282                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6283                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6284                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6285              !!!cp ('t424');              !!!cp ('t424');
6286              last INSCOPE;              last INSCOPE;
6287            }            }
6288          } # INSCOPE          } # INSCOPE
6289            
6290          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6291            !!!cp ('t425');            !!!cp ('t425.1');
6292            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6293          } else {          } else {
6294            !!!cp ('t426');            ## Step 1. generate implied end tags
6295              while ({
6296                      dd => 1, dt => 1, li => 1, p => 1,
6297                     }->{$self->{open_elements}->[-1]->[1]}) {
6298                !!!cp ('t422');
6299                pop @{$self->{open_elements}};
6300              }
6301              
6302              ## Step 2.
6303              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6304                !!!cp ('t425');
6305                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6306              } else {
6307                !!!cp ('t426');
6308              }
6309    
6310              ## Step 3.
6311              splice @{$self->{open_elements}}, $i;
6312          }          }
6313                    
6314          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6315            redo B;
6316          } elsif ($token->{tag_name} eq 'p') {
6317            ## has an element in scope
6318            my $i;
6319            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6320              my $node = $self->{open_elements}->[$_];
6321              if ($node->[1] eq $token->{tag_name}) {
6322                !!!cp ('t410.1');
6323                $i = $_;
6324                last INSCOPE;
6325              } elsif ({
6326                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6327                        button => 1, marquee => 1, object => 1, html => 1,
6328                       }->{$node->[1]}) {
6329                !!!cp ('t411.1');
6330                last INSCOPE;
6331              }
6332            } # INSCOPE
6333    
6334            if (defined $i) {
6335              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6336                !!!cp ('t412.1');
6337                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6338              } else {
6339                !!!cp ('t414.1');
6340              }
6341    
6342              splice @{$self->{open_elements}}, $i;
6343            } else {
6344              !!!cp ('t413.1');
6345              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6346    
6347              !!!cp ('t415.1');
6348              ## As if <p>, then reprocess the current token
6349              my $el;
6350              !!!create-element ($el, 'p');
6351              $insert->($el);
6352              ## NOTE: Not inserted into |$self->{open_elements}|.
6353            }
6354    
6355          !!!next-token;          !!!next-token;
6356          redo B;          redo B;
6357        } elsif ({        } elsif ({
# Line 6232  sub _tree_construction_main ($) { Line 6407  sub _tree_construction_main ($) {
6407            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6408              ## Step 1              ## Step 1
6409              ## generate implied end tags              ## generate implied end tags
6410              if ({              while ({
6411                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6412                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6413                !!!cp ('t430');                !!!cp ('t430');
6414                !!!back-token;                ## ISSUE: Can this case be reached?
6415                $token = {type => END_TAG_TOKEN,                pop @{$self->{open_elements}};
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6416              }              }
6417                    
6418              ## Step 2              ## Step 2
# Line 6287  sub _tree_construction_main ($) { Line 6458  sub _tree_construction_main ($) {
6458      redo B;      redo B;
6459    } # B    } # B
6460    
   ## 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  
   
6461    ## Stop parsing # MUST    ## Stop parsing # MUST
6462        
6463    ## TODO: script stuffs    ## TODO: script stuffs
# Line 6333  sub set_inner_html ($$$) { Line 6499  sub set_inner_html ($$$) {
6499      my $p = $class->new;      my $p = $class->new;
6500      $p->{document} = $doc;      $p->{document} = $doc;
6501    
6502      ## Step 9 # MUST      ## Step 8 # MUST
6503      my $i = 0;      my $i = 0;
6504      my $line = 1;      my $line = 1;
6505      my $column = 0;      my $column = 0;
# Line 6400  sub set_inner_html ($$$) { Line 6566  sub set_inner_html ($$$) {
6566    
6567      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6568    
6569      ## Step 4      ## Step 3
6570      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6571        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6572    
6573      ## Step 5 # MUST      ## Step 4 # MUST
6574      $doc->append_child ($root);      $doc->append_child ($root);
6575    
6576      ## Step 6 # MUST      ## Step 5 # MUST
6577      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6578    
6579      undef $p->{head_element};      undef $p->{head_element};
6580    
6581      ## Step 7 # MUST      ## Step 6 # MUST
6582      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6583    
6584      ## Step 8 # MUST      ## Step 7 # MUST
6585      my $anode = $node;      my $anode = $node;
6586      AN: while (defined $anode) {      AN: while (defined $anode) {
6587        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
# Line 6431  sub set_inner_html ($$$) { Line 6597  sub set_inner_html ($$$) {
6597        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6598      } # AN      } # AN
6599            
6600      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6601      {      {
6602        my $self = $p;        my $self = $p;
6603        !!!next-token;        !!!next-token;
6604      }      }
6605      $p->_tree_construction_main;      $p->_tree_construction_main;
6606    
6607      ## Step 11 # MUST      ## Step 10 # MUST
6608      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6609      for (@cn) {      for (@cn) {
6610        $node->remove_child ($_);        $node->remove_child ($_);
6611      }      }
6612      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6613    
6614      ## Step 12 # MUST      ## Step 11 # MUST
6615      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6616      for (@cn) {      for (@cn) {
6617        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

Legend:
Removed from v.1.82  
changed lines
  Added in v.1.105

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24