ElementGlob.pm

filter: Statementstmt 14.1% 11 / 78 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 21.4% 3 / 14 Podpod 0.0% 0 / 9 totaltotal 10.6% 14 / 132 CC 22 SCAR 43.9
CC
22
Cov
10%
AUTOLOAD
182.0
_content_manipulate
56.0
glob_delete_content
6.0
CRAP 80.3
« ElementTable.pm ↑ Summary ElementSuper.pm »
1 package HTML::ElementGlob;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use HTML::ElementSuper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '1.18';
9  
10 ####################################################
11 # glob_* methods do the HTML::Element type methods #
12 # on the glob structure itself, rather than muxing #
13 # the methods to its children. Most of these are #
14 # taken care of in AUTOLOAD, but we override some. #
15 ####################################################
16  
17 0 ▶sub glob_delete_content {
Subroutine glob_delete_content: not called CC=2 SCAR=17.9
Pod undocumented
18 # Do not propogate delete_content to children, as
19 # this should be the job of the real parent.
20 0 my $self = shift;
21 0 ▶ @{$self->glob_content} = () unless $self->glob_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->glob_is_empty 0 0 0
22 0 $self;
23 }
24  
25 0 ▶sub glob_delete {
Subroutine glob_delete: not called CC=1 SCAR=6.9
Pod undocumented
26 # Do not propogate delete to children, either.
27 0 my $self = shift;
28 0 $self->glob_delete_content;
29 0 %{$self} = ();
30 }
31  
32 0 ▶sub context_is_glob {
Subroutine context_is_glob: not called CC=2 SCAR=17.9
Pod undocumented
33 # The newer HTML::Element class invokes detach() quite a bit
34 # during content operations -- *without* prepending glob_,
35 # obviously. We have to have some way of indicating to children
36 # globs that they should NOT broadcast methods to children --
37 # otherwise, all the regular elements in the child glob will get
38 # detach() invoked as well. So...if a glob knows it is about to
39 # perform an operation on another glob that should not be
40 # broadcast -- set this flag, then unset it afterwards.
41 0 my $self = shift;
42 0 ▶ @_ ? $self->{_context_is_glob} = shift : $self->{_context_is_glob};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
43 }
44  
45 ######################################################
46 # MUXed methods (pass invocation to children) #
47 # Some methods do not really make sense in a globbed #
48 # context, so we try to 'do the right thing' here. #
49 ######################################################
50  
51 # HTML::Element based methods
52 0 ▶sub push_content { shift->_content_manipulate('push_content', @_) }
Subroutine push_content: not called CC=1 SCAR=6.9
Pod undocumented
53 0 ▶sub unshift_content { shift->_content_manipulate('unshift_content', @_) }
Subroutine unshift_content: not called CC=1 SCAR=6.9
Pod undocumented
54 0 ▶sub splice_content { shift->_content_manipulate('splice_content', @_) }
Subroutine splice_content: not called CC=1 SCAR=6.9
Pod undocumented
55 # replace_with_content does not apply, as elements are not passed
56 # in the argument list, they are summoned from each individual
57 # element's content.
58  
59 # HTML::ElementSuper based methods
60 0 ▶sub wrap_content { shift->_content_manipulate('wrap_content', @_) }
Subroutine wrap_content: not called CC=1 SCAR=6.9
Pod undocumented
61 0 ▶sub replace_content { shift->_content_manipulate('replace_content', @_) }
Subroutine replace_content: not called CC=1 SCAR=6.9
Pod undocumented
62  
63 0 ▶sub _content_manipulate {
Subroutine _content_manipulate: not called CC=7 SCAR=40.3
64 # Generic method for cloning and broadcasting the
65 # element trees provided to content methods
66 0 my $self = shift;
67 0 my $name = shift;
68 0 my @children = $self->{_element}->content_list;
69 # Find the first child that will have the method
70 # invoked.
71 0 my $first = undef;
72 0 foreach (0 .. $#children) {
73 0 ▶ if (ref $children[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $children[$_]) 0 0 0
74 0 $first = $_;
75 0 last;
76 }
77 }
78 0 ▶ return undef unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 0 0
79 # Deal with the tail elements first
80 0 ▶ if ($first < $#children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first < $#children) 0 0 0
81 0 foreach ($first+1 .. $#children) {
82 0 ▶ next unless ref $children[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $children[$_] 0 0 0
83 0 $children[$_]->$name($self->{_element}->clone(@_));
84 }
85 }
86 # First child can have the real copy
87 0 $children[$first]->$name(@_);
88 }
89  
90 # Constructor
91  
92 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
93 0 my $that = shift;
94 0 ▶ my $class = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
95 0 my $self = {};
96 0 bless $self,$class;
97 0 $self->{_element} = new HTML::ElementSuper @_;
98 0 $self->{_babysitter} = new HTML::ElementSuper @_;
99 0 $self;
100 }
101  
102 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=13 SCAR=52.0
103 # Methods starting with glob deal with glob management,
104 # otherwise they get passed blindly to all children unless
105 # they have been overridden above.
106 0 my $self = shift;
107 0 my $name = $AUTOLOAD;
108 0 $name =~ s/.*:://;
109 0 ▶ return if $name =~ /^DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^DESTROY/ 0 0 0
110  
111 # First, deal with glob_* induced methods
112 0 ▶ if ($name =~ s/^glob_//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ s/^glob_//) { } 0 0 0
elsif ($self->context_is_glob) { } 0 0 0
113 # First, indicate to other globs that subsequent method
114 # calls are glob_ induced.
115 0 foreach (grep { ref $_ eq ref $self } @_) {
116 0 $_->context_is_glob(1);
117 }
118 # Store the pedigree of all elements, including globs,
119 # since no matter what a glob does it should not disturb
120 # the original lineage of an element. With the new
121 # HTML::Element, detach() gets called which also
122 # adjusts the content of the parent if available,
123 # so we give them to the babysitter for now (there
124 # is no publicly available method for just dropping
125 # a parent, and I'm loathe to mess with internal state
126 # variables and break containment on HTML::Element)
127 0 my @result;
128 my %parents;
129 0 for (grep { ref $_->parent } grep { ref $_ } @_) {
130 0 ▶ next if $parents{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $parents{$_} 0 0 0
131 0 $parents{$_} = $_->parent;
132 0 $_->parent($self->{_babysitter});
133 }
134 # Invoke the method on our internal element
135 0 @result = $self->{_element}->$name(@_);
136 # Restore the lineages.
137  
138 0 for (grep { ref $_ } @_) {
139 0 ▶ $_->parent(delete $parents{$_}) if $parents{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $parents{$_} 0 0 0
140 }
141 # Cancel glob_ induced context.
142 0 foreach (grep { ref $_ eq ref $self } @_) {
143 0 $_->context_is_glob(0);
144 }
145 0 ▶ return wantarray ? @result : $result[$#result];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
146 }
147 elsif ($self->context_is_glob) {
148 # Here, we have intercepted a native method call that should
149 # actually be executing in glob_ context -- so we do so in
150 # order to ensure any overriden glob_* methods get properly
151 # invoked.
152 0 $name = "glob_$name";
153 0 return $self->$name(@_);
154 }
155  
156 # Otherwise broadcast to component elements.
157 0 ▶ if (!$self->{_element}->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_element"}->is_empty) 0 0 0
158 0 my @results;
159 0 foreach (grep { ref $_ } $self->{_element}->content_list) {
160 0 push(@results, $_->$name(@_));
161 }
162 0 return @results;
163 }
164 }
165  
166 1 1;
167 __END__
« ElementTable.pm ↑ Summary ElementSuper.pm »