/[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.13 - (hide annotations) (download) (as text)
Sat Jun 23 06:38:12 2007 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.12: +6 -5 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	23 Jun 2007 06:37:09 -0000
	* tokenizer-test-1.test: |™| test added.  (HTML5 revision 889.)

	* HTML-tree.t: Output test file names.  Escaped
	new line at the end of test data was removed.

	* tokenizer-test-2.dat: Tests for newlines, NULL, and
	escape flag stuff in |set_inner_html|.

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

++ whatpm/Whatpm/ChangeLog	23 Jun 2007 06:35:23 -0000
	* HTML.pm.src (set_inner_html): HTML5 revision 892 (adopt
	nodes before appended).  Parser was not ready for NULL
	parse error and escape flag.

	* NanoDOM.pm (adopt_node): New.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24