summaryrefslogtreecommitdiff
path: root/t/02html.t
blob: 93e0eef60beefa4b7346bbbf22085ee0d6fc60b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# make sure HTML::TreeBuilder is available 
BEGIN {
    eval { require HTML::TreeBuilder; };
    if ($@) {
        eval "use Test::More skip_all => q{02html.t requires HTML::TreeBuilder.};";
        exit;
    }
}
 
use Test::More qw(no_plan);
use strict;
use warnings;
use Class::XPath;
use HTML::TreeBuilder;
 
# build a tree from some HTML 
my $root = HTML::TreeBuilder->new;
isa_ok($root'HTML::TreeBuilder');
$root->parse_file("t/02html.html");
isa_ok($root'HTML::Element');
 
# add Class::XPath routines to HTML::Element 
Class::XPath->add_methods(target         => 'HTML::Element',
                          call_match     => 'xpath_match',
                          call_xpath     => 'xpath_id',
                          get_parent     => 'parent',
                          get_name       => 'tag',
                          get_attr_names => 
                            sub { my %attr = shift->all_external_attr;
                                  return keys %attr; },
                          get_attr_value => 
                          sub { my %attr = shift->all_external_attr;
                                return $attr{$_[0]}; },
                          get_children   =>
                            sub { grep { ref $_ } shift->content_list },
                          get_content    =>
                            sub { grep { not ref $_ } shift->content_list },
                          get_root       => 
                            sub { local $_=shift
                                  while($_->parent) { $_ = $_->parent }
                                  return $_; });
 
# do some matching tests against the HTML 
is($root->xpath_match('//table'), 3);
is($root->xpath_match('/head/title'), 1);
is($root->xpath_match('//head/title'), 1);
is(($root->xpath_match('/head/title'))[0]->xpath_id, '/head[0]/title[0]');
is(($root->xpath_match('/head/title'))[0]->parent->xpath_id, '/head[0]');
is(($root->xpath_match('/head/title'))[0]->parent->parent->xpath_id, '/');
is($root->xpath_match('//a'), 54);
 
my ($head) = $root->xpath_match('/head');
is($head->xpath_match('title'), 1);
is($head->xpath_match('/title'), 0);