/[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.29 - (show annotations) (download) (as text)
Sat Aug 30 12:57:06 2008 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.28: +3 -2 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	30 Aug 2008 12:56:52 -0000
	* HTML-tree.dat: tree-test-3.dat added.

	* tree-test-3.dat: Test data for definitionURL="" are added (cf.
	HTML5 revision 2130).

2008-08-30  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	30 Aug 2008 12:55:11 -0000
	* mkhtmlparser.pl: Support for MathML |definitionURL| attribute (HTML5
	revision 2130).

2008-08-30  Wakaba  <wakaba@suika.fam.cx>

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24