/[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.57 by wakaba, Sat Aug 11 08:08:12 2007 UTC revision 1.68 by wakaba, Sun Feb 17 12:18:06 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
# Line 84  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        ## TODO: Implement HTML5 detection algorithm
101        require Whatpm::Charset::UniversalCharDet;
102        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
103            (substr ($$bytes_s, 0, 1024));
104        $charset ||= 'windows-1252';
105        $s = \ (Encode::decode ($charset, $$bytes_s));
106        $self->{input_encoding} = $charset;
107        $self->{confident} = 0;
108      }
109    
110      $self->{change_encoding} = sub {
111        my $self = shift;
112        my $charset = lc shift;
113        ## TODO: if $charset is supported
114        ## TODO: normalize charset name
115    
116        ## "Change the encoding" algorithm:
117    
118        ## Step 1    
119        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
120          $charset = 'utf-8';
121        }
122    
123        ## Step 2
124        if (defined $self->{input_encoding} and
125            $self->{input_encoding} eq $charset) {
126          $self->{confident} = 1;
127          return;
128        }
129    
130        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
131            ':'.$charset, level => 'w');
132    
133        ## Step 3
134        # if (can) {
135          ## change the encoding on the fly.
136          #$self->{confident} = 1;
137          #return;
138        # }
139    
140        ## Step 4
141        throw Whatpm::HTML::RestartParser (charset => $charset);
142      }; # $self->{change_encoding}
143    
144      my @args = @_; shift @args; # $s
145      my $return;
146      try {
147        $return = $self->parse_char_string ($s, @args);  
148      } catch Whatpm::HTML::RestartParser with {
149        my $charset = shift->{charset};
150        $s = \ (Encode::decode ($charset, $$bytes_s));    
151        $self->{input_encoding} = $charset; ## TODO: normalize
152        $self->{confident} = 1;
153        $return = $self->parse_char_string ($s, @args);
154      };
155      return $return;
156    } # parse_byte_string
157    
158    *parse_char_string = \&parse_string;
159    
160  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
161    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
162    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
163    $self->{document} = $_[1];    $self->{document} = $_[1];
164      @{$self->{document}->child_nodes} = ();
165    
166    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
167    
168      $self->{confident} = 1 unless exists $self->{confident};
169      $self->{document}->input_encoding ($self->{input_encoding})
170          if defined $self->{input_encoding};
171    
172    my $i = 0;    my $i = 0;
173    my $line = 1;    my $line = 1;
174    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
# Line 253  sub _initialize_tokenizer ($) { Line 340  sub _initialize_tokenizer ($) {
340  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343    ##        ->{name}
344    ##        ->{value}
345    ##        ->{has_reference} == 1 or 0
346  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
# Line 263  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 1013  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1123        }        }
1124    
# Line 1739  sub _tokenize_attempt_to_consume_an_enti Line 1845  sub _tokenize_attempt_to_consume_an_enti
1845            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1846          }          }
1847    
1848          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1849                    has_reference => 1};
1850        } # X        } # X
1851      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1852               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1774  sub _tokenize_attempt_to_consume_an_enti Line 1881  sub _tokenize_attempt_to_consume_an_enti
1881          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1882        }        }
1883                
1884        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1885      } else {      } else {
1886        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1887        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1822  sub _tokenize_attempt_to_consume_an_enti Line 1929  sub _tokenize_attempt_to_consume_an_enti
1929      }      }
1930            
1931      if ($match > 0) {      if ($match > 0) {
1932        return {type => CHARACTER_TOKEN, data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1933      } elsif ($match < 0) {      } elsif ($match < 0) {
1934        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1935        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1936          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1937        } else {        } else {
1938          return {type => CHARACTER_TOKEN, data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1939        }        }
1940      } else {      } else {
1941        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1942        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1943        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1944      }      }
1945    } else {    } else {
# Line 2080  sub _tree_construction_root_element ($) Line 2187  sub _tree_construction_root_element ($)
2187              redo B;              redo B;
2188            }            }
2189          }          }
2190    
2191            $self->{application_cache_selection}->(undef);
2192    
2193            #
2194          } elsif ($token->{type} == START_TAG_TOKEN) {
2195            if ($token->{tag_name} eq 'html' and
2196                $token->{attributes}->{manifest}) {
2197              $self->{application_cache_selection}
2198                   ->($token->{attributes}->{manifest}->{value});
2199              ## ISSUE: No relative reference resolution?
2200            } else {
2201              $self->{application_cache_selection}->(undef);
2202            }
2203    
2204            ## ISSUE: There is an issue in the spec
2205          #          #
2206        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
2207                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2208                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2209                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2210            $self->{application_cache_selection}->(undef);
2211    
2212          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2213          #          #
2214        } else {        } else {
2215          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2216        }        }
2217    
2218        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2219        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2220        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2747  sub _tree_construction_main ($) { Line 2871  sub _tree_construction_main ($) {
2871                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2872                }                }
2873                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2874                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2875    
2876                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2877                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2878                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2879                  }                        ->($self, $token->{attributes}->{charset}->{value});
2880                  if ($token->{attributes}->{'http-equiv'}) {                    
2881                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2882                          ->set_user_data (manakai_has_reference =>
2883                                               $token->{attributes}->{charset}
2884                                                   ->{has_reference});
2885                    } elsif ($token->{attributes}->{content}) {
2886                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2887                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2888                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2889                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2890                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2891                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2892                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2893                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2894                            ->set_user_data (manakai_has_reference =>
2895                                                 $token->{attributes}->{content}
2896                                                       ->{has_reference});
2897                      }
2898                    }
2899                  } else {
2900                    if ($token->{attributes}->{charset}) {
2901                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2902                          ->set_user_data (manakai_has_reference =>
2903                                               $token->{attributes}->{charset}
2904                                                   ->{has_reference});
2905                    }
2906                    if ($token->{attributes}->{content}) {
2907                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2908                          ->set_user_data (manakai_has_reference =>
2909                                               $token->{attributes}->{content}
2910                                                   ->{has_reference});
2911                  }                  }
                 ## TODO: Change the encoding  
