1 |
wakaba |
1.1 |
#!/usr/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
|
4 |
wakaba |
1.23 |
my $DEBUG = $ENV{DEBUG}; |
5 |
wakaba |
1.33 |
|
6 |
wakaba |
1.32 |
use lib qw[/home/wakaba/work/manakai2/lib]; |
7 |
wakaba |
1.33 |
|
8 |
wakaba |
1.1 |
my $dir_name; |
9 |
wakaba |
1.2 |
my $test_dir_name; |
10 |
wakaba |
1.1 |
BEGIN { |
11 |
wakaba |
1.2 |
$test_dir_name = 't/'; |
12 |
wakaba |
1.1 |
$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 |
wakaba |
1.30 |
BEGIN { plan tests => 3105 } |
28 |
wakaba |
1.1 |
|
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 |
wakaba |
1.23 |
|
38 |
|
|
if ($DEBUG) { |
39 |
wakaba |
1.25 |
my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}}; |
40 |
wakaba |
1.23 |
$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 |
wakaba |
1.39 |
my @FILES = grep {$_} split /\s+/, qq[ |
53 |
wakaba |
1.12 |
${test_dir_name}tokenizer-test-2.dat |
54 |
wakaba |
1.31 |
${test_dir_name}tokenizer-test-3.dat |
55 |
wakaba |
1.2 |
${dir_name}tests1.dat |
56 |
|
|
${dir_name}tests2.dat |
57 |
|
|
${dir_name}tests3.dat |
58 |
|
|
${dir_name}tests4.dat |
59 |
wakaba |
1.11 |
${dir_name}tests5.dat |
60 |
|
|
${dir_name}tests6.dat |
61 |
wakaba |
1.27 |
${dir_name}tests7.dat |
62 |
wakaba |
1.38 |
${dir_name}tests8.dat |
63 |
|
|
${dir_name}tests9.dat |
64 |
|
|
${dir_name}tests10.dat |
65 |
|
|
${dir_name}tests11.dat |
66 |
|
|
${dir_name}tests12.dat |
67 |
wakaba |
1.2 |
${test_dir_name}tree-test-1.dat |
68 |
wakaba |
1.14 |
${test_dir_name}tree-test-2.dat |
69 |
wakaba |
1.29 |
${test_dir_name}tree-test-3.dat |
70 |
wakaba |
1.34 |
${test_dir_name}tree-test-void.dat |
71 |
wakaba |
1.35 |
${test_dir_name}tree-test-flow.dat |
72 |
|
|
${test_dir_name}tree-test-phrasing.dat |
73 |
wakaba |
1.37 |
${test_dir_name}tree-test-form.dat |
74 |
wakaba |
1.36 |
${test_dir_name}tree-test-foreign.dat |
75 |
wakaba |
1.39 |
]; |
76 |
|
|
|
77 |
|
|
require 't/testfiles.pl'; |
78 |
|
|
execute_test ($_, { |
79 |
|
|
errors => {is_list => 1}, |
80 |
|
|
shoulds => {is_list => 1}, |
81 |
|
|
document => {is_prefixed => 1}, |
82 |
|
|
'document-fragment' => {is_prefixed => 1}, |
83 |
|
|
}, \&test) for @FILES; |
84 |
wakaba |
1.1 |
|
85 |
wakaba |
1.4 |
use Whatpm::HTML; |
86 |
|
|
use Whatpm::NanoDOM; |
87 |
wakaba |
1.33 |
use Whatpm::Charset::UnicodeChecker; |
88 |
wakaba |
1.1 |
|
89 |
|
|
sub test ($) { |
90 |
|
|
my $test = shift; |
91 |
|
|
|
92 |
wakaba |
1.39 |
if ($test->{'document-fragment'}) { |
93 |
|
|
if (@{$test->{'document-fragment'}->[1]}) { |
94 |
|
|
## NOTE: Old format. |
95 |
|
|
$test->{element} = $test->{'document-fragment'}->[1]->[0]; |
96 |
|
|
$test->{document} ||= $test->{'document-fragment'}; |
97 |
|
|
} else { |
98 |
|
|
## NOTE: New format. |
99 |
|
|
$test->{element} = $test->{'document-fragment'}->[0]; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
wakaba |
1.4 |
my $doc = Whatpm::NanoDOM::Document->new; |
104 |
wakaba |
1.1 |
my @errors; |
105 |
wakaba |
1.30 |
my @shoulds; |
106 |
wakaba |
1.1 |
|
107 |
|
|
$SIG{INT} = sub { |
108 |
wakaba |
1.3 |
print scalar serialize ($doc); |
109 |
wakaba |
1.1 |
exit; |
110 |
|
|
}; |
111 |
wakaba |
1.3 |
|
112 |
wakaba |
1.5 |
my $onerror = sub { |
113 |
|
|
my %opt = @_; |
114 |
wakaba |
1.30 |
if ($opt{level} eq 's') { |
115 |
|
|
push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type}; |
116 |
|
|
} else { |
117 |
|
|
push @errors, join ':', $opt{line}, $opt{column}, $opt{type}; |
118 |
|
|
} |
119 |
wakaba |
1.5 |
}; |
120 |
wakaba |
1.33 |
|
121 |
|
|
my $chk = sub { |
122 |
|
|
return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5'); |
123 |
|
|
}; # $chk |
124 |
|
|
|
125 |
wakaba |
1.5 |
my $result; |
126 |
|
|
unless (defined $test->{element}) { |
127 |
wakaba |
1.39 |
Whatpm::HTML->parse_char_string |
128 |
|
|
($test->{data}->[0] => $doc, $onerror, $chk); |
129 |
wakaba |
1.5 |
$result = serialize ($doc); |
130 |
|
|
} else { |
131 |
|
|
my $el = $doc->create_element_ns |
132 |
|
|
('http://www.w3.org/1999/xhtml', [undef, $test->{element}]); |
133 |
wakaba |
1.39 |
Whatpm::HTML->set_inner_html ($el, $test->{data}->[0], $onerror, $chk); |
134 |
wakaba |
1.5 |
$result = serialize ($el); |
135 |
|
|
} |
136 |
wakaba |
1.39 |
|
137 |
|
|
warn "No #errors section" unless $test->{errors}; |
138 |
wakaba |
1.5 |
|
139 |
wakaba |
1.39 |
ok scalar @errors, scalar @{$test->{errors}->[0] or []}, |
140 |
|
|
'Parse error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' . |
141 |
|
|
join (', ', @errors) . ';' . join (', ', @{$test->{errors}->[0] or []}); |
142 |
|
|
ok scalar @shoulds, scalar @{$test->{shoulds}->[0] or []}, |
143 |
|
|
'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}->[0]) . '; ' . |
144 |
|
|
join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds}->[0] or []}); |
145 |
wakaba |
1.1 |
|
146 |
wakaba |
1.39 |
ok $result, $test->{document}->[0] . "\x0A", |
147 |
|
|
'Document tree: ' . Data::Dumper::qquote ($test->{data}->[0]); |
148 |
wakaba |
1.1 |
} # test |
149 |
|
|
|
150 |
wakaba |
1.39 |
## NOTE: Spec: <http://wiki.whatwg.org/wiki/Parser_tests>. |
151 |
wakaba |
1.1 |
sub serialize ($) { |
152 |
|
|
my $node = shift; |
153 |
|
|
my $r = ''; |
154 |
|
|
|
155 |
|
|
my @node = map { [$_, ''] } @{$node->child_nodes}; |
156 |
|
|
while (@node) { |
157 |
|
|
my $child = shift @node; |
158 |
|
|
my $nt = $child->[0]->node_type; |
159 |
|
|
if ($nt == $child->[0]->ELEMENT_NODE) { |
160 |
wakaba |
1.39 |
$r .= $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
161 |
wakaba |
1.1 |
|
162 |
wakaba |
1.2 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
163 |
wakaba |
1.1 |
@{$child->[0]->attributes}) { |
164 |
wakaba |
1.39 |
$r .= $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
165 |
wakaba |
1.1 |
$r .= $attr->[1] . '"' . "\x0A"; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
unshift @node, |
169 |
|
|
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
170 |
|
|
} elsif ($nt == $child->[0]->TEXT_NODE) { |
171 |
wakaba |
1.39 |
$r .= $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
172 |
wakaba |
1.1 |
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
173 |
wakaba |
1.39 |
$r .= $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
174 |
wakaba |
1.1 |
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
175 |
wakaba |
1.39 |
$r .= $child->[1] . '<!DOCTYPE ' . $child->[0]->name; |
176 |
wakaba |
1.24 |
my $pubid = $child->[0]->public_id; |
177 |
|
|
my $sysid = $child->[0]->system_id; |
178 |
wakaba |
1.40 |
if (length $pubid or length $sysid) { |
179 |
|
|
$r .= ' "' . $pubid . '"'; |
180 |
|
|
$r .= ' "' . $sysid . '"'; |
181 |
|
|
} |
182 |
wakaba |
1.24 |
$r .= ">\x0A"; |
183 |
wakaba |
1.1 |
} else { |
184 |
wakaba |
1.39 |
$r .= $child->[1] . $child->[0]->node_type . "\x0A"; # error |
185 |
wakaba |
1.1 |
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
return $r; |
189 |
|
|
} # serialize |
190 |
|
|
|
191 |
|
|
## License: Public Domain. |
192 |
wakaba |
1.40 |
## $Date: 2008/10/14 05:58:26 $ |