/[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.16 - (show annotations) (download) (as text)
Sat Jun 30 13:27:06 2007 UTC (18 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.15: +2 -2 lines
File MIME type: application/x-troff
Sync with latest html5lib tests

1 #!/usr/bin/perl
2 use strict;
3
4 my $dir_name;
5 my $test_dir_name;
6 BEGIN {
7 $test_dir_name = 't/';
8 $dir_name = 't/tree-construction/';
9 my $skip = "You don't have make command";
10 eval q{
11 system ("cd $test_dir_name; make tree-construction-files") == 0 or die
12 unless -f $dir_name.'tests1.dat';
13 $skip = '';
14 };
15 if ($skip) {
16 print "1..1\n";
17 print "ok 1 # $skip\n";
18 exit;
19 }
20 }
21
22 use Test;
23 BEGIN { plan tests => 626 }
24
25 use Data::Dumper;
26 $Data::Dumper::Useqq = 1;
27 sub Data::Dumper::qquote {
28 my $s = shift;
29 $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
30 return q<qq'> . $s . q<'>;
31 } # Data::Dumper::qquote
32
33 for my $file_name (grep {$_} split /\s+/, qq[
34 ${test_dir_name}tokenizer-test-2.dat
35 ${dir_name}tests1.dat
36 ${dir_name}tests2.dat
37 ${dir_name}tests3.dat
38 ${dir_name}tests4.dat
39 ${dir_name}tests5.dat
40 ${dir_name}tests6.dat
41 ${test_dir_name}tree-test-1.dat
42 ${test_dir_name}tree-test-2.dat
43 ]) {
44 open my $file, '<', $file_name
45 or die "$0: $file_name: $!";
46 print "# $file_name\n";
47
48 my $test;
49 my $mode = 'data';
50 my $escaped;
51 while (<$file>) {
52 s/\x0D\x0A/\x0A/;
53 if (/^#data$/) {
54 undef $test;
55 $test->{data} = '';
56 $mode = 'data';
57 undef $escaped;
58 } elsif (/^#data escaped$/) {
59 undef $test;
60 $test->{data} = '';
61 $mode = 'data';
62 $escaped = 1;
63 } elsif (/^#errors$/) {
64 $test->{errors} = [];
65 $mode = 'errors';
66 $test->{data} =~ s/\x0D?\x0A\z//;
67 $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
68 undef $escaped;
69 } elsif (/^#document$/) {
70 $test->{document} = '';
71 $mode = 'document';
72 undef $escaped;
73 } elsif (/^#document escaped$/) {
74 $test->{document} = '';
75 $mode = 'document';
76 $escaped = 1;
77 } elsif (/^#document-fragment (\S+)$/) {
78 $test->{document} = '';
79 $mode = 'document';
80 $test->{element} = $1;
81 undef $escaped;
82 } elsif (/^#document-fragment (\S+) escaped$/) {
83 $test->{document} = '';
84 $mode = 'document';
85 $test->{element} = $1;
86 $escaped = 1;
87 } elsif (defined $test->{document} and /^$/) {
88 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
89 test ($test);
90 undef $test;
91 } else {
92 if ($mode eq 'data' or $mode eq 'document') {
93 $test->{$mode} .= $_;
94 } elsif ($mode eq 'errors') {
95 tr/\x0D\x0A//d;
96 push @{$test->{errors}}, $_;
97 }
98 }
99 }
100 test ($test) if $test->{errors};
101 }
102
103 use Whatpm::HTML;
104 use Whatpm::NanoDOM;
105
106 sub test ($) {
107 my $test = shift;
108
109 my $doc = Whatpm::NanoDOM::Document->new;
110 my @errors;
111
112 $SIG{INT} = sub {
113 print scalar serialize ($doc);
114 exit;
115 };
116
117 my $onerror = sub {
118 my %opt = @_;
119 push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
120 };
121 my $result;
122 unless (defined $test->{element}) {
123 Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);
124 $result = serialize ($doc);
125 } else {
126 my $el = $doc->create_element_ns
127 ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
128 Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);
129 $result = serialize ($el);
130 }
131
132 ok scalar @errors, scalar @{$test->{errors}},
133 'Parse error: ' . $test->{data} . '; ' .
134 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
135
136 ok $result, $test->{document}, 'Document tree: ' . $test->{data};
137 } # test
138
139 sub serialize ($) {
140 my $node = shift;
141 my $r = '';
142
143 my @node = map { [$_, ''] } @{$node->child_nodes};
144 while (@node) {
145 my $child = shift @node;
146 my $nt = $child->[0]->node_type;
147 if ($nt == $child->[0]->ELEMENT_NODE) {
148 $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
149
150 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
151 @{$child->[0]->attributes}) {
152 $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
153 $r .= $attr->[1] . '"' . "\x0A";
154 }
155
156 unshift @node,
157 map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
158 } elsif ($nt == $child->[0]->TEXT_NODE) {
159 $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
160 } elsif ($nt == $child->[0]->COMMENT_NODE) {
161 $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
162 } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
163 $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
164 } else {
165 $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
166 }
167 }
168
169 return $r;
170 } # serialize
171
172 ## License: Public Domain.
173 ## $Date: 2007/06/30 13:12:33 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24