2912                }                }
2913    
               ## TODO: Extracting |charset| from |meta|.  
2914                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2915                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2916                !!!next-token;                !!!next-token;
# Line 3336  sub _tree_construction_main ($) { Line 3480  sub _tree_construction_main ($) {
3480        $insert = $insert_to_current;        $insert = $insert_to_current;
3481        #        #
3482      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3483            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
             ## NOTE: There are "character in table" code clones.  
3484              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3485                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3486                                
# Line 3394  sub _tree_construction_main ($) { Line 3537  sub _tree_construction_main ($) {
3537                            
3538              !!!next-token;              !!!next-token;
3539              redo B;              redo B;
3540            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3541              if ({              if ({
3542                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3543                   th => 1, td => 1,                   th => 1, td => 1,
# Line 3580  sub _tree_construction_main ($) { Line 3723  sub _tree_construction_main ($) {
3723                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3724                }                }
3725              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3726                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3727    
3728                ## As if </table>                ## As if </table>
# Line 3628  sub _tree_construction_main ($) { Line 3770  sub _tree_construction_main ($) {
3770    
3771                ## reprocess                ## reprocess
3772                redo B;                redo B;
3773              } else {          } else {
3774                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3775              }  
3776            } elsif ($token->{type} == END_TAG_TOKEN) {            $insert = $insert_to_foster;
3777              #
3778            }
3779          } elsif ($token->{type} == END_TAG_TOKEN) {
3780              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3781                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
3782                ## have an element in table scope                ## have an element in table scope
# Line 3888  sub _tree_construction_main ($) { Line 4033  sub _tree_construction_main ($) {
4033                ## Ignore the token                ## Ignore the token
4034                !!!next-token;                !!!next-token;
4035                redo B;                redo B;
4036              } else {          } else {
4037                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4038    
4039        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4040        #            #
4041            }
4042          } else {
4043            die "$0: $token->{type}: Unknown token type";
4044          }
4045      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4046            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4047              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 3957  sub _tree_construction_main ($) { Line 4100  sub _tree_construction_main ($) {
4100              redo B;              redo B;
4101            }            }
4102      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4103            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4104              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4105              !!!next-token;          !!!next-token;
4106              redo B;          redo B;
4107            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4108              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4109                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4110                  ## As if </option>                  ## As if </option>
# Line 4014  sub _tree_construction_main ($) { Line 4157  sub _tree_construction_main ($) {
4157    
4158                !!!next-token;                !!!next-token;
4159                redo B;                redo B;
4160              } else {          } else {
4161                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4162              }            ## Ignore the token
4163            } elsif ($token->{type} == END_TAG_TOKEN) {            !!!next-token;
4164              redo B;
4165            }
4166          } elsif ($token->{type} == END_TAG_TOKEN) {
4167              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4168                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4169                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4119  sub _tree_construction_main ($) { Line 4265  sub _tree_construction_main ($) {
4265    
4266                ## reprocess                ## reprocess
4267                redo B;                redo B;
4268              } else {          } else {
4269                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4270            ## Ignore the token            ## Ignore the token
4271            !!!next-token;            !!!next-token;
4272            redo B;            redo B;
4273            }
4274          } else {
4275            die "$0: $token->{type}: Unknown token type";
4276          }
4277      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4278        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4279          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4337  sub _tree_construction_main ($) { Line 4481  sub _tree_construction_main ($) {
4481        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4482          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4483          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4484          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4485    
4486          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4487            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4488              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4489            }                  ->($self, $token->{attributes}->{charset}->{value});
4490            if ($token->{attributes}->{'http-equiv'}) {              
4491                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4492                    ->set_user_data (manakai_has_reference =>
4493                                         $token->{attributes}->{charset}
4494                                             ->{has_reference});
4495              } elsif ($token->{attributes}->{content}) {
4496              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4497              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4498                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4499                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4500                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4501                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4502              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4503                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4504                      ->set_user_data (manakai_has_reference =>
4505                                           $token->{attributes}->{content}
4506                                                 ->{has_reference});
4507                }
4508              }
4509            } else {
4510              if ($token->{attributes}->{charset}) {
4511                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4512                    ->set_user_data (manakai_has_reference =>
4513                                         $token->{attributes}->{charset}
4514                                             ->{has_reference});
4515              }
4516              if ($token->{attributes}->{content}) {
4517                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4518                    ->set_user_data (manakai_has_reference =>
4519                                         $token->{attributes}->{content}
4520                                             ->{has_reference});
4521            }            }
           ## TODO: Change the encoding  
