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