/[suikacvs]/markup/html/whatpm/t/HTML-tokenizer.t
Suika

Diff of /markup/html/whatpm/t/HTML-tokenizer.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.16 by wakaba, Mon Jul 16 07:48:19 2007 UTC revision 1.26 by wakaba, Mon Mar 3 09:17:10 2008 UTC
# Line 24  BEGIN { Line 24  BEGIN {
24  }  }
25    
26  use Test;  use Test;
27  BEGIN { plan tests => 337 }  BEGIN { plan tests => 420 }
28    
29  use Data::Dumper;  use Data::Dumper;
30  $Data::Dumper::Useqq = 1;  $Data::Dumper::Useqq = 1;
# Line 85  for my $file_name (grep {$_} split /\s+/ Line 85  for my $file_name (grep {$_} split /\s+/
85        my $p = Whatpm::HTML->new;        my $p = Whatpm::HTML->new;
86        my $i = 0;        my $i = 0;
87        my @token;        my @token;
88        $p->{set_next_input_character} = sub {        $p->{set_next_char} = sub {
89          my $self = shift;          my $self = shift;
90    
91          pop @{$self->{prev_input_character}};          pop @{$self->{prev_char}};
92          unshift @{$self->{prev_input_character}}, $self->{next_input_character};          unshift @{$self->{prev_char}}, $self->{next_char};
93    
94          $self->{next_input_character} = -1 and return if $i >= length $s;          $self->{next_char} = -1 and return if $i >= length $s;
95          $self->{next_input_character} = ord substr $s, $i++, 1;          $self->{next_char} = ord substr $s, $i++, 1;
96    
97          if ($self->{next_input_character} == 0x000D) { # CR          if ($self->{next_char} == 0x000D) { # CR
98            $i++ if substr ($s, $i, 1) eq "\x0A";            $i++ if substr ($s, $i, 1) eq "\x0A";
99            $self->{next_input_character} = 0x000A; # LF # MUST            $self->{next_char} = 0x000A; # LF # MUST
100          } elsif ($self->{next_input_character} > 0x10FFFF) {          } elsif ($self->{next_char} > 0x10FFFF) {
101            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
102            push @token, 'ParseError';            push @token, 'ParseError';
103          } elsif ($self->{next_input_character} == 0x0000) { # NULL          } elsif ($self->{next_char} == 0x0000) { # NULL
104            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
105            push @token, 'ParseError';            push @token, 'ParseError';
106          }          }
107        };        };
108        $p->{prev_input_character} = [-1, -1, -1];        $p->{prev_char} = [-1, -1, -1];
109        $p->{next_input_character} = -1;        $p->{next_char} = -1;
110                
111        $p->{parse_error} = sub {        $p->{parse_error} = sub {
112          push @token, 'ParseError';          push @token, 'ParseError';
113        };        };
114                
115        $p->_initialize_tokenizer;        $p->_initialize_tokenizer;
116        $p->{content_model_flag} = $cm;        $p->{content_model} = {
117            CDATA => Whatpm::HTML::CDATA_CONTENT_MODEL (),
118            RCDATA => Whatpm::HTML::RCDATA_CONTENT_MODEL (),
119            PCDATA => Whatpm::HTML::PCDATA_CONTENT_MODEL (),
120            PLAINTEXT => Whatpm::HTML::PLAINTEXT_CONTENT_MODEL (),
121          }->{$cm};
122        $p->{last_emitted_start_tag_name} = $last_start_tag;        $p->{last_emitted_start_tag_name} = $last_start_tag;
123    
124        while (1) {        while (1) {
125          my $token = $p->_get_next_token;          my $token = $p->_get_next_token;
126          last if $token->{type} eq 'end-of-file';          last if $token->{type} == Whatpm::HTML::END_OF_FILE_TOKEN ();
127                    
128          my $test_token = [          my $test_token = [
129           {           {
130            DOCTYPE => 'DOCTYPE',            Whatpm::HTML::DOCTYPE_TOKEN () => 'DOCTYPE',
131            'start tag' => 'StartTag',            Whatpm::HTML::START_TAG_TOKEN () => 'StartTag',
132            'end tag' => 'EndTag',            Whatpm::HTML::END_TAG_TOKEN () => 'EndTag',
133            comment => 'Comment',            Whatpm::HTML::COMMENT_TOKEN () => 'Comment',
134            character => 'Character',            Whatpm::HTML::CHARACTER_TOKEN () => 'Character',
135           }->{$token->{type}} || $token->{type},           }->{$token->{type}} || $token->{type},
136          ];          ];
137          $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};          $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};
138          $test_token->[1] = $token->{data} if defined $token->{data};          $test_token->[1] = $token->{data} if defined $token->{data};
139          if ($token->{type} eq 'start tag') {          if ($token->{type} == Whatpm::HTML::START_TAG_TOKEN ()) {
140            $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}};            $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}};
141          } elsif ($token->{type} eq 'DOCTYPE') {          } elsif ($token->{type} == Whatpm::HTML::DOCTYPE_TOKEN ()) {
142            $test_token->[1] = $token->{name};            $test_token->[1] = $token->{name};
143            $test_token->[2] = $token->{public_identifier};            $test_token->[2] = $token->{public_identifier};
144            $test_token->[3] = $token->{system_identifier};            $test_token->[3] = $token->{system_identifier};
145            $test_token->[4] = $token->{correct} ? 1 : 0;            $test_token->[4] = $token->{quirks} ? 0 : 1;
146          }          }
147    
148          if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and          if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and
# Line 151  for my $file_name (grep {$_} split /\s+/ Line 156  for my $file_name (grep {$_} split /\s+/
156        my $expected_dump = Dumper ($test->{output});        my $expected_dump = Dumper ($test->{output});
157        my $parser_dump = Dumper (\@token);        my $parser_dump = Dumper (\@token);
158        ok $parser_dump, $expected_dump,        ok $parser_dump, $expected_dump,
159          $test->{description} . ': ' . $test->{input};          $test->{description} . ': ' . Data::Dumper::qquote ($test->{input});
160      }      }
161    }    }
162  }  }
163    
164    ## License: Public Domain.
165  ## $Date$  ## $Date$

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.26

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24