4522          }          }
4523    
4524          !!!next-token;          !!!next-token;
# Line 4660  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4826            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4827            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4828              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4829              !!!back-token;              !!!back-token;
4830              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4831              redo B;              redo B;
# Line 4865  sub _tree_construction_main ($) { Line 5030  sub _tree_construction_main ($) {
5030                  noframes => 1,                  noframes => 1,
5031                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5032                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5033          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5034          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5035          redo B;          redo B;
5036        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 5021  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5187            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5188          } else {          } else {
5189            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5190          }          }
5191    
5192          undef $self->{form_element};          undef $self->{form_element};
# Line 5059  sub _tree_construction_main ($) { Line 5224  sub _tree_construction_main ($) {
5224          } # INSCOPE          } # INSCOPE
5225                    
5226          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5227            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5228          }          }
5229                    
5230          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5128  sub _tree_construction_main ($) { Line 5293  sub _tree_construction_main ($) {
5293                    
5294              ## Step 2              ## Step 2
5295              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5296                  ## NOTE: <x><y></x>
5297                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5298              }              }
5299                            
# Line 5178  sub set_inner_html ($$$) { Line 5344  sub set_inner_html ($$$) {
5344    my $s = \$_[0];    my $s = \$_[0];
5345    my $onerror = $_[1];    my $onerror = $_[1];
5346    
5347      ## ISSUE: Should {confident} be true?
5348    
5349    my $nt = $node->node_type;    my $nt = $node->node_type;
5350    if ($nt == 9) {    if ($nt == 9) {
5351      # MUST      # MUST
# Line 5330  sub set_inner_html ($$$) { Line 5498  sub set_inner_html ($$$) {
5498    
5499  } # tree construction stage  } # tree construction stage
5500    
5501  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5502    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5503    
5504  1;  1;
5505  # $Date$  # $Date$

Legend:
Removed from v.1.57  
changed lines
  Added in v.1.68

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24