| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use utf8; |
| 6 |
|
| 7 |
use lib './t/lib'; |
| 8 |
use TestHelpers qw(utf8_slurp eq_or_diff); |
| 9 |
|
| 10 |
use Test::More tests => 3; |
| 11 |
|
| 12 |
use XML::LibXML; |
| 13 |
|
| 14 |
# This is a check for: |
| 15 |
# https://rt.cpan.org/Ticket/Display.html?id=53270 |
| 16 |
|
| 17 |
{ |
| 18 |
my $content = utf8_slurp('example/yahoo-finance-html-with-errors.html'); |
| 19 |
|
| 20 |
my $parser = XML::LibXML->new; |
| 21 |
|
| 22 |
$parser->set_option('recover', 1); |
| 23 |
$parser->set_option('suppress_errors', 1); |
| 24 |
|
| 25 |
my @warnings; |
| 26 |
|
| 27 |
local $SIG{__WARN__} = sub { |
| 28 |
my $warning = shift; |
| 29 |
push @warnings, $warning; |
| 30 |
}; |
| 31 |
my $dom = $parser->load_html(string => $content); |
| 32 |
|
| 33 |
# TEST |
| 34 |
eq_or_diff( |
| 35 |
\@warnings, |
| 36 |
[], |
| 37 |
'suppress_errors worked.', |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
# These are tests for https://rt.cpan.org/Ticket/Display.html?id=58024 : |
| 43 |
# <<< |
| 44 |
# In XML::LibXML, warnings are not suppressed when specifying the recover |
| 45 |
# or recover_silently flags as per the following excerpt from the manpage: |
| 46 |
# >>> |
| 47 |
|
| 48 |
my $txt = <<'EOS'; |
| 49 |
<div> |
| 50 |
<a href="milu?a=eins&b=zwei"> ampersand not URL-encoded </a> |
| 51 |
<!-- HTML parser error : htmlParseEntityRef: expecting ';' --> |
| 52 |
</div> |
| 53 |
EOS |
| 54 |
|
| 55 |
{ |
| 56 |
my $buf = ''; |
| 57 |
open my $fh, '>', \$buf; |
| 58 |
# redirect STDERR there |
| 59 |
local *STDERR = $fh; |
| 60 |
|
| 61 |
XML::LibXML->new(recover => 1)->load_html( string => $txt ); |
| 62 |
close($fh); |
| 63 |
|
| 64 |
# TEST |
| 65 |
like ($buf, qr/htmlParseEntityRef:/, 'warning emitted'); |
| 66 |
} |
| 67 |
{ |
| 68 |
my $buf = ''; |
| 69 |
open my $fh, '>', \$buf; |
| 70 |
local *STDERR = $fh; |
| 71 |
XML::LibXML->new(recover => 2)->load_html( string => $txt ); |
| 72 |
close($fh); |
| 73 |
# TEST |
| 74 |
is ($buf, '', 'No warning emitted.'); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
=head1 COPYRIGHT & LICENSE |
| 79 |
|
| 80 |
Copyright 2011 by Shlomi Fish |
| 81 |
|
| 82 |
This program is distributed under the MIT (X11) License: |
| 83 |
L<http://www.opensource.org/licenses/mit-license.php> |
| 84 |
|
| 85 |
Permission is hereby granted, free of charge, to any person |
| 86 |
obtaining a copy of this software and associated documentation |
| 87 |
files (the "Software"), to deal in the Software without |
| 88 |
restriction, including without limitation the rights to use, |
| 89 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 90 |
copies of the Software, and to permit persons to whom the |
| 91 |
Software is furnished to do so, subject to the following |
| 92 |
conditions: |
| 93 |
|
| 94 |
The above copyright notice and this permission notice shall be |
| 95 |
included in all copies or substantial portions of the Software. |
| 96 |
|
| 97 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 98 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 99 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 100 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 101 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 102 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 103 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 104 |
OTHER DEALINGS IN THE SOFTWARE. |
| 105 |
|
| 106 |
=cut |