blib/lib/HTML/Element/AbsoluteXPath.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 59 SCAR 37.4
CC
6
Cov
0%
HTML::Element::abs_xpath
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 42.0
1 use strict;
2 use warnings;
3 package HTML::Element::AbsoluteXPath;
4  
5 # ABSTRACT: Add absolute XPath support to HTML::Element
6  
7 our $VERSION = '0.02'; # VERSION
8  
9 use HTML::Element 5.03;
10  
11 sub HTML::Element::abs_xpath{
12 my $self = shift;
13 my @hint = @_;
14
15 push(@hint,'_tag') unless grep{$_ eq '_tag'}@hint;
16  
17 my $raddr = $self->root()->address;
18 my @lin;
19 my $ee = $self;
20 while( $ee->address ne $raddr ){
21 my $p = $ee->parent();
22 my %filters;
23 foreach (sort @hint){
24 my $v = $ee->attr($_);
25 next unless $v;
26 $filters{$_} = $v;
27 }
28 my @sib = $p->look_down(%filters);
29  
30 @sib = grep{$_->depth() == $ee->depth()}@sib;
31 my $idx = 1;
32 foreach (@sib){
33 last if $_->address eq $ee->address;
34 $idx++;
35 }
36  
37 my @attrs;
38 foreach (keys %filters){
39 next if $_ eq '_tag';
40 my $v = $filters{$_};
41 my $pat = "\@$_='$v'";
42 push(@attrs,$pat);
43 }
44  
45 my $attr = '';
46 $attr = "[".join(' and ',@attrs)."]" if @attrs;
47  
48 push(@lin, $ee->tag() . $attr . "[$idx]" );
49 $ee = $p;
50 }
51 push(@lin, $ee->tag().'[1]' );
52 return '/'.join('/',reverse @lin);
53 }
54  
55  
56  
57  
58 1;
59  
60 __END__