/[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.30 - (hide annotations) (download) (as text)
Fri Sep 5 17:57:47 2008 UTC (16 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.29: +17 -3 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	5 Sep 2008 17:40:06 -0000
	* content-model-1.dat: Test data for |XSLT-compat|
	added (HTML5 revision 2141).  A redundant test
	entry is removed.

	* HTML-tree.t: Support for should-level errors.

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

++ whatpm/Whatpm/ChangeLog	5 Sep 2008 17:28:08 -0000
2008-09-06  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: Support for |XSLT-compat| (HTML5 revision 2141).

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24