/[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.12 - (hide annotations) (download) (as text)
Sat Jun 23 03:53:35 2007 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.11: +24 -2 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	23 Jun 2007 03:51:41 -0000
	* tokenizer-test-2.dat: New test.  This test file contains
	some tests that cannot be reliably tested by |HTML-tokenize.t|,
	since they depends on how characeters are supplied to the
	tokenizer.

	* HTML-tree.t: |tokenizer-test-2.dat| added.  |#data escaped|
	etc. supported.

2007-06-23  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.1 ]) {
43 wakaba 1.2 open my $file, '<', $file_name
44     or die "$0: $file_name: $!";
45 wakaba 1.1
46     my $test;
47     my $mode = 'data';
48 wakaba 1.12 my $escaped;
49 wakaba 1.1 while (<$file>) {
50     s/\x0D\x0A/\x0A/;
51     if (/^#data$/) {
52     undef $test;
53     $test->{data} = '';
54     $mode = 'data';
55 wakaba 1.12 undef $escaped;
56     } elsif (/^#data escaped$/) {
57     undef $test;
58     $test->{data} = '';
59     $mode = 'data';
60     $escaped = 1;
61 wakaba 1.1 } elsif (/^#errors$/) {
62     $test->{errors} = [];
63     $mode = 'errors';
64 wakaba 1.12 undef $escaped;
65 wakaba 1.1 $test->{data} =~ s/\x0D?\x0A\z//;
66     } elsif (/^#document$/) {
67     $test->{document} = '';
68     $mode = 'document';
69 wakaba 1.12 undef $escaped;
70     } elsif (/^#document escaped$/) {
71     $test->{document} = '';
72     $mode = 'document';
73     $escaped = 1;
74 wakaba 1.5 } elsif (/^#document-fragment (\S+)$/) {
75     $test->{document} = '';
76     $mode = 'document';
77     $test->{element} = $1;
78 wakaba 1.12 undef $escaped;
79     } elsif (/^#document-fragment (\S+) escaped$/) {
80     $test->{document} = '';
81     $mode = 'document';
82     $test->{element} = $1;
83     $escaped = 1;
84 wakaba 1.2 } elsif (defined $test->{document} and /^$/) {
85     test ($test);
86 wakaba 1.1 undef $test;
87     } else {
88     if ($mode eq 'data' or $mode eq 'document') {
89 wakaba 1.12 my $s = $_;
90     $s =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
91     $test->{$mode} .= $s;
92 wakaba 1.1 } elsif ($mode eq 'errors') {
93     tr/\x0D\x0A//d;
94     push @{$test->{errors}}, $_;
95     }
96     }
97     }
98     test ($test) if $test->{errors};
99     }
100    
101 wakaba 1.4 use Whatpm::HTML;
102     use Whatpm::NanoDOM;
103 wakaba 1.1
104     sub test ($) {
105     my $test = shift;
106    
107 wakaba 1.4 my $doc = Whatpm::NanoDOM::Document->new;
108 wakaba 1.1 my @errors;
109    
110     $SIG{INT} = sub {
111 wakaba 1.3 print scalar serialize ($doc);
112 wakaba 1.1 exit;
113     };
114 wakaba 1.3
115 wakaba 1.5 my $onerror = sub {
116     my %opt = @_;
117     push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
118     };
119     my $result;
120     unless (defined $test->{element}) {
121     Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);
122     $result = serialize ($doc);
123     } else {
124     my $el = $doc->create_element_ns
125     ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
126     Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);
127     $result = serialize ($el);
128     }
129    
130 wakaba 1.1 ok scalar @errors, scalar @{$test->{errors}},
131     'Parse error: ' . $test->{data} . '; ' .
132     join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
133    
134 wakaba 1.5 ok $result, $test->{document}, 'Document tree: ' . $test->{data};
135 wakaba 1.1 } # test
136    
137     sub serialize ($) {
138     my $node = shift;
139     my $r = '';
140    
141     my @node = map { [$_, ''] } @{$node->child_nodes};
142     while (@node) {
143     my $child = shift @node;
144     my $nt = $child->[0]->node_type;
145     if ($nt == $child->[0]->ELEMENT_NODE) {
146     $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
147    
148 wakaba 1.2 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
149 wakaba 1.1 @{$child->[0]->attributes}) {
150     $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
151     $r .= $attr->[1] . '"' . "\x0A";
152     }
153    
154     unshift @node,
155     map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
156     } elsif ($nt == $child->[0]->TEXT_NODE) {
157     $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
158     } elsif ($nt == $child->[0]->COMMENT_NODE) {
159     $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
160     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
161     $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
162     } else {
163     $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
164     }
165     }
166    
167     return $r;
168     } # serialize
169    
170     ## License: Public Domain.
171 wakaba 1.12 ## $Date: 2007/06/23 02:26:51 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24