| 1 |
|
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
# should be 43. |
| 6 |
use Test::More tests => 43; |
| 7 |
|
| 8 |
use XML::LibXML; |
| 9 |
use IO::File; |
| 10 |
|
| 11 |
# TEST |
| 12 |
ok(1, ' TODO : Add test name'); |
| 13 |
|
| 14 |
my $html = "example/test.html"; |
| 15 |
|
| 16 |
my $parser = XML::LibXML->new(); |
| 17 |
{ |
| 18 |
my $doc = $parser->parse_html_file($html); |
| 19 |
# TEST |
| 20 |
ok($doc, ' TODO : Add test name'); |
| 21 |
} |
| 22 |
|
| 23 |
my $fh; |
| 24 |
|
| 25 |
open $fh, '<', $html |
| 26 |
or die "Can't open '$html': $!"; |
| 27 |
|
| 28 |
my $string; |
| 29 |
{ |
| 30 |
local $/; |
| 31 |
$string = <$fh>; |
| 32 |
} |
| 33 |
|
| 34 |
seek($fh, 0, 0); |
| 35 |
|
| 36 |
# TEST |
| 37 |
|
| 38 |
ok($string, ' TODO : Add test name'); |
| 39 |
|
| 40 |
my $doc = $parser->parse_html_string($string); |
| 41 |
|
| 42 |
# TEST |
| 43 |
|
| 44 |
ok($doc, ' TODO : Add test name'); |
| 45 |
|
| 46 |
undef $doc; |
| 47 |
|
| 48 |
$doc = $parser->parse_html_fh($fh); |
| 49 |
|
| 50 |
# TEST |
| 51 |
|
| 52 |
ok($doc, ' TODO : Add test name'); |
| 53 |
|
| 54 |
$fh->close(); |
| 55 |
|
| 56 |
# parsing HTML's CGI calling links |
| 57 |
|
| 58 |
my $strhref = <<EOHTML; |
| 59 |
|
| 60 |
<html> |
| 61 |
<body> |
| 62 |
<a href="http:/foo.bar/foobar.pl?foo=bar&bar=foo"> |
| 63 |
foo |
| 64 |
</a> |
| 65 |
<p>test |
| 66 |
</body> |
| 67 |
</html> |
| 68 |
EOHTML |
| 69 |
|
| 70 |
my $htmldoc; |
| 71 |
|
| 72 |
$parser->recover(1); |
| 73 |
eval { |
| 74 |
local $SIG{'__WARN__'} = sub { }; |
| 75 |
$htmldoc = $parser->parse_html_string( $strhref ); |
| 76 |
}; |
| 77 |
|
| 78 |
# ok( not $@ ); |
| 79 |
# TEST |
| 80 |
ok( $htmldoc, ' TODO : Add test name' ); |
| 81 |
|
| 82 |
# parse_html_string with encoding |
| 83 |
# encodings |
| 84 |
SKIP: |
| 85 |
{ |
| 86 |
if (! eval { require Encode; }) |
| 87 |
{ |
| 88 |
skip("Encoding related tests require Encode", 14); |
| 89 |
} |
| 90 |
use utf8; |
| 91 |
|
| 92 |
my $utf_str = "ěščř"; |
| 93 |
|
| 94 |
# w/o 'meta' charset |
| 95 |
$strhref = <<EOHTML; |
| 96 |
<html> |
| 97 |
<body> |
| 98 |
<p>$utf_str</p> |
| 99 |
</body> |
| 100 |
</html> |
| 101 |
EOHTML |
| 102 |
|
| 103 |
# TEST |
| 104 |
|
| 105 |
ok( Encode::is_utf8($strhref), ' TODO : Add test name' ); |
| 106 |
$htmldoc = $parser->parse_html_string( $strhref ); |
| 107 |
# TEST |
| 108 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 109 |
# TEST |
| 110 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 111 |
|
| 112 |
$htmldoc = $parser->parse_html_string( $strhref, |
| 113 |
{ |
| 114 |
encoding => 'UTF-8' |
| 115 |
} |
| 116 |
); |
| 117 |
# TEST |
| 118 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 119 |
# TEST |
| 120 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 121 |
|
| 122 |
|
| 123 |
my $iso_str = Encode::encode('iso-8859-2', $strhref); |
| 124 |
$htmldoc = $parser->parse_html_string( $iso_str, |
| 125 |
{ |
| 126 |
encoding => 'iso-8859-2' |
| 127 |
} |
| 128 |
); |
| 129 |
# TEST |
| 130 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 131 |
# TEST |
| 132 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 133 |
|
| 134 |
# w/ 'meta' charset |
| 135 |
$strhref = <<EOHTML; |
| 136 |
<html> |
| 137 |
<head> |
| 138 |
<meta http-equiv="Content-Type" content="text/html; |
| 139 |
charset=iso-8859-2"> |
| 140 |
</head> |
| 141 |
<body> |
| 142 |
<p>$utf_str</p> |
| 143 |
</body> |
| 144 |
</html> |
| 145 |
EOHTML |
| 146 |
|
| 147 |
$htmldoc = $parser->parse_html_string( $strhref, { encoding => 'UTF-8' }); |
| 148 |
# TEST |
| 149 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 150 |
# TEST |
| 151 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 152 |
|
| 153 |
$iso_str = Encode::encode('iso-8859-2', $strhref); |
| 154 |
$htmldoc = $parser->parse_html_string( $iso_str ); |
| 155 |
# TEST |
| 156 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 157 |
# TEST |
| 158 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 159 |
|
| 160 |
$htmldoc = $parser->parse_html_string( $iso_str, { encoding => 'iso-8859-2', |
| 161 |
URI => 'foo' |
| 162 |
} ); |
| 163 |
# TEST |
| 164 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 165 |
# TEST |
| 166 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 167 |
# TEST |
| 168 |
is($htmldoc->URI, 'foo', ' TODO : Add test name'); |
| 169 |
} |
| 170 |
|
| 171 |
# parse example/enc_latin2.html |
| 172 |
# w/ 'meta' charset |
| 173 |
{ |
| 174 |
use utf8; |
| 175 |
my $utf_str = "ěščř"; |
| 176 |
my $test_file = 'example/enc_latin2.html'; |
| 177 |
my $fh; |
| 178 |
|
| 179 |
$htmldoc = $parser->parse_html_file( $test_file ); |
| 180 |
# TEST |
| 181 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 182 |
# TEST |
| 183 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 184 |
|
| 185 |
$htmldoc = $parser->parse_html_file( $test_file, { encoding => 'iso-8859-2', |
| 186 |
URI => 'foo' |
| 187 |
}); |
| 188 |
# TEST |
| 189 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 190 |
# TEST |
| 191 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 192 |
# TEST |
| 193 |
is($htmldoc->URI, 'foo', ' TODO : Add test name'); |
| 194 |
|
| 195 |
open $fh, '<', $test_file |
| 196 |
or die "Cannot open '$test_file' for reading - $!"; |
| 197 |
$htmldoc = $parser->parse_html_fh( $fh ); |
| 198 |
close $fh; |
| 199 |
# TEST |
| 200 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 201 |
# TEST |
| 202 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 203 |
|
| 204 |
open $fh, '<', $test_file |
| 205 |
or die "Cannot open '$test_file' for reading - $!"; |
| 206 |
$htmldoc = $parser->parse_html_fh( $fh, { encoding => 'iso-8859-2', |
| 207 |
URI => 'foo', |
| 208 |
}); |
| 209 |
close $fh; |
| 210 |
# TEST |
| 211 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 212 |
# TEST |
| 213 |
is($htmldoc->URI, 'foo', ' TODO : Add test name'); |
| 214 |
# TEST |
| 215 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 216 |
|
| 217 |
SKIP: |
| 218 |
{ |
| 219 |
my $num_tests = 2; |
| 220 |
|
| 221 |
# LibXML_read_perl doesn't play well with encoding layers. Skip |
| 222 |
# unconditionally for now. |
| 223 |
skip("skipping until LibXML_read_perl is fixed", $num_tests); |
| 224 |
|
| 225 |
if (1000*$] < 5008) |
| 226 |
{ |
| 227 |
skip("skipping for Perl < 5.8", $num_tests); |
| 228 |
} |
| 229 |
elsif (20627 > XML::LibXML::LIBXML_VERSION) |
| 230 |
{ |
| 231 |
skip("skipping for libxml2 < 2.6.27", $num_tests); |
| 232 |
} |
| 233 |
# translate to UTF8 on perl-side |
| 234 |
open $fh, '<:encoding(iso-8859-2)', $test_file |
| 235 |
or die "Cannot open '$test_file' for reading - $!"; |
| 236 |
$htmldoc = $parser->parse_html_fh( $fh, { encoding => 'UTF-8' }); |
| 237 |
close $fh; |
| 238 |
# TEST |
| 239 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 240 |
# TEST |
| 241 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
# parse example/enc2_latin2.html |
| 246 |
# w/o 'meta' charset |
| 247 |
{ |
| 248 |
use utf8; |
| 249 |
my $utf_str = "ěščř"; |
| 250 |
my $test_file = 'example/enc2_latin2.html'; |
| 251 |
my $fh; |
| 252 |
|
| 253 |
$htmldoc = $parser->parse_html_file( $test_file, { encoding => 'iso-8859-2' }); |
| 254 |
# TEST |
| 255 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 256 |
# TEST |
| 257 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 258 |
|
| 259 |
open $fh, '<', $test_file |
| 260 |
or die "Cannot open '$test_file' for reading - $!"; |
| 261 |
$htmldoc = $parser->parse_html_fh( $fh, { encoding => 'iso-8859-2' }); |
| 262 |
close $fh; |
| 263 |
# TEST |
| 264 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 265 |
# TEST |
| 266 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 267 |
|
| 268 |
SKIP: |
| 269 |
{ |
| 270 |
my $num_tests = 2; |
| 271 |
|
| 272 |
# LibXML_read_perl doesn't play well with encoding layers. Skip |
| 273 |
# unconditionally for now. |
| 274 |
skip("skipping until LibXML_read_perl is fixed", $num_tests); |
| 275 |
|
| 276 |
if (1000*$] < 5008) |
| 277 |
{ |
| 278 |
skip("skipping for Perl < 5.8", $num_tests); |
| 279 |
} |
| 280 |
# translate to UTF8 on perl-side |
| 281 |
open my $fh, '<:encoding(iso-8859-2)', $test_file |
| 282 |
or die "Cannot open '$test_file' for reading - $!"; |
| 283 |
$htmldoc = $parser->parse_html_fh( $fh, { encoding => 'UTF-8' } ); |
| 284 |
close $fh; |
| 285 |
# TEST |
| 286 |
ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' ); |
| 287 |
# TEST |
| 288 |
is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name'); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
{ |
| 294 |
# 44715 |
| 295 |
|
| 296 |
my $html = <<'EOF'; |
| 297 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 298 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 299 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 300 |
<head> |
| 301 |
<title>Test & Test some more</title> |
| 302 |
</head> |
| 303 |
<body> |
| 304 |
<p>Meet you at the café?</p> |
| 305 |
<p>How about <a href="http://example.com?mode=cafe&id=1&ref=foo">this one</a>? |
| 306 |
</p> |
| 307 |
<input class="wibble" id="foo" value="working" /> |
| 308 |
</body> |
| 309 |
</html> |
| 310 |
EOF |
| 311 |
my $parser = XML::LibXML->new; |
| 312 |
eval { |
| 313 |
$doc = $parser->parse_html_string( |
| 314 |
$html => { recover => 1, suppress_errors => 1 } |
| 315 |
); |
| 316 |
}; |
| 317 |
# TEST |
| 318 |
ok (!$@, 'No exception was thrown.'); |
| 319 |
# TEST |
| 320 |
ok ($doc, ' Parsing was successful.'); |
| 321 |
my $root = $doc && $doc->documentElement; |
| 322 |
my $val = $root && $root->findvalue('//input[@id="foo"]/@value'); |
| 323 |
# TEST |
| 324 |
is ($val, 'working', 'XPath'); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
{ |
| 329 |
# 70878 |
| 330 |
# HTML_PARSE_NODEFDTD |
| 331 |
|
| 332 |
SKIP: { |
| 333 |
skip("LibXML version is below 20708", 2) unless ( XML::LibXML::LIBXML_VERSION >= 20708 ); |
| 334 |
|
| 335 |
my $html = q(<body bgcolor='#ffffff' style="overflow: hidden;" leftmargin=0 MARGINWIDTH=0 CLASS="text">); |
| 336 |
my $p = XML::LibXML->new; |
| 337 |
|
| 338 |
# TEST |
| 339 |
like( $p->parse_html_string( $html, { |
| 340 |
recover => 2, |
| 341 |
no_defdtd => 1, |
| 342 |
encoding => 'UTF-8' } )->toStringHTML, qr/^\Q<html>\E/, 'do not add a default DOCTYPE' ); |
| 343 |
|
| 344 |
# TEST |
| 345 |
like ( $p->parse_html_string( $html, { |
| 346 |
recover => 2, |
| 347 |
encoding => 'UTF-8' } )->toStringHTML, qr/^\Q<!DOCTYPE html\E/, 'add a default DOCTYPE' ); |
| 348 |
} |
| 349 |
} |
| 350 |
|