Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add new_from_url (Using LWP) (David Pottage) (RT #68097)

  • Loading branch information...
commit 5785d7fc8c33e7c43b8d3b678ed4c1f38beeab86 1 parent 9e691ff
@jfearn authored
View
36 Build.PL
@@ -25,25 +25,29 @@ my $class = Module::Build->subclass(
);
my $builder = $class->new(
- module_name => 'HTML::Tree',
- license => 'perl',
- dist_author => 'Jeff Fearn <jfearn@cpan.org>',
- dist_version_from => 'lib/HTML/Element.pm',
- create_makefile_pl => 'traditional',
- build_requires => {
- 'Module::Build' => 0,
- 'Test::Exception' => 0,
- 'Test::More' => 0,
- 'HTML::Parser' => 3.46,
- 'HTML::Tagset' => 3.02,
+ module_name => 'HTML::Tree',
+ license => 'perl',
+ dist_author => 'Jeff Fearn <jfearn@cpan.org>',
+ dist_version_from => 'lib/HTML/Element.pm',
+ create_makefile_pl => 'traditional',
+ build_requires => {
+ 'Module::Build' => 0,
+ 'Test::Exception' => 0,
+ 'Test::More' => 0,
+ 'HTML::Parser' => 3.46,
+ 'HTML::Tagset' => 3.02,
+ 'LWP::UserAgent' => 0,
+ 'URI::file' => 0,
},
requires => {
- 'HTML::Parser' => 3.46,
- 'HTML::Tagset' => 3.02,
- 'perl' => '5.8.0',
+ 'HTML::Parser' => 3.46,
+ 'HTML::Tagset' => 3.02,
+ 'perl' => '5.8.0',
+ 'LWP::UserAgent' => 0,
},
- add_to_cleanup => [ 'HTML-Tree-*', 'tmp', 'blib', 'lwp-test-*', 'MANIFEST.bak' ],
- script_files => ['htmltree'],
+ add_to_cleanup =>
+ [ 'HTML-Tree-*', 'tmp', 'blib', 'lwp-test-*', 'MANIFEST.bak' ],
+ script_files => ['htmltree'],
);
$builder->create_build_script();
View
6 Changes
@@ -1,5 +1,9 @@
Changelog for HTML-Tree
-4.2
+4.3 UNRELEASED
+ [FIXES]
+ * add new_from_url (Using LWP) (David Pottage) (RT #68097)
+
+4.2 Wed 06 Apr 2011
[FIXES]
* Tied all $VERSION to HTML::Element to ensure latest package is used
for all modules. RT #66110
View
1  Makefile.PL
@@ -15,6 +15,7 @@ WriteMakefile
'Test::Exception' => 0,
'HTML::Tagset' => '3.02',
'HTML::Parser' => '3.46',
+ 'LWP::UserAgent' => 0,
'Module::Build' => 0
}
)
View
6 lib/HTML/Tree.pm
@@ -55,6 +55,12 @@ sub new_from_content {
goto &HTML::TreeBuilder::new_from_content;
}
+sub new_from_url {
+ shift;
+ unshift @_, 'HTML::TreeBuilder';
+ goto &HTML::TreeBuilder::new_from_url;
+}
+
1;
__END__
View
18 lib/HTML/TreeBuilder.pm
@@ -52,12 +52,13 @@ BEGIN {
#---------------------------------------------------------------------------
+use LWP::UserAgent;
use HTML::Entities ();
use HTML::Tagset 3.02 ();
use HTML::Element ();
use HTML::Parser ();
-@ISA = qw(HTML::Element HTML::Parser);
+@ISA = qw(HTML::Element HTML::Parser);
$VERSION = 4.2;
# This looks schizoid, I know.
@@ -109,8 +110,21 @@ sub new_from_content { # from any number of scalars
return $new;
}
+sub new_from_url { # should accept anything that LWP does.
+ my $class = shift;
+ Carp::croak("new_from_url takes only one argument")
+ unless @_ == 1;
+ Carp::croak("new_from_url is a class method only")
+ if ref $class;
+ my $new = $class->new();
+
+ my $fetch_result = LWP::UserAgent->new->get( $_[0] );
+ $new->parse( $fetch_result->content );
+ return $new;
+}
+
# TODO: document more fully?
-sub parse_content { # from any number of scalars
+sub parse_content { # from any number of scalars
my $tree = shift;
my $retval;
foreach my $whunk (@_) {
View
19 t/construct_tree.t
@@ -1,9 +1,9 @@
-#!/usr/bin/perl -T
+#!/usr/bin/perl
use warnings;
use strict;
-use Test::More tests => ( 3 + 7 * 8 );
+use Test::More tests => ( 3 + 7 * 10 );
#initial tests + number of tests in test_new_obj() * number of times called
@@ -69,6 +69,21 @@ is( $HTMLPart1 . $HTMLPart2, $HTML, "split \$HTML correctly" );
test_new_obj( $parse_content_obj, "new(); parse_content Scalar" );
}
+# URL tests
+{
+ my $file_url = URI->new( "file:" . $TestInput );
+
+ {
+ my $file_obj = HTML::Tree->new_from_url( $file_url->as_string );
+ test_new_obj( $file_obj, "new_from_url Scalar" );
+ }
+
+ {
+ my $file_obj = HTML::Tree->new_from_url($file_url);
+ test_new_obj( $file_obj, "new_from_url Object" );
+ }
+}
+
# Scalar REF Tests
{
my $content_obj = HTML::Tree->new_from_content($HTML);
Please sign in to comment.
Something went wrong with that request. Please try again.