File Coverage

blib/lib/XML/LibXML/QuerySelector.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1 2     2   38572 use 5.008;
  2         7  
  2         67  
2 2     2   11 use strict;
  2         3  
  2         61  
3 2     2   1204 use utf8;
  2         4  
  2         11  
4              
5             package XML::LibXML::QuerySelector;
6              
7 2     2   1921 use HTML::Selector::XPath 0.13 qw//;
  2         5542  
  2         59  
8 2     2   2073 use XML::LibXML 1.70 qw//;
  0            
  0            
9              
10             BEGIN
11             {
12             our $AUTHORITY = 'cpan:TOBYINK';
13             our $VERSION = '0.004';
14            
15             push @XML::LibXML::Document::ISA, __PACKAGE__;
16             push @XML::LibXML::DocumentFragment::ISA, __PACKAGE__;
17             push @XML::LibXML::Element::ISA, __PACKAGE__;
18             }
19              
20             my $contains = sub
21             {
22             my $self = shift;
23             my ($node) = @_;
24             my $self_path = $self->nodePath;
25             my $node_path = $node->nodePath;
26             my $sub_node_path = substr $node_path, 0, length $self_path;
27             $sub_node_path eq $self_path;
28             };
29              
30             sub querySelectorAll
31             {
32             my $self = shift;
33             my ($selector_string) = @_;
34             my $selector = "HTML::Selector::XPath"->new($selector_string);
35            
36             my $document = $self->nodeName =~ /^#/ ? $self : $self->ownerDocument;
37             my $nsuri = $document->documentElement->lookupNamespaceURI('');
38            
39             my $xc = "XML::LibXML::XPathContext"->new($document);
40             $xc->registerNs(defaultns => $nsuri) if $nsuri;
41              
42             my $xpath = defined $nsuri
43             ? $selector->to_xpath(prefix => 'defaultns')
44             : $selector->to_xpath;
45              
46             if ($document == $self)
47             {
48             return $xc->findnodes($xpath);
49             }
50            
51             my @results = map
52             { $self->$contains($_) ? ($_) : () }
53             @{[ $xc->findnodes($xpath) ]};
54            
55             wantarray ? @results : "XML::LibXML::NodeList"->new(@results);
56             }
57              
58             sub querySelector
59             {
60             my $self = shift;
61             my ($selector_string) = @_;
62             my $results = $self->querySelectorAll($selector_string);
63             return unless $results->size;
64             $results->shift;
65             }
66              
67             __FILE__
68             __END__