/[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.70 by wakaba, Sat Mar 1 00:42:52 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 13  our $VERSION=do{my @r=(q$Revision$=~/\d+ Line 14  our $VERSION=do{my @r=(q$Revision$=~/\d+
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
15  ## "{U+FEFF}..." in GB18030?  ## "{U+FEFF}..." in GB18030?
16    
17    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
18    ## TODO: 1252 parse error (revision 1264)
19    ## TODO: 8859-11 = 874 (revision 1271)
20    
21  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
22    base => 1,    base => 1,
23    link => 1,    link => 1,
# Line 84  my $formatting_category = { Line 89  my $formatting_category = {
89  };  };
90  # $phrasing_category: all other elements  # $phrasing_category: all other elements
91    
92    sub parse_byte_string ($$$$;$) {
93      my $self = ref $_[0] ? shift : shift->new;
94      my $charset = shift;
95      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
96      my $s;
97      
98      if (defined $charset) {
99        require Encode; ## TODO: decode(utf8) don't delete BOM
100        $s = \ (Encode::decode ($charset, $$bytes_s));
101        $self->{input_encoding} = lc $charset; ## TODO: normalize name
102        $self->{confident} = 1;
103      } else {
104        ## TODO: Implement HTML5 detection algorithm
105        require Whatpm::Charset::UniversalCharDet;
106        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
107            (substr ($$bytes_s, 0, 1024));
108        $charset ||= 'windows-1252';
109        $s = \ (Encode::decode ($charset, $$bytes_s));
110        $self->{input_encoding} = $charset;
111        $self->{confident} = 0;
112      }
113    
114      $self->{change_encoding} = sub {
115        my $self = shift;
116        my $charset = lc shift;
117        ## TODO: if $charset is supported
118        ## TODO: normalize charset name
119    
120        ## "Change the encoding" algorithm:
121    
122        ## Step 1    
123        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
124          $charset = 'utf-8';
125        }
126    
127        ## Step 2
128        if (defined $self->{input_encoding} and
129            $self->{input_encoding} eq $charset) {
130          $self->{confident} = 1;
131          return;
132        }
133    
134        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
135            ':'.$charset, level => 'w');
136    
137        ## Step 3
138        # if (can) {
139          ## change the encoding on the fly.
140          #$self->{confident} = 1;
141          #return;
142        # }
143    
144        ## Step 4
145        throw Whatpm::HTML::RestartParser (charset => $charset);
146      }; # $self->{change_encoding}
147    
148      my @args = @_; shift @args; # $s
149      my $return;
150      try {
151        $return = $self->parse_char_string ($s, @args);  
152      } catch Whatpm::HTML::RestartParser with {
153        my $charset = shift->{charset};
154        $s = \ (Encode::decode ($charset, $$bytes_s));    
155        $self->{input_encoding} = $charset; ## TODO: normalize
156        $self->{confident} = 1;
157        $return = $self->parse_char_string ($s, @args);
158      };
159      return $return;
160    } # parse_byte_string
161    
162    *parse_char_string = \&parse_string;
163    
164  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
165    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
166    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
167    $self->{document} = $_[1];    $self->{document} = $_[1];
168      @{$self->{document}->child_nodes} = ();
169    
170    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
171    
172      $self->{confident} = 1 unless exists $self->{confident};
173      $self->{document}->input_encoding ($self->{input_encoding})
174          if defined $self->{input_encoding};
175    
176    my $i = 0;    my $i = 0;
177    my $line = 1;    my $line = 1;
178    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 229  sub new ($) {
229    $self->{parse_error} = sub {    $self->{parse_error} = sub {
230      #      #
231    };    };
232      $self->{change_encoding} = sub {
233        # if ($_[0] is a supported encoding) {
234        #   run "change the encoding" algorithm;
235        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
236        # }
237      };
238      $self->{application_cache_selection} = sub {
239        #
240      };
241    return $self;    return $self;
242  } # new  } # new
243    
# Line 253  sub _initialize_tokenizer ($) { Line 344  sub _initialize_tokenizer ($) {
344  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
345  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
346  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
347    ##        ->{name}
348    ##        ->{value}
349    ##        ->{has_reference} == 1 or 0
350  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
351    
352  ## 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 357  sub _initialize_tokenizer ($) {
357  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
358  ## and removed from the list.  ## and removed from the list.
359    
360    ## NOTE: HTML5 "Writing HTML documents" section, applied to
361    ## documents and not to user agents and conformance checkers,
362    ## contains some requirements that are not detected by the
363    ## parsing algorithm:
364    ## - Some requirements on character encoding declarations. ## TODO
365    ## - "Elements MUST NOT contain content that their content model disallows."
366    ##   ... Some are parse error, some are not (will be reported by c.c.).
367    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
368    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
369    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
370    
371    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
372    ## be detected by the HTML5 parsing algorithm:
373    ## - Text,
374    
375  sub _get_next_token ($) {  sub _get_next_token ($) {
376    my $self = shift;    my $self = shift;
377    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 1013  sub _get_next_token ($) { Line 1122  sub _get_next_token ($) {
1122          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1123        } else {        } else {
1124          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1125            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1126          ## 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"
1127        }        }
1128    
# Line 1449  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1560          !!!next-input-character;          !!!next-input-character;
1561          redo A;          redo A;
1562          } elsif ($self->{next_input_character} == 0x003E) { # >
1563            !!!parse-error (type => 'unclosed PUBLIC literal');
1564    
1565            $self->{state} = DATA_STATE;
1566            !!!next-input-character;
1567    
1568            delete $self->{current_token}->{correct};
1569            !!!emit ($self->{current_token}); # DOCTYPE
1570    
1571            redo A;
1572        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1573          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1574    
# Line 1471  sub _get_next_token ($) { Line 1591  sub _get_next_token ($) {
1591          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594          } elsif ($self->{next_input_character} == 0x003E) { # >
1595            !!!parse-error (type => 'unclosed PUBLIC literal');
1596    
1597            $self->{state} = DATA_STATE;
1598            !!!next-input-character;
1599    
1600            delete $self->{current_token}->{correct};
1601            !!!emit ($self->{current_token}); # DOCTYPE
1602    
1603            redo A;
1604        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1605          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1606    
# Line 1577  sub _get_next_token ($) { Line 1707  sub _get_next_token ($) {
1707          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1708          !!!next-input-character;          !!!next-input-character;
1709          redo A;          redo A;
1710          } elsif ($self->{next_input_character} == 0x003E) { # >
1711            !!!parse-error (type => 'unclosed PUBLIC literal');
1712    
1713            $self->{state} = DATA_STATE;
1714            !!!next-input-character;
1715    
1716            delete $self->{current_token}->{correct};
1717            !!!emit ($self->{current_token}); # DOCTYPE
1718    
1719            redo A;
1720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1721          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1722    
# Line 1599  sub _get_next_token ($) { Line 1739  sub _get_next_token ($) {
1739          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742          } elsif ($self->{next_input_character} == 0x003E) { # >
1743            !!!parse-error (type => 'unclosed PUBLIC literal');
1744    
1745            $self->{state} = DATA_STATE;
1746            !!!next-input-character;
1747    
1748            delete $self->{current_token}->{correct};
1749            !!!emit ($self->{current_token}); # DOCTYPE
1750    
1751            redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1753          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1754    
# Line 1739  sub _tokenize_attempt_to_consume_an_enti Line 1889  sub _tokenize_attempt_to_consume_an_enti
1889            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1890          }          }
1891    
1892          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1893                    has_reference => 1};
1894        } # X        } # X
1895      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1896               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1774  sub _tokenize_attempt_to_consume_an_enti Line 1925  sub _tokenize_attempt_to_consume_an_enti
1925          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1926        }        }
1927                
1928        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1929      } else {      } else {
1930        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1931        !!!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 1973  sub _tokenize_attempt_to_consume_an_enti
1973      }      }
1974            
1975      if ($match > 0) {      if ($match > 0) {
1976        return {type => CHARACTER_TOKEN, data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1977      } elsif ($match < 0) {      } elsif ($match < 0) {
1978        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1979        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1980          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1981        } else {        } else {
1982          return {type => CHARACTER_TOKEN, data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1983        }        }
1984      } else {      } else {
1985        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1986        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1987        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1988      }      }
1989    } else {    } else {
# Line 2080  sub _tree_construction_root_element ($) Line 2231  sub _tree_construction_root_element ($)
2231              redo B;              redo B;
2232            }            }
2233          }          }
2234    
2235            $self->{application_cache_selection}->(undef);
2236    
2237            #
2238          } elsif ($token->{type} == START_TAG_TOKEN) {
2239            if ($token->{tag_name} eq 'html' and
2240                $token->{attributes}->{manifest}) {
2241              $self->{application_cache_selection}
2242                   ->($token->{attributes}->{manifest}->{value});
2243              ## ISSUE: No relative reference resolution?
2244            } else {
2245              $self->{application_cache_selection}->(undef);
2246            }
2247    
2248            ## ISSUE: There is an issue in the spec
2249          #          #
2250        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
2251                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2252                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2253                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2254            $self->{application_cache_selection}->(undef);
2255    
2256          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2257          #          #
2258        } else {        } else {
2259          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2260        }        }
2261    
2262        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2263        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2264        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2747  sub _tree_construction_main ($) { Line 2915  sub _tree_construction_main ($) {
2915                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2916                }                }
2917                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2918                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.
2919    
2920                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2921                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2922                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2923                  }                        ->($self, $token->{attributes}->{charset}->{value});
2924                  if ($token->{attributes}->{'http-equiv'}) {                    
2925                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2926                          ->set_user_data (manakai_has_reference =>
2927                                               $token->{attributes}->{charset}
2928                                                   ->{has_reference});
2929                    } elsif ($token->{attributes}->{content}) {
2930                    ## 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.
2931                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2932                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2933                              [\x09-\x0D\x20]*=
2934                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2935                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2936                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2937                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2938                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2939                            ->set_user_data (manakai_has_reference =>
2940                                                 $token->{attributes}->{content}
2941                                                       ->{has_reference});
2942                      }
2943                    }
2944                  } else {
2945                    if ($token->{attributes}->{charset}) {
2946                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2947                          ->set_user_data (manakai_has_reference =>
2948                                               $token->{attributes}->{charset}
2949                                                   ->{has_reference});
2950                    }
2951                    if ($token->{attributes}->{content}) {
2952                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2953                          ->set_user_data (manakai_has_reference =>
2954                                               $token->{attributes}->{content}
2955                                                   ->{has_reference});
2956                  }                  }
                 ## TODO: Change the encoding  
