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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (show annotations) (download) (as text)
Sun Jun 1 06:47:12 2008 UTC (18 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.36: +3 -7 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	1 Jun 2008 06:46:25 -0000
	* HTML-tokenizer.t, tokenizer-test-1.test: Update for new format.

2008-06-01  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	1 Jun 2008 06:39:50 -0000
	* HTML.pm.src (_get_next_token): A parse error was missing.

2008-06-01  Wakaba  <wakaba@suika.fam.cx>

1 #!/usr/bin/perl
2 use strict;
3
4 my $DEBUG = $ENV{DEBUG};
5
6 my $dir_name;
7 my $test_dir_name;
8 BEGIN {
9 $test_dir_name = 't/';
10 $dir_name = 't/tokenizer/';
11 my $skip = "You don't have JSON module";
12 eval q{
13 use JSON 1.07;
14 $skip = "You don't have make command";
15 system ("cd $test_dir_name; make tokenizer-files") == 0 or die
16 unless -f $dir_name.'test1.test';
17 $skip = '';
18 };
19 if ($skip) {
20 print "1..1\n";
21 print "ok 1 # $skip\n";
22 exit;
23 }
24 $JSON::UnMapping = 1;
25 $JSON::UTF8 = 1;
26 }
27
28 use Test;
29 BEGIN { plan tests => 1073 }
30
31 use Data::Dumper;
32 $Data::Dumper::Useqq = 1;
33 sub Data::Dumper::qquote {
34 my $s = shift;
35 $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
36 return q<qq'> . $s . q<'>;
37 } # Data::Dumper::qquote
38
39 if ($DEBUG) {
40 my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
41
42 $Whatpm::HTML::Debug::cp_pass = sub {
43 my $id = shift;
44 delete $not_found->{$id};
45 };
46
47 END {
48 for my $id (sort {$a <=> $b || $a cmp $b} grep {!/^[ti]/}
49 keys %$not_found) {
50 print "# checkpoint $id is not reached\n";
51 }
52 }
53 }
54
55 use Whatpm::HTML;
56
57 for my $file_name (grep {$_} split /\s+/, qq[
58 ${dir_name}test1.test
59 ${dir_name}test2.test
60 ${dir_name}test3.test
61 ${dir_name}test4.test
62 ${dir_name}contentModelFlags.test
63 ${dir_name}escapeFlag.test
64 ${dir_name}entities.test
65 ${dir_name}xmlViolation.test
66 ${test_dir_name}tokenizer-test-1.test
67 ]) {
68 open my $file, '<', $file_name
69 or die "$0: $file_name: $!";
70 local $/ = undef;
71 my $js = <$file>;
72 close $file;
73
74 print "# $file_name\n";
75 $js =~ s{\\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
76 \\u[Dd]([89A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])}{
77 ## NOTE: JSON::Parser does not decode surrogate pair escapes
78 ## NOTE: In older version of JSON::Parser, utf8 string will be broken
79 ## by parsing. Use latest version!
80 ## NOTE: Encode.pm is broken; it converts e.g. U+10FFFF to U+FFFD.
81 my $c = 0x10000;
82 $c += ((((hex $1) & 0b1111111111) << 10) | ((hex $2) & 0b1111111111));
83 chr $c;
84 }gex;
85 my $json = jsonToObj ($js);
86 my $tests = $json->{tests} || $json->{xmlViolationTests};
87 TEST: for my $test (@$tests) {
88 my $s = $test->{input};
89
90 my $j = 1;
91 while ($j < @{$test->{output}}) {
92 if (ref $test->{output}->[$j - 1] and
93 $test->{output}->[$j - 1]->[0] eq 'Character' and
94 ref $test->{output}->[$j] and
95 $test->{output}->[$j]->[0] eq 'Character') {
96 $test->{output}->[$j - 1]->[1]
97 .= $test->{output}->[$j]->[1];
98 splice @{$test->{output}}, $j, 1;
99 }
100 $j++;
101 }
102
103 my @cm = @{$test->{contentModelFlags} || ['PCDATA']};
104 my $last_start_tag = $test->{lastStartTag};
105 for my $cm (@cm) {
106 my $p = Whatpm::HTML->new;
107 my $i = 0;
108 my @token;
109 $p->{set_next_char} = sub {
110 my $self = shift;
111
112 pop @{$self->{prev_char}};
113 unshift @{$self->{prev_char}}, $self->{next_char};
114
115 $self->{next_char} = -1 and return if $i >= length $s;
116 $self->{next_char} = ord substr $s, $i++, 1;
117
118 if ($self->{next_char} == 0x000D) { # CR
119 $i++ if substr ($s, $i, 1) eq "\x0A";
120 $self->{next_char} = 0x000A; # LF # MUST
121 } elsif ($self->{next_char} > 0x10FFFF) {
122 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
123 push @token, 'ParseError';
124 } elsif ($self->{next_char} == 0x0000) { # NULL
125 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
126 push @token, 'ParseError';
127 } elsif ($self->{next_char} <= 0x0008 or
128 (0x000E <= $self->{next_char} and
129 $self->{next_char} <= 0x001F) or
130 (0x007F <= $self->{next_char} and
131 $self->{next_char} <= 0x009F) or
132 (0xD800 <= $self->{next_char} and
133 $self->{next_char} <= 0xDFFF) or
134 (0xFDD0 <= $self->{next_char} and
135 $self->{next_char} <= 0xFDDF) or
136 {
137 0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
138 0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
139 0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
140 0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
141 0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
142 0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
143 0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
144 0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
145 0x10FFFE => 1, 0x10FFFF => 1,
146 }->{$self->{next_char}}) {
147 push @token, 'ParseError';
148 }
149 };
150 $p->{prev_char} = [-1, -1, -1];
151 $p->{next_char} = -1;
152
153 $p->{parse_error} = sub {
154 push @token, 'ParseError';
155 };
156
157 $p->_initialize_tokenizer;
158 $p->{content_model} = {
159 CDATA => Whatpm::HTML::CDATA_CONTENT_MODEL (),
160 RCDATA => Whatpm::HTML::RCDATA_CONTENT_MODEL (),
161 PCDATA => Whatpm::HTML::PCDATA_CONTENT_MODEL (),
162 PLAINTEXT => Whatpm::HTML::PLAINTEXT_CONTENT_MODEL (),
163 }->{$cm};
164 $p->{last_emitted_start_tag_name} = $last_start_tag;
165
166 while (1) {
167 my $token = $p->_get_next_token;
168 last if $token->{type} == Whatpm::HTML::END_OF_FILE_TOKEN ();
169
170 my $test_token = [
171 {
172 Whatpm::HTML::DOCTYPE_TOKEN () => 'DOCTYPE',
173 Whatpm::HTML::START_TAG_TOKEN () => 'StartTag',
174 Whatpm::HTML::END_TAG_TOKEN () => 'EndTag',
175 Whatpm::HTML::COMMENT_TOKEN () => 'Comment',
176 Whatpm::HTML::CHARACTER_TOKEN () => 'Character',
177 }->{$token->{type}} || $token->{type},
178 ];
179 $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};
180 $test_token->[1] = $token->{data} if defined $token->{data};
181 if ($token->{type} == Whatpm::HTML::START_TAG_TOKEN ()) {
182 $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}};
183 $test_token->[3] = 1 if $p->{self_closing};
184 delete $p->{self_closing};
185 } elsif ($token->{type} == Whatpm::HTML::DOCTYPE_TOKEN ()) {
186 $test_token->[1] = $token->{name};
187 $test_token->[2] = $token->{public_identifier};
188 $test_token->[3] = $token->{system_identifier};
189 $test_token->[4] = $token->{quirks} ? 0 : 1;
190 }
191
192 if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and
193 $test_token->[0] eq 'Character') {
194 $token[-1]->[1] .= $test_token->[1];
195 } else {
196 push @token, $test_token;
197 }
198 }
199
200 my $expected_dump = Dumper ($test->{output});
201 my $parser_dump = Dumper (\@token);
202 ok $parser_dump, $expected_dump,
203 $test->{description} . ': ' . Data::Dumper::qquote ($test->{input});
204 }
205 }
206 }
207
208 ## License: Public Domain.
209 ## $Date: 2008/05/24 10:18:26 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24