/[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.31 - (show annotations) (download) (as text)
Sat Sep 13 04:19:56 2008 UTC (17 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.30: +2 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	13 Sep 2008 04:17:38 -0000
2008-09-13  Wakaba  <wakaba@suika.fam.cx>

	* HTML-tree.t: tokenizer-test-3.dat added.

	* tokenizer-test-3.dat: New test data.

++ whatpm/Whatpm/ChangeLog	13 Sep 2008 04:19:14 -0000
2008-09-13  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: |MARKUP_DECLARATION_OPEN_STATE| broken
	into four states so that no longer does the tokenizer have to push
	back next input characters in that state.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24