| 2 |
use strict; |
use strict; |
| 3 |
|
|
| 4 |
my $dir_name; |
my $dir_name; |
| 5 |
|
my $test_dir_name; |
| 6 |
BEGIN { |
BEGIN { |
| 7 |
my $test_dir_name = 't/'; |
$test_dir_name = 't/'; |
| 8 |
$dir_name = 't/tree-construction/'; |
$dir_name = 't/tree-construction/'; |
| 9 |
my $skip = "You don't have make command"; |
my $skip = "You don't have make command"; |
| 10 |
eval q{ |
eval q{ |
| 20 |
} |
} |
| 21 |
|
|
| 22 |
use Test; |
use Test; |
| 23 |
BEGIN { plan tests => 67 } |
BEGIN { plan tests => 980 } |
| 24 |
|
|
| 25 |
use Data::Dumper; |
use Data::Dumper; |
| 26 |
$Data::Dumper::Useqq = 1; |
$Data::Dumper::Useqq = 1; |
| 30 |
return q<qq'> . $s . q<'>; |
return q<qq'> . $s . q<'>; |
| 31 |
} # Data::Dumper::qquote |
} # Data::Dumper::qquote |
| 32 |
|
|
| 33 |
for my $file_name (qw[ |
for my $file_name (grep {$_} split /\s+/, qq[ |
| 34 |
tests1.dat |
${test_dir_name}tokenizer-test-2.dat |
| 35 |
tests2.dat |
${dir_name}tests1.dat |
| 36 |
tests3.dat |
${dir_name}tests2.dat |
| 37 |
tests4.dat |
${dir_name}tests3.dat |
| 38 |
|
${dir_name}tests4.dat |
| 39 |
|
${dir_name}tests5.dat |
| 40 |
|
${dir_name}tests6.dat |
| 41 |
|
${test_dir_name}tree-test-1.dat |
| 42 |
|
${test_dir_name}tree-test-2.dat |
| 43 |
]) { |
]) { |
| 44 |
open my $file, '<', $dir_name.$file_name |
open my $file, '<', $file_name |
| 45 |
or die "$0: $dir_name$file_name: $!"; |
or die "$0: $file_name: $!"; |
| 46 |
|
print "# $file_name\n"; |
| 47 |
|
|
| 48 |
my $test; |
my $test; |
| 49 |
my $mode = 'data'; |
my $mode = 'data'; |
| 50 |
|
my $escaped; |
| 51 |
while (<$file>) { |
while (<$file>) { |
| 52 |
s/\x0D\x0A/\x0A/; |
s/\x0D\x0A/\x0A/; |
| 53 |
if (/^#data$/) { |
if (/^#data$/) { |
| 54 |
undef $test; |
undef $test; |
| 55 |
$test->{data} = ''; |
$test->{data} = ''; |
| 56 |
$mode = 'data'; |
$mode = 'data'; |
| 57 |
|
undef $escaped; |
| 58 |
|
} elsif (/^#data escaped$/) { |
| 59 |
|
undef $test; |
| 60 |
|
$test->{data} = ''; |
| 61 |
|
$mode = 'data'; |
| 62 |
|
$escaped = 1; |
| 63 |
} elsif (/^#errors$/) { |
} elsif (/^#errors$/) { |
| 64 |
$test->{errors} = []; |
$test->{errors} = []; |
| 65 |
$mode = 'errors'; |
$mode = 'errors'; |
| 66 |
$test->{data} =~ s/\x0D?\x0A\z//; |
$test->{data} =~ s/\x0D?\x0A\z//; |
| 67 |
|
$test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped; |
| 68 |
|
undef $escaped; |
| 69 |
} elsif (/^#document$/) { |
} elsif (/^#document$/) { |
| 70 |
$test->{document} = ''; |
$test->{document} = ''; |
| 71 |
$mode = 'document'; |
$mode = 'document'; |
| 72 |
} elsif (/^$/) { |
undef $escaped; |
| 73 |
test ($test) if $test->{errors}; |
} elsif (/^#document escaped$/) { |
| 74 |
|
$test->{document} = ''; |
| 75 |
|
$mode = 'document'; |
| 76 |
|
$escaped = 1; |
| 77 |
|
} elsif (/^#document-fragment$/) { |
| 78 |
|
$test->{element} = ''; |
| 79 |
|
$mode = 'element'; |
| 80 |
|
undef $escaped; |
| 81 |
|
} elsif (/^#document-fragment (\S+)$/) { |
| 82 |
|
$test->{document} = ''; |
| 83 |
|
$mode = 'document'; |
| 84 |
|
$test->{element} = $1; |
| 85 |
|
undef $escaped; |
| 86 |
|
} elsif (/^#document-fragment (\S+) escaped$/) { |
| 87 |
|
$test->{document} = ''; |
| 88 |
|
$mode = 'document'; |
| 89 |
|
$test->{element} = $1; |
| 90 |
|
$escaped = 1; |
| 91 |
|
} elsif (defined $test->{document} and /^$/) { |
| 92 |
|
$test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped; |
| 93 |
|
test ($test); |
| 94 |
undef $test; |
undef $test; |
| 95 |
} else { |
} else { |
| 96 |
if ($mode eq 'data' or $mode eq 'document') { |
if ($mode eq 'data' or $mode eq 'document') { |
| 97 |
$test->{$mode} .= $_; |
$test->{$mode} .= $_; |
| 98 |
|
} elsif ($mode eq 'element') { |
| 99 |
|
tr/\x0D\x0A//d; |
| 100 |
|
$test->{$mode} .= $_; |
| 101 |
} elsif ($mode eq 'errors') { |
} elsif ($mode eq 'errors') { |
| 102 |
tr/\x0D\x0A//d; |
tr/\x0D\x0A//d; |
| 103 |
push @{$test->{errors}}, $_; |
push @{$test->{errors}}, $_; |
| 107 |
test ($test) if $test->{errors}; |
test ($test) if $test->{errors}; |
| 108 |
} |
} |
| 109 |
|
|
| 110 |
use What::HTML; |
use Whatpm::HTML; |
| 111 |
|
use Whatpm::NanoDOM; |
| 112 |
|
|
| 113 |
sub test ($) { |
sub test ($) { |
| 114 |
my $test = shift; |
my $test = shift; |
| 115 |
|
|
| 116 |
my $s = $test->{data}; |
my $doc = Whatpm::NanoDOM::Document->new; |
|
|
|
|
my $p = What::HTML->new; |
|
|
my $i = 0; |
|
|
$p->{set_next_input_character} = sub { |
|
|
my $self = shift; |
|
|
$self->{next_input_character} = -1 and return if $i >= length $s; |
|
|
$self->{next_input_character} = ord substr $s, $i++, 1; |
|
|
|
|
|
if ($self->{next_input_character} == 0x000D) { # CR |
|
|
if ($i >= length $s) { |
|
|
# |
|
|
} else { |
|
|
my $next_char = ord substr $s, $i++, 1; |
|
|
if ($next_char == 0x000A) { # LF |
|
|
# |
|
|
} else { |
|
|
push @{$self->{char}}, $next_char; |
|
|
} |
|
|
} |
|
|
$self->{next_input_character} = 0x000A; # LF # MUST |
|
|
} elsif ($self->{next_input_character} > 0x10FFFF) { |
|
|
$self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST |
|
|
} elsif ($self->{next_input_character} == 0x0000) { # NULL |
|
|
$self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST |
|
|
} |
|
|
}; |
|
|
|
|
| 117 |
my @errors; |
my @errors; |
|
$p->{parse_error} = sub { |
|
|
my $msg = shift; |
|
|
push @errors, $msg; |
|
|
}; |
|
| 118 |
|
|
| 119 |
$SIG{INT} = sub { |
$SIG{INT} = sub { |
| 120 |
print scalar serialize ($p->{document}); |
print scalar serialize ($doc); |
| 121 |
exit; |
exit; |
| 122 |
}; |
}; |
|
|
|
|
$p->_initialize_tokenizer; |
|
|
$p->_initialize_tree_constructor; |
|
|
$p->_construct_tree; |
|
|
$p->_terminate_tree_constructor; |
|
| 123 |
|
|
| 124 |
|
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 |
ok scalar @errors, scalar @{$test->{errors}}, |
ok scalar @errors, scalar @{$test->{errors}}, |
| 140 |
'Parse error: ' . $test->{data} . '; ' . |
'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' . |
| 141 |
join (', ', @errors) . ';' . join (', ', @{$test->{errors}}); |
join (', ', @errors) . ';' . join (', ', @{$test->{errors}}); |
| 142 |
|
|
| 143 |
my $doc = $p->{document}; |
ok $result, $test->{document}, |
| 144 |
my $doc_s = serialize ($doc); |
'Document tree: ' . Data::Dumper::qquote ($test->{data}); |
|
ok $doc_s, $test->{document}, 'Document tree: ' . $test->{data}; |
|
| 145 |
} # test |
} # test |
| 146 |
|
|
| 147 |
sub serialize ($) { |
sub serialize ($) { |
| 155 |
if ($nt == $child->[0]->ELEMENT_NODE) { |
if ($nt == $child->[0]->ELEMENT_NODE) { |
| 156 |
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
| 157 |
|
|
| 158 |
for my $attr (sort {$a->[1] cmp $b->[1]} map { [$_->name, $_->value] } |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
| 159 |
@{$child->[0]->attributes}) { |
@{$child->[0]->attributes}) { |
| 160 |
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
| 161 |
$r .= $attr->[1] . '"' . "\x0A"; |
$r .= $attr->[1] . '"' . "\x0A"; |