2957                }                }
2958    
               ## TODO: Extracting |charset| from |meta|.  
2959                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2960                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2961                !!!next-token;                !!!next-token;
# Line 3336  sub _tree_construction_main ($) { Line 3525  sub _tree_construction_main ($) {
3525        $insert = $insert_to_current;        $insert = $insert_to_current;
3526        #        #
3527      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3528            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
             ## NOTE: There are "character in table" code clones.  
3529              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3530                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3531                                
# Line 3394  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582                            
3583              !!!next-token;              !!!next-token;
3584              redo B;              redo B;
3585            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3586              if ({              if ({
3587                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3588                   th => 1, td => 1,                   th => 1, td => 1,
# Line 3580  sub _tree_construction_main ($) { Line 3768  sub _tree_construction_main ($) {
3768                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3769                }                }
3770              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3771                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3772    
3773                ## As if </table>                ## As if </table>
# Line 3628  sub _tree_construction_main ($) { Line 3815  sub _tree_construction_main ($) {
3815    
3816                ## reprocess                ## reprocess
3817                redo B;                redo B;
3818              } else {          } else {
3819                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3820              }  
3821            } elsif ($token->{type} == END_TAG_TOKEN) {            $insert = $insert_to_foster;
3822              #
3823            }
3824          } elsif ($token->{type} == END_TAG_TOKEN) {
3825              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3826                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
3827                ## have an element in table scope                ## have an element in table scope
# Line 3888  sub _tree_construction_main ($) { Line 4078  sub _tree_construction_main ($) {
4078                ## Ignore the token                ## Ignore the token
4079                !!!next-token;                !!!next-token;
4080                redo B;                redo B;
4081              } else {          } else {
4082                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4083    
4084        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4085        #            #
4086            }
4087          } else {
4088            die "$0: $token->{type}: Unknown token type";
4089          }
4090      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4091            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4092              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 4145  sub _tree_construction_main ($) {
4145              redo B;              redo B;
4146            }            }
4147      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4148            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4149              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4150              !!!next-token;          !!!next-token;
4151              redo B;          redo B;
4152            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4153              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4154                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4155                  ## As if </option>                  ## As if </option>
# Line 4014  sub _tree_construction_main ($) { Line 4202  sub _tree_construction_main ($) {
4202    
4203                !!!next-token;                !!!next-token;
4204                redo B;                redo B;
4205              } else {          } else {
4206                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4207              }            ## Ignore the token
4208            } elsif ($token->{type} == END_TAG_TOKEN) {            !!!next-token;
4209              redo B;
4210            }
4211          } elsif ($token->{type} == END_TAG_TOKEN) {
4212              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4213                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4214                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4119  sub _tree_construction_main ($) { Line 4310  sub _tree_construction_main ($) {
4310    
4311                ## reprocess                ## reprocess
4312                redo B;                redo B;
4313              } else {          } else {
4314                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4315            ## Ignore the token            ## Ignore the token
4316            !!!next-token;            !!!next-token;
4317            redo B;            redo B;
4318            }
4319          } else {
4320            die "$0: $token->{type}: Unknown token type";
4321          }
4322      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4323        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4324          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 4526  sub _tree_construction_main ($) {
4526        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4527          ## 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
4528          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4529          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.
4530    
4531          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4532            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4533              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4534            }                  ->($self, $token->{attributes}->{charset}->{value});
4535            if ($token->{attributes}->{'http-equiv'}) {              
4536                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4537                    ->set_user_data (manakai_has_reference =>
4538                                         $token->{attributes}->{charset}
4539                                             ->{has_reference});
4540              } elsif ($token->{attributes}->{content}) {
4541              ## 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.
4542              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4543                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4544                        [\x09-\x0D\x20]*=
4545                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4546                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4547                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4548              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4549                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4550                      ->set_user_data (manakai_has_reference =>
4551                                           $token->{attributes}->{content}
4552                                                 ->{has_reference});
4553                }
4554              }
4555            } else {
4556              if ($token->{attributes}->{charset}) {
4557                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4558                    ->set_user_data (manakai_has_reference =>
4559                                         $token->{attributes}->{charset}
4560                                             ->{has_reference});
4561              }
4562              if ($token->{attributes}->{content}) {
4563                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4564                    ->set_user_data (manakai_has_reference =>
4565                                         $token->{attributes}->{content}
4566                                             ->{has_reference});
4567            }            }
           ## TODO: Change the encoding  
