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