/[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.35 - (hide annotations) (download) (as text)
Sat Oct 4 06:30:34 2008 UTC (16 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.34: +3 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	4 Oct 2008 06:19:37 -0000
	* tree-test-phrasing.dat, tree-test-flow.dat: New test data files.

	* HTML-tree.t: |tree-test-phrasing.dat| and |tree-test-flow.dat|
	are added.

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

++ whatpm/Whatpm/ChangeLog	4 Oct 2008 06:24:54 -0000
	* HTML.pm.src: Support for new flow (but not phrasing) elements (HTML5
	revisions 1731 and 1778).  Support for the </sarcasm> end tag (HTML5
	revision 1731).

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

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.2 for my $file_name (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.2 ${test_dir_name}tree-test-1.dat
63 wakaba 1.14 ${test_dir_name}tree-test-2.dat
64 wakaba 1.29 ${test_dir_name}tree-test-3.dat
65 wakaba 1.34 ${test_dir_name}tree-test-void.dat
66 wakaba 1.35 ${test_dir_name}tree-test-flow.dat
67     ${test_dir_name}tree-test-phrasing.dat
68 wakaba 1.1 ]) {
69 wakaba 1.2 open my $file, '<', $file_name
70     or die "$0: $file_name: $!";
71 wakaba 1.13 print "# $file_name\n";
72 wakaba 1.1
73     my $test;
74     my $mode = 'data';
75 wakaba 1.12 my $escaped;
76 wakaba 1.1 while (<$file>) {
77     s/\x0D\x0A/\x0A/;
78     if (/^#data$/) {
79     undef $test;
80     $test->{data} = '';
81     $mode = 'data';
82 wakaba 1.12 undef $escaped;
83     } elsif (/^#data escaped$/) {
84     undef $test;
85     $test->{data} = '';
86     $mode = 'data';
87     $escaped = 1;
88 wakaba 1.1 } elsif (/^#errors$/) {
89     $test->{errors} = [];
90     $mode = 'errors';
91 wakaba 1.13 $test->{data} =~ s/\x0D?\x0A\z//;
92     $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
93 wakaba 1.24 $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
94 wakaba 1.12 undef $escaped;
95 wakaba 1.30 } elsif (/^#shoulds$/) {
96     $test->{shoulds} = [];
97     $mode = 'shoulds';
98 wakaba 1.1 } elsif (/^#document$/) {
99     $test->{document} = '';
100     $mode = 'document';
101 wakaba 1.12 undef $escaped;
102     } elsif (/^#document escaped$/) {
103     $test->{document} = '';
104     $mode = 'document';
105     $escaped = 1;
106 wakaba 1.19 } elsif (/^#document-fragment$/) {
107     $test->{element} = '';
108     $mode = 'element';
109     undef $escaped;
110 wakaba 1.5 } elsif (/^#document-fragment (\S+)$/) {
111     $test->{document} = '';
112     $mode = 'document';
113     $test->{element} = $1;
114 wakaba 1.12 undef $escaped;
115     } elsif (/^#document-fragment (\S+) escaped$/) {
116     $test->{document} = '';
117     $mode = 'document';
118     $test->{element} = $1;
119     $escaped = 1;
120 wakaba 1.2 } elsif (defined $test->{document} and /^$/) {
121 wakaba 1.13 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
122 wakaba 1.24 $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
123 wakaba 1.2 test ($test);
124 wakaba 1.1 undef $test;
125     } else {
126     if ($mode eq 'data' or $mode eq 'document') {
127 wakaba 1.13 $test->{$mode} .= $_;
128 wakaba 1.19 } elsif ($mode eq 'element') {
129     tr/\x0D\x0A//d;
130     $test->{$mode} .= $_;
131 wakaba 1.1 } elsif ($mode eq 'errors') {
132     tr/\x0D\x0A//d;
133     push @{$test->{errors}}, $_;
134 wakaba 1.30 } elsif ($mode eq 'shoulds') {
135     tr/\x0D\x0A//d;
136     push @{$test->{shoulds}}, $_;
137 wakaba 1.1 }
138     }
139     }
140     test ($test) if $test->{errors};
141     }
142    
143 wakaba 1.4 use Whatpm::HTML;
144     use Whatpm::NanoDOM;
145 wakaba 1.33 use Whatpm::Charset::UnicodeChecker;
146 wakaba 1.1
147     sub test ($) {
148     my $test = shift;
149    
150 wakaba 1.4 my $doc = Whatpm::NanoDOM::Document->new;
151 wakaba 1.1 my @errors;
152 wakaba 1.30 my @shoulds;
153 wakaba 1.1
154     $SIG{INT} = sub {
155 wakaba 1.3 print scalar serialize ($doc);
156 wakaba 1.1 exit;
157     };
158 wakaba 1.3
159 wakaba 1.5 my $onerror = sub {
160     my %opt = @_;
161 wakaba 1.30 if ($opt{level} eq 's') {
162     push @shoulds, join ':', $opt{line}, $opt{column}, $opt{type};
163     } else {
164     push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
165     }
166 wakaba 1.5 };
167 wakaba 1.33
168     my $chk = sub {
169     return Whatpm::Charset::UnicodeChecker->new_handle ($_[0], 'html5');
170     }; # $chk
171    
172 wakaba 1.5 my $result;
173     unless (defined $test->{element}) {
174 wakaba 1.33 Whatpm::HTML->parse_char_string ($test->{data} => $doc, $onerror, $chk);
175 wakaba 1.5 $result = serialize ($doc);
176     } else {
177     my $el = $doc->create_element_ns
178     ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
179 wakaba 1.33 Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror, $chk);
180 wakaba 1.5 $result = serialize ($el);
181     }
182    
183 wakaba 1.1 ok scalar @errors, scalar @{$test->{errors}},
184 wakaba 1.21 'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
185 wakaba 1.1 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
186 wakaba 1.30 ok scalar @shoulds, scalar @{$test->{shoulds} or []},
187     'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
188     join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
189 wakaba 1.1
190 wakaba 1.21 ok $result, $test->{document},
191     'Document tree: ' . Data::Dumper::qquote ($test->{data});
192 wakaba 1.1 } # test
193    
194     sub serialize ($) {
195     my $node = shift;
196     my $r = '';
197    
198     my @node = map { [$_, ''] } @{$node->child_nodes};
199     while (@node) {
200     my $child = shift @node;
201     my $nt = $child->[0]->node_type;
202     if ($nt == $child->[0]->ELEMENT_NODE) {
203     $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
204    
205 wakaba 1.2 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
206 wakaba 1.1 @{$child->[0]->attributes}) {
207     $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
208     $r .= $attr->[1] . '"' . "\x0A";
209     }
210    
211     unshift @node,
212     map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
213     } elsif ($nt == $child->[0]->TEXT_NODE) {
214     $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
215     } elsif ($nt == $child->[0]->COMMENT_NODE) {
216     $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
217     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
218 wakaba 1.24 $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
219     my $pubid = $child->[0]->public_id;
220     $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
221     my $sysid = $child->[0]->system_id;
222     $r .= ' SYSTEM' if not length $pubid and length $sysid;
223     $r .= ' "' . $sysid . '"' if length $sysid;
224     $r .= ">\x0A";
225 wakaba 1.1 } else {
226     $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
227     }
228     }
229    
230     return $r;
231     } # serialize
232    
233     ## License: Public Domain.
234 wakaba 1.35 ## $Date: 2008/10/04 05:53:46 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24