/[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.11 - (show annotations) (download) (as text)
Sat Jun 23 02:26:51 2007 UTC (19 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +3 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	23 Jun 2007 02:21:24 -0000
2007-06-23  Wakaba  <wakaba@suika.fam.cx>

	* Makefile, HTML-tokenizer.t, HTML-tree.t: New test
	files are added.

	* tokenize/, tree-construction/: Sync with latest html5lib
	trunk.

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 => 472 }
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 ${dir_name}tests1.dat
35 ${dir_name}tests2.dat
36 ${dir_name}tests3.dat
37 ${dir_name}tests4.dat
38 ${dir_name}tests5.dat
39 ${dir_name}tests6.dat
40 ${test_dir_name}tree-test-1.dat
41 ]) {
42 open my $file, '<', $file_name
43 or die "$0: $file_name: $!";
44
45 my $test;
46 my $mode = 'data';
47 while (<$file>) {
48 s/\x0D\x0A/\x0A/;
49 if (/^#data$/) {
50 undef $test;
51 $test->{data} = '';
52 $mode = 'data';
53 } elsif (/^#errors$/) {
54 $test->{errors} = [];
55 $mode = 'errors';
56 $test->{data} =~ s/\x0D?\x0A\z//;
57 } elsif (/^#document$/) {
58 $test->{document} = '';
59 $mode = 'document';
60 } elsif (/^#document-fragment (\S+)$/) {
61 $test->{document} = '';
62 $mode = 'document';
63 $test->{element} = $1;
64 } elsif (defined $test->{document} and /^$/) {
65 test ($test);
66 undef $test;
67 } else {
68 if ($mode eq 'data' or $mode eq 'document') {
69 $test->{$mode} .= $_;
70 } elsif ($mode eq 'errors') {
71 tr/\x0D\x0A//d;
72 push @{$test->{errors}}, $_;
73 }
74 }
75 }
76 test ($test) if $test->{errors};
77 }
78
79 use Whatpm::HTML;
80 use Whatpm::NanoDOM;
81
82 sub test ($) {
83 my $test = shift;
84
85 my $doc = Whatpm::NanoDOM::Document->new;
86 my @errors;
87
88 $SIG{INT} = sub {
89 print scalar serialize ($doc);
90 exit;
91 };
92
93 my $onerror = sub {
94 my %opt = @_;
95 push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
96 };
97 my $result;
98 unless (defined $test->{element}) {
99 Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);
100 $result = serialize ($doc);
101 } else {
102 my $el = $doc->create_element_ns
103 ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
104 Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);
105 $result = serialize ($el);
106 }
107
108 ok scalar @errors, scalar @{$test->{errors}},
109 'Parse error: ' . $test->{data} . '; ' .
110 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
111
112 ok $result, $test->{document}, 'Document tree: ' . $test->{data};
113 } # test
114
115 sub serialize ($) {
116 my $node = shift;
117 my $r = '';
118
119 my @node = map { [$_, ''] } @{$node->child_nodes};
120 while (@node) {
121 my $child = shift @node;
122 my $nt = $child->[0]->node_type;
123 if ($nt == $child->[0]->ELEMENT_NODE) {
124 $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
125
126 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
127 @{$child->[0]->attributes}) {
128 $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
129 $r .= $attr->[1] . '"' . "\x0A";
130 }
131
132 unshift @node,
133 map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
134 } elsif ($nt == $child->[0]->TEXT_NODE) {
135 $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
136 } elsif ($nt == $child->[0]->COMMENT_NODE) {
137 $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
138 } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
139 $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
140 } else {
141 $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
142 }
143 }
144
145 return $r;
146 } # serialize
147
148 ## License: Public Domain.
149 ## $Date: 2007/05/30 12:24:50 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24