| 1 | package HTML::DOM::NodeList::Magic; | ||||||||||||
| 2 | |||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 25 | ▶ | use overload fallback => 1, '@{}' => \&_get_tie; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 25 | ▶ | use Scalar::Util 'weaken'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 25 | our $VERSION = '0.057'; | |||||||||||
| 10 | |||||||||||||
| 11 | # Innards: { | ||||||||||||
| 12 | # get => sub { ... }, # sub that gets the list | ||||||||||||
| 13 | # list => [ ... ], # the list, or undef | ||||||||||||
| 14 | # tie => \@tied_array, # or undef, if the array has not been | ||||||||||||
| 15 | # # accessed yet | ||||||||||||
| 16 | # } | ||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | # new NodeList sub { ... } | ||||||||||||
| 20 | # new NodeList sub { ... }, $doc | ||||||||||||
| 21 | # The sub needs to return the list of nodes. | ||||||||||||
| 22 | |||||||||||||
| 23 | sub new { | ||||||||||||
| 24 | 217 | ▶ | my $self = bless {get => $_[1]}, shift; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 25 | 217 | ▶ | ($_[1]||return $self)->_register_magic_node_list($self); | ||||||||||
|
Condition100%2/2
$_[1] || (return $self)
MC/DC100%1/1
$_[1] || (return $self)
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||
| 26 | 1 | $self; | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub item { | ||||||||||||
| 30 | 504 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
item: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 31 | # Oh boy! Look at these brackets! | ||||||||||||
| 32 | 504 | ▶ | ${$$self{list} ||= [&{$$self{get}}]}[$_[0]]; | ||||||||||
|
Condition100%2/2
$self->{"list"} ||= [&{$self->{"get"};}]
MC/DC100%1/1
$self->{"list"} ||= [&{$self->{"get"};}]
$self->{"list"}
Truth table100%2/2
A
A= $self->{"list"}
| |||||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | sub length { | ||||||||||||
| 36 | 182 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
length: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 37 | # Oh no, here we go again. | ||||||||||||
| 38 | 182 | ▶ | scalar @{$$self{list} ||= [&{$$self{get}}]}; | ||||||||||
|
Condition100%2/2
$self->{"list"} ||= [&{$self->{"get"};}]
MC/DC100%1/1
$self->{"list"} ||= [&{$self->{"get"};}]
$self->{"list"}
Truth table100%2/2
A
A= $self->{"list"}
| |||||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | sub _you_are_stale { | ||||||||||||
| 42 | 191 | ▶ | delete $_[0]{list}; | ||||||||||
|
Subroutine
_you_are_stale: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | sub DOES { | ||||||||||||
| 46 | 3 | ▶ | return !0 if $_[1] eq 'HTML::DOM::NodeList'; | ||||||||||
|
Subroutine
DOES: called CC=3 SCAR=18.4
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 47 | 0 | ▶ | eval { shift->SUPER::DOES(@_) } || !1 | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | # ---------- TIES --------- # | ||||||||||||
| 51 | |||||||||||||
| 52 | sub _get_tie { | ||||||||||||
| 53 | 156 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_tie: called CC=2 SCAR=6.9
| |||||||||||||
| 54 | $$self{tie} or | ||||||||||||
| 55 | 96 | weaken(tie @{ $$self{tie} }, __PACKAGE__, $self), | |||||||||||
| 56 | 156 | ▶ | $$self{tie}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 96 | ▶ | sub TIEARRAY { $_[1] } | ||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||
| 60 | 287 | ▶ | sub FETCH { $_[0]->item($_[1]) } | ||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 93 | ▶ | sub FETCHSIZE { $_[0]->length } | ||||||||||
|
Subroutine
FETCHSIZE: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 0 | ▶ | sub EXISTS { $_[0]->item($_[1]) } # nodes are true, undef is false | ||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||
| 63 | 0 | ▶ | sub DDS_freeze { my $self = shift; delete $$self{tie}; $self } | ||||||||||
|
Subroutine
DDS_freeze: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 64 | |||||||||||||
| 65 | # These are here solely to make HTML::DOM::Collection::Options work: | ||||||||||||
| 66 | sub STORE { | ||||||||||||
| 67 | 1 | ▶ | my($self,$indx,$val) = @_; | ||||||||||
|
Subroutine
STORE: called CC=3 SCAR=14.2
| |||||||||||||
| 68 | 1 | ▶ | if(defined $val) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | ▶ | if(my $deletee = $self->item($indx)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | $deletee->replace_with($val)->delete; | |||||||||||
| 71 | } | ||||||||||||
| 72 | else { | ||||||||||||
| 73 | 0 | $self->item($self->length-1)->parentElement | |||||||||||
| 74 | ->appendChild($val); | ||||||||||||
| 75 | } | ||||||||||||
| 76 | } | ||||||||||||
| 77 | else { | ||||||||||||
| 78 | 1 | (my $thing = $self->item($indx))->ownerDocument; | |||||||||||
| 79 | 1 | $self->item($indx)->detach | |||||||||||
| 80 | } | ||||||||||||
| 81 | 1 | $self->_you_are_stale; | |||||||||||
| 82 | } | ||||||||||||
| 83 | sub DELETE { | ||||||||||||
| 84 | 0 | ▶ | for(shift) { | ||||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||||
| 85 | 0 | $_->item(shift)->detach; | |||||||||||
| 86 | 0 | $_->_you_are_stale; | |||||||||||
| 87 | } | ||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 25 | 1; | |||||||||||
| 91 | |||||||||||||
| 92 | __END__ | ||||||||||||