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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (show annotations) (download) (as text)
Sat Oct 4 11:32:16 2008 UTC (17 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.35: +2 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	4 Oct 2008 10:23:01 -0000
	* tree-test-foreign.dat: New test data file.

2008-10-04  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	4 Oct 2008 11:32:06 -0000
	* HTML.pm.src: Make <foreignObject> scoping (HTML5 revision 1837).
	Support for end tags of camelCase SVG elements were broken.  A
	wrong error type text fixed.

2008-10-04  Wakaba  <wakaba@suika.fam.cx>

1 #!/usr/bin/perl
2 use strict;
3
4 my $DEBUG = $ENV{DEBUG};
5
6 use lib qw[/home/wakaba/work/manakai2/lib];
7
8 my $dir_name;
9 my $test_dir_name;
10 BEGIN {
11 $test_dir_name = 't/';
12 $dir_name = 't/tree-construction/';
13 my $skip = "You don't have make command";
14 eval q{
15 system ("cd $test_dir_name; make tree-construction-files") == 0 or die
16 unless -f $dir_name.'tests1.dat';
17 $skip = '';
18 };
19 if ($skip) {
20 print "1..1\n";
21 print "ok 1 # $skip\n";
22 exit;
23 }
24 }
25
26 use Test;
27 BEGIN { plan tests => 3105 }
28
29 use Data::Dumper;
30 $Data::Dumper::Useqq = 1;
31 sub Data::Dumper::qquote {
32 my $s = shift;
33 $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
34 return q<qq'> . $s . q<'>;
35 } # Data::Dumper::qquote
36
37
38 if ($DEBUG) {
39 my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
40 $Whatpm::HTML::Debug::cp_pass = sub {
41 my $id = shift;
42 delete $not_found->{$id};
43 };
44
45 END {
46 for my $id (sort {$a <=> $b || $a cmp $b} keys %$not_found) {
47 print "# checkpoint $id is not reached\n";
48 }
49 }
50 }
51
52 for my $file_name (grep {$_} split /\s+/, qq[
53 ${test_dir_name}tokenizer-test-2.dat
54 ${test_dir_name}tokenizer-test-3.dat
55 ${dir_name}tests1.dat
56 ${dir_name}tests2.dat
57 ${dir_name}tests3.dat
58 ${dir_name}tests4.dat
59 ${dir_name}tests5.dat
60 ${dir_name}tests6.dat
61 ${dir_name}tests7.dat
62 ${test_dir_name}tree-test-1.dat
63 ${test_dir_name}tree-test-2.dat
64 ${test_dir_name}tree-test-3.dat
65 ${test_dir_name}tree-test-void.dat
66 ${test_dir_name}tree-test-flow.dat
67 ${test_dir_name}tree-test-phrasing.dat
68 ${test_dir_name}tree-test-foreign.dat
69 ]) {
70 open my $file, '<', $file_name
71 or die "$0: $file_name: $!";
72 print "# $file_name\n";
73
74 my $test;
75 my $mode = 'data';
76 my $escaped;
77 while (<$file>) {
78 s/\x0D\x0A/\x0A/;
79 if (/^#data$/) {
80 undef $test;
81 $test->{data} = '';
82 $mode = 'data';
83 undef $escaped;
84 } elsif (/^#data escaped$/) {
85 undef $test;
86 $test->{data} = '';
87 $mode = 'data';
88 $escaped = 1;
89 } elsif (/^#errors$/) {
90 $test->{errors} = [];
91 $mode = 'errors';
92 $test->{data} =~ s/\x0D?\x0A\z//;
93 $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
94 $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
95 undef $escaped;
96 } elsif (/^#shoulds$/) {
97 $test->{shoulds} = [];
98 $mode = 'shoulds';
99 } elsif (/^#document$/) {
100 $test->{document} = '';
101 $mode = 'document';
102 undef $escaped;
103 } elsif (/^#document escaped$/) {
104 $test->{document} = '';
105 $mode = 'document';
106 $escaped = 1;
107 } elsif (/^#document-fragment$/) {
108 $test->{element} = '';
109 $mode = 'element';
110 undef $escaped;
111 } elsif (/^#document-fragment (\S+)$/) {
112 $test->{document} = '';
113 $mode = 'document';
114 $test->{element} = $1;
115 undef $escaped;
116 } elsif (/^#document-fragment (\S+) escaped$/) {
117 $test->{document} = '';
118 $mode = 'document';
119 $test->{element} = $1;
120 $escaped = 1;
121 } elsif (defined $test->{document} and /^$/) {
122 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
123 $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
124 test ($test);
125 undef $test;
126 } else {
127 if ($mode eq 'data' or $mode eq 'document') {
128 $test->{$mode} .= $_;
129 } elsif ($mode eq 'element') {
130 tr/\x0D\x0A//d;
131 $test->{$mode} .= $_;
132 } elsif ($mode eq 'errors') {
133 tr/\x0D\x0A//d;
134 push @{$test->{errors}}, $_;
135 } elsif ($mode eq 'shoulds') {
136 tr/\x0D\x0A//d;
137 push @{$test->{shoulds}}, $_;
138 }
139 }
140 }
141 test ($test) if $test->{errors};
142 }
143
144 use Whatpm::HTML;
145 use Whatpm::NanoDOM;
146 use Whatpm::Charset::UnicodeChecker;
147
148 sub test ($) {
149 my $test = shift;
150
151 my $doc = Whatpm::NanoDOM::Document->new;
152 my @errors;
153 my @shoulds;
154
155 $SIG{INT} = sub {
156 print scalar serialize ($doc);
157 exit;
158 };
159
160 my $onerror = sub {
161 my %opt = @_;
162 if ($opt{level} eq 's') {
163 push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
164 } else {
165 push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
166 }
167 };
168
169 my $chk = sub {
170 return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
171 }; # $chk
172
173 my $result;
174 unless (defined $test->{element}) {
175 Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
176 $result = serialize ($doc);
177 } else {
178 my $el = $doc->create_element_ns
179 ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
180 Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
181 $result = serialize ($el);
182 }
183
184 ok scalar @errors, scalar @{$test->{errors}},
185 'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
186 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
187 ok scalar @shoulds, scalar @{$test->{shoulds} or []},
188 'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
189 join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
190
191 ok $result, $test->{document},
192 'Document tree: ' . Data::Dumper::qquote ($test->{data});
193 } # test
194
195 sub serialize ($) {
196 my $node = shift;
197 my $r = '';
198
199 my @node = map { [$_, ''] } @{$node->child_nodes};
200 while (@node) {
201 my $child = shift @node;
202 my $nt = $child->[0]->node_type;
203 if ($nt == $child->[0]->ELEMENT_NODE) {
204 $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
205
206 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
207 @{$child->[0]->attributes}) {
208 $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
209 $r .= $attr->[1] . '"' . "\x0A";
210 }
211
212 unshift @node,
213 map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
214 } elsif ($nt == $child->[0]->TEXT_NODE) {
215 $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
216 } elsif ($nt == $child->[0]->COMMENT_NODE) {
217 $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
218 } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
219 $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
220 my $pubid = $child->[0]->public_id;
221 $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
222 my $sysid = $child->[0]->system_id;
223 $r .= ' SYSTEM' if not length $pubid and length $sysid;
224 $r .= ' "' . $sysid . '"' if length $sysid;
225 $r .= ">\x0A";
226 } else {
227 $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
228 }
229 }
230
231 return $r;
232 } # serialize
233
234 ## License: Public Domain.
235 ## $Date: 2008/10/04 06:30:34 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24