4568          }          }
4569    
4570          !!!next-token;          !!!next-token;
# Line 4660  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4872            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4873            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4874              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4875              !!!back-token;              !!!back-token;
4876              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4877              redo B;              redo B;
# Line 4865  sub _tree_construction_main ($) { Line 5076  sub _tree_construction_main ($) {
5076                  noframes => 1,                  noframes => 1,
5077                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5078                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5079          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5080          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5081          redo B;          redo B;
5082        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 5021  sub _tree_construction_main ($) { Line 5232  sub _tree_construction_main ($) {
5232          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5233            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5234          } else {          } else {
5235            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5236          }          }
5237    
5238          undef $self->{form_element};          undef $self->{form_element};
# Line 5059  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270          } # INSCOPE          } # INSCOPE
5271                    
5272          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5273            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5274          }          }
5275                    
5276          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5128  sub _tree_construction_main ($) { Line 5339  sub _tree_construction_main ($) {
5339                    
5340              ## Step 2              ## Step 2
5341              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5342                  ## NOTE: <x><y></x>
5343                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5344              }              }
5345                            
# Line 5178  sub set_inner_html ($$$) { Line 5390  sub set_inner_html ($$$) {
5390    my $s = \$_[0];    my $s = \$_[0];
5391    my $onerror = $_[1];    my $onerror = $_[1];
5392    
5393      ## ISSUE: Should {confident} be true?
5394    
5395    my $nt = $node->node_type;    my $nt = $node->node_type;
5396    if ($nt == 9) {    if ($nt == 9) {
5397      # MUST      # MUST
# Line 5330  sub set_inner_html ($$$) { Line 5544  sub set_inner_html ($$$) {
5544    
5545  } # tree construction stage  } # tree construction stage
5546    
5547  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5548    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  
5549    
5550  1;  1;
5551  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24