/[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.14 - (hide annotations) (download) (as text)
Mon Jun 25 11:05:57 2007 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.13: +2 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	25 Jun 2007 11:03:58 -0000
	* tree-test-1.dat: The tests using |#document-fragment|
	are move to |tree-test-2.dat|.

	* tree-test-2.dat: New test data.  In addition to
	tests from |tree-test-1.dat|, two tests
	for |</table>| are added.

	* HTML-tree.t: |tree-test-2.dat| is added.

2007-06-25  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	25 Jun 2007 11:05:50 -0000
	* HTML.pm.src (_reset_insertion_mode): Interpretation
	of Step 3 has been changed.

2007-06-25  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4     my $dir_name;
5 wakaba 1.2 my $test_dir_name;
6 wakaba 1.1 BEGIN {
7 wakaba 1.2 $test_dir_name = 't/';
8 wakaba 1.1 $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 wakaba 1.10 BEGIN { plan tests => 472 }
24 wakaba 1.1
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 wakaba 1.2 for my $file_name (grep {$_} split /\s+/, qq[
34 wakaba 1.12 ${test_dir_name}tokenizer-test-2.dat
35 wakaba 1.2 ${dir_name}tests1.dat
36     ${dir_name}tests2.dat
37     ${dir_name}tests3.dat
38     ${dir_name}tests4.dat
39 wakaba 1.11 ${dir_name}tests5.dat
40     ${dir_name}tests6.dat
41 wakaba 1.2 ${test_dir_name}tree-test-1.dat
42 wakaba 1.14 ${test_dir_name}tree-test-2.dat
43 wakaba 1.1 ]) {
44 wakaba 1.2 open my $file, '<', $file_name
45     or die "$0: $file_name: $!";
46 wakaba 1.13 print "# $file_name\n";
47 wakaba 1.1
48     my $test;
49     my $mode = 'data';
50 wakaba 1.12 my $escaped;
51 wakaba 1.1 while (<$file>) {
52     s/\x0D\x0A/\x0A/;
53     if (/^#data$/) {
54     undef $test;
55     $test->{data} = '';
56     $mode = 'data';
57 wakaba 1.12 undef $escaped;
58     } elsif (/^#data escaped$/) {
59     undef $test;
60     $test->{data} = '';
61     $mode = 'data';
62     $escaped = 1;
63 wakaba 1.1 } elsif (/^#errors$/) {
64     $test->{errors} = [];
65     $mode = 'errors';
66 wakaba 1.13 $test->{data} =~ s/\x0D?\x0A\z//;
67     $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
68 wakaba 1.12 undef $escaped;
69 wakaba 1.1 } elsif (/^#document$/) {
70     $test->{document} = '';
71     $mode = 'document';
72 wakaba 1.12 undef $escaped;
73     } elsif (/^#document escaped$/) {
74     $test->{document} = '';
75     $mode = 'document';
76     $escaped = 1;
77 wakaba 1.5 } elsif (/^#document-fragment (\S+)$/) {
78     $test->{document} = '';
79     $mode = 'document';
80     $test->{element} = $1;
81 wakaba 1.12 undef $escaped;
82     } elsif (/^#document-fragment (\S+) escaped$/) {
83     $test->{document} = '';
84     $mode = 'document';
85     $test->{element} = $1;
86     $escaped = 1;
87 wakaba 1.2 } elsif (defined $test->{document} and /^$/) {
88 wakaba 1.13 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
89 wakaba 1.2 test ($test);
90 wakaba 1.1 undef $test;
91     } else {
92     if ($mode eq 'data' or $mode eq 'document') {
93 wakaba 1.13 $test->{$mode} .= $_;
94 wakaba 1.1 } 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 wakaba 1.4 use Whatpm::HTML;
104     use Whatpm::NanoDOM;
105 wakaba 1.1
106     sub test ($) {
107     my $test = shift;
108    
109 wakaba 1.4 my $doc = Whatpm::NanoDOM::Document->new;
110 wakaba 1.1 my @errors;
111    
112     $SIG{INT} = sub {
113 wakaba 1.3 print scalar serialize ($doc);
114 wakaba 1.1 exit;
115     };
116 wakaba 1.3
117 wakaba 1.5 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 wakaba 1.1 ok scalar @errors, scalar @{$test->{errors}},
133     'Parse error: ' . $test->{data} . '; ' .
134     join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
135    
136 wakaba 1.5 ok $result, $test->{document}, 'Document tree: ' . $test->{data};
137 wakaba 1.1 } # 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 wakaba 1.2 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
151 wakaba 1.1 @{$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 wakaba 1.14 ## $Date: 2007/06/23 06:38:12 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24