/[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.22 - (hide annotations) (download) (as text)
Sat Aug 25 02:44:39 2007 UTC (17 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.21: +2 -2 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	25 Aug 2007 02:43:56 -0000
2007-08-25  Wakaba  <wakaba@suika.fam.cx>

	* content-model-1.dat: New tests for |meta| corner cases
	are added.

2007-08-22  Wakaba  <wakaba@suika.fam.cx>

	* tree-test-2.dat: New tests for |html| innerHTML are added.

++ whatpm/Whatpm/ContentChecker/ChangeLog	25 Aug 2007 02:39:52 -0000
2007-08-25  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm (head, noscript): Treatement for invalid |meta|
	elenments are changed as per HTML5 revision 1018.

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.22 BEGIN { plan tests => 980 }
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.19 } elsif (/^#document-fragment$/) {
78     $test->{element} = '';
79     $mode = 'element';
80     undef $escaped;
81 wakaba 1.5 } elsif (/^#document-fragment (\S+)$/) {
82     $test->{document} = '';
83     $mode = 'document';
84     $test->{element} = $1;
85 wakaba 1.12 undef $escaped;
86     } elsif (/^#document-fragment (\S+) escaped$/) {
87     $test->{document} = '';
88     $mode = 'document';
89     $test->{element} = $1;
90     $escaped = 1;
91 wakaba 1.2 } elsif (defined $test->{document} and /^$/) {
92 wakaba 1.13 $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
93 wakaba 1.2 test ($test);
94 wakaba 1.1 undef $test;
95     } else {
96     if ($mode eq 'data' or $mode eq 'document') {
97 wakaba 1.13 $test->{$mode} .= $_;
98 wakaba 1.19 } elsif ($mode eq 'element') {
99     tr/\x0D\x0A//d;
100     $test->{$mode} .= $_;
101 wakaba 1.1 } elsif ($mode eq 'errors') {
102     tr/\x0D\x0A//d;
103     push @{$test->{errors}}, $_;
104     }
105     }
106     }
107     test ($test) if $test->{errors};
108     }
109    
110 wakaba 1.4 use Whatpm::HTML;
111     use Whatpm::NanoDOM;
112 wakaba 1.1
113     sub test ($) {
114     my $test = shift;
115    
116 wakaba 1.4 my $doc = Whatpm::NanoDOM::Document->new;
117 wakaba 1.1 my @errors;
118    
119     $SIG{INT} = sub {
120 wakaba 1.3 print scalar serialize ($doc);
121 wakaba 1.1 exit;
122     };
123 wakaba 1.3
124 wakaba 1.5 my $onerror = sub {
125     my %opt = @_;
126     push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
127     };
128     my $result;
129     unless (defined $test->{element}) {
130     Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);
131     $result = serialize ($doc);
132     } else {
133     my $el = $doc->create_element_ns
134     ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
135     Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);
136     $result = serialize ($el);
137     }
138    
139 wakaba 1.1 ok scalar @errors, scalar @{$test->{errors}},
140 wakaba 1.21 'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
141 wakaba 1.1 join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
142    
143 wakaba 1.21 ok $result, $test->{document},
144     'Document tree: ' . Data::Dumper::qquote ($test->{data});
145 wakaba 1.1 } # test
146    
147     sub serialize ($) {
148     my $node = shift;
149     my $r = '';
150    
151     my @node = map { [$_, ''] } @{$node->child_nodes};
152     while (@node) {
153     my $child = shift @node;
154     my $nt = $child->[0]->node_type;
155     if ($nt == $child->[0]->ELEMENT_NODE) {
156     $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
157    
158 wakaba 1.2 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
159 wakaba 1.1 @{$child->[0]->attributes}) {
160     $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
161     $r .= $attr->[1] . '"' . "\x0A";
162     }
163    
164     unshift @node,
165     map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
166     } elsif ($nt == $child->[0]->TEXT_NODE) {
167     $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
168     } elsif ($nt == $child->[0]->COMMENT_NODE) {
169     $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
170     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
171     $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
172     } else {
173     $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
174     }
175     }
176    
177     return $r;
178     } # serialize
179    
180     ## License: Public Domain.
181 wakaba 1.22 ## $Date: 2007/07/21 08:11:48 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24