Skip to content

Commit

Permalink
upgrade to HTML::TreeBuilder 5 to get weak references
Browse files Browse the repository at this point in the history
This might break people's code on really old Perls.

Closes #251
  • Loading branch information
simbabque authored and oalders committed Nov 11, 2018
1 parent a672a2c commit e5ed735
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Revision history for WWW::Mechanize
[DOCUMENTATION]
- Fix broken Pod

[ENHANCEMENTS]
- Upgrade to HTML::TreeBuilder version 5 to get support for weak references in
HTML::Element (GH#251) (Julien Fiegehenn)

1.89 2018-10-18 19:13:34Z
[ENHANCEMENTS]
- Add support to find_image() and find_all_images() via 'id'
Expand Down
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dir = bin

[Prereqs / RuntimeRequires]
perl = 5.006
Scalar::Util = 1.14

[Prereqs / TestRequires]
CGI = 4.32
Expand Down
15 changes: 9 additions & 6 deletions lib/WWW/Mechanize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,9 @@ are passed to I<content()>:
=item I<< $mech->content( format => 'text' ) >>
Returns a text-only version of the page, with all HTML markup
stripped. This feature requires I<HTML::TreeBuilder> to be installed,
or a fatal error will be thrown. This works only if the contents are
HTML.
stripped. This feature requires I<HTML::TreeBuilder> version 5 or higher
to be installed, or a fatal error will be thrown. This works only if
the contents are HTML.
=item I<< $mech->content( base_href => [$base_href|undef] ) >>
Expand Down Expand Up @@ -822,7 +822,7 @@ sub content {
=head2 $mech->text()
Returns the text of the current HTML content. If the content isn't
HTML, $mech will die.
HTML, C<$mech> will die.
The text is extracted by parsing the content, and then the extracted
text is cached, so don't worry about performance of calling this
Expand All @@ -834,13 +834,16 @@ sub text {
my $self = shift;

if ( not defined $self->{text} ) {
require HTML::TreeBuilder;
unless ( exists $INC{'HTML::TreeBuilder'} ) {
require HTML::TreeBuilder;
HTML::TreeBuilder->VERSION(5);
HTML::TreeBuilder->import('-weak');
}
my $tree = HTML::TreeBuilder->new();
$tree->parse( $self->content );
$tree->eof();
$tree->elementify(); # just for safety
$self->{text} = $tree->as_text();
$tree->delete;
}

return $self->{text};
Expand Down
4 changes: 2 additions & 2 deletions t/content.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ $mech->update_html($html);
=cut

SKIP: {
eval 'use HTML::TreeBuilder';
skip 'HTML::TreeBuilder not installed', 2 if $@;
eval 'use HTML::TreeBuilder 5';
skip 'HTML::TreeBuilder version 5 not installed', 2 if $@;

my $text = $mech->content(format => 'text');
like( $text, qr/Fine/, 'Found Fine' );
Expand Down

0 comments on commit e5ed735

Please sign in to comment.