/[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.32 - (hide annotations) (download) (as text)
Sun Sep 14 09:05:55 2008 UTC (16 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.31: +2 -2 lines
File MIME type: application/x-troff
++ whatpm/Whatpm/ChangeLog	14 Sep 2008 09:05:44 -0000
	* HTML.pm.src: White-space-leaded non-white-space character
	tokens in "before head insertion mode" was not
	correctly handled.
	(set_inner_html): Reimplemented using CharString decodehandle
	class.  Support for $get_wrapper argument.  Support
	for |{read_until}| feature.

2008-09-14  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.32 use lib qw[/home/wakaba/work/manakai2/lib];
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.31 ${test_dir_name}tokenizer-test-3.dat
53 wakaba 1.2 ${dir_name}tests1.dat
54     ${dir_name}tests2.dat
55     ${dir_name}tests3.dat
56     ${dir_name}tests4.dat
57 wakaba 1.11 ${dir_name}tests5.dat
58     ${dir_name}tests6.dat
59 wakaba 1.27 ${dir_name}tests7.dat
60 wakaba 1.2 ${test_dir_name}tree-test-1.dat
61 wakaba 1.14 ${test_dir_name}tree-test-2.dat
62 wakaba 1.29 ${test_dir_name}tree-test-3.dat
63 wakaba 1.1 ]) {
64 wakaba 1.2 open my $file, '<', $file_name
65     or die "$0: $file_name: $!";
66 wakaba 1.13 print "# $file_name\n";
67 wakaba 1.1
68     my $test;
69     my $mode = 'data';
70 wakaba 1.12 my $escaped;
71 wakaba 1.1 while (<$file>) {
72     s/\x0D\x0A/\x0A/;
73     if (/^#data$/) {
74     undef $test;
75     $test->{data} = '';
76     $mode = 'data';
77 wakaba 1.12 undef $escaped;
78     } elsif (/^#data escaped$/) {
79     undef $test;
80     $test->{data} = '';
81     $mode = 'data';
82     $escaped = 1;
83 wakaba 1.1 } elsif (/^#errors$/) {
84     $test->{errors} = [];
85     $mode = 'errors';
86 wakaba 1.13 $test->{data} =~ s/\x0D?\x0A\z//;
87     $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
88 wakaba 1.24 $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
89 wakaba 1.12 undef $escaped;
90 wakaba 1.30 } elsif (/^#shoulds$/) {
91     $test->{shoulds} = [];
92     $mode = 'shoulds';
93 wakaba 1.1 } elsif (/^#document$/) {
94     $test->{document} = '';
95     $mode = 'document';
96 wakaba 1.12 undef $escaped;
97     } elsif (/^#document escaped$/) {
98     $test->{document} = '';
99     $mode = 'document';
100     $escaped = 1;
101 wakaba 1.19 } elsif (/^#document-fragment$/) {
102     $test->{element} = '';
103     $mode = 'element';
104     undef $escaped;
105 wakaba 1.5 } elsif (/^#document-fragment (\S+)$/) {
106     $test->{document} = '';
107     $mode = 'document';
108     $test->{element} = $1;
109 wakaba 1.12 undef $escaped;
110     } elsif (/^#document-fragment (\S+) escaped$/) {
111     $test->{document} = '';
112     $mode = 'document';
113     $test->{element} = $1;
114     $escaped = 1;
115 wakaba 1.2 } elsif (defined $test->{document} and /^$/) {
116 wakaba 1.13 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
117 wakaba 1.24 $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
118 wakaba 1.2 test ($test);
119 wakaba 1.1 undef $test;
120     } else {
121     if ($mode eq 'data' or $mode eq 'document') {
122 wakaba 1.13 $test->{$mode} .= $_;
123 wakaba 1.19 } elsif ($mode eq 'element') {
124     tr/\x0D\x0A//d;
125     $test->{$mode} .= $_;
126 wakaba 1.1 } elsif ($mode eq 'errors') {
127     tr/\x0D\x0A//d;
128     push @{$test->{errors}}, $_;
129 wakaba 1.30 } elsif ($mode eq 'shoulds') {
130     tr/\x0D\x0A//d;
131     push @{$test->{shoulds}}, $_;
132 wakaba 1.1 }
133     }
134     }
135     test ($test) if $test->{errors};
136     }
137    
138 wakaba 1.4 use Whatpm::HTML;
139     use Whatpm::NanoDOM;
140 wakaba 1.1
141     sub test ($) {
142     my $test = shift;
143    
144 wakaba 1.4 my $doc = Whatpm::NanoDOM::Document->new;
145 wakaba 1.1 my @errors;
146 wakaba 1.30 my @shoulds;
147 wakaba 1.1
148     $SIG{INT} = sub {
149 wakaba 1.3 print scalar serialize ($doc);
150 wakaba 1.1 exit;
151     };
152 wakaba 1.3
153 wakaba 1.5 my $onerror = sub {
154     my %opt = @_;
155 wakaba 1.30 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 wakaba 1.5 };
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 wakaba 1.1 ok scalar @errors, scalar @{$test->{errors}},
173 wakaba 1.21 'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
174 wakaba 1.1 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
175 wakaba 1.30 ok scalar @shoulds, scalar @{$test->{shoulds} or []},
176     'SHOULD-level error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
177     join (', ', @shoulds) . ';' . join (', ', @{$test->{shoulds} or []});
178 wakaba 1.1
179 wakaba 1.21 ok $result, $test->{document},
180     'Document tree: ' . Data::Dumper::qquote ($test->{data});
181 wakaba 1.1 } # 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 wakaba 1.2 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
195 wakaba 1.1 @{$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 wakaba 1.24 $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 wakaba 1.1 } 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 wakaba 1.32 ## $Date: 2008/09/13 04:19:56 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24