File Coverage

blib/lib/XML/TinyXML/Selector/XPath/Axes.pm
Criterion Covered Total %
statement 75 79 94.9
branch 20 30 66.6
condition n/a
subroutine 12 13 92.3
pod 0 10 0.0
total 107 132 81.0


line stmt bran cond sub pod time code
1             package XML::TinyXML::Selector::XPath::Axes;
2              
3 4     4   24 use strict;
  4         8  
  4         349  
4 4     4   26 use warnings;
  4         9  
  4         109  
5 4     4   3111 use XML::TinyXML::NodeAttribute;
  4         11  
  4         5274  
6              
7             our $VERSION = "0.34";
8              
9             sub child {
10 55     55 0 94 my ($class, $context) = @_;
11 55         64 my @res = map { $_->children } grep { defined } @{$context->items};
  188         473  
  189         339  
  55         254  
12 55 50       312 return wantarray?@res:\@res;
13             }
14              
15             sub descendant {
16 46     46 0 74 my ($class, $context) = @_;
17 46         56 my @res = map { $_->children } @{$context->items};
  180         479  
  46         206  
18 46 100       130 if (@res) {
19 30         148 my $newctx = XML::TinyXML::Selector::XPath::Context->new($context->xml);
20 30         70 $newctx->{items} = \@res;
21 30         114 push (@res, descendant($class, $newctx));
22             }
23 46 50       203 return wantarray?@res:\@res;
24             }
25              
26             sub parent {
27 3     3 0 7 my ($class, $context) = @_;
28 3         4 my @res = map { $_->parent } grep { defined } @{$context->items};
  2         11  
  2         7  
  3         17  
29 3 50       17 return wantarray?@res:\@res;
30             }
31              
32             sub ancestor {
33 3     3 0 5 my ($class, $context) = @_;
34 3         4 my @res;
35 3         4 foreach my $node (@{$context->items}) {
  3         13  
36 3         10 my $parent = $node->parent;
37 3 100       11 push(@res, $parent)
38             if($parent);
39             }
40              
41 3 100       9 if (@res) {
42 2         9 my $newctx = XML::TinyXML::Selector::XPath::Context->new($context->xml);
43 2         6 $newctx->{items} = \@res;
44 2         12 my @new = ancestor($class, $newctx);
45 2 100       9 push (@res, @new)
46             if (@new);
47             }
48 3 50       14 return wantarray?@res:\@res;
49             }
50              
51             sub following_sibling {
52 2     2 0 3 my ($class, $context) = @_;
53 2         3 my @res;
54 2         14 foreach my $node (@{$context->items}) {
  2         11  
55 2         7 while ($node) {
56 5         14 my $next = $node->nextSibling;
57 5 100       12 push (@res, $next) if $next;
58 5         17 $node = $next;
59             }
60             }
61 2 50       10 return wantarray?@res:\@res;
62             }
63              
64             sub preceding_sibling {
65 1     1 0 3 my ($class, $context) = @_;
66 1         2 my @res;
67 1         2 foreach my $node (@{$context->items}) {
  1         5  
68 1         4 while ($node) {
69 4         12 my $prev = $node->prevSibling;
70 4 100       11 push (@res, $prev) if $prev;
71 4         11 $node = $prev;
72             }
73             }
74 1 50       9 return wantarray?@res:\@res;
75             }
76              
77             sub attribute {
78 12     12 0 24 my ($class, $context) = @_;
79 12         18 my @res = map { $_->getAttributes } grep { defined } @{$context->items};
  37         101  
  37         70  
  12         55  
80 12 50       58 return wantarray?@res:\@res;
81             }
82              
83             sub self {
84 2     2 0 5 my ($class, $context) = @_;
85 2         3 return @{$context->items};
  2         11  
86             }
87              
88             sub descendant_or_self {
89 13     13 0 23 my ($class, $context) = @_;
90 13         35 my @res = descendant($class, $context);
91 13         20 foreach my $node (@{$context->items}) {
  13         61  
92 12         32 push (@res, $node);
93             }
94 13 50       89 return wantarray?@res:\@res;
95             }
96              
97             sub ancestor_or_self {
98 0     0 0   my ($class, $context) = @_;
99 0           my @res = ancestor($context);
100 0           unshift(@res, $context->{node});
101 0 0         return wantarray?@res:\@res;
102             }
103              
104              
105             1;