blib/lib/HTML5/DOM/Collection.pm

filter: Statementstmt 93.0% 80 / 86 Branchbran 96.1% 25 / 26 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 88.8% 24 / 27 Podpod 0.0% 0 / 18 totaltotal 80.2% 138 / 172 SCAR 30.1
CC
20
Cov
92%
slice
8.0
new
3.5
first
3.0
CRAP 20.2
↑ Summary blib/lib/HTML5/DOM/TokenList.pm »
1 package HTML5::DOM::Collection;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use overload
8 0 ▶ '""' => sub { shift->html },
Subroutine __ANON__: not called CC=1 SCAR=6.9
9 0 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 3 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 29 ▶ my ($class, $nodes) = @_;
Subroutine new: called CC=3 SCAR=12.5
Pod undocumented
14 29 ▶ $nodes = [] if (!$nodes || ref($nodes) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if not $nodes or ref $nodes ne "ARRAY" 0 29 29
Condition33%1/3
not $nodes or ref $nodes ne "ARRAY"
l!l&&r!l&&!r
0029
MC/DC0%0/2
not $nodes or ref $nodes ne "ARRAY"
not $nodes ref $nodes ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not $nodesB= ref $nodes ne "ARRAY"
ABresult
1X1
011
000
15 29 return bless $nodes, $class;
16 }
17  
18 sub add {
19 0 ▶ my $self = shift;
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
20 0 push @{$self}, shift;
21 0 return $self;
22 }
23  
24 256 ▶sub length { scalar(@{shift()}); }
Subroutine length: called CC=1 SCAR=0.0
Pod undocumented
25  
26 sub first {
27 3 ▶ my ($self, $callback) = (shift, shift);
Subroutine first: called CC=3 SCAR=11.0
Pod undocumented
28 3 ▶ return $self->[0] if (!$callback);
Branch: 1 branch
BranchTrueFalseTotal
unless $callback 1 2 3
29 2 ▶ return List::Util::first { $_ =~ $callback } @$self if (_is_regexp($callback));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if _is_regexp($callback) 1 1 2
30 1 ▶ return List::Util::first { $_->$callback(@_) } @$self;
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32  
33 1 ▶sub last { shift->[-1] }
Subroutine last: called CC=1 SCAR=0.0
Pod undocumented
34 47 ▶sub item { shift->[shift] }
Subroutine item: called CC=1 SCAR=0.0
Pod undocumented
35 2 ▶sub array { [@{shift()}] }
Subroutine array: called CC=1 SCAR=0.0
Pod undocumented
36  
37 sub slice {
38 17 ▶ my ($self, $offset, $length) = @_;
Subroutine slice: called CC=8 SCAR=20.8
Pod undocumented
39
40 # handle negative value as offset from end
41 17 ▶ $offset = scalar(@$self) + $offset if ($offset < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 7 10 17
42
43 # validate offset
44 17 ▶ return HTML5::DOM::Collection->new([]) if ($offset < 0 || $offset >= scalar(@$self) - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 or $offset >= scalar(@$self) - 1 6 11 17
Condition100%3/3
$offset < 0 or $offset >= scalar(@$self) - 1
l!l&&r!l&&!r
3311
MC/DC100%2/2
$offset < 0 or $offset >= scalar(@$self) - 1
$offset < 0 $offset >= scalar(@$self) - 1
Truth table100%3/3
A or B
A= $offset < 0B= $offset >= scalar(@$self) - 1
ABresult
1X1
011
000
45
46 # mean all available elements if no length specified
47 11 ▶ $length = scalar(@$self) if !defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 4 7 11
48
49 # get maximum available length from offset
50 11 my $max_length = scalar(@$self) - $offset;
51
52 # handle negative length
53 11 ▶ $length = $max_length + $length if ($length < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 3 8 11
54
55 # limit available length
56 11 ▶ $length = $max_length if ($length > $max_length);
Branch: 1 branch
BranchTrueFalseTotal
if $length > $max_length 5 6 11
57
58 # validate length
59 11 ▶ return HTML5::DOM::Collection->new([]) if ($length <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $length <= 0 1 10 11
60
61 # return requested slice
62 10 return HTML5::DOM::Collection->new([@$self[$offset..$offset + $length - 1]]);
63 }
64  
65 sub head {
66 4 ▶ my ($self, $length) = @_;
Subroutine head: called CC=1 SCAR=0.0
Pod undocumented
67 4 return $self->slice(0, $length);
68 }
69  
70 sub tail {
71 4 ▶ my ($self, $length) = @_;
Subroutine tail: called CC=1 SCAR=0.0
Pod undocumented
72 4 return $self->slice(-$length);
73 }
74  
75 sub each {
76 1 ▶ my ($self, $callback) = (shift, shift);
Subroutine each: called CC=2 SCAR=6.9
Pod undocumented
77 1 my $index = 0;
78 1 for my $node (@$self) {
79 5 $callback->($node, $index++, @_);
80 }
81 1 return $self;
82 }
83  
84 sub uniq {
85 2 ▶ my ($self, $callback) = (shift, shift);
Subroutine uniq: called CC=3 SCAR=11.0
Pod undocumented
86 2 my %used;
87 2 ▶ return HTML5::DOM::Collection->new([grep { my $id = $_->$callback(@_); !$used{defined($id) ? $id : ''}++ } @$self]) if ($callback);
Branch: 2 branches
BranchTrueFalseTotal
if $callback 1 1 2
defined $id ? : 4 1 5
88 1 return HTML5::DOM::Collection->new([grep { !$used{$_->hash}++ } @$self]);
89 }
90  
91 sub grep {
92 2 ▶ my ($self, $callback) = (shift, shift);
Subroutine grep: called CC=2 SCAR=6.9
Pod undocumented
93 2 ▶ return HTML5::DOM::Collection->new([grep { $_ =~ $callback } @$self]) if (_is_regexp($callback));
Branch: 1 branch
BranchTrueFalseTotal
if _is_regexp($callback) 1 1 2
94 1 return HTML5::DOM::Collection->new([grep { $_->$callback(@_) } @$self]);
95 }
96  
97 sub reverse {
98 1 ▶ my ($self) = @_;
Subroutine reverse: called CC=1 SCAR=0.0
Pod undocumented
99 1 return HTML5::DOM::Collection->new([reverse @$self]);
100 }
101  
102 sub shuffle {
103 1 ▶ my ($self) = @_;
Subroutine shuffle: called CC=1 SCAR=0.0
Pod undocumented
104 1 return HTML5::DOM::Collection->new([List::Util::shuffle @$self]);
105 }
106  
107 sub map {
108 6 ▶ my ($self, $callback) = (shift, shift);
Subroutine map: called CC=2 SCAR=6.9
Pod undocumented
109 6 ▶ if (ref($callback) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback eq 'CODE') { } 1 5 6
110 1 my $index = 0;
111 1 return HTML5::DOM::Collection->new([map { $_->$callback($index++, @_) } @$self]);
112 } else {
113 5 return HTML5::DOM::Collection->new([map { $_->$callback(@_) } @$self]);
114 }
115 }
116  
117 sub text {
118 22 ▶ my @nodes;
Subroutine text: called CC=2 SCAR=6.9
Pod undocumented
119 22 for my $node (@{shift()}) {
120 49 push @nodes, $node->text;
121 }
122 22 return join("", @nodes);
123 }
124  
125 sub html {
126 1 ▶ my @nodes;
Subroutine html: called CC=2 SCAR=6.9
Pod undocumented
127 1 for my $node (@{shift()}) {
128 5 push @nodes, $node->html;
129 }
130 1 return join("", @nodes);
131 }
132  
133 sub _is_regexp {
134 4 ▶ return ref($_[0]) eq 'Regexp' || ref(\$_[0]) eq 'Regexp';
Subroutine _is_regexp: called CC=2 SCAR=7.2
Condition66%2/3
ref $_[0] eq 'Regexp' || ref \$_[0] eq 'Regexp'
l!l&&r!l&&!r
202
MC/DC50%1/2
ref $_[0] eq 'Regexp' || ref \$_[0] eq 'Regexp'
ref $_[0] eq 'Regexp' ref \$_[0] eq 'Regexp'
Truth table66%2/3
A || B
A= ref $_[0] eq 'Regexp'B= ref \$_[0] eq 'Regexp'
ABresult
1X1
011
000
135 }
136  
137 3 1;
↑ Summary blib/lib/HTML5/DOM/TokenList.pm »