Element.pm

filter: Statementstmt 64.1% 676 / 1054 Branchbran 48.6% 383 / 788 Conditioncond 47.9% 238 / 496 MC/DCmcdc 26.8% 75 / 279 Subroutinesub 61.8% 73 / 118 Podpod 98.7% 81 / 82 totaltotal 54.1% 1526 / 2817 CC 609 SCAR 43.7
CC
609
Cov
55%
number_lists
552.0
traverse
283.5
right
272.0
CRAP 78.8
« TreeBuilder.pm ↑ Summary Element/traverse.pm »
1 package HTML::Element;
2  
3 # ABSTRACT: Class for objects that represent HTML elements
4  
5 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 our $VERSION = '5.07'; # VERSION from OurPkgVersion
9  
10 23 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use HTML::Tagset ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use integer; # vroom vroom!
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # This controls encoding entities on output.
16 # When set entities won't be re-encoded.
17 # Defaulting off because parser defaults to unencoding entities
18 23 our $encoded_content = 0;
19  
20 23 ▶use vars qw($html_uc $Debug $ID_COUNTER $VERSION %list_type_to_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Set up support for weak references, if possible:
23 23 my $using_weaken;
24  
25 #=head1 CLASS METHODS
26  
27  
28 26 ▶sub Use_Weak_Refs {
Subroutine Use_Weak_Refs: called CC=5 SCAR=16.2
Pod documented
29 26 my $self_or_class = shift;
30  
31 26 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 25 1 26
32 25 $using_weaken = !! shift; # Normalize boolean value
33 25 ▶ Carp::croak("The installed Scalar::Util lacks support for weak references")
Branch: 1 branch
BranchTrueFalseTotal
if $using_weaken and not defined &Scalar::Util::weaken 0 25 25
Condition66%2/3
$using_weaken and not defined &Scalar::Util::weaken
!ll&&!rl&&r
1240
MC/DC0%0/2
$using_weaken and not defined &Scalar::Util::weaken
$using_weaken not defined &Scalar::Util::weaken
Truth table66%2/3
A and not(B)
A= $using_weakenB= not defined &Scalar::Util::weaken
ABresult
0X0
100
111
34 if $using_weaken and not defined &Scalar::Util::weaken;
35  
36 23 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 25 ▶ *_weaken = $using_weaken ? \&Scalar::Util::weaken : sub ($) {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$using_weaken ? : 24 1 25
38 } # end if setting value
39  
40 26 return $using_weaken;
41 } # end Use_Weak_Refs
42  
43 23 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
44 # Attempt to import weaken from Scalar::Util, but don't complain
45 # if we can't. Also, rename it to _weaken.
46 23 require Scalar::Util;
47  
48 23 __PACKAGE__->Use_Weak_Refs(defined &Scalar::Util::weaken);
49 }
50  
51 21 ▶sub import {
Subroutine import: called CC=3 SCAR=11.7
52 21 my $class = shift;
53  
54 21 for (@_) {
55 1 ▶ if (/^-(no_?)?weak$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-(no_?)?weak$/) { } 1 0 1
56 1 $class->Use_Weak_Refs(not $1);
57 } else {
58 0 Carp::croak("$_ is not exported by the $class module");
59 }
60 }
61 } # end import
62  
63  
64 23 ▶$Debug = 0 unless defined $Debug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $HTML::Element::Debug 23 0 23
65  
66 #=head1 SUBROUTINES
67  
68  
69 0 ▶sub Version {
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
70 0 Carp::carp("Deprecated subroutine HTML::Element::Version called");
71 0 $VERSION;
72 }
73  
74 23 my $nillio = [];
75  
76 23 *HTML::Element::emptyElement = \%HTML::Tagset::emptyElement; # legacy
77 23 *HTML::Element::optionalEndTag = \%HTML::Tagset::optionalEndTag; # legacy
78 23 *HTML::Element::linkElements = \%HTML::Tagset::linkElements; # legacy
79 23 *HTML::Element::boolean_attr = \%HTML::Tagset::boolean_attr; # legacy
80 23 *HTML::Element::canTighten = \%HTML::Tagset::canTighten; # legacy
81  
82 # Constants for signalling back to the traverser:
83 23 my $travsignal_package = __PACKAGE__ . '::_travsignal';
84 my ( $ABORT, $PRUNE, $PRUNE_SOFTLY, $OK, $PRUNE_UP )
85 23 = map { my $x = $_; bless \$x, $travsignal_package; }
86 qw(
87 ABORT PRUNE PRUNE_SOFTLY OK PRUNE_UP
88 );
89  
90  
91 ## Comments from Father Chrysostomos RT #58880
92 ## The sole purpose for empty parentheses after a sub name is to make it
93 ## parse as a 0-ary (nihilary?) function. I.e., ABORT+1 should parse as
94 ## ABORT()+1, not ABORT(+1). The parentheses also tell perl that it can
95 ### be inlined.
96 ##Deparse is really useful for demonstrating this:
97 ##$ perl -MO=Deparse,-p -e 'sub ABORT {7} print ABORT+8'
98 # Vs
99 # perl -MO=Deparse,-p -e 'sub ABORT() {7} print ABORT+8'
100 #
101 # With the parentheses, it not only makes it parse as a term.
102 # It even resolves the constant at compile-time, making the code run faster.
103  
104 ## no critic
105 19 ▶sub ABORT () {$ABORT}
Subroutine ABORT: called CC=1 SCAR=0.0
Pod documented
106 0 ▶sub PRUNE () {$PRUNE}
Subroutine PRUNE: not called CC=1 SCAR=6.9
Pod documented
107 0 ▶sub PRUNE_SOFTLY () {$PRUNE_SOFTLY}
Subroutine PRUNE_SOFTLY: not called CC=1 SCAR=6.9
Pod documented
108 0 ▶sub OK () {$OK}
Subroutine OK: not called CC=1 SCAR=6.9
Pod documented
109 0 ▶sub PRUNE_UP () {$PRUNE_UP}
Subroutine PRUNE_UP: not called CC=1 SCAR=6.9
Pod documented
110 ## use critic
111  
112 23 $html_uc = 0;
113  
114 # set to 1 if you want tag and attribute names from starttag and endtag
115 # to be uc'd
116  
117 # regexs for XML names
118 # http://www.w3.org/TR/2006/REC-xml11-20060816/NT-NameStartChar
119 23 my $START_CHAR
120 = qr/(?:\:|[A-Z]|_|[a-z]|[\x{C0}-\x{D6}]|[\x{D8}-\x{F6}]|[\x{F8}-\x{2FF}]|[\x{370}-\x{37D}]|[\x{37F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/;
121  
122 # http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-NameChar
123 23 my $NAME_CHAR
124 = qr/(?:$START_CHAR|-|\.|[0-9]|\x{B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}])/;
125  
126 # Elements that does not have corresponding end tags (i.e. are empty)
127  
128 #==========================================================================
129  
130 #=head1 BASIC METHODS
131  
132  
133 #
134 # An HTML::Element is represented by blessed hash reference, much like
135 # Tree::DAG_Node objects. Key-names not starting with '_' are reserved
136 # for the SGML attributes of the element.
137 # The following special keys are used:
138 #
139 # '_tag': The tag name (i.e., the generic identifier)
140 # '_parent': A reference to the HTML::Element above (when forming a tree)
141 # '_pos': The current position (a reference to a HTML::Element) is
142 # where inserts will be placed (look at the insert_element
143 # method) If not set, the implicit value is the object itself.
144 # '_content': A ref to an array of nodes under this.
145 # It might not be set.
146 #
147 # Example: <img src="gisle.jpg" alt="Gisle's photo"> is represented like this:
148 #
149 # bless {
150 # _tag => 'img',
151 # src => 'gisle.jpg',
152 # alt => "Gisle's photo",
153 # }, 'HTML::Element';
154 #
155  
156 2409 ▶sub new {
Subroutine new: called CC=9 SCAR=22.9
Pod documented
157 2409 my $class = shift;
158 2409 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
024090
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
159  
160 2409 my $tag = shift;
161 2409 ▶ Carp::croak("No tagname") unless defined $tag and length $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag and length $tag 0 2409 2409
Condition33%1/3
defined $tag and length $tag
!ll&&!rl&&r
002409
MC/DC0%0/2
defined $tag and length $tag
defined $tag length $tag
Truth table33%1/3
A and B
A= defined $tagB= length $tag
ABresult
0X0
100
111
162 2409 ▶ Carp::croak "\"$tag\" isn't a good tag name!"
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ m[[<>/\x00-\x20]] 0 2409 2409
163 if $tag =~ m/[<>\/\x00-\x20]/; # minimal sanity, certainly!
164 2409 my $self = bless { _tag => scalar( $class->_fold_case($tag) ) }, $class;
165 2409 my ( $attr, $val );
166 2409 ▶ while ( ( $attr, $val ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 513 2409 2922
167 ## RT #42209 why does this default to the attribute name and not remain unset or the empty string?
168 513 ▶ $val = $attr unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 512 513
169 513 $self->{ $class->_fold_case($attr) } = $val;
170 }
171 2409 ▶ if ( $tag eq 'html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "html") 358 2051 2409
172 358 $self->{'_pos'} = undef;
173 }
174 2409 ▶ _weaken($self->{'_parent'}) if $self->{'_parent'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_parent"} 0 2409 2409
175 2409 return $self;
176 }
177  
178  
179 40 ▶sub attr {
Subroutine attr: called CC=3 SCAR=11.0
Pod documented
180 40 my $self = shift;
181 40 my $attr = scalar( $self->_fold_case(shift) );
182 40 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 37 3 40
183 37 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 28 9 37
184 28 my $old = $self->{$attr};
185 28 $self->{$attr} = $_[0];
186 28 return $old;
187 }
188 else { # delete, actually
189 9 return delete $self->{$attr};
190 }
191 }
192 else { # get
193 3 return $self->{$attr};
194 }
195 }
196  
197  
198 1034 ▶sub tag {
Subroutine tag: called CC=2 SCAR=7.6
Pod documented
199 1034 my $self = shift;
200 1034 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 1034 1034
201 0 $self->{'_tag'} = $self->_fold_case( $_[0] );
202 }
203 else { # get
204 1034 $self->{'_tag'};
205 }
206 }
207  
208  
209 118 ▶sub parent {
Subroutine parent: called CC=5 SCAR=27.0
Pod documented
210 118 my $self = shift;
211 118 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 118 118
212 0 ▶ Carp::croak "an element can't be made its own parent"
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] and $self eq $_[0] 0 0 0
Condition0%0/6
defined $_[0] and ref $_[0] and $self eq $_[0]
!ll&&!rl&&r
000
defined $_[0] and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_[0] and ref $_[0] and $self eq $_[0]
defined $_[0] ref $_[0] $self eq $_[0]
Truth table0%0/4
A and B and C
A= defined $_[0]B= ref $_[0]C= $self eq $_[0]
ABCresult
0XX0
10X0
1100
1111
213 if defined $_[0] and ref $_[0] and $self eq $_[0]; # sanity
214 0 _weaken($self->{'_parent'} = $_[0]);
215 }
216 else {
217 118 $self->{'_parent'}; # get
218 }
219 }
220  
221  
222 1619 ▶sub content_list {
Subroutine content_list: called CC=4 SCAR=13.9
Pod documented
223 return wantarray
224 176 ▶ ? @{ shift->{'_content'} || return () }
Condition50%1/2
+(shift())->{'_content'} || (return ())
l!l
1760
MC/DC0%0/1
+(shift())->{'_content'} || (return ())
+(shift())->{'_content'}
Truth table50%1/2
A
A= +(shift())->{'_content'}
Aresult
11
00
225 1619 ▶ : scalar @{ shift->{'_content'} || return 0 };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 176 1443 1619
Condition100%2/2
+(shift())->{'_content'} || (return 0)
l!l
141528
MC/DC100%1/1
+(shift())->{'_content'} || (return 0)
+(shift())->{'_content'}
Truth table100%2/2
A
A= +(shift())->{'_content'}
Aresult
11
00
226 }
227  
228  
229 # a read-only method! can't say $h->content( [] )!
230 9 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
231 9 return shift->{'_content'};
232 }
233  
234  
235 0 ▶sub content_array_ref {
Subroutine content_array_ref: not called CC=2 SCAR=17.9
Pod documented
236 0 ▶ return shift->{'_content'} ||= [];
Condition0%0/2
+(shift())->{"_content"} ||= []
l!l
00
MC/DC0%0/1
+(shift())->{"_content"} ||= []
+(shift())->{"_content"}
Truth table0%0/2
A
A= +(shift())->{"_content"}
Aresult
11
00
237 }
238  
239  
240 0 ▶sub content_refs_list {
Subroutine content_refs_list: not called CC=2 SCAR=17.9
Pod documented
241 0 ▶ return \( @{ shift->{'_content'} || return () } );
Condition0%0/2
+(shift())->{'_content'} || (return ())
l!l
00
MC/DC0%0/1
+(shift())->{'_content'} || (return ())
+(shift())->{'_content'}
Truth table0%0/2
A
A= +(shift())->{'_content'}
Aresult
11
00
242 }
243  
244  
245 0 ▶sub implicit {
Subroutine implicit: not called CC=1 SCAR=6.9
Pod documented
246 0 return shift->attr( '_implicit', @_ );
247 }
248  
249  
250 0 ▶sub pos {
Subroutine pos: not called CC=5 SCAR=34.0
Pod documented
251 0 my $self = shift;
252 0 my $pos = $self->{'_pos'};
253 0 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
254 0 my $parm = shift;
255 0 ▶ if ( defined $parm and $parm ne $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parm and $parm ne $self) { } 0 0 0
Condition0%0/3
defined $parm and $parm ne $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $parm and $parm ne $self
defined $parm $parm ne $self
Truth table0%0/3
A and B
A= defined $parmB= $parm ne $self
ABresult
0X0
100
111
256 0 $self->{'_pos'} = $parm; # means that element
257 }
258 else {
259 0 $self->{'_pos'} = undef; # means $self
260 }
261 }
262 0 ▶ return $pos if defined($pos);
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 0 0 0
263 0 return $self;
264 }
265  
266  
267 9 ▶sub all_attr {
Subroutine all_attr: called CC=1 SCAR=0.0
Pod documented
268 9 return %{ $_[0] };
269  
270 # Yes, trivial. But no other way for the user to do the same
271 # without breaking encapsulation.
272 # And if our object representation changes, this method's behavior
273 # should stay the same.
274 }
275  
276 598 ▶sub all_attr_names {
Subroutine all_attr_names: called CC=1 SCAR=0.0
Pod documented
277 598 return keys %{ $_[0] };
278 }
279  
280  
281 3 ▶sub all_external_attr {
Subroutine all_external_attr: called CC=3 SCAR=11.1
Pod documented
282 3 my $self = $_[0];
283 return map( ( length($_) && substr( $_, 0, 1 ) eq '_' )
284 ? ()
285 3 ▶ : ( $_, $self->{$_} ),
Branch: 1 branch
BranchTrueFalseTotal
length $_ && substr($_, 0, 1) eq '_' ? : 6 2 8
Condition66%2/3
length $_ and substr($_, 0, 1) eq "_"
!ll&&!rl&&r
026
MC/DC50%1/2
length $_ and substr($_, 0, 1) eq "_"
length $_ substr($_, 0, 1) eq "_"
Truth table66%2/3
A and B
A= length $_B= substr($_, 0, 1) eq "_"
ABresult
0X0
100
111
286 keys %$self );
287 }
288  
289 0 ▶sub all_external_attr_names {
Subroutine all_external_attr_names: not called CC=2 SCAR=17.9
Pod documented
290 0 ▶ return grep !( length($_) && substr( $_, 0, 1 ) eq '_' ), keys %{ $_[0] };
Condition0%0/3
length $_ && substr($_, 0, 1) eq '_'
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && substr($_, 0, 1) eq '_'
length $_ substr($_, 0, 1) eq '_'
Truth table0%0/3
A && B
A= length $_B= substr($_, 0, 1) eq '_'
ABresult
0X0
100
111
291 }
292  
293  
294 0 ▶sub id {
Subroutine id: not called CC=4 SCAR=30.0
Pod documented
295 0 ▶ if ( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
296 0 return $_[0]{'id'};
297 }
298 elsif ( @_ == 2 ) {
299 0 ▶ if ( defined $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
300 0 return $_[0]{'id'} = $_[1];
301 }
302 else {
303 0 return delete $_[0]{'id'};
304 }
305 }
306 else {
307 0 Carp::croak '$node->id can\'t take ' . scalar(@_) . ' parameters!';
308 }
309 }
310  
311  
312 0 ▶sub _gensym {
Subroutine _gensym: not called CC=2 SCAR=17.9
313 0 ▶ unless ( defined $ID_COUNTER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HTML::Element::ID_COUNTER) 0 0 0
314  
315 # start it out...
316 0 $ID_COUNTER = sprintf( '%04x', rand(0x1000) );
317 0 $ID_COUNTER =~ tr<0-9a-f><J-NP-Z>; # yes, skip letter "oh"
318 0 $ID_COUNTER .= '00000';
319 }
320 0 ++$ID_COUNTER;
321 }
322  
323 0 ▶sub idf {
Subroutine idf: not called CC=6 SCAR=37.4
Pod documented
324 0 my $nparms = scalar @_;
325  
326 0 ▶ if ( $nparms == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nparms == 1) 0 0 0
327 0 my $x;
328 0 ▶ if ( defined( $x = $_[0]{'id'} ) and length $x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($x = $_[0]{'id'}) and length $x) { } 0 0 0
Condition0%0/3
defined($x = $_[0]{"id"}) and length $x
!ll&&!rl&&r
000
MC/DC0%0/2
defined($x = $_[0]{"id"}) and length $x
defined($x = $_[0]{"id"}) length $x
Truth table0%0/3
A and B
A= defined($x = $_[0]{"id"})B= length $x
ABresult
0X0
100
111
329 0 return $x;
330 }
331 else {
332 0 return $_[0]{'id'} = _gensym();
333 }
334 }
335 0 ▶ if ( $nparms == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nparms == 2) 0 0 0
336 0 ▶ if ( defined $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
337 0 return $_[0]{'id'} = $_[1];
338 }
339 else {
340 0 return delete $_[0]{'id'};
341 }
342 }
343 0 Carp::croak '$node->idf can\'t take ' . scalar(@_) . ' parameters!';
344 }
345  
346  
347 3956 ▶sub push_content {
Subroutine push_content: called CC=9 SCAR=22.0
Pod documented
348 3956 my $self = shift;
349 3956 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 3955 3956
350  
351 3955 ▶ my $content = ( $self->{'_content'} ||= [] );
Condition100%2/2
$self->{"_content"} ||= []
l!l
20941861
MC/DC100%1/1
$self->{"_content"} ||= []
$self->{"_content"}
Truth table100%2/2
A
A= $self->{"_content"}
Aresult
11
00
352 3955 for (@_) {
353 3994 ▶ if ( ref($_) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 3 3991 3994
elsif (ref $_) { } 1721 2270 3991
354  
355 # magically call new_from_lol
356 3 push @$content, $self->new_from_lol($_);
357 3 _weaken($content->[-1]->{'_parent'} = $self);
358 }
359 elsif ( ref($_) ) { # insert an element
360 1721 ▶ $_->detach if $_->{'_parent'};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"_parent"} 1 1720 1721
361 1721 _weaken($_->{'_parent'} = $self);
362 1721 push( @$content, $_ );
363 }
364 else { # insert text segment
365 2270 ▶ if ( @$content && !ref $content->[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$content and not ref $content->[-1]) { } 398 1872 2270
Condition100%3/3
@$content and not ref $content->[-1]
!ll&&!rl&&r
1016856398
MC/DC100%2/2
@$content and not ref $content->[-1]
@$content not ref $content->[-1]
Truth table100%3/3
A and not(B)
A= @$contentB= not ref $content->[-1]
ABresult
0X0
100
111
366  
367 # last content element is also text segment -- append
368 398 $content->[-1] .= $_;
369 }
370 else {
371 1872 push( @$content, $_ );
372 }
373 }
374 }
375 3955 return $self;
376 }
377  
378  
379 2 ▶sub unshift_content {
Subroutine unshift_content: called CC=9 SCAR=26.9
Pod documented
380 2 my $self = shift;
381 2 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
382  
383 2 ▶ my $content = ( $self->{'_content'} ||= [] );
Condition50%1/2
$self->{"_content"} ||= []
l!l
20
MC/DC0%0/1
$self->{"_content"} ||= []
$self->{"_content"}
Truth table50%1/2
A
A= $self->{"_content"}
Aresult
11
00
384 2 for ( reverse @_ ) { # so they get added in the order specified
385 2 ▶ if ( ref($_) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 1 1 2
elsif (ref $_) { } 1 0 1
386  
387 # magically call new_from_lol
388 1 unshift @$content, $self->new_from_lol($_);
389 1 _weaken($content->[0]->{'_parent'} = $self);
390 }
391 elsif ( ref $_ ) { # insert an element
392 1 ▶ $_->detach if $_->{'_parent'};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"_parent"} 0 1 1
393 1 _weaken($_->{'_parent'} = $self);
394 1 unshift( @$content, $_ );
395 }
396 else { # insert text segment
397 0 ▶ if ( @$content && !ref $content->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$content and not ref $content->[0]) { } 0 0 0
Condition0%0/3
@$content and not ref $content->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@$content and not ref $content->[0]
@$content not ref $content->[0]
Truth table0%0/3
A and not(B)
A= @$contentB= not ref $content->[0]
ABresult
0X0
100
111
398  
399 # last content element is also text segment -- prepend
400 0 $content->[0] = $_ . $content->[0];
401 }
402 else {
403 0 unshift( @$content, $_ );
404 }
405 }
406 }
407 2 return $self;
408 }
409  
410 # Cf. splice ARRAY,OFFSET,LENGTH,LIST
411  
412  
413 2 ▶sub splice_content {
Subroutine splice_content: called CC=9 SCAR=22.8
Pod documented
414 2 my ( $self, $offset, $length, @to_add ) = @_;
415 2 ▶ Carp::croak "splice_content requires at least one argument"
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 2 2
416 if @_ < 2; # at least $h->splice_content($offset);
417  
418 2 ▶ my $content = ( $self->{'_content'} ||= [] );
Condition50%1/2
$self->{"_content"} ||= []
l!l
20
MC/DC0%0/1
$self->{"_content"} ||= []
$self->{"_content"}
Truth table50%1/2
A
A= $self->{"_content"}
Aresult
11
00
419  
420 # prep the list
421  
422 2 my @out;
423 2 ▶ if ( @_ > 2 ) { # self, offset, length, ...
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 2 0 2
424 2 foreach my $n (@to_add) {
425 2 ▶ if ( ref($n) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $n eq 'ARRAY') { } 1 1 2
elsif (ref $n) { } 1 0 1
426 1 $n = $self->new_from_lol($n);
427 1 _weaken($n->{'_parent'} = $self);
428 }
429 elsif ( ref($n) ) {
430 1 $n->detach;
431 1 _weaken($n->{'_parent'} = $self);
432 }
433 }
434 2 @out = splice @$content, $offset, $length, @to_add;
435 }
436 else { # self, offset
437 0 @out = splice @$content, $offset;
438 }
439 2 foreach my $n (@out) {
440 4 ▶ $n->{'_parent'} = undef if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 4 0 4
441 }
442 2 return @out;
443 }
444  
445  
446 4 ▶sub detach {
Subroutine detach: called CC=4 SCAR=15.5
Pod documented
447 4 my $self = $_[0];
448 4 ▶ return undef unless ( my $parent = $self->{'_parent'} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $parent = $self->{"_parent"} 3 1 4
449 1 $self->{'_parent'} = undef;
450 1 ▶ my $cohort = $parent->{'_content'} || return $parent;
Condition50%1/2
$parent->{'_content'} || (return $parent)
l!l
10
MC/DC0%0/1
$parent->{'_content'} || (return $parent)
$parent->{'_content'}
Truth table50%1/2
A
A= $parent->{'_content'}
Aresult
11
00
451 0 ▶ @$cohort = grep { not( ref($_) and $_ eq $self ) } @$cohort;
Condition0%0/3
ref $_ && $_ eq $self
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ && $_ eq $self
ref $_ $_ eq $self
Truth table0%0/3
A && B
A= ref $_B= $_ eq $self
ABresult
0X0
100
111
452  
453 # filter $self out, if parent has any evident content
454  
455 1 return $parent;
456 }
457  
458  
459 9 ▶sub detach_content {
Subroutine detach_content: called CC=4 SCAR=13.9
Pod documented
460 9 ▶ my $c = $_[0]->{'_content'} || return (); # in case of no content
Condition50%1/2
$_[0]{'_content'} || (return ())
l!l
90
MC/DC0%0/1
$_[0]{'_content'} || (return ())
$_[0]{'_content'}
Truth table50%1/2
A
A= $_[0]{'_content'}
Aresult
11
00
461 9 for (@$c) {
462 45 ▶ $_->{'_parent'} = undef if ref $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 36 9 45
463 }
464 9 return splice @$c;
465 }
466  
467  
468 4 ▶sub replace_with {
Subroutine replace_with: called CC=13 SCAR=27.4
Pod documented
469 4 my ( $self, @replacers ) = @_;
470 Carp::croak "the target node has no parent"
471 4 ▶ unless my ($parent) = $self->{'_parent'};
Branch: 1 branch
BranchTrueFalseTotal
unless my($parent) = $self->{"_parent"} 0 4 4
472  
473 4 my $parent_content = $parent->{'_content'};
474 4 ▶ Carp::croak "the target node's parent has no content!?"
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_content and @$parent_content 0 4 4
Condition33%1/3
$parent_content and @$parent_content
!ll&&!rl&&r
004
MC/DC0%0/2
$parent_content and @$parent_content
$parent_content @$parent_content
Truth table33%1/3
A and B
A= $parent_contentB= @$parent_content
ABresult
0X0
100
111
475 unless $parent_content and @$parent_content;
476  
477 4 my $replacers_contains_self;
478 4 for (@replacers) {
479 8 ▶ if ( !ref $_ ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $_) { } 0 8 8
elsif ($_ eq $self) { } 4 4 8
elsif ($_ eq $parent) { } 0 4 4
elsif (ref $_ eq 'ARRAY') { } 2 2 4
480  
481 # noop
482 }
483 elsif ( $_ eq $self ) {
484  
485 # noop, but check that it's there just once.
486 4 ▶ Carp::croak "Replacement list contains several copies of target!"
Branch: 1 branch
BranchTrueFalseTotal
if $replacers_contains_self++ 0 4 4
487 if $replacers_contains_self++;
488 }
489 elsif ( $_ eq $parent ) {
490 0 Carp::croak "Can't replace an item with its parent!";
491 }
492 elsif ( ref($_) eq 'ARRAY' ) {
493 2 $_ = $self->new_from_lol($_);
494 2 _weaken($_->{'_parent'} = $parent);
495 }
496 else {
497 2 $_->detach;
498 2 _weaken($_->{'_parent'} = $parent);
499  
500 # each of these are necessary
501 }
502 } # for @replacers
503 4 ▶ @$parent_content = map { ( ref($_) and $_ eq $self ) ? @replacers : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_ eq $self ? : 4 24 28
Condition66%2/3
ref $_ and $_ eq $self
!ll&&!rl&&r
0244
MC/DC50%1/2
ref $_ and $_ eq $self
ref $_ $_ eq $self
Truth table66%2/3
A and B
A= ref $_B= $_ eq $self
ABresult
0X0
100
111
504 @$parent_content;
505  
506 4 ▶ $self->{'_parent'} = undef unless $replacers_contains_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $replacers_contains_self 0 4 4
507  
508 # if replacers does contain self, then the parent attribute is fine as-is
509  
510 4 return $self;
511 }
512  
513  
514 2 ▶sub preinsert {
Subroutine preinsert: called CC=2 SCAR=7.1
Pod documented
515 2 my $self = shift;
516 2 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
517 2 return $self->replace_with( @_, $self );
518 }
519  
520  
521 2 ▶sub postinsert {
Subroutine postinsert: called CC=2 SCAR=7.1
Pod documented
522 2 my $self = shift;
523 2 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
524 2 return $self->replace_with( $self, @_ );
525 }
526  
527  
528 10 ▶sub replace_with_content {
Subroutine replace_with_content: called CC=9 SCAR=24.8
Pod documented
529 10 my $self = $_[0];
530 Carp::croak "the target node has no parent"
531 10 ▶ unless my ($parent) = $self->{'_parent'};
Branch: 1 branch
BranchTrueFalseTotal
unless my($parent) = $self->{"_parent"} 0 10 10
532  
533 10 my $parent_content = $parent->{'_content'};
534 10 ▶ Carp::croak "the target node's parent has no content!?"
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_content and @$parent_content 0 10 10
Condition33%1/3
$parent_content and @$parent_content
!ll&&!rl&&r
0010
MC/DC0%0/2
$parent_content and @$parent_content
$parent_content @$parent_content
Truth table33%1/3
A and B
A= $parent_contentB= @$parent_content
ABresult
0X0
100
111
535 unless $parent_content and @$parent_content;
536  
537 10 ▶ my $content_r = $self->{'_content'} || [];
Condition50%1/2
$self->{'_content'} || []
l!l
010
MC/DC0%0/1
$self->{'_content'} || []
$self->{'_content'}
Truth table50%1/2
A
A= $self->{'_content'}
Aresult
11
00
538 10 ▶ @$parent_content = map { ( ref($_) and $_ eq $self ) ? @$content_r : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_ eq $self ? : 10 25 35
Condition66%2/3
ref $_ and $_ eq $self
!ll&&!rl&&r
02510
MC/DC50%1/2
ref $_ and $_ eq $self
ref $_ $_ eq $self
Truth table66%2/3
A and B
A= ref $_B= $_ eq $self
ABresult
0X0
100
111
539 @$parent_content;
540  
541 10 $self->{'_parent'} = undef; # detach $self from its parent
542  
543 # Update parentage link, removing from $self's content list
544 0 ▶ for ( splice @$content_r ) { _weaken($_->{'_parent'} = $parent) if ref $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 0 0 0
545  
546 10 return $self; # note: doesn't destroy it.
547 }
548  
549  
550 1242 ▶sub delete_content {
Subroutine delete_content: called CC=4 SCAR=13.9
Pod documented
551 1242 for (
552 splice @{
553 1242 ▶ delete( $_[0]->{'_content'} )
Condition50%1/2
delete $_[0]{'_content'} || (return $_[0])
l!l
12420
MC/DC0%0/1
delete $_[0]{'_content'} || (return $_[0])
delete $_[0]{'_content'}
Truth table50%1/2
A
A= delete $_[0]{'_content'}
Aresult
11
00
554  
555 # Deleting it here (while holding its value, for the moment)
556 # will keep calls to detach() from trying to uselessly filter
557 # the list (as they won't be able to see it once it's been
558 # deleted)
559 || return ( $_[0] ) # in case of no content
560 },
561 0
562  
563 # the splice is so we can null the array too, just in case
564 # something somewhere holds a ref to it
565 )
566 {
567 2112 ▶ $_->delete if ref $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 868 1244 2112
568 }
569 1242 $_[0];
570 }
571  
572  
573 # two handy aliases
574 0 ▶sub destroy { shift->delete(@_) }
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
575 0 ▶sub destroy_content { shift->delete_content(@_) }
Subroutine destroy_content: not called CC=1 SCAR=6.9
Pod documented
576  
577 1410 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.2
Pod documented
578 1410 my $self = $_[0];
579 $self->delete_content # recurse down
580 1410 ▶ if $self->{'_content'} && @{ $self->{'_content'} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_content"} and @{$self->{"_content"};} 1242 168 1410
Condition100%3/3
$self->{"_content"} and @{$self->{"_content"};}
!ll&&!rl&&r
16621242
MC/DC100%2/2
$self->{"_content"} and @{$self->{"_content"};}
$self->{"_content"} @{$self->{"_content"};}
Truth table100%3/3
A and B
A= $self->{"_content"}B= @{$self->{"_content"};}
ABresult
0X0
100
111
581  
582 1410 ▶ $self->detach if $self->{'_parent'} and $self->{'_parent'}{'_content'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_parent"} and $self->{"_parent"}{"_content"} 0 1410 1410
Condition66%2/3
$self->{"_parent"} and $self->{"_parent"}{"_content"}
!ll&&!rl&&r
2213880
MC/DC0%0/2
$self->{"_parent"} and $self->{"_parent"}{"_content"}
$self->{"_parent"} $self->{"_parent"}{"_content"}
Truth table66%2/3
A and B
A= $self->{"_parent"}B= $self->{"_parent"}{"_content"}
ABresult
0X0
100
111
583  
584 # not the typical case
585  
586 1410 %$self = (); # null out the whole object on the way out
587 1410 return;
588 }
589  
590  
591 4 ▶sub clone {
Subroutine clone: called CC=7 SCAR=19.6
Pod documented
592  
593 #print "Cloning $_[0]\n";
594 4 my $it = shift;
595 4 ▶ Carp::croak "clone() can be called only as an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $it 0 4 4
596 unless ref $it;
597 4 ▶ Carp::croak "clone() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
598  
599 4 my $new = bless {%$it}, ref($it); # COPY!!! HOOBOY!
600 4 delete @$new{ '_content', '_parent', '_pos', '_head', '_body' };
601  
602 # clone any contents
603 4 ▶ if ( $it->{'_content'} and @{ $it->{'_content'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($it->{"_content"} and @{$it->{"_content"};}) 3 1 4
Condition66%2/3
$it->{"_content"} and @{$it->{"_content"};}
!ll&&!rl&&r
103
MC/DC50%1/2
$it->{"_content"} and @{$it->{"_content"};}
$it->{"_content"} @{$it->{"_content"};}
Truth table66%2/3
A and B
A= $it->{"_content"}B= @{$it->{"_content"};}
ABresult
0X0
100
111
604 $new->{'_content'}
605 3 = [ ref($it)->clone_list( @{ $it->{'_content'} } ) ];
606 3 for ( @{ $new->{'_content'} } ) {
607 5 ▶ _weaken($_->{'_parent'} = $new) if ref $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 3 2 5
608 }
609 }
610  
611 4 return $new;
612 }
613  
614  
615 3 ▶sub clone_list {
Subroutine clone_list: called CC=3 SCAR=11.1
Pod documented
616 3 ▶ Carp::croak "clone_list can be called only as a class method"
Branch: 1 branch
BranchTrueFalseTotal
if ref shift @_ 0 3 3
617 if ref shift @_;
618  
619 # all that does is get me here
620 return map {
621 3 ▶ ref($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 3 2 5
622 ? $_->clone # copy by method
623 : $_ # copy by evaluation
624 } @_;
625 }
626  
627  
628 9 ▶sub normalize_content {
Subroutine normalize_content: called CC=14 SCAR=27.4
Pod documented
629 9 my $start = $_[0];
630 9 my $c;
631 return
632 9 ▶ unless $c = $start->{'_content'} and ref $c and @$c; # nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless $c = $start->{"_content"} and ref $c and @$c 0 9 9
Condition33%2/6
$c = $start->{"_content"} and ref $c and @$c
!ll&&!rl&&r
009
$c = $start->{"_content"} and ref $c
!ll&&!rl&&r
009
MC/DC0%0/3
$c = $start->{"_content"} and ref $c and @$c
$c = $start->{"_content"} ref $c @$c
Truth table25%1/4
A and B and C
A= $c = $start->{"_content"}B= ref $cC= @$c
ABCresult
0XX0
10X0
1100
1111
633 # TODO: if we start having text elements, deal with catenating those too?
634 9 my @stretches = (undef); # start with a barrier
635  
636 # I suppose this could be rewritten to treat stretches as it goes, instead
637 # of at the end. But feh.
638  
639 # Scan:
640 9 ▶ for ( my $i = 0; $i < @$c; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 54 9 63
641 54 ▶ if ( defined $c->[$i] and ref $c->[$i] ) { # not a text segment
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->[$i] and ref $c->[$i]) { } 9 45 54
Condition100%3/3
defined $c->[$i] and ref $c->[$i]
!ll&&!rl&&r
27189
MC/DC100%2/2
defined $c->[$i] and ref $c->[$i]
defined $c->[$i] ref $c->[$i]
Truth table100%3/3
A and B
A= defined $c->[$i]B= ref $c->[$i]
ABresult
0X0
100
111
642 9 ▶ if ( $stretches[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stretches[0]) 9 0 9
643  
644 # put in a barrier
645 9 ▶ if ( $stretches[0][1] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stretches[0][1] == 1) { } 0 9 9
646  
647 #print "Nixing stretch at ", $i-1, "\n";
648 0 undef $stretches[0]; # nix the previous one-node "stretch"
649 }
650 else {
651  
652 #print "End of stretch at ", $i-1, "\n";
653 9 unshift @stretches, undef;
654 }
655 }
656  
657 # else no need for a barrier
658 }
659 else { # text segment
660 45 ▶ $c->[$i] = '' unless defined $c->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->[$i] 27 18 45
661 45 ▶ if ( $stretches[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stretches[0]) { } 27 18 45
662 27 ++$stretches[0][1]; # increase length
663 }
664 else {
665  
666 #print "New stretch at $i\n";
667 18 unshift @stretches, [ $i, 1 ]; # start and length
668 }
669 }
670 }
671  
672 # Now combine. Note that @stretches is in reverse order, so the indexes
673 # still make sense as we work our way thru (i.e., backwards thru $c).
674 9 foreach my $s (@stretches) {
675 36 ▶ if ( $s and $s->[1] > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s and $s->[1] > 1) 18 18 36
Condition66%2/3
$s and $s->[1] > 1
!ll&&!rl&&r
18018
MC/DC50%1/2
$s and $s->[1] > 1
$s $s->[1] > 1
Truth table66%2/3
A and B
A= $sB= $s->[1] > 1
ABresult
0X0
100
111
676  
677 #print "Stretch at ", $s->[0], " for ", $s->[1], "\n";
678 18 $c->[ $s->[0] ]
679 .= join( '', splice( @$c, $s->[0] + 1, $s->[1] - 1 ) )
680  
681 # append the subsequent ones onto the first one.
682 }
683 }
684 9 return;
685 }
686  
687  
688 310 ▶sub delete_ignorable_whitespace {
Subroutine delete_ignorable_whitespace: called CC=31 SCAR=43.0
Pod documented
689  
690 # This doesn't delete all sorts of whitespace that won't actually
691 # be used in rendering, tho -- that's up to the rendering application.
692 # For example:
693 # <input type='text' name='foo'>
694 # [some whitespace]
695 # <input type='text' name='bar'>
696 # The WS between the two elements /will/ get used by the renderer.
697 # But here:
698 # <input type='hidden' name='foo' value='1'>
699 # [some whitespace]
700 # <input type='text' name='bar' value='2'>
701 # the WS between them won't be rendered in any way, presumably.
702  
703 #my $Debug = 4;
704 310 ▶ die "delete_ignorable_whitespace can be called only as an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 310 310
705 unless ref $_[0];
706  
707 310 ▶ print "About to tighten up...\n" if $Debug > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Element::Debug > 2 0 310 310
708 310 my (@to_do) = ( $_[0] ); # Start off.
709 310 my ( $i, $sibs, $ptag, $this ); # scratch for the loop...
710 310 ▶ while (@to_do) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_do) 1929 310 2239
711 1929 ▶ if ( ( $ptag = ( $this = shift @to_do )->{'_tag'} ) eq 'pre'
Branch: 1 branch
BranchTrueFalseTotal
if (($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre" or $ptag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$ptag}) 2 1927 1929
Condition83%5/6
($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre" or $ptag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$ptag}
l!l&&r!l&&!r
201927
($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre" or $ptag eq "textarea"
l!l&&r!l&&!r
111927
MC/DC66%2/3
($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre" or $ptag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$ptag}
($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre" $ptag eq "textarea" $HTML::Tagset::isCDATA_Parent{$ptag}
Truth table75%3/4
A or B or C
A= ($ptag = +($this = shift @to_do)->{"_tag"}) eq "pre"B= $ptag eq "textarea"C= $HTML::Tagset::isCDATA_Parent{$ptag}
ABCresult
1XX1
01X1
0011
0000
712 or $ptag eq 'textarea'
713 or $HTML::Tagset::isCDATA_Parent{$ptag} )
714 {
715  
716 # block the traversal under those
717 2 ▶ print "Blocking traversal under $ptag\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Element::Debug 0 2 2
718 2 next;
719 }
720 1927 ▶ next unless ( $sibs = $this->{'_content'} and @$sibs );
Branch: 1 branch
BranchTrueFalseTotal
unless $sibs = $this->{"_content"} and @$sibs 194 1733 1927
Condition66%2/3
$sibs = $this->{"_content"} and @$sibs
!ll&&!rl&&r
19401733
MC/DC50%1/2
$sibs = $this->{"_content"} and @$sibs
$sibs = $this->{"_content"} @$sibs
Truth table66%2/3
A and B
A= $sibs = $this->{"_content"}B= @$sibs
ABresult
0X0
100
111
721 1733 ▶ for ( $i = $#$sibs; $i >= 0; --$i ) { # work backwards thru the list
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 3341 1733 5074
722 3341 ▶ if ( ref $sibs->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sibs->[$i]) 1619 1722 3341
723 1619 unshift @to_do, $sibs->[$i];
724  
725 # yes, this happens in pre order -- we're going backwards
726 # thru this sibling list. I doubt it actually matters, tho.
727 1619 next;
728 }
729 1722 ▶ next if $sibs->[$i] =~ m<[^\n\r\f\t ]>s; # it's /all/ whitespace
Branch: 1 branch
BranchTrueFalseTotal
if $sibs->[$i] =~ /[^\n\r\f\t ]/s 1212 510 1722
730  
731 print "Under $ptag whose canTighten ",
732 510 ▶ "value is ", 0 + $HTML::Element::canTighten{$ptag}, ".\n"
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Element::Debug > 3 0 510 510
733 if $Debug > 3;
734  
735 # It's all whitespace...
736  
737 510 ▶ if ( $i == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 0) { } 2 508 510
elsif ($i == $sibs->$#*) { } 346 162 508
738 2 ▶ if ( @$sibs == 1 ) { # I'm an only child
Branch: 1 branch
BranchTrueFalseTotal
if (@$sibs == 1) { } 2 0 2
739 2 ▶ next unless $HTML::Element::canTighten{$ptag}; # parent
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Element::canTighten{$ptag} 0 2 2
740 }
741 else { # I'm leftmost of many
742 # if either my parent or sib are eligible, I'm good.
743 next
744 unless $HTML::Element::canTighten{$ptag} # parent
745 or (ref $sibs->[1]
746 and $HTML::Element::canTighten{ $sibs->[1]
747 0 ▶ {'_tag'} } # right sib
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Element::canTighten{$ptag} or ref $sibs->[1] and $HTML::Element::canTighten{$sibs->[1]{"_tag"}} 0 0 0
Condition0%0/6
$HTML::Element::canTighten{$ptag} or ref $sibs->[1] and $HTML::Element::canTighten{$sibs->[1]{"_tag"}}
l!l&&r!l&&!r
000
ref $sibs->[1] and $HTML::Element::canTighten{$sibs->[1]{"_tag"}}
!ll&&!rl&&r
000
MC/DC0%0/3
$HTML::Element::canTighten{$ptag} or ref $sibs->[1] and $HTML::Element::canTighten{$sibs->[1]{"_tag"}}
$HTML::Element::canTighten{$ptag} ref $sibs->[1] $HTML::Element::canTighten{$sibs->[1]{"_tag"}}
Truth table0%0/4
A or B and C
A= $HTML::Element::canTighten{$ptag}B= ref $sibs->[1]C= $HTML::Element::canTighten{$sibs->[1]{"_tag"}}
ABCresult
1XX1
0111
00X0
0100
748 );
749 }
750 }
751 elsif ( $i == $#$sibs ) { # I'm rightmost of many
752 # if either my parent or sib are eligible, I'm good.
753 next
754 unless $HTML::Element::canTighten{$ptag} # parent
755 or (ref $sibs->[ $i - 1 ]
756 and $HTML::Element::canTighten{ $sibs->[ $i - 1 ]
757 346 ▶ {'_tag'} } # left sib
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Element::canTighten{$ptag} or ref $$sibs[$i - 1] and $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}} 0 346 346
Condition16%1/6
$HTML::Element::canTighten{$ptag} or ref $$sibs[$i - 1] and $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
l!l&&r!l&&!r
34600
ref $$sibs[$i - 1] and $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
!ll&&!rl&&r
000
MC/DC0%0/3
$HTML::Element::canTighten{$ptag} or ref $$sibs[$i - 1] and $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
$HTML::Element::canTighten{$ptag} ref $$sibs[$i - 1] $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
Truth table25%1/4
A or B and C
A= $HTML::Element::canTighten{$ptag}B= ref $$sibs[$i - 1]C= $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
ABCresult
1XX1
0111
00X0
0100
758 );
759 }
760 else { # I'm the piggy in the middle
761 # My parent doesn't matter -- it all depends on my sibs
762 next
763 162 ▶ unless ref $sibs->[ $i - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$sibs[$i - 1] or ref $$sibs[$i + 1] 0 162 162
Condition33%1/3
ref $$sibs[$i - 1] or ref $$sibs[$i + 1]
l!l&&r!l&&!r
16200
MC/DC0%0/2
ref $$sibs[$i - 1] or ref $$sibs[$i + 1]
ref $$sibs[$i - 1] ref $$sibs[$i + 1]
Truth table33%1/3
A or B
A= ref $$sibs[$i - 1]B= ref $$sibs[$i + 1]
ABresult
1X1
011
000
764 or ref $sibs->[ $i + 1 ];
765  
766 # if NEITHER sib is a node, quit
767  
768 next if
769  
770 # bailout condition: if BOTH are INeligible nodes
771 # (as opposed to being text, or being eligible nodes)
772 ref $sibs->[ $i - 1 ]
773 and ref $sibs->[ $i + 1 ]
774 and !$HTML::Element::canTighten{ $sibs->[ $i - 1 ]
775 {'_tag'} } # left sib
776 and !$HTML::Element::canTighten{ $sibs->[ $i + 1 ]
777 162 ▶ {'_tag'} } # right sib
Branch: 1 branch
BranchTrueFalseTotal
if ref $$sibs[$i - 1] and ref $$sibs[$i + 1] and not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}} and not $HTML::Element::canTighten{$$sibs[$i + 1]{"_tag"}} 162 0 162
Condition33%3/9
ref $$sibs[$i - 1] and ref $$sibs[$i + 1] and not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}} and not $HTML::Element::canTighten{$$sibs[$i + 1]{"_tag"}}
!ll&&!rl&&r
00162
ref $$sibs[$i - 1] and ref $$sibs[$i + 1] and not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}
!ll&&!rl&&r
00162
ref $$sibs[$i - 1] and ref $$sibs[$i + 1]
!ll&&!rl&&r
00162
MC/DC0%0/4
ref $$sibs[$i - 1] and ref $$sibs[$i + 1] and not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}} and not $HTML::Element::canTighten{$$sibs[$i + 1]{"_tag"}}
ref $$sibs[$i - 1] ref $$sibs[$i + 1] not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}} not $HTML::Element::canTighten{$$sibs[$i + 1]{"_tag"}}
Truth table20%1/5
A and B and not(C) and not(D)
A= ref $$sibs[$i - 1]B= ref $$sibs[$i + 1]C= not $HTML::Element::canTighten{$$sibs[$i - 1]{"_tag"}}D= not $HTML::Element::canTighten{$$sibs[$i + 1]{"_tag"}}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
778 ;
779 }
780  
781 # Unknown tags aren't in canTighten and so AREN'T subject to tightening
782  
783 348 ▶ print " delendum: child $i of $ptag\n" if $Debug > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Element::Debug > 3 0 348 348
784 348 splice @$sibs, $i, 1;
785 }
786  
787 # end of the loop-over-children
788 }
789  
790 # end of the while loop.
791  
792 310 return;
793 }
794  
795  
796 1681 ▶sub insert_element {
Subroutine insert_element: called CC=8 SCAR=20.9
Pod documented
797 1681 my ( $self, $tag, $implicit ) = @_;
798 1681 ▶ return $self->pos() unless $tag; # noop if nothing to insert
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 0 1681 1681
799  
800 1681 my $e;
801 1681 ▶ if ( ref $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tag) { } 1001 680 1681
802 1001 $e = $tag;
803 1001 $tag = $e->tag;
804 }
805 else { # just a tag name -- so make the element
806 680 $e = $self->element_class->new($tag);
807 680 ▶ ++( $self->{'_element_count'} ) if exists $self->{'_element_count'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_element_count"} 680 0 680
808  
809 # undocumented. see TreeBuilder.
810 }
811  
812 1681 ▶ $e->{'_implicit'} = 1 if $implicit;
Branch: 1 branch
BranchTrueFalseTotal
if $implicit 680 1001 1681
813  
814 1681 my $pos = $self->{'_pos'};
815 1681 ▶ $pos = $self unless defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 656 1025 1681
816  
817 1681 $pos->push_content($e);
818  
819 $self->{'_pos'} = $pos = $e
820 1681 ▶ unless $self->_empty_element_map->{$tag} || $e->{'_empty_element'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_empty_element_map->{$tag} or $e->{"_empty_element"} 1662 19 1681
Condition66%2/3
$self->_empty_element_map->{$tag} or $e->{"_empty_element"}
l!l&&r!l&&!r
1901662
MC/DC50%1/2
$self->_empty_element_map->{$tag} or $e->{"_empty_element"}
$self->_empty_element_map->{$tag} $e->{"_empty_element"}
Truth table66%2/3
A or B
A= $self->_empty_element_map->{$tag}B= $e->{"_empty_element"}
ABresult
1X1
011
000
821  
822 1681 $pos;
823 }
824  
825 #==========================================================================
826 # Some things to override in XML::Element
827  
828 2483 ▶sub _empty_element_map {
Subroutine _empty_element_map: called CC=1 SCAR=0.0
829 2483 \%HTML::Element::emptyElement;
830 }
831  
832 3100 ▶sub _fold_case_LC {
Subroutine _fold_case_LC: called CC=2 SCAR=6.9
833 3100 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 95 3005 3100
834 95 shift;
835 95 map lc($_), @_;
836 }
837 else {
838 3005 return lc( $_[1] );
839 }
840 }
841  
842 0 ▶sub _fold_case_NOT {
Subroutine _fold_case_NOT: not called CC=2 SCAR=17.9
843 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
844 0 shift;
845 0 @_;
846 }
847 else {
848 0 return $_[1];
849 }
850 }
851  
852 23 *_fold_case = \&_fold_case_LC;
853  
854 #==========================================================================
855  
856 #=head1 DUMPING METHODS
857  
858  
859 0 ▶sub dump {
Subroutine dump: not called CC=9 SCAR=45.0
Pod documented
860 0 my ( $self, $fh, $depth ) = @_;
861 0 ▶ $fh = *STDOUT{IO} unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
862 0 ▶ $depth = 0 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
863 print $fh " " x $depth, $self->starttag, " \@", $self->address,
864 0 ▶ $self->{'_implicit'} ? " (IMPLICIT)\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_implicit'} ? : 0 0 0
865 0 for ( @{ $self->{'_content'} } ) {
866 0 ▶ if ( ref $_ ) { # element
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
867 0 $_->dump( $fh, $depth + 1 ); # recurse
868 }
869 else { # text node
870 0 print $fh " " x ( $depth + 1 );
871 0 ▶ if ( length($_) > 65 or m<[\x00-\x1F]> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > 65 or /[\x00-\x1F]/) { } 0 0 0
Condition0%0/3
length $_ > 65 or /[\x00-\x1F]/
l!l&&r!l&&!r
000
MC/DC0%0/2
length $_ > 65 or /[\x00-\x1F]/
length $_ > 65 /[\x00-\x1F]/
Truth table0%0/3
A or B
A= length $_ > 65B= /[\x00-\x1F]/
ABresult
1X1
011
000
872  
873 # it needs prettyin' up somehow or other
874 0 ▶ my $x
Branch: 1 branch
BranchTrueFalseTotal
length $_ <= 65 ? : 0 0 0
875 = ( length($_) <= 65 )
876 ? $_
877 : ( substr( $_, 0, 65 ) . '...' );
878 0 $x =~ s<([\x00-\x1F])>
879 0 <'\\x'.(unpack("H2",$1))>eg;
880 0 print $fh qq{"$x"\n};
881 }
882 else {
883 0 print $fh qq{"$_"\n};
884 }
885 }
886 }
887 }
888  
889  
890 262 ▶sub as_HTML {
Subroutine as_HTML: called CC=6 SCAR=18.1
Pod documented
891 262 my ( $self, $entities, $indent, $omissible_map ) = @_;
892  
893 #my $indent_on = defined($indent) && length($indent);
894 262 my @html = ();
895  
896 262 ▶ $omissible_map ||= \%HTML::Element::optionalEndTag;
Condition100%2/2
$omissible_map ||= \%HTML::Element::optionalEndTag
l!l
5257
MC/DC100%1/1
$omissible_map ||= \%HTML::Element::optionalEndTag
$omissible_map
Truth table100%2/2
A
A= $omissible_map
Aresult
11
00
897 262 my $empty_element_map = $self->_empty_element_map;
898  
899 262 my $last_tag_tightenable = 0;
900 262 my $this_tag_tightenable = 0;
901 262 my $nonindentable_ancestors = 0; # count of nonindentible tags over us.
902  
903 262 my ( $tag, $node, $start, $depth ); # per-iteration scratch
904  
905 262 ▶ if ( defined($indent) && length($indent) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent and length $indent) { } 15 247 262
Condition66%2/3
defined $indent and length $indent
!ll&&!rl&&r
247015
MC/DC50%1/2
defined $indent and length $indent
defined $indent length $indent
Truth table66%2/3
A and B
A= defined $indentB= length $indent
ABresult
0X0
100
111
906 $self->traverse(
907 616 ▶ sub {
Subroutine __ANON__: called CC=24 SCAR=34.1
908 616 ( $node, $start, $depth ) = @_;
909 616 ▶ if ( ref $node ) { # it's an element
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 456 160 616
910  
911 # detect bogus classes. RT #35948, #61673
912 456 ▶ $node->can('starttag')
Branch: 1 branch
BranchTrueFalseTotal
$node->can("starttag") or Carp::confess('Object of class ' . ref($node) . ' cannot be processed by HTML::Element') 456 0 456
913 or Carp::confess( "Object of class "
914 . ref($node)
915 . " cannot be processed by HTML::Element" );
916  
917 456 $tag = $node->{'_tag'};
918  
919 456 ▶ if ($start) { # on the way in
Branch: 2 branches
BranchTrueFalseTotal
if ($start) { } 229 227 456
elsif (not $empty_element_map->{$tag} || $omissible_map->{$tag}) { } 203 24 227
920 229 ▶ if (( $this_tag_tightenable
Branch: 1 branch
BranchTrueFalseTotal
if ($this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable) { } 164 65 229
Condition83%5/6
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable
!ll&&!rl&&r
2540164
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors
!ll&&!rl&&r
250204
MC/DC66%2/3
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable
$this_tag_tightenable = $HTML::Element::canTighten{$tag} not $nonindentable_ancestors $last_tag_tightenable
Truth table75%3/4
A and not(B) and C
A= $this_tag_tightenable = $HTML::Element::canTighten{$tag}B= not $nonindentable_ancestorsC= $last_tag_tightenable
ABCresult
0XX0
10X0
1100
1111
921 = $HTML::Element::canTighten{$tag}
922 )
923 and !$nonindentable_ancestors
924 and $last_tag_tightenable
925 )
926 {
927 164 push
928 @html,
929 "\n",
930 $indent x $depth,
931 $node->starttag($entities),
932 ;
933 }
934 else {
935 65 push( @html, $node->starttag($entities) );
936 }
937 229 $last_tag_tightenable = $this_tag_tightenable;
938  
939 ++$nonindentable_ancestors
940 if $tag eq 'pre' or $tag eq 'textarea'
941 229 ▶ or $HTML::Tagset::isCDATA_Parent{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "pre" or $tag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$tag} 2 227 229
Condition83%5/6
$tag eq "pre" or $tag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$tag}
l!l&&r!l&&!r
20227
$tag eq "pre" or $tag eq "textarea"
l!l&&r!l&&!r
11227
MC/DC66%2/3
$tag eq "pre" or $tag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$tag}
$tag eq "pre" $tag eq "textarea" $HTML::Tagset::isCDATA_Parent{$tag}
Truth table75%3/4
A or B or C
A= $tag eq "pre"B= $tag eq "textarea"C= $HTML::Tagset::isCDATA_Parent{$tag}
ABCresult
1XX1
01X1
0011
0000
942  
943 }
944 ▶ elsif (
Condition66%2/3
$empty_element_map->{$tag} || $omissible_map->{$tag}
l!l&&r!l&&!r
024203
MC/DC50%1/2
$empty_element_map->{$tag} || $omissible_map->{$tag}
$empty_element_map->{$tag} $omissible_map->{$tag}
Truth table66%2/3
A || B
A= $empty_element_map->{$tag}B= $omissible_map->{$tag}
ABresult
1X1
011
000
945 not( $empty_element_map->{$tag}
946 or $omissible_map->{$tag} )
947 )
948 {
949  
950 # on the way out
951 203 ▶ if ( $tag eq 'pre' or $tag eq 'textarea'
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'pre' or $tag eq 'textarea' or $HTML::Tagset::isCDATA_Parent{$tag}) { } 2 201 203
Condition83%5/6
$tag eq "pre" or $tag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$tag}
l!l&&r!l&&!r
20201
$tag eq "pre" or $tag eq "textarea"
l!l&&r!l&&!r
11201
MC/DC66%2/3
$tag eq "pre" or $tag eq "textarea" or $HTML::Tagset::isCDATA_Parent{$tag}
$tag eq "pre" $tag eq "textarea" $HTML::Tagset::isCDATA_Parent{$tag}
Truth table75%3/4
A or B or C
A= $tag eq "pre"B= $tag eq "textarea"C= $HTML::Tagset::isCDATA_Parent{$tag}
ABCresult
1XX1
01X1
0011
0000
952 or $HTML::Tagset::isCDATA_Parent{$tag} )
953 {
954 2 --$nonindentable_ancestors;
955 $last_tag_tightenable
956 2 = $HTML::Element::canTighten{$tag};
957 2 push @html, $node->endtag;
958  
959 }
960 else { # general case
961 201 ▶ if (( $this_tag_tightenable
Branch: 1 branch
BranchTrueFalseTotal
if ($this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable) { } 89 112 201
Condition83%5/6
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable
!ll&&!rl&&r
248889
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors
!ll&&!rl&&r
240177
MC/DC66%2/3
$this_tag_tightenable = $HTML::Element::canTighten{$tag} and not $nonindentable_ancestors and $last_tag_tightenable
$this_tag_tightenable = $HTML::Element::canTighten{$tag} not $nonindentable_ancestors $last_tag_tightenable
Truth table75%3/4
A and not(B) and C
A= $this_tag_tightenable = $HTML::Element::canTighten{$tag}B= not $nonindentable_ancestorsC= $last_tag_tightenable
ABCresult
0XX0
10X0
1100
1111
962 = $HTML::Element::canTighten{$tag}
963 )
964 and !$nonindentable_ancestors
965 and $last_tag_tightenable
966 )
967 {
968 89 push
969 @html,
970 "\n",
971 $indent x $depth,
972 $node->endtag,
973 ;
974 }
975 else {
976 112 push @html, $node->endtag;
977 }
978 201 $last_tag_tightenable = $this_tag_tightenable;
979  
980 #print "$tag tightenable: $this_tag_tightenable\n";
981 }
982 }
983 }
984 else { # it's a text segment
985  
986 160 $last_tag_tightenable = 0; # I guess this is right
987 HTML::Entities::encode_entities( $node, $entities )
988  
989 # That does magic things if $entities is undef.
990 unless (
991 ( defined($entities) && !length($entities) )
992  
993 # If there's no entity to encode, don't call it
994 160 ▶ || $HTML::Tagset::isCDATA_Parent{ $_[3]{'_tag'} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content 160 0 160
Condition33%3/9
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content
l!l&&r!l&&!r
00160
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}}
l!l&&r!l&&!r
00160
defined $entities and not length $entities
!ll&&!rl&&r
16000
MC/DC0%0/4
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content
defined $entities not length $entities $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} $HTML::Element::encoded_content
Truth table14%1/7
A and not(B) or C or D
A= defined $entitiesB= not length $entitiesC= $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}}D= $HTML::Element::encoded_content
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
995  
996 # To keep from amp-escaping children of script et al.
997 # That doesn't deal with descendants; but then, CDATA
998 # parents shouldn't /have/ descendants other than a
999 # text children (or comments?)
1000 || $encoded_content
1001 );
1002 160 ▶ if ($nonindentable_ancestors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nonindentable_ancestors) { } 0 160 160
1003 0 push @html, $node; # say no go
1004 }
1005 else {
1006 160 ▶ if ($last_tag_tightenable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_tag_tightenable) { } 0 160 160
1007 0 $node =~ s<[\n\r\f\t ]+>< >s;
1008  
1009 #$node =~ s< $><>s;
1010 0 $node =~ s<^ ><>s;
1011 0 push
1012 @html,
1013 "\n",
1014 $indent x $depth,
1015 $node,
1016  
1017 #Text::Wrap::wrap($indent x $depth, $indent x $depth, "\n" . $node)
1018 ;
1019 }
1020 else {
1021 160 push
1022 @html,
1023 $node,
1024  
1025 #Text::Wrap::wrap('', $indent x $depth, $node)
1026 ;
1027 }
1028 }
1029 }
1030 616 1; # keep traversing
1031 }
1032 15 ); # End of parms to traverse()
1033 }
1034 else { # no indenting -- much simpler code
1035 $self->traverse(
1036 3532 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
1037 3532 ( $node, $start ) = @_;
1038 3532 ▶ if ( ref $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 2385 1147 3532
1039  
1040 # detect bogus classes. RT #35948
1041 2385 ▶ $node->isa( $self->element_class )
Branch: 1 branch
BranchTrueFalseTotal
$node->isa($self->element_class) or Carp::confess('Object of class ' . ref($node) . ' cannot be processed by HTML::Element') 2385 0 2385
1042 or Carp::confess( "Object of class "
1043 . ref($node)
1044 . " cannot be processed by HTML::Element" );
1045  
1046 2385 $tag = $node->{'_tag'};
1047 2385 ▶ if ($start) { # on the way in
Branch: 2 branches
BranchTrueFalseTotal
if ($start) { } 1195 1190 2385
elsif (not $empty_element_map->{$tag} || $omissible_map->{$tag}) { } 1172 18 1190
1048 1195 push( @html, $node->starttag($entities) );
1049 }
1050 ▶ elsif (
Condition100%3/3
$empty_element_map->{$tag} || $omissible_map->{$tag}
l!l&&r!l&&!r
1171172
MC/DC100%2/2
$empty_element_map->{$tag} || $omissible_map->{$tag}
$empty_element_map->{$tag} $omissible_map->{$tag}
Truth table100%3/3
A || B
A= $empty_element_map->{$tag}B= $omissible_map->{$tag}
ABresult
1X1
011
000
1051 not( $empty_element_map->{$tag}
1052 or $omissible_map->{$tag} )
1053 )
1054 {
1055  
1056 # on the way out
1057 1172 push( @html, $node->endtag );
1058 }
1059 }
1060 else {
1061  
1062 # simple text content
1063 HTML::Entities::encode_entities( $node, $entities )
1064  
1065 # That does magic things if $entities is undef.
1066 unless (
1067 ( defined($entities) && !length($entities) )
1068  
1069 # If there's no entity to encode, don't call it
1070 1147 ▶ || $HTML::Tagset::isCDATA_Parent{ $_[3]{'_tag'} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content 1143 4 1147
Condition100%9/9
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content
l!l&&r!l&&!r
311143
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}}
l!l&&r!l&&!r
121144
defined $entities and not length $entities
!ll&&!rl&&r
1132141
MC/DC100%4/4
defined $entities and not length $entities or $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} or $HTML::Element::encoded_content
defined $entities not length $entities $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}} $HTML::Element::encoded_content
Truth table71%5/7
A and not(B) or C or D
A= defined $entitiesB= not length $entitiesC= $HTML::Tagset::isCDATA_Parent{$_[3]{"_tag"}}D= $HTML::Element::encoded_content
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
1071  
1072 # To keep from amp-escaping children of script et al.
1073 # That doesn't deal with descendants; but then, CDATA
1074 # parents shouldn't /have/ descendants other than a
1075 # text children (or comments?)
1076 || $encoded_content
1077 );
1078 1147 push( @html, $node );
1079 }
1080 3532 1; # keep traversing
1081 }
1082 247 ); # End of parms to traverse()
1083 }
1084  
1085 262 ▶ if ( $self->{_store_declarations} && defined $self->{_decl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_store_declarations"} and defined $self->{"_decl"}) 1 261 262
Condition100%3/3
$self->{"_store_declarations"} and defined $self->{"_decl"}
!ll&&!rl&&r
901711
MC/DC100%2/2
$self->{"_store_declarations"} and defined $self->{"_decl"}
$self->{"_store_declarations"} defined $self->{"_decl"}
Truth table100%3/3
A and B
A= $self->{"_store_declarations"}B= defined $self->{"_decl"}
ABresult
0X0
100
111
1086 1 unshift @html, sprintf "<!%s>\n", $self->{_decl}->{text};
1087 }
1088  
1089 262 return join( '', @html );
1090 }
1091  
1092  
1093 11 ▶sub as_text {
Subroutine as_text: called CC=10 SCAR=25.5
Pod documented
1094  
1095 # Yet another iteratively implemented traverser
1096 11 my ( $this, %options ) = @_;
1097 11 ▶ my $skip_dels = $options{'skip_dels'} || 0;
Condition50%1/2
$options{'skip_dels'} || 0
l!l
011
MC/DC0%0/1
$options{'skip_dels'} || 0
$options{'skip_dels'}
Truth table50%1/2
A
A= $options{'skip_dels'}
Aresult
11
00
1098 11 my (@pile) = ($this);
1099 11 my $tag;
1100 11 my $text = '';
1101 11 ▶ while (@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 27 11 38
1102 27 ▶ if ( !defined( $pile[0] ) ) { # undef!
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pile[0]) { } 0 27 27
elsif (not ref $pile[0]) { } 12 15 27
1103 # no-op
1104 }
1105 elsif ( !ref( $pile[0] ) ) { # text bit! save it!
1106 12 $text .= shift @pile;
1107 }
1108 else { # it's a ref -- traverse under it
1109 15 ▶ unshift @pile, @{ $this->{'_content'} || $nillio }
Condition66%2/3
$this->{'_content'} || $nillio
l!l&&r!l&&!r
1410
MC/DC0%0/2
$this->{'_content'} || $nillio
$this->{'_content'} $nillio
Truth table66%2/3
A || B
A= $this->{'_content'}B= $nillio
ABresult
1X1
011
000
1110 15 ▶ unless ( $tag = ( $this = shift @pile )->{'_tag'} ) eq 'style'
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag = +($this = shift @pile)->{"_tag"}) eq "style" or $tag eq "script" or $skip_dels and $tag eq "del" 15 0 15
Condition33%3/9
($tag = +($this = shift @pile)->{"_tag"}) eq "style" or $tag eq "script" or $skip_dels and $tag eq "del"
l!l&&r!l&&!r
0015
($tag = +($this = shift @pile)->{"_tag"}) eq "style" or $tag eq "script"
l!l&&r!l&&!r
0015
$skip_dels and $tag eq "del"
!ll&&!rl&&r
1500
MC/DC0%0/4
($tag = +($this = shift @pile)->{"_tag"}) eq "style" or $tag eq "script" or $skip_dels and $tag eq "del"
($tag = +($this = shift @pile)->{"_tag"}) eq "style" $tag eq "script" $skip_dels $tag eq "del"
Truth table20%1/5
A or B or C and D
A= ($tag = +($this = shift @pile)->{"_tag"}) eq "style"B= $tag eq "script"C= $skip_delsD= $tag eq "del"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
1111 or $tag eq 'script'
1112 or ( $skip_dels and $tag eq 'del' );
1113 }
1114 }
1115 11 return $text;
1116 }
1117  
1118 # extra_chars added for RT #26436
1119 7 ▶sub as_trimmed_text {
Subroutine as_trimmed_text: called CC=2 SCAR=6.9
Pod documented
1120 7 my ( $this, %options ) = @_;
1121 7 my $text = $this->as_text(%options);
1122 my $extra_chars = defined $options{'extra_chars'}
1123 7 ▶ ? $options{'extra_chars'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'extra_chars'} ? : 3 4 7
1124  
1125 7 $text =~ s/[\n\r\f\t$extra_chars ]+$//s;
1126 7 $text =~ s/^[\n\r\f\t$extra_chars ]+//s;
1127 7 $text =~ s/[\n\r\f\t$extra_chars ]+/ /g;
1128 7 return $text;
1129 }
1130  
1131 1 ▶sub as_text_trimmed { shift->as_trimmed_text(@_) } # alias, because I forget
Subroutine as_text_trimmed: called CC=1 SCAR=0.0
Pod undocumented
1132  
1133  
1134 # TODO: make it wrap, if not indent?
1135  
1136 115 ▶sub as_XML {
Subroutine as_XML: called CC=1 SCAR=0.0
Pod documented
1137  
1138 # based an as_HTML
1139 115 my ($self) = @_;
1140  
1141 #my $indent_on = defined($indent) && length($indent);
1142 115 my @xml = ();
1143 115 my $empty_element_map = $self->_empty_element_map;
1144  
1145 115 my ( $tag, $node, $start ); # per-iteration scratch
1146 $self->traverse(
1147 1374 ▶ sub {
Subroutine __ANON__: called CC=12 SCAR=25.1
1148 1374 ( $node, $start ) = @_;
1149 1374 ▶ if ( ref $node ) { # it's an element
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 1164 210 1374
1150 1164 $tag = $node->{'_tag'};
1151 1164 ▶ if ($start) { # on the way in
Branch: 1 branch
BranchTrueFalseTotal
if ($start) { } 589 575 1164
1152  
1153 589 foreach my $attr ( $node->all_attr_names() ) {
1154 3680 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
unless $attr eq "/" or $self->_valid_name($attr) 1 3679 3680
Condition100%3/3
$attr eq "/" or $self->_valid_name($attr)
l!l&&r!l&&!r
136781
MC/DC100%2/2
$attr eq "/" or $self->_valid_name($attr)
$attr eq "/" $self->_valid_name($attr)
Truth table100%3/3
A or B
A= $attr eq "/"B= $self->_valid_name($attr)
ABresult
1X1
011
000
1155 "$tag has an invalid attribute name '$attr'")
1156 unless ( $attr eq '/' || $self->_valid_name($attr) );
1157 }
1158  
1159 588 ▶ if ( $empty_element_map->{$tag}
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_element_map->{$tag} and not @{$node->{'_content'} or $nillio;}) { } 11 577 588
Condition100%3/3
$empty_element_map->{$tag} and not @{$node->{"_content"} or $nillio;}
!ll&&!rl&&r
576111
MC/DC100%2/2
$empty_element_map->{$tag} and not @{$node->{"_content"} or $nillio;}
$empty_element_map->{$tag} not @{$node->{"_content"} or $nillio;}
Truth table100%3/3
A and not(B)
A= $empty_element_map->{$tag}B= not @{$node->{"_content"} or $nillio;}
ABresult
0X0
100
111
1160 12 ▶ and !@{ $node->{'_content'} || $nillio } )
Condition66%2/3
$node->{'_content'} || $nillio
l!l&&r!l&&!r
1110
MC/DC0%0/2
$node->{'_content'} || $nillio
$node->{'_content'} $nillio
Truth table66%2/3
A || B
A= $node->{'_content'}B= $nillio
ABresult
1X1
011
000
1161 {
1162 11 push( @xml, $node->starttag_XML( undef, 1 ) );
1163 }
1164 else {
1165 577 push( @xml, $node->starttag_XML(undef) );
1166 }
1167 }
1168 else { # on the way out
1169 575 ▶ unless ( $empty_element_map->{$tag}
Branch: 1 branch
BranchTrueFalseTotal
unless ($empty_element_map->{$tag} and not @{$node->{"_content"} or $nillio;}) 575 0 575
Condition66%2/3
$empty_element_map->{$tag} and not @{$node->{"_content"} or $nillio;}
!ll&&!rl&&r
57410
MC/DC0%0/2
$empty_element_map->{$tag} and not @{$node->{"_content"} or $nillio;}
$empty_element_map->{$tag} not @{$node->{"_content"} or $nillio;}
Truth table66%2/3
A and not(B)
A= $empty_element_map->{$tag}B= not @{$node->{"_content"} or $nillio;}
ABresult
0X0
100
111
1170 1 ▶ and !@{ $node->{'_content'} || $nillio } )
Condition33%1/3
$node->{'_content'} || $nillio
l!l&&r!l&&!r
100
MC/DC0%0/2
$node->{'_content'} || $nillio
$node->{'_content'} $nillio
Truth table33%1/3
A || B
A= $node->{'_content'}B= $nillio
ABresult
1X1
011
000
1171 {
1172 575 push( @xml, $node->endtag_XML() );
1173 } # otherwise it will have been an <... /> tag.
1174 }
1175 }
1176 else { # it's just text
1177 210 _xml_escape($node);
1178 210 push( @xml, $node );
1179 }
1180 1373 1; # keep traversing
1181 }
1182 115 );
1183  
1184 114 join( '', @xml, "\n" );
1185 }
1186  
1187 255 ▶sub _xml_escape {
Subroutine _xml_escape: called CC=3 SCAR=11.0
1188  
1189 # DESTRUCTIVE (a.k.a. "in-place")
1190 # Five required escapes: http://www.w3.org/TR/2006/REC-xml11-20060816/#syntax
1191 # We allow & if it's part of a valid escape already: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-references
1192 255 foreach my $x (@_) {
1193  
1194 # In strings with no encoded entities all & should be encoded.
1195 255 ▶ if ($encoded_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Element::encoded_content) { } 26 229 255
1196 26 $x
1197 =~ s/&(?! # An ampersand that isn't followed by...
1198 (\#\d+; | # A hash mark, digits and semicolon, or
1199 \#x[\da-f]+; | # A hash mark, "x", hex digits and semicolon, or
1200 $START_CHAR$NAME_CHAR+; ) # A valid unicode entity name and semicolon
1201 )/&amp;/gx; # Needs to be escaped to amp
1202 }
1203 else {
1204 229 $x =~ s/&/&amp;/g;
1205 }
1206  
1207 # simple character escapes
1208 255 $x =~ s/</&lt;/g;
1209 255 $x =~ s/>/&gt;/g;
1210 255 $x =~ s/"/&quot;/g;
1211 255 $x =~ s/'/&apos;/g;
1212 }
1213 255 return;
1214 }
1215  
1216  
1217 # NOTES:
1218 #
1219 # It's been suggested that attribute names be made :-keywords:
1220 # (:_tag "img" :border 0 :src "pie.png" :usemap "#main.map")
1221 # However, it seems that Scheme has no such data type as :-keywords.
1222 # So, for the moment at least, I tend toward simplicity, uniformity,
1223 # and universality, where everything a string or a list.
1224  
1225 1 ▶sub as_Lisp_form {
Subroutine as_Lisp_form: called CC=1 SCAR=0.1
Pod documented
1226 1 my @out;
1227  
1228 my $sub;
1229 1 my $depth = 0;
1230 1 my ( @list, $val );
1231 1 ▶ $sub = sub { # Recursor
Subroutine __ANON__: called CC=13 SCAR=26.9
1232 1 my $self = $_[0];
1233 1 @list = ( '_tag', $self->{'_tag'} );
1234 1 ▶ @list = () unless defined $list[-1]; # unlikely
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list[-1] 0 1 1
1235  
1236 1 for ( sort keys %$self ) { # predictable ordering
1237 next
1238 4 ▶ if $_ eq '_content'
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_content" or $_ eq "_tag" or $_ eq "_parent" or $_ eq "/" 3 1 4
Condition88%8/9
$_ eq "_content" or $_ eq "_tag" or $_ eq "_parent" or $_ eq "/"
l!l&&r!l&&!r
301
$_ eq "_content" or $_ eq "_tag" or $_ eq "_parent"
l!l&&r!l&&!r
211
$_ eq "_content" or $_ eq "_tag"
l!l&&r!l&&!r
112
MC/DC75%3/4
$_ eq "_content" or $_ eq "_tag" or $_ eq "_parent" or $_ eq "/"
$_ eq "_content" $_ eq "_tag" $_ eq "_parent" $_ eq "/"
Truth table80%4/5
A or B or C or D
A= $_ eq "_content"B= $_ eq "_tag"C= $_ eq "_parent"D= $_ eq "/"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1239 or $_ eq '_tag'
1240 or $_ eq '_parent'
1241 or $_ eq '/';
1242  
1243 # Leave the other private attributes, I guess.
1244 push @list, $_, $val
1245 1 ▶ if defined( $val = $self->{$_} ); # and !ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined($val = $self->{$_}) 1 0 1
1246 }
1247  
1248 1 for (@list) {
1249  
1250 # octal-escape it
1251 4 s<([^\x20\x21\x23\x27-\x5B\x5D-\x7E])>
1252 0 <sprintf('\\%03o',ord($1))>eg;
1253 4 $_ = qq{"$_"};
1254 }
1255 1 push @out, ( ' ' x $depth ) . '(' . join ' ', splice @list;
1256 1 ▶ if ( @{ $self->{'_content'} || $nillio } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'_content'} or $nillio;}) { } 1 0 1
Condition33%1/3
$self->{'_content'} || $nillio
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'_content'} || $nillio
$self->{'_content'} $nillio
Truth table33%1/3
A || B
A= $self->{'_content'}B= $nillio
ABresult
1X1
011
000
1257 1 $out[-1] .= " \"_content\" (\n";
1258 1 ++$depth;
1259 1 foreach my $c ( @{ $self->{'_content'} } ) {
1260 1 ▶ if ( ref($c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 0 1 1
1261  
1262 # an element -- recurse
1263 0 $sub->($c);
1264 }
1265 else {
1266  
1267 # a text segment -- stick it in and octal-escape it
1268 1 push @out, $c;
1269 1 $out[-1] =~ s<([^\x20\x21\x23\x27-\x5B\x5D-\x7E])>
1270 0 <sprintf('\\%03o',ord($1))>eg;
1271  
1272 # And quote and indent it.
1273 1 $out[-1] .= "\"\n";
1274 1 $out[-1] = ( ' ' x $depth ) . '"' . $out[-1];
1275 }
1276 }
1277 1 --$depth;
1278 1 substr( $out[-1], -1 )
1279 = "))\n"; # end of _content and of the element
1280 }
1281 else {
1282 0 $out[-1] .= ")\n";
1283 }
1284 1 return;
1285 1 };
1286  
1287 1 $sub->( $_[0] );
1288 1 undef $sub;
1289 1 return join '', @out;
1290 }
1291  
1292  
1293 0 ▶sub format {
Subroutine format: not called CC=2 SCAR=17.9
Pod documented
1294 0 my ( $self, $formatter ) = @_;
1295 0 ▶ unless ( defined $formatter ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $formatter) 0 0 0
1296 # RECOMMEND PREREQ: HTML::FormatText
1297 0 require HTML::FormatText;
1298 0 $formatter = HTML::FormatText->new();
1299 }
1300 0 $formatter->format($self);
1301 }
1302  
1303  
1304 1425 ▶sub starttag {
Subroutine starttag: called CC=26 SCAR=39.7
Pod documented
1305 1425 my ( $self, $entities ) = @_;
1306  
1307 1425 my $name = $self->{'_tag'};
1308  
1309 1425 ▶ return $self->{'text'} if $name eq '~literal';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~literal" 0 1425 1425
1310 1425 ▶ return "<!" . $self->{'text'} . ">" if $name eq '~declaration';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~declaration" 0 1425 1425
1311 1425 ▶ return "<?" . $self->{'text'} . ">" if $name eq '~pi';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~pi" 0 1425 1425
1312  
1313 1425 ▶ if ( $name eq '~comment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~comment") 0 1425 1425
1314 0 ▶ if ( ref( $self->{'text'} || '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self->{'text'} || '') eq 'ARRAY') { } 0 0 0
Condition0%0/2
$self->{'text'} || ''
l!l
00
MC/DC0%0/1
$self->{'text'} || ''
$self->{'text'}
Truth table0%0/2
A
A= $self->{'text'}
Aresult
11
00
1315  
1316 # Does this ever get used? And is this right?
1317 return
1318 "<!"
1319 0 . join( ' ', map( "--$_--", @{ $self->{'text'} } ) ) . ">";
1320 }
1321 else {
1322 0 return "<!--" . $self->{'text'} . "-->";
1323 }
1324 }
1325  
1326 1425 ▶ my $tag = $html_uc ? "<\U$name" : "<\L$name";
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Element::html_uc ? : 0 1425 1425
1327 1425 my $val;
1328 1425 for ( sort keys %$self ) { # predictable ordering
1329 8127 ▶ next if !length $_ or m/^_/s or $_ eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if not length $_ or /^_/s or $_ eq "/" 7640 487 8127
Condition83%5/6
not length $_ or /^_/s or $_ eq "/"
l!l&&r!l&&!r
76382487
not length $_ or /^_/s
l!l&&r!l&&!r
07638489
MC/DC66%2/3
not length $_ or /^_/s or $_ eq "/"
not length $_ /^_/s $_ eq "/"
Truth table75%3/4
not(A) or B or C
A= not length $_B= /^_/sC= $_ eq "/"
ABCresult
1XX1
01X1
0011
0000
1330 487 $val = $self->{$_};
1331 487 ▶ next if !defined $val; # or ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 487 487
1332 487 ▶ if ($_ eq $val && # if attribute is boolean, for this element
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq $val and exists $HTML::Element::boolean_attr{$name} and ref $HTML::Element::boolean_attr{$name} ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_) { } 0 487 487
ref $HTML::Element::boolean_attr{$name} ? : 1 0 1
Condition83%5/6
$_ eq $val and exists $HTML::Element::boolean_attr{$name} and ref $HTML::Element::boolean_attr{$name} ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_
!ll&&!rl&&r
48610
$_ eq $val and exists $HTML::Element::boolean_attr{$name}
!ll&&!rl&&r
3371491
MC/DC0%0/3
$_ eq $val and exists $HTML::Element::boolean_attr{$name} and ref $HTML::Element::boolean_attr{$name} ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_
$_ eq $val exists $HTML::Element::boolean_attr{$name} ref $HTML::Element::boolean_attr{$name} ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_
Truth table75%3/4
A and B and C
A= $_ eq $valB= exists $HTML::Element::boolean_attr{$name}C= ref $HTML::Element::boolean_attr{$name} ? $HTML::Element::boolean_attr{$name}{$_} : $HTML::Element::boolean_attr{$name} eq $_
ABCresult
0XX0
10X0
1100
1111
1333 exists( $HTML::Element::boolean_attr{$name} )
1334 && (ref( $HTML::Element::boolean_attr{$name} )
1335 ? $HTML::Element::boolean_attr{$name}{$_}
1336 : $HTML::Element::boolean_attr{$name} eq $_
1337 )
1338 )
1339 {
1340 0 ▶ $tag .= $html_uc ? " \U$_" : " \L$_";
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Element::html_uc ? : 0 0 0
1341 }
1342 else { # non-boolean attribute
1343  
1344 487 ▶ if ( ref $val eq 'HTML::Element'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'HTML::Element' and $val->{'_tag'} eq '~literal') { } 1 486 487
Condition66%2/3
ref $val eq "HTML::Element" and $val->{"_tag"} eq "~literal"
!ll&&!rl&&r
48601
MC/DC50%1/2
ref $val eq "HTML::Element" and $val->{"_tag"} eq "~literal"
ref $val eq "HTML::Element" $val->{"_tag"} eq "~literal"
Truth table66%2/3
A and B
A= ref $val eq "HTML::Element"B= $val->{"_tag"} eq "~literal"
ABresult
0X0
100
111
1345 and $val->{_tag} eq '~literal' )
1346 {
1347 1 $val = $val->{text};
1348 }
1349 else {
1350 486 ▶ HTML::Entities::encode_entities( $val, $entities )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entities and not length $entities or $HTML::Element::encoded_content 486 0 486
Condition33%2/6
defined $entities and not length $entities or $HTML::Element::encoded_content
l!l&&r!l&&!r
00486
defined $entities and not length $entities
!ll&&!rl&&r
48600
MC/DC0%0/3
defined $entities and not length $entities or $HTML::Element::encoded_content
defined $entities not length $entities $HTML::Element::encoded_content
Truth table20%1/5
A and not(B) or C
A= defined $entitiesB= not length $entitiesC= $HTML::Element::encoded_content
ABCresult
11X1
0X11
1011
0X00
1000
1351 unless (
1352 defined($entities) && !length($entities)
1353 || $encoded_content
1354  
1355 );
1356 }
1357  
1358 487 $val = qq{"$val"};
1359 487 ▶ $tag .= $html_uc ? qq{ \U$_\E=$val} : qq{ \L$_\E=$val};
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Element::html_uc ? : 0 487 487
1360 }
1361 } # for keys
1362 1425 ▶ if ( scalar $self->content_list == 0
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->content_list == 0 and $self->_empty_element_map->{$self->tag}) { } 7 1418 1425
Condition100%3/3
scalar $self->content_list == 0 and $self->_empty_element_map->{$self->tag}
!ll&&!rl&&r
1397217
MC/DC100%2/2
scalar $self->content_list == 0 and $self->_empty_element_map->{$self->tag}
scalar $self->content_list == 0 $self->_empty_element_map->{$self->tag}
Truth table100%3/3
A and B
A= scalar $self->content_list == 0B= $self->_empty_element_map->{$self->tag}
ABresult
0X0
100
111
1363 && $self->_empty_element_map->{ $self->tag } )
1364 {
1365 7 return $tag . " />";
1366 }
1367 else {
1368 1418 return $tag . ">";
1369 }
1370 }
1371  
1372  
1373 588 ▶sub starttag_XML {
Subroutine starttag_XML: called CC=13 SCAR=26.7
Pod documented
1374 588 my ($self) = @_;
1375  
1376 # and a third parameter to signal emptiness?
1377  
1378 588 my $name = $self->{'_tag'};
1379  
1380 588 ▶ return $self->{'text'} if $name eq '~literal';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~literal" 0 588 588
1381 588 ▶ return '<!' . $self->{'text'} . '>' if $name eq '~declaration';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~declaration" 0 588 588
1382 588 ▶ return "<?" . $self->{'text'} . "?>" if $name eq '~pi';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "~pi" 0 588 588
1383  
1384 588 ▶ if ( $name eq '~comment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~comment") 1 587 588
1385 1 ▶ if ( ref( $self->{'text'} || '' ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self->{'text'} || '') eq 'ARRAY') { } 0 1 1
Condition50%1/2
$self->{'text'} || ''
l!l
10
MC/DC0%0/1
$self->{'text'} || ''
$self->{'text'}
Truth table50%1/2
A
A= $self->{'text'}
Aresult
11
00
1386  
1387 # Does this ever get used? And is this right?
1388 0 $name = join( ' ', @{ $self->{'text'} } );
1389 }
1390 else {
1391 1 $name = $self->{'text'};
1392 }
1393 1 $name =~ s/--/-&#45;/g; # can't have double --'s in XML comments
1394 1 return "<!--$name-->";
1395 }
1396  
1397 587 my $tag = "<$name";
1398 587 my $val;
1399 587 for ( sort keys %$self ) { # predictable ordering
1400 3675 ▶ next if !length $_ or m/^_/s or $_ eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if not length $_ or /^_/s or $_ eq "/" 3656 19 3675
Condition83%5/6
not length $_ or /^_/s or $_ eq "/"
l!l&&r!l&&!r
3655119
not length $_ or /^_/s
l!l&&r!l&&!r
0365520
MC/DC66%2/3
not length $_ or /^_/s or $_ eq "/"
not length $_ /^_/s $_ eq "/"
Truth table75%3/4
not(A) or B or C
A= not length $_B= /^_/sC= $_ eq "/"
ABCresult
1XX1
01X1
0011
0000
1401  
1402 # Hm -- what to do if val is undef?
1403 # I suppose that shouldn't ever happen.
1404 19 ▶ next if !defined( $val = $self->{$_} ); # or ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined($val = $self->{$_}) 0 19 19
1405 19 _xml_escape($val);
1406 19 $tag .= qq{ $_="$val"};
1407 }
1408 587 ▶ @_ == 3 ? "$tag />" : "$tag>";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 10 577 587
1409 }
1410  
1411  
1412 1375 ▶sub endtag {
Subroutine endtag: called CC=2 SCAR=7.6
Pod documented
1413 1375 ▶ $html_uc ? "</\U$_[0]->{'_tag'}>" : "</\L$_[0]->{'_tag'}>";
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Element::html_uc ? : 0 1375 1375
1414 }
1415  
1416 575 ▶sub endtag_XML {
Subroutine endtag_XML: called CC=1 SCAR=0.0
Pod documented
1417 575 "</$_[0]->{'_tag'}>";
1418 }
1419  
1420 #==========================================================================
1421 # This, ladies and germs, is an iterative implementation of a
1422 # recursive algorithm. DON'T TRY THIS AT HOME.
1423 # Basically, the algorithm says:
1424 #
1425 # To traverse:
1426 # 1: pre-order visit this node
1427 # 2: traverse any children of this node
1428 # 3: post-order visit this node, unless it's a text segment,
1429 # or a prototypically empty node (like "br", etc.)
1430 # Add to that the consideration of the callbacks' return values,
1431 # so you can block visitation of the children, or siblings, or
1432 # abort the whole excursion, etc.
1433 #
1434 # So, why all this hassle with making the code iterative?
1435 # It makes for real speed, because it eliminates the whole
1436 # hassle of Perl having to allocate scratch space for each
1437 # instance of the recursive sub. Since the algorithm
1438 # is basically simple (and not all recursive ones are!) and
1439 # has few necessary lexicals (basically just the current node's
1440 # content list, and the current position in it), it was relatively
1441 # straightforward to store that information not as the frame
1442 # of a sub, but as a stack, i.e., a simple Perl array (well, two
1443 # of them, actually: one for content-listrefs, one for indexes of
1444 # current position in each of those).
1445  
1446 23 my $NIL = [];
1447  
1448 396 ▶sub traverse {
Subroutine traverse: called CC=54 SCAR=56.5
Pod documented
1449 396 my ( $start, $callback, $ignore_text ) = @_;
1450  
1451 396 ▶ Carp::croak "traverse can be called only as an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $start 0 396 396
1452 unless ref $start;
1453  
1454 396 ▶ Carp::croak('must provide a callback for traverse()!')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback and ref $callback 0 396 396
Condition33%1/3
defined $callback and ref $callback
!ll&&!rl&&r
00396
MC/DC0%0/2
defined $callback and ref $callback
defined $callback ref $callback
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback
ABresult
0X0
100
111
1455 unless defined $callback and ref $callback;
1456  
1457 # Elementary type-checking:
1458 396 my ( $c_pre, $c_post );
1459 396 ▶ if ( UNIVERSAL::isa( $callback, 'CODE' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($callback, 'CODE')) { } 377 19 396
elsif (UNIVERSAL::isa($callback, 'ARRAY')) { } 19 0 19
1460 377 $c_pre = $c_post = $callback;
1461 }
1462 elsif ( UNIVERSAL::isa( $callback, 'ARRAY' ) ) {
1463 19 ( $c_pre, $c_post ) = @$callback;
1464 19 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if $c_pre and not UNIVERSAL::isa($c_pre, "CODE") 0 19 19
Condition33%1/3
$c_pre and not UNIVERSAL::isa($c_pre, "CODE")
!ll&&!rl&&r
0190
MC/DC0%0/2
$c_pre and not UNIVERSAL::isa($c_pre, "CODE")
$c_pre not UNIVERSAL::isa($c_pre, "CODE")
Truth table33%1/3
A and not(B)
A= $c_preB= not UNIVERSAL::isa($c_pre, "CODE")
ABresult
0X0
100
111
1465 "pre-order callback \"$c_pre\" is true but not a coderef!")
1466 if $c_pre and not UNIVERSAL::isa( $c_pre, 'CODE' );
1467 19 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if $c_post and not UNIVERSAL::isa($c_post, "CODE") 0 19 19
Condition33%1/3
$c_post and not UNIVERSAL::isa($c_post, "CODE")
!ll&&!rl&&r
1900
MC/DC0%0/2
$c_post and not UNIVERSAL::isa($c_post, "CODE")
$c_post not UNIVERSAL::isa($c_post, "CODE")
Truth table33%1/3
A and not(B)
A= $c_postB= not UNIVERSAL::isa($c_post, "CODE")
ABresult
0X0
100
111
1468 "pre-order callback \"$c_post\" is true but not a coderef!")
1469 if $c_post and not UNIVERSAL::isa( $c_post, 'CODE' );
1470 19 ▶ return $start unless $c_pre or $c_post;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_pre or $c_post 0 19 19
Condition33%1/3
$c_pre or $c_post
l!l&&r!l&&!r
1900
MC/DC0%0/2
$c_pre or $c_post
$c_pre $c_post
Truth table33%1/3
A or B
A= $c_preB= $c_post
ABresult
1X1
011
000
1471  
1472 # otherwise there'd be nothing to actually do!
1473 }
1474 else {
1475 0 ▶ Carp::croak("$callback is not a known kind of reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback 0 0 0
1476 unless ref($callback);
1477 }
1478  
1479 396 my $empty_element_map = $start->_empty_element_map;
1480  
1481 396 my (@C) = [$start]; # a stack containing lists of children
1482 396 my (@I) = (-1); # initial value must be -1 for each list
1483 # a stack of indexes to current position in corresponding lists in @C
1484 # In each of these, 0 is the active point
1485  
1486 # scratch:
1487 396 my ($rv, # return value of callback
1488 $this, # current node
1489 $content_r, # child list of $this
1490 );
1491  
1492 # THE BIG LOOP
1493 396 ▶ while (@C) {
Branch: 1 branch
BranchTrueFalseTotal
if (@C) 6147 376 6523
1494  
1495 # Move to next item in this frame
1496 6147 ▶ if ( !defined( $I[0] ) or ++$I[0] >= @{ $C[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $I[0] or ++$I[0] >= @{$C[0];}) 2432 3715 6147
Condition66%2/3
not defined $I[0] or ++$I[0] >= @{$C[0];}
l!l&&r!l&&!r
024323715
MC/DC50%1/2
not defined $I[0] or ++$I[0] >= @{$C[0];}
not defined $I[0] ++$I[0] >= @{$C[0];}
Truth table66%2/3
not(A) or B
A= not defined $I[0]B= ++$I[0] >= @{$C[0];}
ABresult
1X1
011
000
1497  
1498 # We either went off the end of this list, or aborted the list
1499 # So call the post-order callback:
1500 2432 ▶ if ( $c_post
Branch: 1 branch
BranchTrueFalseTotal
if ($c_post and defined $I[0] and @C > 1 and defined($this = $C[1][$I[1]]) and ref $this and not $this->{'_empty_element'} || $$empty_element_map{$this->{'_tag'} or ''} && !@{$this->{'_content'};}) { } 1992 440 2432
Condition65%15/23
$c_post and defined $I[0] and @C > 1 and defined($this = $C[1][$I[1]]) and ref $this and not $this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""} && !@{$this->{"_content"};}
!ll&&!rl&&r
44001992
$c_post and defined $I[0] and @C > 1 and defined($this = $C[1][$I[1]]) and ref $this
!ll&&!rl&&r
44001992
$c_post and defined $I[0] and @C > 1 and defined($this = $C[1][$I[1]])
!ll&&!rl&&r
44001992
$c_post and defined $I[0] and @C > 1
!ll&&!rl&&r
643761992
$c_post and defined $I[0]
!ll&&!rl&&r
6402368
$this->{'_empty_element'} || $$empty_element_map{$this->{'_tag'} or ''} && !@{$this->{'_content'};}
l!l&&r!l&&!r
001992
$$empty_element_map{$this->{"_tag"} or ""} and not @{$this->{"_content"};}
!ll&&!rl&&r
1980120
$this->{'_tag'} || ''
l!l
19920
MC/DC25%2/8
$c_post and defined $I[0] and @C > 1 and defined($this = $C[1][$I[1]]) and ref $this and not $this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""} && !@{$this->{"_content"};}
$c_post defined $I[0] @C > 1 defined($this = $C[1][$I[1]]) ref $this $this->{'_empty_element'} $$empty_element_map{$this->{"_tag"} or ""} not @{$this->{"_content"};}
MC/DC0%0/1
$this->{'_tag'} || ''
$this->{'_tag'}
Truth table44%4/9
A and B and C and D and E and not(F || G and not(H))
A= $c_postB= defined $I[0]C= @C > 1D= defined($this = $C[1][$I[1]])E= ref $thisF= $this->{'_empty_element'}G= $$empty_element_map{$this->{"_tag"} or ""}H= not @{$this->{"_content"};}
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111111XX0
111110110
1111100X1
111110101
Truth table50%1/2
A
A= $this->{'_tag'}
Aresult
11
00
1501 and defined $I[0]
1502 and @C > 1
1503  
1504 # to keep the next line from autovivifying
1505 and defined( $this = $C[1][ $I[1] ] ) # sanity, and
1506 # suppress callbacks on exiting the fictional top frame
1507 and ref($this) # sanity
1508 and not(
1509 $this->{'_empty_element'}
1510 || ( $empty_element_map->{ $this->{'_tag'} || '' }
1511 12 && !@{ $this->{'_content'} } ) # RT #49932
1512 ) # things that don't get post-order callbacks
1513 )
1514 {
1515 1992 shift @I;
1516 1992 shift @C;
1517  
1518 #print "Post! at depth", scalar(@I), "\n";
1519 1992 $rv = $c_post->(
1520  
1521 #map $_, # copy to avoid any messiness
1522 $this, # 0: this
1523 0, # 1: startflag (0 for post-order call)
1524 @I - 1, # 2: depth
1525 );
1526  
1527 1992 ▶ if ( defined($rv) and ref($rv) eq $travsignal_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv and ref $rv eq $travsignal_package) 0 1992 1992
Condition33%1/3
defined $rv and ref $rv eq $travsignal_package
!ll&&!rl&&r
019920
MC/DC0%0/2
defined $rv and ref $rv eq $travsignal_package
defined $rv ref $rv eq $travsignal_package
Truth table33%1/3
A and B
A= defined $rvB= ref $rv eq $travsignal_package
ABresult
0X0
100
111
1528 0 $rv = $$rv; #deref
1529 0 ▶ if ( $rv eq 'ABORT' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($rv eq 'ABORT') { } 0 0 0
elsif ($rv eq 'PRUNE') { } 0 0 0
elsif ($rv eq 'PRUNE_SOFTLY') { } 0 0 0
elsif ($rv eq 'OK') { } 0 0 0
elsif ($rv eq 'PRUNE_UP') { } 0 0 0
1530 0 last; # end of this excursion!
1531 }
1532 elsif ( $rv eq 'PRUNE' ) {
1533  
1534 # NOOP on post!!
1535 }
1536 elsif ( $rv eq 'PRUNE_SOFTLY' ) {
1537  
1538 # NOOP on post!!
1539 }
1540 elsif ( $rv eq 'OK' ) {
1541  
1542 # noop
1543 }
1544 elsif ( $rv eq 'PRUNE_UP' ) {
1545 0 $I[0] = undef;
1546 }
1547 else {
1548 0 die "Unknown travsignal $rv\n";
1549  
1550 # should never happen
1551 }
1552 }
1553 }
1554 else {
1555 440 shift @I;
1556 440 shift @C;
1557 }
1558 2432 next;
1559 }
1560  
1561 3715 $this = $C[0][ $I[0] ];
1562  
1563 3715 ▶ if ($c_pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_pre) 3715 0 3715
1564 3715 ▶ if ( defined $this and ref $this ) { # element
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this and ref $this) { } 2134 1581 3715
Condition66%2/3
defined $this and ref $this
!ll&&!rl&&r
015812134
MC/DC50%1/2
defined $this and ref $this
defined $this ref $this
Truth table66%2/3
A and B
A= defined $thisB= ref $this
ABresult
0X0
100
111
1565 2134 $rv = $c_pre->(
1566  
1567 #map $_, # copy to avoid any messiness
1568 $this, # 0: this
1569 1, # 1: startflag (1 for pre-order call)
1570 @I - 1, # 2: depth
1571 );
1572 }
1573 else { # text segment
1574 1581 ▶ next if $ignore_text;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_text 64 1517 1581
1575 1517 $rv = $c_pre->(
1576  
1577 #map $_, # copy to avoid any messiness
1578 $this, # 0: this
1579 1, # 1: startflag (1 for pre-order call)
1580 @I - 1, # 2: depth
1581 $C[1][ $I[1] ], # 3: parent
1582 # And there will always be a $C[1], since
1583 # we can't start traversing at a text node
1584 $I[0] # 4: index of self in parent's content list
1585 );
1586 }
1587 3650 ▶ if ( not $rv ) { # returned false. Same as PRUNE.
Branch: 2 branches
BranchTrueFalseTotal
if (not $rv) { } 0 3650 3650
elsif (ref $rv eq $travsignal_package) { } 19 3631 3650
1588 0 next; # prune
1589 }
1590 elsif ( ref($rv) eq $travsignal_package ) {
1591 19 $rv = $$rv; # deref
1592 19 ▶ if ( $rv eq 'ABORT' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($rv eq 'ABORT') { } 19 0 19
elsif ($rv eq 'PRUNE') { } 0 0 0
elsif ($rv eq 'PRUNE_SOFTLY') { } 0 0 0
elsif ($rv eq 'OK') { } 0 0 0
elsif ($rv eq 'PRUNE_UP') { } 0 0 0
1593 19 last; # end of this excursion!
1594 }
1595 elsif ( $rv eq 'PRUNE' ) {
1596 0 next;
1597 }
1598 elsif ( $rv eq 'PRUNE_SOFTLY' ) {
1599 0 ▶ if (ref($this)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this and not $this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""}) 0 0 0
Condition0%0/8
ref $this and not $this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""}
!ll&&!rl&&r
000
$this->{'_empty_element'} || $$empty_element_map{$this->{'_tag'} or ''}
l!l&&r!l&&!r
000
$this->{'_tag'} || ''
l!l
00
MC/DC0%0/3
ref $this and not $this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""}
ref $this $this->{'_empty_element'} $$empty_element_map{$this->{'_tag'} or ''}
MC/DC0%0/1
$this->{'_tag'} || ''
$this->{'_tag'}
Truth table0%0/4
A and not(B || C)
A= ref $thisB= $this->{'_empty_element'}C= $$empty_element_map{$this->{'_tag'} or ''}
ABCresult
0XX0
11X0
1010
1001
Truth table0%0/2
A
A= $this->{'_tag'}
Aresult
11
00
1600 and not( $this->{'_empty_element'}
1601 || $empty_element_map->{ $this->{'_tag'} || '' } )
1602 )
1603 {
1604  
1605 # push a dummy empty content list just to trigger a post callback
1606 0 unshift @I, -1;
1607 0 unshift @C, $NIL;
1608 }
1609 0 next;
1610 }
1611 elsif ( $rv eq 'OK' ) {
1612  
1613 # noop
1614 }
1615 elsif ( $rv eq 'PRUNE_UP' ) {
1616 0 $I[0] = undef;
1617 0 next;
1618  
1619 # equivalent of last'ing out of the current child list.
1620  
1621 # Used to have PRUNE_UP_SOFTLY and ABORT_SOFTLY here, but the code
1622 # for these was seriously upsetting, served no particularly clear
1623 # purpose, and could not, I think, be easily implemented with a
1624 # recursive routine. All bad things!
1625 }
1626 else {
1627 0 die "Unknown travsignal $rv\n";
1628  
1629 # should never happen
1630 }
1631 }
1632  
1633 # else fall thru to meaning same as \'OK'.
1634 }
1635  
1636 # end of pre-order calling
1637  
1638 # Now queue up content list for the current element...
1639 3631 ▶ if (ref $this
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this and not !(($content_r = $this->{"_content"}) && @$content_r) && ($this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""})) 2096 1535 3631
Condition85%12/14
ref $this and not !(($content_r = $this->{"_content"}) && @$content_r) && ($this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""})
!ll&&!rl&&r
1517182096
!(($content_r = $this->{'_content'}) && @$content_r) && ($this->{'_empty_element'} || $$empty_element_map{$this->{'_tag'} or ''})
!ll&&!rl&&r
195214418
($content_r = $this->{'_content'}) && @$content_r
!ll&&!rl&&r
16021952
$this->{"_empty_element"} or $$empty_element_map{$this->{"_tag"} or ""}
l!l&&r!l&&!r
018144
$this->{'_tag'} || ''
l!l
1620
MC/DC60%3/5
ref $this and not !(($content_r = $this->{"_content"}) && @$content_r) && ($this->{"_empty_element"} || $$empty_element_map{$this->{"_tag"} or ""})
ref $this ($content_r = $this->{'_content'}) @$content_r $this->{"_empty_element"} $$empty_element_map{$this->{"_tag"} or ""}
MC/DC0%0/1
$this->{'_tag'} || ''
$this->{'_tag'}
Truth table62%5/8
A and not(not(B && C) && D or E)
A= ref $thisB= ($content_r = $this->{'_content'})C= @$content_rD= $this->{"_empty_element"}E= $$empty_element_map{$this->{"_tag"} or ""}
ABCDEresult
0XXXX0
10X1X0
10X010
1101X0
110010
111XX1
10X001
110001
Truth table50%1/2
A
A= $this->{'_tag'}
Aresult
11
00
1640 and not( # ...except for those which...
1641 not( $content_r = $this->{'_content'} and @$content_r )
1642  
1643 # ...have empty content lists...
1644 and $this->{'_empty_element'}
1645 || $empty_element_map->{ $this->{'_tag'} || '' }
1646  
1647 # ...and that don't get post-order callbacks
1648 )
1649 )
1650 {
1651 2096 unshift @I, -1;
1652 2096 ▶ unshift @C, $content_r || $NIL;
Condition66%2/3
$content_r || $NIL
l!l&&r!l&&!r
19541420
MC/DC0%0/2
$content_r || $NIL
$content_r $NIL
Truth table66%2/3
A || B
A= $content_rB= $NIL
ABresult
1X1
011
000
1653  
1654 #print $this->{'_tag'}, " ($this) adds content_r ", $C[0], "\n";
1655 }
1656 }
1657 395 return $start;
1658 }
1659  
1660  
1661 3049 ▶sub is_inside {
Subroutine is_inside: called CC=11 SCAR=24.6
Pod documented
1662 3049 my $self = shift;
1663 3049 ▶ return 0 unless @_; # if no items specified, I guess this is right.
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3049 3049
1664  
1665 3049 my $current = $self;
1666 # the loop starts by looking at the given element
1667  
1668 3049 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 831 2218 3049
1669 831 ▶ while ( defined $current and ref $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and ref $current) 1021 512 1533
Condition66%2/3
defined $current and ref $current
!ll&&!rl&&r
51201021
MC/DC50%1/2
defined $current and ref $current
defined $current ref $current
Truth table66%2/3
A and B
A= defined $currentB= ref $current
ABresult
0X0
100
111
1670 1021 ▶ return 1 if $current eq $_[0] || $current->{'_tag'} eq $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $current eq $_[0] or $current->{"_tag"} eq $_[0] 319 702 1021
Condition66%2/3
$current eq $_[0] or $current->{"_tag"} eq $_[0]
l!l&&r!l&&!r
0319702
MC/DC50%1/2
$current eq $_[0] or $current->{"_tag"} eq $_[0]
$current eq $_[0] $current->{"_tag"} eq $_[0]
Truth table66%2/3
A or B
A= $current eq $_[0]B= $current->{"_tag"} eq $_[0]
ABresult
1X1
011
000
1671 702 $current = $current->{'_parent'};
1672 }
1673 512 return 0;
1674 } else {
1675 2218 my %elements = map { $_ => 1 } @_;
1676 2218 ▶ while ( defined $current and ref $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and ref $current) 5524 2218 7742
Condition66%2/3
defined $current and ref $current
!ll&&!rl&&r
221805524
MC/DC50%1/2
defined $current and ref $current
defined $current ref $current
Truth table66%2/3
A and B
A= defined $currentB= ref $current
ABresult
0X0
100
111
1677 5524 ▶ return 1 if $elements{$current} || $elements{ $current->{'_tag'} };
Branch: 1 branch
BranchTrueFalseTotal
if $elements{$current} or $elements{$current->{"_tag"}} 0 5524 5524
Condition33%1/3
$elements{$current} or $elements{$current->{"_tag"}}
l!l&&r!l&&!r
005524
MC/DC0%0/2
$elements{$current} or $elements{$current->{"_tag"}}
$elements{$current} $elements{$current->{"_tag"}}
Truth table33%1/3
A or B
A= $elements{$current}B= $elements{$current->{"_tag"}}
ABresult
1X1
011
000
1678 5524 $current = $current->{'_parent'};
1679 }
1680 }
1681 2218 return 0;
1682 }
1683  
1684  
1685 1 ▶sub is_empty {
Subroutine is_empty: called CC=2 SCAR=9.2
Pod documented
1686 1 my $self = shift;
1687 0 ▶ !$self->{'_content'} || !@{ $self->{'_content'} };
Condition33%1/3
$self->{'_content'} && @{$self->{'_content'};}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{'_content'} && @{$self->{'_content'};}
$self->{'_content'} @{$self->{'_content'};}
Truth table33%1/3
A && B
A= $self->{'_content'}B= @{$self->{'_content'};}
ABresult
0X0
100
111
1688 }
1689  
1690  
1691 3 ▶sub pindex {
Subroutine pindex: called CC=6 SCAR=18.2
Pod documented
1692 3 my $self = shift;
1693  
1694 3 ▶ my $parent = $self->{'_parent'} || return undef;
Condition100%2/2
$self->{'_parent'} || (return undef)
l!l
21
MC/DC100%1/1
$self->{'_parent'} || (return undef)
$self->{'_parent'}
Truth table100%2/2
A
A= $self->{'_parent'}
Aresult
11
00
1695 2 ▶ my $pc = $parent->{'_content'} || return undef;
Condition50%1/2
$parent->{'_content'} || (return undef)
l!l
20
MC/DC0%0/1
$parent->{'_content'} || (return undef)
$parent->{'_content'}
Truth table50%1/2
A
A= $parent->{'_content'}
Aresult
11
00
1696 2 ▶ for ( my $i = 0; $i < @$pc; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pc) 5 0 5
1697 5 ▶ return $i if ref $pc->[$i] and $pc->[$i] eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $pc->[$i] and $pc->[$i] eq $self 2 3 5
Condition100%3/3
ref $pc->[$i] and $pc->[$i] eq $self
!ll&&!rl&&r
122
MC/DC100%2/2
ref $pc->[$i] and $pc->[$i] eq $self
ref $pc->[$i] $pc->[$i] eq $self
Truth table100%3/3
A and B
A= ref $pc->[$i]B= $pc->[$i] eq $self
ABresult
0X0
100
111
1698 }
1699 0 return undef; # we shouldn't ever get here
1700 }
1701  
1702 #--------------------------------------------------------------------------
1703  
1704  
1705 0 ▶sub left {
Subroutine left: not called CC=14 SCAR=53.5
Pod documented
1706 0 ▶ Carp::croak "left() is supposed to be an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
1707 unless ref $_[0];
1708 0 ▶ my $pc = ( $_[0]->{'_parent'} || return )->{'_content'}
Condition0%0/4
+($_[0]{'_parent'} || (return))->{'_content'} || die('parent is childless?')
l!l
00
$_[0]{'_parent'} || (return)
l!l
00
MC/DC0%0/1
+($_[0]{'_parent'} || (return))->{'_content'} || die('parent is childless?')
$_[0]{'_parent'}
Truth table0%0/2
A
A= $_[0]{'_parent'}
Aresult
11
00
1709 || die "parent is childless?";
1710  
1711 0 ▶ die "parent is childless" unless @$pc;
Branch: 1 branch
BranchTrueFalseTotal
unless @$pc 0 0 0
1712 0 ▶ return if @$pc == 1; # I'm an only child
Branch: 1 branch
BranchTrueFalseTotal
if @$pc == 1 0 0 0
1713  
1714 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1715 0 my @out;
1716 0 foreach my $j (@$pc) {
1717 0 ▶ return @out if ref $j and $j eq $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $j and $j eq $_[0] 0 0 0
Condition0%0/3
ref $j and $j eq $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $j and $j eq $_[0]
ref $j $j eq $_[0]
Truth table0%0/3
A and B
A= ref $jB= $j eq $_[0]
ABresult
0X0
100
111
1718 0 push @out, $j;
1719 }
1720 }
1721 else {
1722 0 ▶ for ( my $i = 0; $i < @$pc; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pc) 0 0 0
1723 0 ▶ return $i ? $pc->[ $i - 1 ] : undef
Branch: 2 branches
BranchTrueFalseTotal
if ref $pc->[$i] and $pc->[$i] eq $_[0] 0 0 0
$i ? : 0 0 0
Condition0%0/3
ref $pc->[$i] and $pc->[$i] eq $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $pc->[$i] and $pc->[$i] eq $_[0]
ref $pc->[$i] $pc->[$i] eq $_[0]
Truth table0%0/3
A and B
A= ref $pc->[$i]B= $pc->[$i] eq $_[0]
ABresult
0X0
100
111
1724 if ref $pc->[$i] and $pc->[$i] eq $_[0];
1725 }
1726 }
1727  
1728 0 die "I'm not in my parent's content list?";
1729 0 return;
1730 }
1731  
1732  
1733 0 ▶sub right {
Subroutine right: not called CC=16 SCAR=56.1
Pod documented
1734 0 ▶ Carp::croak "right() is supposed to be an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
1735 unless ref $_[0];
1736 0 ▶ my $pc = ( $_[0]->{'_parent'} || return )->{'_content'}
Condition0%0/4
+($_[0]{'_parent'} || (return))->{'_content'} || die('parent is childless?')
l!l
00
$_[0]{'_parent'} || (return)
l!l
00
MC/DC0%0/1
+($_[0]{'_parent'} || (return))->{'_content'} || die('parent is childless?')
$_[0]{'_parent'}
Truth table0%0/2
A
A= $_[0]{'_parent'}
Aresult
11
00
1737 || die "parent is childless?";
1738  
1739 0 ▶ die "parent is childless" unless @$pc;
Branch: 1 branch
BranchTrueFalseTotal
unless @$pc 0 0 0
1740 0 ▶ return if @$pc == 1; # I'm an only child
Branch: 1 branch
BranchTrueFalseTotal
if @$pc == 1 0 0 0
1741  
1742 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1743 0 my ( @out, $seen );
1744 0 foreach my $j (@$pc) {
1745 0 ▶ if ($seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen) { } 0 0 0
1746 0 push @out, $j;
1747 }
1748 else {
1749 0 ▶ $seen = 1 if ref $j and $j eq $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $j and $j eq $_[0] 0 0 0
Condition0%0/3
ref $j and $j eq $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $j and $j eq $_[0]
ref $j $j eq $_[0]
Truth table0%0/3
A and B
A= ref $jB= $j eq $_[0]
ABresult
0X0
100
111
1750 }
1751 }
1752 0 ▶ die "I'm not in my parent's content list?" unless $seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen 0 0 0
1753 0 return @out;
1754 }
1755 else {
1756 0 ▶ for ( my $i = 0; $i < @$pc; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pc) 0 0 0
1757 0 ▶ return +( $i == $#$pc ) ? undef : $pc->[ $i + 1 ]
Branch: 2 branches
BranchTrueFalseTotal
if ref $pc->[$i] and $pc->[$i] eq $_[0] 0 0 0
$i == $pc->$#* ? : 0 0 0
Condition0%0/3
ref $pc->[$i] and $pc->[$i] eq $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $pc->[$i] and $pc->[$i] eq $_[0]
ref $pc->[$i] $pc->[$i] eq $_[0]
Truth table0%0/3
A and B
A= ref $pc->[$i]B= $pc->[$i] eq $_[0]
ABresult
0X0
100
111
1758 if ref $pc->[$i] and $pc->[$i] eq $_[0];
1759 }
1760 0 die "I'm not in my parent's content list?";
1761 0 return;
1762 }
1763 }
1764  
1765 #--------------------------------------------------------------------------
1766  
1767  
1768 21 ▶sub address {
Subroutine address: called CC=11 SCAR=26.3
Pod documented
1769 21 ▶ if ( @_ == 1 ) { # report-address form
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 20 21
1770 1 ▶ return join(
Condition100%2/2
$_->pindex || '0'
l!l
21
MC/DC100%1/1
$_->pindex || '0'
$_->pindex
Truth table100%2/2
A
A= $_->pindex
Aresult
11
00
1771 '.',
1772 reverse( # so it starts at the top
1773 map( $_->pindex() || '0', # so that root's undef -> '0'
1774 $_[0], # self and...
1775 $_[0]->lineage )
1776 )
1777 );
1778 }
1779 else { # get-node-at-address
1780 20 my @stack = split( /\./, $_[1] );
1781 20 my $here;
1782  
1783 20 ▶ if ( @stack and !length $stack[0] ) { # relative addressing
Branch: 1 branch
BranchTrueFalseTotal
if (@stack and not length $stack[0]) { } 0 20 20
Condition33%1/3
@stack and not length $stack[0]
!ll&&!rl&&r
0200
MC/DC0%0/2
@stack and not length $stack[0]
@stack not length $stack[0]
Truth table33%1/3
A and not(B)
A= @stackB= not length $stack[0]
ABresult
0X0
100
111
1784 0 $here = $_[0];
1785 0 shift @stack;
1786 }
1787 else { # absolute addressing
1788 20 ▶ return undef unless 0 == shift @stack; # pop the initial 0-for-root
Branch: 1 branch
BranchTrueFalseTotal
unless 0 == shift @stack 0 20 20
1789 20 $here = $_[0]->root;
1790 }
1791  
1792 20 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 36 20 56
1793 return undef
1794 unless $here->{'_content'}
1795 36 ▶ and @{ $here->{'_content'} } > $stack[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $here->{"_content"} and @{$here->{"_content"};} > $stack[0] 0 36 36
Condition33%1/3
$here->{"_content"} and @{$here->{"_content"};} > $stack[0]
!ll&&!rl&&r
0036
MC/DC0%0/2
$here->{"_content"} and @{$here->{"_content"};} > $stack[0]
$here->{"_content"} @{$here->{"_content"};} > $stack[0]
Truth table33%1/3
A and B
A= $here->{"_content"}B= @{$here->{"_content"};} > $stack[0]
ABresult
0X0
100
111
1796  
1797 # make sure the index isn't too high
1798 36 $here = $here->{'_content'}[ shift @stack ];
1799 36 ▶ return undef if @stack and not ref $here;
Branch: 1 branch
BranchTrueFalseTotal
if @stack and not ref $here 0 36 36
Condition66%2/3
@stack and not ref $here
!ll&&!rl&&r
20160
MC/DC0%0/2
@stack and not ref $here
@stack not ref $here
Truth table66%2/3
A and not(B)
A= @stackB= not ref $here
ABresult
0X0
100
111
1800  
1801 # we hit a text node when we expected a non-terminal element node
1802 }
1803  
1804 20 return $here;
1805 }
1806 }
1807  
1808  
1809 0 ▶sub depth {
Subroutine depth: not called CC=3 SCAR=24.8
Pod documented
1810 0 my $here = $_[0];
1811 0 my $depth = 0;
1812 0 ▶ while ( defined( $here = $here->{'_parent'} ) and ref($here) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($here = $here->{"_parent"}) and ref $here) 0 0 0
Condition0%0/3
defined($here = $here->{"_parent"}) and ref $here
!ll&&!rl&&r
000
MC/DC0%0/2
defined($here = $here->{"_parent"}) and ref $here
defined($here = $here->{"_parent"}) ref $here
Truth table0%0/3
A and B
A= defined($here = $here->{"_parent"})B= ref $here
ABresult
0X0
100
111
1813 0 ++$depth;
1814 }
1815 0 return $depth;
1816 }
1817  
1818  
1819 20 ▶sub root {
Subroutine root: called CC=3 SCAR=13.3
Pod documented
1820 20 my $here = my $root = shift;
1821 20 ▶ while ( defined( $here = $here->{'_parent'} ) and ref($here) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($here = $here->{"_parent"}) and ref $here) 0 20 20
Condition33%1/3
defined($here = $here->{"_parent"}) and ref $here
!ll&&!rl&&r
2000
MC/DC0%0/2
defined($here = $here->{"_parent"}) and ref $here
defined($here = $here->{"_parent"}) ref $here
Truth table33%1/3
A and B
A= defined($here = $here->{"_parent"})B= ref $here
ABresult
0X0
100
111
1822 0 $root = $here;
1823 }
1824 20 return $root;
1825 }
1826  
1827  
1828 1 ▶sub lineage {
Subroutine lineage: called CC=3 SCAR=11.0
Pod documented
1829 1 my $here = shift;
1830 1 my @lineage;
1831 1 ▶ while ( defined( $here = $here->{'_parent'} ) and ref($here) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($here = $here->{"_parent"}) and ref $here) 2 1 3
Condition66%2/3
defined($here = $here->{"_parent"}) and ref $here
!ll&&!rl&&r
102
MC/DC50%1/2
defined($here = $here->{"_parent"}) and ref $here
defined($here = $here->{"_parent"}) ref $here
Truth table66%2/3
A and B
A= defined($here = $here->{"_parent"})B= ref $here
ABresult
0X0
100
111
1832 2 push @lineage, $here;
1833 }
1834 1 return @lineage;
1835 }
1836  
1837  
1838 0 ▶sub lineage_tag_names {
Subroutine lineage_tag_names: not called CC=3 SCAR=24.8
Pod documented
1839 0 my $here = my $start = shift;
1840 0 my @lineage_names;
1841 0 ▶ while ( defined( $here = $here->{'_parent'} ) and ref($here) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($here = $here->{"_parent"}) and ref $here) 0 0 0
Condition0%0/3
defined($here = $here->{"_parent"}) and ref $here
!ll&&!rl&&r
000
MC/DC0%0/2
defined($here = $here->{"_parent"}) and ref $here
defined($here = $here->{"_parent"}) ref $here
Truth table0%0/3
A and B
A= defined($here = $here->{"_parent"})B= ref $here
ABresult
0X0
100
111
1842 0 push @lineage_names, $here->{'_tag'};
1843 }
1844 0 return @lineage_names;
1845 }
1846  
1847  
1848 0 ▶sub descendents { shift->descendants(@_) }
Subroutine descendents: not called CC=1 SCAR=6.9
Pod documented
1849  
1850 0 ▶sub descendants {
Subroutine descendants: not called CC=2 SCAR=17.9
Pod documented
1851 0 my $start = shift;
1852 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1853 0 my @descendants;
1854 $start->traverse(
1855 [ # pre-order sub only
1856 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1857 0 push( @descendants, $_[0] );
1858 0 return 1;
1859 },
1860 undef # no post
1861 0 ],
1862 1, # ignore text
1863 );
1864 0 shift @descendants; # so $self doesn't appear in the list
1865 0 return @descendants;
1866 }
1867 else { # just returns a scalar
1868 0 my $descendants = -1; # to offset $self being counted
1869 $start->traverse(
1870 [ # pre-order sub only
1871 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1872 0 ++$descendants;
1873 0 return 1;
1874 },
1875 undef # no post
1876 0 ],
1877 1, # ignore text
1878 );
1879 0 return $descendants;
1880 }
1881 }
1882  
1883  
1884 9 ▶sub find { shift->find_by_tag_name(@_) }
Subroutine find: called CC=1 SCAR=0.0
Pod documented
1885  
1886 # yup, a handy alias
1887  
1888 19 ▶sub find_by_tag_name {
Subroutine find_by_tag_name: called CC=9 SCAR=24.6
Pod documented
1889 19 my (@pile) = shift(@_); # start out the to-do stack for the traverser
1890 19 ▶ Carp::croak "find_by_tag_name can be called only as an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pile[0] 0 19 19
1891 unless ref $pile[0];
1892 19 ▶ return () unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 19 19
1893 19 my (@tags) = $pile[0]->_fold_case(@_);
1894 19 my ( @matching, $this, $this_tag );
1895 19 ▶ while (@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 112 0 112
1896 112 $this_tag = ( $this = shift @pile )->{'_tag'};
1897 112 foreach my $t (@tags) {
1898 112 ▶ if ( $t eq $this_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq $this_tag) 19 93 112
1899 19 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 19 19
1900 0 push @matching, $this;
1901 0 last;
1902 }
1903 else {
1904 19 return $this;
1905 }
1906 }
1907 }
1908 93 ▶ unshift @pile, grep ref($_), @{ $this->{'_content'} || next };
Condition100%2/2
$this->{'_content'} || (next)
l!l
921
MC/DC100%1/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table100%2/2
A
A= $this->{'_content'}
Aresult
11
00
1909 }
1910 0 ▶ return @matching if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1911 0 return;
1912 }
1913  
1914  
1915 19 ▶sub find_by_attribute {
Subroutine find_by_attribute: called CC=3 SCAR=11.2
Pod documented
1916  
1917 # We could limit this to non-internal attributes, but hey.
1918 19 my ( $self, $attribute, $value ) = @_;
1919 19 ▶ Carp::croak "Attribute must be a defined value!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 19 19
1920 unless defined $attribute;
1921 19 $attribute = $self->_fold_case($attribute);
1922  
1923 19 my @matching;
1924 19 my $wantarray = wantarray;
1925 19 my $quit;
1926 $self->traverse(
1927 [ # pre-order only
1928 121 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
1929 121 ▶ if ( exists $_[0]{$attribute}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{$attribute} and $_[0]{$attribute} eq $value) 19 102 121
Condition66%2/3
exists $_[0]{$attribute} and $_[0]{$attribute} eq $value
!ll&&!rl&&r
102019
MC/DC50%1/2
exists $_[0]{$attribute} and $_[0]{$attribute} eq $value
exists $_[0]{$attribute} $_[0]{$attribute} eq $value
Truth table66%2/3
A and B
A= exists $_[0]{$attribute}B= $_[0]{$attribute} eq $value
ABresult
0X0
100
111
1930 and $_[0]{$attribute} eq $value )
1931 {
1932 19 push @matching, $_[0];
1933 19 ▶ return HTML::Element::ABORT
Branch: 1 branch
BranchTrueFalseTotal
unless $wantarray 19 0 19
1934 unless $wantarray; # only take the first
1935 }
1936 102 1; # keep traversing
1937 },
1938 undef # no post
1939 19 ],
1940 1, # yes, ignore text nodes.
1941 );
1942  
1943 19 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 19 19
1944 0 return @matching;
1945 }
1946 else {
1947 19 return $matching[0];
1948 }
1949 }
1950  
1951 #--------------------------------------------------------------------------
1952  
1953  
1954 13 ▶sub look_down {
Subroutine look_down: called CC=22 SCAR=31.8
Pod documented
1955 13 ▶ ref( $_[0] ) or Carp::croak "look_down works only as an object method";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or Carp::croak("look_down works only as an object method") 13 0 13
1956  
1957 13 my @criteria;
1958 13 ▶ for ( my $i = 1; $i < @_; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 19 13 32
1959 19 ▶ Carp::croak "Can't use undef as an attribute name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[$i] 0 19 19
1960 unless defined $_[$i];
1961 19 ▶ if ( ref $_[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i]) { } 4 15 19
1962 4 ▶ Carp::croak "A " . ref( $_[$i] ) . " value is not a criterion"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[$i] eq "CODE" 0 4 4
1963 unless ref $_[$i] eq 'CODE';
1964 4 push @criteria, $_[ $i++ ];
1965 }
1966 else {
1967 15 ▶ Carp::croak "param list to look_down ends in a key!" if $i == $#_;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#_ 0 15 15
1968 15 ▶ push @criteria, [
Branch: 2 branches
BranchTrueFalseTotal
defined $_[$i + 1] ? : 15 0 15
ref $_[$i + 1] ? : 3 12 15
1969 scalar( $_[0]->_fold_case( $_[$i] ) ),
1970 defined( $_[ $i + 1 ] )
1971 ? ( ( ref $_[ $i + 1 ] ? $_[ $i + 1 ] : lc( $_[ $i + 1 ] ) ),
1972 ref( $_[ $i + 1 ] )
1973 )
1974  
1975 # yes, leave that LC!
1976 : undef
1977 ];
1978 15 $i += 2;
1979 }
1980 }
1981 13 ▶ Carp::croak "No criteria?" unless @criteria;
Branch: 1 branch
BranchTrueFalseTotal
unless @criteria 0 13 13
1982  
1983 13 my (@pile) = ( $_[0] );
1984 13 my ( @matching, $val, $this );
1985 Node:
1986 13 ▶ while ( defined( $this = shift @pile ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($this = shift @pile)) 58 4 62
1987  
1988 # Yet another traverser implemented with merely iterative code.
1989 58 foreach my $c (@criteria) {
1990 66 ▶ if ( ref($c) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'CODE') { } 9 57 66
1991 9 ▶ next Node unless $c->($this); # jump to the continue block
Branch: 1 branch
BranchTrueFalseTotal
unless &$c($this) 5 4 9
1992 }
1993 else { # it's an attr-value pair
1994 next Node # jump to the continue block
1995 if # two values are unequal if:
1996 57 ▶ ( defined( $val = $this->{ $c->[0] } ) )
Branch: 3 branches
BranchTrueFalseTotal
if defined($val = $$this{$c->[0]}) ? !defined($c->[1]) || ($c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne lc $c->[1]) : defined $c->[1] 40 17 57
defined($val = $$this{$c->[0]}) ? : 57 0 57
$c->[2] eq 'Regexp' ? : 0 57 57
Condition66%4/6
not defined $c->[1] or $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne lc $c->[1]
l!l&&r!l&&!r
04017
ref $val ne $c->[2] or lc $val ne lc $c->[1]
l!l&&r!l&&!r
04017
MC/DC50%1/2
not defined $c->[1] or $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne lc $c->[1]
not defined $c->[1] $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne lc $c->[1]
MC/DC50%1/2
ref $val ne $c->[2] or lc $val ne lc $c->[1]
ref $val ne $c->[2] lc $val ne lc $c->[1]
Truth table66%2/3
not(A) or B
A= not defined $c->[1]B= $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne lc $c->[1]
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= ref $val ne $c->[2]B= lc $val ne lc $c->[1]
ABresult
1X1
011
000
1997 ? ( !defined $c->[ 1
1998 ] # actual is def, critval is undef => fail
1999 # allow regex matching
2000 # allow regex matching
2001 or (
2002 $c->[2] eq 'Regexp'
2003 ? $val !~ $c->[1]
2004 : ( ref $val ne $c->[2]
2005  
2006 # have unequal ref values => fail
2007 or lc($val) ne lc( $c->[1] )
2008  
2009 # have unequal lc string values => fail
2010 )
2011 )
2012 )
2013 : ( defined $c->[1]
2014 ) # actual is undef, critval is def => fail
2015 }
2016 }
2017  
2018 # We make it this far only if all the criteria passed.
2019 13 ▶ return $this unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 9 4 13
2020 4 push @matching, $this;
2021 }
2022 continue {
2023 49 ▶ unshift @pile, grep ref($_), @{ $this->{'_content'} || $nillio };
Condition66%2/3
$this->{'_content'} || $nillio
l!l&&r!l&&!r
39100
MC/DC0%0/2
$this->{'_content'} || $nillio
$this->{'_content'} $nillio
Truth table66%2/3
A || B
A= $this->{'_content'}B= $nillio
ABresult
1X1
011
000
2024 }
2025 4 ▶ return @matching if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 1 4
2026 1 return;
2027 }
2028  
2029  
2030 9 ▶sub look_up {
Subroutine look_up: called CC=22 SCAR=40.8
Pod documented
2031 9 ▶ ref( $_[0] ) or Carp::croak "look_up works only as an object method";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or Carp::croak("look_up works only as an object method") 9 0 9
2032  
2033 9 my @criteria;
2034 9 ▶ for ( my $i = 1; $i < @_; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 9 9 18
2035 9 ▶ Carp::croak "Can't use undef as an attribute name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[$i] 0 9 9
2036 unless defined $_[$i];
2037 9 ▶ if ( ref $_[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i]) { } 0 9 9
2038 0 ▶ Carp::croak "A " . ref( $_[$i] ) . " value is not a criterion"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[$i] eq "CODE" 0 0 0
2039 unless ref $_[$i] eq 'CODE';
2040 0 push @criteria, $_[ $i++ ];
2041 }
2042 else {
2043 9 ▶ Carp::croak "param list to look_up ends in a key!" if $i == $#_;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#_ 0 9 9
2044 9 ▶ push @criteria, [
Branch: 2 branches
BranchTrueFalseTotal
defined $_[$i + 1] ? : 9 0 9
ref $_[$i + 1] ? : 0 9 9
2045 scalar( $_[0]->_fold_case( $_[$i] ) ),
2046 defined( $_[ $i + 1 ] )
2047 ? ( ( ref $_[ $i + 1 ] ? $_[ $i + 1 ] : lc( $_[ $i + 1 ] ) ),
2048 ref( $_[ $i + 1 ] )
2049 )
2050 : undef # Yes, leave that LC!
2051 ];
2052 9 $i += 2;
2053 }
2054 }
2055 9 ▶ Carp::croak "No criteria?" unless @criteria;
Branch: 1 branch
BranchTrueFalseTotal
unless @criteria 0 9 9
2056  
2057 9 my ( @matching, $val );
2058 9 my $this = $_[0];
2059 Node:
2060 9 while (1) {
2061  
2062 # You'll notice that the code here is almost the same as for look_down.
2063 18 foreach my $c (@criteria) {
2064 18 ▶ if ( ref($c) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'CODE') { } 0 18 18
2065 0 ▶ next Node unless $c->($this); # jump to the continue block
Branch: 1 branch
BranchTrueFalseTotal
unless &$c($this) 0 0 0
2066 }
2067 else { # it's an attr-value pair
2068 next Node # jump to the continue block
2069 if # two values are unequal if:
2070 18 ▶ ( defined( $val = $this->{ $c->[0] } ) )
Branch: 3 branches
BranchTrueFalseTotal
if defined($val = $$this{$c->[0]}) ? !defined($c->[1]) || ($c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne $c->[1]) : defined $c->[1] 9 9 18
defined($val = $$this{$c->[0]}) ? : 18 0 18
$c->[2] eq 'Regexp' ? : 0 18 18
Condition66%4/6
not defined $c->[1] or $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne $c->[1]
l!l&&r!l&&!r
099
ref $val ne $c->[2] or lc $val ne $c->[1]
l!l&&r!l&&!r
099
MC/DC50%1/2
not defined $c->[1] or $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne $c->[1]
not defined $c->[1] $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne $c->[1]
MC/DC50%1/2
ref $val ne $c->[2] or lc $val ne $c->[1]
ref $val ne $c->[2] lc $val ne $c->[1]
Truth table66%2/3
not(A) or B
A= not defined $c->[1]B= $c->[2] eq "Regexp" ? !($val =~ /$c->[1]/) : ref $val ne $c->[2] || lc $val ne $c->[1]
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= ref $val ne $c->[2]B= lc $val ne $c->[1]
ABresult
1X1
011
000
2071 ? ( !defined $c->[ 1
2072 ] # actual is def, critval is undef => fail
2073 or (
2074 $c->[2] eq 'Regexp'
2075 ? $val !~ $c->[1]
2076 : ( ref $val ne $c->[2]
2077  
2078 # have unequal ref values => fail
2079 or lc($val) ne $c->[1]
2080  
2081 # have unequal lc string values => fail
2082 )
2083 )
2084 )
2085 : ( defined $c->[1]
2086 ) # actual is undef, critval is def => fail
2087 }
2088 }
2089  
2090 # We make it this far only if all the criteria passed.
2091 9 ▶ return $this unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 9 0 9
2092 0 push @matching, $this;
2093 }
2094 continue {
2095 9 ▶ last unless defined( $this = $this->{'_parent'} ) and ref $this;
Branch: 1 branch
BranchTrueFalseTotal
unless defined($this = $this->{"_parent"}) and ref $this 0 9 9
Condition33%1/3
defined($this = $this->{"_parent"}) and ref $this
!ll&&!rl&&r
009
MC/DC0%0/2
defined($this = $this->{"_parent"}) and ref $this
defined($this = $this->{"_parent"}) ref $this
Truth table33%1/3
A and B
A= defined($this = $this->{"_parent"})B= ref $this
ABresult
0X0
100
111
2096 }
2097  
2098 0 ▶ return @matching if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
2099 0 return;
2100 }
2101  
2102 #--------------------------------------------------------------------------
2103  
2104  
2105 0 ▶sub attr_get_i {
Subroutine attr_get_i: not called CC=14 SCAR=53.5
Pod documented
2106 0 ▶ if ( @_ > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 0 0 0
2107 0 my $self = shift;
2108 0 ▶ Carp::croak "No attribute names can be undef!"
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), @_) 0 0 0
2109 if grep !defined($_), @_;
2110 0 my @attributes = $self->_fold_case(@_);
2111 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
2112 0 my @out;
2113 0 foreach my $x ( $self, $self->lineage ) {
2114 push @out,
2115 0 ▶ map { exists( $x->{$_} ) ? $x->{$_} : () } @attributes;
Branch: 1 branch
BranchTrueFalseTotal
exists $x->{$_} ? : 0 0 0
2116 }
2117 0 return @out;
2118 }
2119 else {
2120 0 foreach my $x ( $self, $self->lineage ) {
2121 0 foreach my $attribute (@attributes) {
2122 return $x->{$attribute}
2123 0 ▶ if exists $x->{$attribute}; # found
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{$attribute} 0 0 0
2124 }
2125 }
2126 0 return; # never found
2127 }
2128 }
2129 else {
2130  
2131 # Single-attribute search. Simpler, most common, so optimize
2132 # for the most common case
2133 0 ▶ Carp::croak "Attribute name must be a defined value!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 0 0
2134 unless defined $_[1];
2135 0 my $self = $_[0];
2136 0 my $attribute = $self->_fold_case( $_[1] );
2137 0 ▶ if (wantarray) { # list context
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
2138 return
2139 0 ▶ map { exists( $_->{$attribute} ) ? $_->{$attribute} : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{$attribute} ? : 0 0 0
2140 $self, $self->lineage;
2141 }
2142 else { # scalar context
2143 0 foreach my $x ( $self, $self->lineage ) {
2144 0 ▶ return $x->{$attribute} if exists $x->{$attribute}; # found
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{$attribute} 0 0 0
2145 }
2146 0 return; # never found
2147 }
2148 }
2149 }
2150  
2151  
2152 0 ▶sub tagname_map {
Subroutine tagname_map: not called CC=6 SCAR=37.4
Pod documented
2153 0 my (@pile) = $_[0]; # start out the to-do stack for the traverser
2154 0 ▶ Carp::croak "find_by_tag_name can be called only as an object method"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pile[0] 0 0 0
2155 unless ref $pile[0];
2156 0 my ( %map, $this_tag, $this );
2157 0 ▶ while (@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 0 0 0
2158 $this_tag = ''
2159 0 ▶ unless defined( $this_tag = ( $this = shift @pile )->{'_tag'} )
Branch: 1 branch
BranchTrueFalseTotal
unless defined($this_tag = +($this = shift @pile)->{"_tag"}) 0 0 0
2160 ; # dance around the strange case of having an undef tagname.
2161 0 ▶ push @{ $map{$this_tag} ||= [] }, $this; # add to map
Condition0%0/2
$map{$this_tag} ||= []
l!l
00
MC/DC0%0/1
$map{$this_tag} ||= []
$map{$this_tag}
Truth table0%0/2
A
A= $map{$this_tag}
Aresult
11
00
2162 unshift @pile, grep ref($_),
2163 0 ▶ @{ $this->{'_content'} || next }; # traverse
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2164 }
2165 0 return \%map;
2166 }
2167  
2168  
2169 0 ▶sub extract_links {
Subroutine extract_links: not called CC=1 SCAR=6.9
Pod documented
2170 0 my $start = shift;
2171  
2172 0 my %wantType;
2173 0 @wantType{ $start->_fold_case(@_) } = (1) x @_; # if there were any
2174 0 my $wantType = scalar(@_);
2175  
2176 0 my @links;
2177  
2178 # TODO: add xml:link?
2179  
2180 0 my ( $link_attrs, $tag, $self, $val ); # scratch for each iteration
2181 $start->traverse(
2182 0 ▶ [ sub { # pre-order call only
Subroutine __ANON__: not called CC=7 SCAR=40.3
2183 0 $self = $_[0];
2184  
2185 0 $tag = $self->{'_tag'};
2186 return 1
2187 0 ▶ if $wantType && !$wantType{$tag}; # if we're selective
Branch: 1 branch
BranchTrueFalseTotal
if $wantType and not $wantType{$tag} 0 0 0
Condition0%0/3
$wantType and not $wantType{$tag}
!ll&&!rl&&r
000
MC/DC0%0/2
$wantType and not $wantType{$tag}
$wantType not $wantType{$tag}
Truth table0%0/3
A and not(B)
A= $wantTypeB= not $wantType{$tag}
ABresult
0X0
100
111
2188  
2189 0 ▶ if (defined(
Branch: 1 branch
BranchTrueFalseTotal
if (defined($link_attrs = $HTML::Element::linkElements{$tag})) 0 0 0
2190 $link_attrs = $HTML::Element::linkElements{$tag}
2191 )
2192 )
2193 {
2194  
2195 # If this is a tag that has any link attributes,
2196 # look over possibly present link attributes,
2197 # saving the value, if found.
2198 0 ▶ for ( ref($link_attrs) ? @$link_attrs : $link_attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $link_attrs ? : 0 0 0
2199 0 ▶ if ( defined( $val = $self->attr($_) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($val = $self->attr($_))) 0 0 0
2200 0 push( @links, [ $val, $self, $_, $tag ] );
2201 }
2202 }
2203 }
2204 0 1; # return true, so we keep recursing
2205 },
2206 undef
2207 0 ],
2208 1, # ignore text nodes
2209 );
2210 0 \@links;
2211 }
2212  
2213  
2214 0 ▶sub simplify_pres {
Subroutine simplify_pres: not called CC=1 SCAR=6.9
Pod documented
2215 0 my $pre = 0;
2216  
2217 0 my $sub;
2218 my $line;
2219 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
2220 0 ▶ ++$pre if $_[0]->{'_tag'} eq 'pre';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"_tag"} eq "pre" 0 0 0
2221 0 ▶ foreach my $it ( @{ $_[0]->{'_content'} || return } ) {
Condition0%0/2
$_[0]{'_content'} || (return)
l!l
00
MC/DC0%0/1
$_[0]{'_content'} || (return)
$_[0]{'_content'}
Truth table0%0/2
A
A= $_[0]{'_content'}
Aresult
11
00
2222 0 ▶ if ( ref $it ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $it) { } 0 0 0
elsif ($pre) { } 0 0 0
2223 0 $sub->($it); # recurse!
2224 }
2225 elsif ($pre) {
2226  
2227 #$it =~ s/(?:(?:\cm\cj*)|(?:\cj))/\n/g;
2228  
2229 $it = join "\n", map {
2230 0 ;
2231 0 $line = $_;
2232 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^([^\t]*)(\t+)/$1 . " " x ((length($2) << 3) - (length $1 & 7));/e) 0 0 0
2233 $line
2234 0 =~ s/^([^\t]*)(\t+)/$1.(" " x ((length($2)<<3)-(length($1)&7)))/e
2235  
2236 # Sort of adapted from Text::Tabs -- yes, it's hardwired-in that
2237 # tabs are at every EIGHTH column.
2238 )
2239 {
2240 }
2241 0 $line;
2242 }
2243 split /(?:(?:\cm\cj*)|(?:\cj))/, $it, -1;
2244 }
2245 }
2246 0 ▶ --$pre if $_[0]->{'_tag'} eq 'pre';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"_tag"} eq "pre" 0 0 0
2247 0 return;
2248 0 };
2249 0 $sub->( $_[0] );
2250  
2251 0 undef $sub;
2252 0 return;
2253 }
2254  
2255  
2256 25 ▶sub same_as {
Subroutine same_as: called CC=23 SCAR=32.9
Pod documented
2257 25 ▶ die 'same_as() takes only one argument: $h->same_as($i)' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 25 25
2258 25 my ( $h, $i ) = @_[ 0, 1 ];
2259 25 ▶ die "same_as() can be called only as an object method" unless ref $h;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h 0 25 25
2260  
2261 25 ▶ return 0 unless defined $i and ref $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i and ref $i 0 25 25
Condition33%1/3
defined $i and ref $i
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $i and ref $i
defined $i ref $i
Truth table33%1/3
A and B
A= defined $iB= ref $i
ABresult
0X0
100
111
2262  
2263 # An element can't be same_as anything but another element!
2264 # They needn't be of the same class, tho.
2265  
2266 25 ▶ return 1 if $h eq $i;
Branch: 1 branch
BranchTrueFalseTotal
if $h eq $i 2 23 25
2267  
2268 # special (if rare) case: anything is the same as... itself!
2269  
2270 # assumes that no content lists in/under $h or $i contain subsequent
2271 # text segments, like: ['foo', ' bar']
2272  
2273 # compare attributes now.
2274 #print "Comparing tags of $h and $i...\n";
2275  
2276 23 ▶ return 0 unless $h->{'_tag'} eq $i->{'_tag'};
Branch: 1 branch
BranchTrueFalseTotal
unless $h->{"_tag"} eq $i->{"_tag"} 1 22 23
2277  
2278 # only significant attribute whose name starts with "_"
2279  
2280 #print "Comparing attributes of $h and $i...\n";
2281 # Compare attributes, but only the real ones.
2282 {
2283  
2284 # Bear in mind that the average element has very few attributes,
2285 # and that element names are rather short.
2286 # (Values are a different story.)
2287  
2288 # XXX I would think that /^[^_]/ would be faster, at least easier to read.
2289 22 my @keys_h
2290 22 ▶ = sort grep { length $_ and substr( $_, 0, 1 ) ne '_' } keys %$h;
Condition66%2/3
length $_ && substr($_, 0, 1) ne '_'
!ll&&!rl&&r
010010
MC/DC50%1/2
length $_ && substr($_, 0, 1) ne '_'
length $_ substr($_, 0, 1) ne '_'
Truth table66%2/3
A && B
A= length $_B= substr($_, 0, 1) ne '_'
ABresult
0X0
100
111
2291 my @keys_i
2292 22 ▶ = sort grep { length $_ and substr( $_, 0, 1 ) ne '_' } keys %$i;
Condition66%2/3
length $_ && substr($_, 0, 1) ne '_'
!ll&&!rl&&r
01009
MC/DC50%1/2
length $_ && substr($_, 0, 1) ne '_'
length $_ substr($_, 0, 1) ne '_'
Truth table66%2/3
A && B
A= length $_B= substr($_, 0, 1) ne '_'
ABresult
0X0
100
111
2293  
2294 22 ▶ return 0 unless @keys_h == @keys_i;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys_h == @keys_i 1 21 22
2295  
2296 # different number of real attributes? they're different.
2297 21 ▶ for ( my $x = 0; $x < @keys_h; ++$x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @keys_h) 8 21 29
2298 return 0
2299 unless $keys_h[$x] eq $keys_i[$x] and # same key name
2300 8 ▶ $h->{ $keys_h[$x] } eq $i->{ $keys_h[$x] }; # same value
Branch: 1 branch
BranchTrueFalseTotal
unless $keys_h[$x] eq $keys_i[$x] and $$h{$keys_h[$x]} eq $$i{$keys_h[$x]} 0 8 8
Condition33%1/3
$keys_h[$x] eq $keys_i[$x] and $$h{$keys_h[$x]} eq $$i{$keys_h[$x]}
!ll&&!rl&&r
008
MC/DC0%0/2
$keys_h[$x] eq $keys_i[$x] and $$h{$keys_h[$x]} eq $$i{$keys_h[$x]}
$keys_h[$x] eq $keys_i[$x] $$h{$keys_h[$x]} eq $$i{$keys_h[$x]}
Truth table33%1/3
A and B
A= $keys_h[$x] eq $keys_i[$x]B= $$h{$keys_h[$x]} eq $$i{$keys_h[$x]}
ABresult
0X0
100
111
2301 # Should this test for definedness on values?
2302 # People shouldn't be putting undef in attribute values, I think.
2303 }
2304 }
2305  
2306 #print "Comparing children of $h and $i...\n";
2307 21 ▶ my $hcl = $h->{'_content'} || [];
Condition100%2/2
$h->{'_content'} || []
l!l
183
MC/DC100%1/1
$h->{'_content'} || []
$h->{'_content'}
Truth table100%2/2
A
A= $h->{'_content'}
Aresult
11
00
2308 21 ▶ my $icl = $i->{'_content'} || [];
Condition100%2/2
$i->{'_content'} || []
l!l
183
MC/DC100%1/1
$i->{'_content'} || []
$i->{'_content'}
Truth table100%2/2
A
A= $i->{'_content'}
Aresult
11
00
2309  
2310 21 ▶ return 0 unless @$hcl == @$icl;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hcl == @$icl 0 21 21
2311  
2312 # different numbers of children? they're different.
2313  
2314 21 ▶ if (@$hcl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hcl) 18 3 21
2315  
2316 # compare each of the children:
2317 18 ▶ for ( my $x = 0; $x < @$hcl; ++$x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @$hcl) 30 16 46
2318 30 ▶ if ( ref $hcl->[$x] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hcl->[$x]) { } 18 12 30
2319 18 ▶ return 0 unless ref( $icl->[$x] );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $icl->[$x] 0 18 18
2320  
2321 # an element can't be the same as a text segment
2322 # Both elements:
2323 18 ▶ return 0 unless $hcl->[$x]->same_as( $icl->[$x] ); # RECURSE!
Branch: 1 branch
BranchTrueFalseTotal
unless $hcl->[$x]->same_as($icl->[$x]) 2 16 18
2324 }
2325 else {
2326 12 ▶ return 0 if ref( $icl->[$x] );
Branch: 1 branch
BranchTrueFalseTotal
if ref $icl->[$x] 0 12 12
2327  
2328 # a text segment can't be the same as an element
2329 # Both text segments:
2330 12 ▶ return 0 unless $hcl->[$x] eq $icl->[$x];
Branch: 1 branch
BranchTrueFalseTotal
unless $hcl->[$x] eq $icl->[$x] 0 12 12
2331 }
2332 }
2333 }
2334  
2335 19 return 1; # passed all the tests!
2336 }
2337  
2338  
2339 53 ▶sub new_from_lol {
Subroutine new_from_lol: called CC=6 SCAR=18.4
Pod documented
2340 53 my $class = shift;
2341 53 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
7460
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
2342  
2343 # calling as an object method is just the same as ref($h)->new_from_lol(...)
2344 53 my $lol = $_[1];
2345  
2346 53 my @ancestor_lols;
2347  
2348 # So we can make sure there's no cyclicities in this lol.
2349 # That would be perverse, but one never knows.
2350 53 my ( $sub, $k, $v, $node ); # last three are scratch values
2351 305 ▶ $sub = sub {
Subroutine __ANON__: called CC=24 SCAR=33.9
2352  
2353 #print "Building for $_[0]\n";
2354 305 my $lol = $_[0];
2355 305 ▶ return unless @$lol;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lol 0 305 305
2356 305 my ( @attributes, @children );
2357 305 ▶ Carp::croak "Cyclicity detected in source LOL tree, around $lol?!?"
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ eq $lol), @ancestor_lols) 0 305 305
2358 if grep( $_ eq $lol, @ancestor_lols );
2359 305 push @ancestor_lols, $lol;
2360  
2361 305 my $tag_name = 'null';
2362  
2363 # Recursion in in here:
2364 305 ▶ for ( my $i = 0; $i < @$lol; ++$i ) { # Iterate over children
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lol) 848 305 1153
2365 848 ▶ if ( ref( $lol->[$i] ) eq 'ARRAY' )
Branch: 4 branches
BranchTrueFalseTotal
if (ref $lol->[$i] eq 'ARRAY') { } 252 596 848
elsif (not ref $lol->[$i]) { } 519 77 596
elsif (ref $lol->[$i] eq 'HASH') { } 76 1 77
elsif (UNIVERSAL::isa($lol->[$i], __PACKAGE__)) { } 1 0 1
2366 { # subtree: most common thing in loltree
2367 252 push @children, $sub->( $lol->[$i] );
2368 }
2369 elsif ( !ref( $lol->[$i] ) ) {
2370 519 ▶ if ( $i == 0 ) { # name
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 305 214 519
2371 305 $tag_name = $lol->[$i];
2372 305 ▶ Carp::croak "\"$tag_name\" isn't a good tag name!"
Branch: 1 branch
BranchTrueFalseTotal
if $tag_name =~ m[[<>/\x00-\x20]] 0 305 305
2373 if $tag_name =~ m/[<>\/\x00-\x20]/
2374 ; # minimal sanity, certainly!
2375 }
2376 else { # text segment child
2377 214 push @children, $lol->[$i];
2378 }
2379 }
2380 elsif ( ref( $lol->[$i] ) eq 'HASH' ) { # attribute hashref
2381 76 keys %{ $lol->[$i] }; # reset the each-counter, just in case
2382 76 ▶ while ( ( $k, $v ) = each %{ $lol->[$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$lol[$i];}) 76 76 152
2383 76 ▶ push @attributes, $class->_fold_case($k), $v
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $k ne "_name" and $k ne "_content" and $k ne "_parent" 76 0 76
Condition33%3/9
defined $v and $k ne "_name" and $k ne "_content" and $k ne "_parent"
!ll&&!rl&&r
0076
defined $v and $k ne "_name" and $k ne "_content"
!ll&&!rl&&r
0076
defined $v and $k ne "_name"
!ll&&!rl&&r
0076
MC/DC0%0/4
defined $v and $k ne "_name" and $k ne "_content" and $k ne "_parent"
defined $v $k ne "_name" $k ne "_content" $k ne "_parent"
Truth table20%1/5
A and B and C and D
A= defined $vB= $k ne "_name"C= $k ne "_content"D= $k ne "_parent"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2384 if defined $v
2385 and $k ne '_name'
2386 and $k ne '_content'
2387 and $k ne '_parent';
2388  
2389 # enforce /some/ sanity!
2390 }
2391 }
2392 elsif ( UNIVERSAL::isa( $lol->[$i], __PACKAGE__ ) ) {
2393 1 ▶ if ( $lol->[$i]->{'_parent'} ) { # if claimed
Branch: 1 branch
BranchTrueFalseTotal
if ($lol->[$i]{'_parent'}) { } 0 1 1
2394 #print "About to clone ", $lol->[$i], "\n";
2395 0 push @children, $lol->[$i]->clone();
2396 }
2397 else {
2398 1 push @children, $lol->[$i]; # if unclaimed...
2399 #print "Claiming ", $lol->[$i], "\n";
2400 1 $lol->[$i]->{'_parent'} = 1; # claim it NOW
2401 # This WILL be replaced by the correct value once we actually
2402 # construct the parent, just after the end of this loop...
2403 }
2404 }
2405 else {
2406 0 Carp::croak "new_from_lol doesn't handle references of type "
2407 . ref( $lol->[$i] );
2408 }
2409 }
2410  
2411 305 pop @ancestor_lols;
2412 305 $node = $class->new($tag_name);
2413  
2414 #print "Children: @children\n";
2415  
2416 305 ▶ if ( $class eq __PACKAGE__ ) { # Special-case it, for speed:
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) { } 304 1 305
2417 304 ▶ %$node = ( %$node, @attributes ) if @attributes;
Branch: 1 branch
BranchTrueFalseTotal
if @attributes 76 228 304
2418  
2419 #print join(' ', $node, ' ' , map("<$_>", %$node), "\n");
2420 304 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 276 28 304
2421 276 $node->{'_content'} = \@children;
2422 276 foreach my $c (@children) {
2423 466 ▶ _weaken($c->{'_parent'} = $node)
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 252 214 466
2424 if ref $c;
2425 }
2426 }
2427 }
2428 else { # Do it the clean way...
2429 #print "Done neatly\n";
2430 0 ▶ while (@attributes) { $node->attr( splice @attributes, 0, 2 ) }
Branch: 1 branch
BranchTrueFalseTotal
if (@attributes) 0 1 1
2431 $node->push_content(
2432 1 ▶ map { _weaken($_->{'_parent'} = $node) if ref $_; $_ }
Branch: 2 branches
BranchTrueFalseTotal
if @children 1 0 1
if ref $_ 1 0 1
2433 @children )
2434 if @children;
2435 }
2436  
2437 305 return $node;
2438 53 };
2439  
2440 # End of sub definition.
2441  
2442 53 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 49 53
2443 4 ▶ my (@nodes) = map { ; ( ref($_) eq 'ARRAY' ) ? $sub->($_) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 4 0 4
2444 # Let text bits pass thru, I guess. This makes this act more like
2445 # unshift_content et al. Undocumented.
2446  
2447 4 undef $sub;
2448 # so it won't be in its own frame, so its refcount can hit 0
2449  
2450 4 return @nodes;
2451 }
2452 else {
2453 49 ▶ Carp::croak "new_from_lol in scalar context needs exactly one lol"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 49 49
2454 unless @_ == 1;
2455 49 ▶ return $_[0] unless ref( $_[0] ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 49 49
2456 # used to be a fatal error. still undocumented tho.
2457  
2458 49 $node = $sub->( $_[0] );
2459 49 undef $sub;
2460 # so it won't be in its own frame, so its refcount can hit 0
2461  
2462 49 return $node;
2463 }
2464 }
2465  
2466  
2467 0 ▶sub objectify_text {
Subroutine objectify_text: not called CC=4 SCAR=30.0
Pod documented
2468 0 my (@stack) = ( $_[0] );
2469  
2470 0 my ($this);
2471 0 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 0 0
2472 0 foreach my $c ( @{ ( $this = shift @stack )->{'_content'} } ) {
2473 0 ▶ if ( ref($c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 0 0 0
2474 0 unshift @stack, $c; # visit it later.
2475 }
2476 else {
2477 0 $c = $this->element_class->new(
2478 '~text',
2479 'text' => $c,
2480 '_parent' => $this
2481 );
2482 }
2483 }
2484 }
2485 0 return;
2486 }
2487  
2488 0 ▶sub deobjectify_text {
Subroutine deobjectify_text: not called CC=10 SCAR=47.0
Pod documented
2489 0 my (@stack) = ( $_[0] );
2490 0 my ($old_node);
2491  
2492 0 ▶ if ( $_[0]{'_tag'} eq '~text' ) { # special case
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_tag"} eq "~text") 0 0 0
2493 # Puts the $old_node variable to a different purpose
2494 0 ▶ if ( $_[0]{'_parent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'_parent'}) { } 0 0 0
2495 0 $_[0]->replace_with( $old_node = delete $_[0]{'text'} )->delete;
2496 }
2497 else { # well, that's that, then!
2498 0 $old_node = delete $_[0]{'text'};
2499 }
2500  
2501 0 ▶ if ( ref( $_[0] ) eq __PACKAGE__ ) { # common case
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq __PACKAGE__) { } 0 0 0
2502 0 %{ $_[0] } = (); # poof!
2503 }
2504 else {
2505  
2506 # play nice:
2507 0 delete $_[0]{'_parent'};
2508 0 $_[0]->delete;
2509 }
2510 0 ▶ return '' unless defined $old_node; # sanity!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_node 0 0 0
2511 0 return $old_node;
2512 }
2513  
2514 0 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 0 0
2515 0 foreach my $c ( @{ ( shift @stack )->{'_content'} } ) {
2516 0 ▶ if ( ref($c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) 0 0 0
2517 0 ▶ if ( $c->{'_tag'} eq '~text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'_tag'} eq '~text') { } 0 0 0
2518 0 $c = ( $old_node = $c )->{'text'};
2519 0 ▶ if ( ref($old_node) eq __PACKAGE__ ) { # common case
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_node eq __PACKAGE__) { } 0 0 0
2520 0 %$old_node = (); # poof!
2521 }
2522 else {
2523  
2524 # play nice:
2525 0 delete $old_node->{'_parent'};
2526 0 $old_node->delete;
2527 }
2528 }
2529 else {
2530 0 unshift @stack, $c; # visit it later.
2531 }
2532 }
2533 }
2534 }
2535  
2536 0 return undef;
2537 }
2538  
2539  
2540 {
2541  
2542 # The next three subs are basically copied from Number::Latin,
2543 # based on a one-liner by Abigail. Yes, I could simply require that
2544 # module, and a Roman numeral module too, but really, HTML-Tree already
2545 # has enough dependecies as it is; and anyhow, I don't need the functions
2546 # that do latin2int or roman2int.
2547 23 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
2548  
2549 0 ▶ sub _int2latin {
Subroutine _int2latin: not called CC=5 SCAR=34.0
2550 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
2551 0 ▶ return '0' if $_[0] < 1 and $_[0] > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 and $_[0] > -1 0 0 0
Condition0%0/3
$_[0] < 1 and $_[0] > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] < 1 and $_[0] > -1
$_[0] < 1 $_[0] > -1
Truth table0%0/3
A and B
A= $_[0] < 1B= $_[0] > -1
ABresult
0X0
100
111
2552 0 ▶ return '-' . _i2l( abs int $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] <= -1 0 0 0
2553 if $_[0] <= -1; # tolerate negatives
2554 0 return _i2l( int $_[0] );
2555 }
2556  
2557 0 ▶ sub _int2LATIN {
Subroutine _int2LATIN: not called CC=5 SCAR=34.0
2558  
2559 # just the above plus uc
2560 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
2561 0 ▶ return '0' if $_[0] < 1 and $_[0] > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 and $_[0] > -1 0 0 0
Condition0%0/3
$_[0] < 1 and $_[0] > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] < 1 and $_[0] > -1
$_[0] < 1 $_[0] > -1
Truth table0%0/3
A and B
A= $_[0] < 1B= $_[0] > -1
ABresult
0X0
100
111
2562 0 ▶ return '-' . uc( _i2l( abs int $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] <= -1 0 0 0
2563 if $_[0] <= -1; # tolerate negs
2564 0 return uc( _i2l( int $_[0] ) );
2565 }
2566  
2567 23 my @alpha = ( 'a' .. 'z' );
2568  
2569 0 ▶ sub _i2l { # the real work
Subroutine _i2l: not called CC=2 SCAR=17.9
2570 0 ▶ my $int = $_[0] || return "";
Condition0%0/2
$_[0] || (return '')
l!l
00
MC/DC0%0/1
$_[0] || (return '')
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
2571 0 _i2l( int( ( $int - 1 ) / 26 ) )
2572 . $alpha[ $int % 26 - 1 ]; # yes, recursive
2573 # Yes, 26 => is (26 % 26 - 1), which is -1 => Z!
2574 }
2575 }
2576  
2577 {
2578  
2579 # And now, some much less impressive Roman numerals code:
2580  
2581 23 my (@i) = ( '', qw(I II III IV V VI VII VIII IX) );
2582 23 my (@x) = ( '', qw(X XX XXX XL L LX LXX LXXX XC) );
2583 23 my (@c) = ( '', qw(C CC CCC CD D DC DCC DCCC CM) );
2584 23 my (@m) = ( '', qw(M MM MMM) );
2585  
2586 0 ▶ sub _int2ROMAN {
Subroutine _int2ROMAN: not called CC=9 SCAR=45.0
2587 0 my ( $i, $pref );
2588 0 ▶ return '0'
Branch: 1 branch
BranchTrueFalseTotal
if 0 == ($i = int($_[0] || 0)) 0 0 0
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
2589 if 0 == ( $i = int( $_[0] || 0 ) ); # zero is a special case
2590 0 ▶ return $i + 0 if $i <= -4000 or $i >= 4000;
Branch: 1 branch
BranchTrueFalseTotal
if $i <= -4000 or $i >= 4000 0 0 0
Condition0%0/3
$i <= -4000 or $i >= 4000
l!l&&r!l&&!r
000
MC/DC0%0/2
$i <= -4000 or $i >= 4000
$i <= -4000 $i >= 4000
Truth table0%0/3
A or B
A= $i <= -4000B= $i >= 4000
ABresult
1X1
011
000
2591  
2592 # Because over 3999 would require non-ASCII chars, like D-with-)-inside
2593 0 ▶ if ( $i < 0 ) { # grumble grumble tolerate negatives grumble
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
2594 0 $pref = '-';
2595 0 $i = abs($i);
2596 }
2597 else {
2598 0 $pref = ''; # normal case
2599 }
2600  
2601 0 my ( $x, $c, $m ) = ( 0, 0, 0 );
2602 0 ▶ if ( $i >= 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 10) 0 0 0
2603 0 $x = $i / 10;
2604 0 $i %= 10;
2605 0 ▶ if ( $x >= 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 10) 0 0 0
2606 0 $c = $x / 10;
2607 0 $x %= 10;
2608 0 ▶ if ( $c >= 10 ) { $m = $c / 10; $c %= 10; }
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= 10) 0 0 0
2609 }
2610 }
2611  
2612 #print "m$m c$c x$x i$i\n";
2613  
2614 0 return join( '', $pref, $m[$m], $c[$c], $x[$x], $i[$i] );
2615 }
2616  
2617 0 ▶ sub _int2roman { lc( _int2ROMAN( $_[0] ) ) }
Subroutine _int2roman: not called CC=1 SCAR=6.9
2618 }
2619  
2620 0 ▶sub _int2int { $_[0] } # dummy
Subroutine _int2int: not called CC=1 SCAR=1.2
2621  
2622 23 %list_type_to_sub = (
2623 'I' => \&_int2ROMAN,
2624 'i' => \&_int2roman,
2625 'A' => \&_int2LATIN,
2626 'a' => \&_int2latin,
2627 '1' => \&_int2int,
2628 );
2629  
2630 0 ▶sub number_lists {
Subroutine number_lists: not called CC=23 SCAR=63.1
Pod documented
2631 0 my (@stack) = ( $_[0] );
2632 0 my ( $this, $tag, $counter, $numberer ); # scratch
2633 0 ▶ while (@stack) { # yup, pre-order-traverser idiom
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 0 0
2634 0 ▶ if ( ( $tag = ( $this = shift @stack )->{'_tag'} ) eq 'ol' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (($tag = +($this = shift @stack)->{'_tag'}) eq 'ol') { } 0 0 0
elsif ($tag eq 'ul' or $tag eq 'dir' or $tag eq 'menu') { } 0 0 0
2635  
2636 # Prep some things:
2637 $counter
2638 0 ▶ = ( ( $this->{'start'} || '' ) =~ m<^\s*(\d{1,7})\s*$>s )
Branch: 1 branch
BranchTrueFalseTotal
($this->{'start'} || '') =~ /^\s*(\d{1,7})\s*$/s ? : 0 0 0
Condition0%0/2
$this->{'start'} || ''
l!l
00
MC/DC0%0/1
$this->{'start'} || ''
$this->{'start'}
Truth table0%0/2
A
A= $this->{'start'}
Aresult
11
00
2639 ? $1
2640 : 1;
2641 $numberer = $list_type_to_sub{ $this->{'type'} || '' }
2642 0 ▶ || $list_type_to_sub{'1'};
Condition0%0/5
$HTML::Element::list_type_to_sub{$this->{'type'} or ''} || $HTML::Element::list_type_to_sub{'1'}
l!l&&r!l&&!r
000
$this->{'type'} || ''
l!l
00
MC/DC0%0/2
$HTML::Element::list_type_to_sub{$this->{'type'} or ''} || $HTML::Element::list_type_to_sub{'1'}
$HTML::Element::list_type_to_sub{$this->{'type'} or ''} $HTML::Element::list_type_to_sub{'1'}
MC/DC0%0/1
$this->{'type'} || ''
$this->{'type'}
Truth table0%0/3
A || B
A= $HTML::Element::list_type_to_sub{$this->{'type'} or ''}B= $HTML::Element::list_type_to_sub{'1'}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $this->{'type'}
Aresult
11
00
2643  
2644 # Immeditately iterate over all children
2645 0 ▶ foreach my $c ( @{ $this->{'_content'} || next } ) {
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2646 0 ▶ next unless ref $c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c 0 0 0
2647 0 unshift @stack, $c;
2648 0 ▶ if ( $c->{'_tag'} eq 'li' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"_tag"} eq "li") 0 0 0
2649 $counter = $1
2650 if (
2651 0 ▶ ( $c->{'value'} || '' ) =~ m<^\s*(\d{1,7})\s*$>s );
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"value"} || "") =~ /^\s*(\d{1,7})\s*$/s 0 0 0
Condition0%0/2
$c->{'value'} || ''
l!l
00
MC/DC0%0/1
$c->{'value'} || ''
$c->{'value'}
Truth table0%0/2
A
A= $c->{'value'}
Aresult
11
00
2652 0 $c->{'_bullet'} = $numberer->($counter) . '.';
2653 0 ++$counter;
2654 }
2655 }
2656  
2657 }
2658 ▶ elsif ( $tag eq 'ul' or $tag eq 'dir' or $tag eq 'menu' ) {
Condition0%0/6
$tag eq "ul" or $tag eq "dir" or $tag eq "menu"
l!l&&r!l&&!r
000
$tag eq "ul" or $tag eq "dir"
l!l&&r!l&&!r
000
MC/DC0%0/3
$tag eq "ul" or $tag eq "dir" or $tag eq "menu"
$tag eq "ul" $tag eq "dir" $tag eq "menu"
Truth table0%0/4
A or B or C
A= $tag eq "ul"B= $tag eq "dir"C= $tag eq "menu"
ABCresult
1XX1
01X1
0011
0000
2659  
2660 # Immeditately iterate over all children
2661 0 ▶ foreach my $c ( @{ $this->{'_content'} || next } ) {
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2662 0 ▶ next unless ref $c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c 0 0 0
2663 0 unshift @stack, $c;
2664 0 ▶ $c->{'_bullet'} = '*' if $c->{'_tag'} eq 'li';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"_tag"} eq "li" 0 0 0
2665 }
2666  
2667 }
2668 else {
2669 0 ▶ foreach my $c ( @{ $this->{'_content'} || next } ) {
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2670 0 ▶ unshift @stack, $c if ref $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 0 0 0
2671 }
2672 }
2673 }
2674 0 return;
2675 }
2676  
2677  
2678 0 ▶sub has_insane_linkage {
Subroutine has_insane_linkage: not called CC=13 SCAR=52.0
Pod documented
2679 0 my @pile = ( $_[0] );
2680 0 my ( $c, $i, $p, $this ); # scratch
2681  
2682 # Another iterative traverser; this time much simpler because
2683 # only in pre-order:
2684 0 my %parent_of = ( $_[0], 'TOP-OF-SCAN' );
2685 0 ▶ while (@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 0 0 0
2686 0 $this = shift @pile;
2687 0 ▶ $c = $this->{'_content'} || next;
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2688 0 ▶ return ( $this, "_content attribute is true but nonref." )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "ARRAY" 0 0 0
2689 unless ref($c) eq 'ARRAY';
2690 0 ▶ next unless @$c;
Branch: 1 branch
BranchTrueFalseTotal
unless @$c 0 0 0
2691 0 ▶ for ( $i = 0; $i < @$c; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 0 0 0
2692 0 ▶ return ( $this, "Child $i is undef" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->[$i] 0 0 0
2693 unless defined $c->[$i];
2694 0 ▶ if ( ref( $c->[$i] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->[$i]) 0 0 0
2695 0 ▶ return ( $c->[$i], "appears in its own content list" )
Branch: 1 branch
BranchTrueFalseTotal
if $c->[$i] eq $this 0 0 0
2696 if $c->[$i] eq $this;
2697 return ( $c->[$i],
2698 "appears twice in the tree: once under $this, once under $parent_of{$c->[$i]}"
2699 0 ▶ ) if exists $parent_of{ $c->[$i] };
Branch: 1 branch
BranchTrueFalseTotal
if exists $parent_of{$c->[$i]} 0 0 0
2700 0 $parent_of{ $c->[$i] } = '' . $this;
2701  
2702 # might as well just use the stringification of it.
2703  
2704 return ( $c->[$i],
2705 "_parent attribute is wrong (not defined)" )
2706 0 ▶ unless defined( $p = $c->[$i]{'_parent'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($p = $c->[$i]{"_parent"}) 0 0 0
2707 0 ▶ return ( $c->[$i], "_parent attribute is wrong (nonref)" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p 0 0 0
2708 unless ref($p);
2709 0 ▶ return ( $c->[$i],
Branch: 1 branch
BranchTrueFalseTotal
unless $p eq $this 0 0 0
2710 "_parent attribute is wrong (is $p; should be $this)" )
2711 unless $p eq $this;
2712 }
2713 }
2714 0 unshift @pile, grep ref($_), @$c;
2715  
2716 # queue up more things on the pile stack
2717 }
2718 0 return; #okay
2719 }
2720  
2721 0 ▶sub _asserts_fail { # to be run on trusted documents only
Subroutine _asserts_fail: not called CC=11 SCAR=48.8
2722 0 my (@pile) = ( $_[0] );
2723 0 my ( @errors, $this, $id, $assert, $parent, $rv );
2724 0 ▶ while (@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 0 0 0
2725 0 $this = shift @pile;
2726 0 ▶ if ( defined( $assert = $this->{'assert'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($assert = $this->{"assert"})) 0 0 0
2727 0 ▶ $id = ( $this->{'id'} ||= $this->address )
Condition0%0/3
$this->{"id"} ||= $this->address
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"id"} ||= $this->address
$this->{"id"} $this->address
Truth table0%0/3
A ||= B
A= $this->{"id"}B= $this->address
ABresult
1X1
011
000
2728 ; # don't use '0' as an ID, okay?
2729 0 ▶ unless ( ref($assert) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $assert) 0 0 0
2730  
2731 package main;
2732 ## no critic
2733 0 ▶ $assert = $this->{'assert'} = (
Branch: 1 branch
BranchTrueFalseTotal
$assert =~ /\bsub\b/ ? : 0 0 0
2734 $assert =~ m/\bsub\b/
2735 ? eval($assert)
2736 : eval("sub { $assert\n}")
2737 );
2738 ## use critic
2739 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2740 0 push @errors,
2741 [ $this, "assertion at $id broke in eval: $@" ];
2742 0 ▶ $assert = $this->{'assert'} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2743 }
2744 }
2745 0 $parent = $this->{'_parent'};
2746 0 $rv = undef;
2747 0 eval {
2748 $rv = $assert->(
2749 $this, $this->{'_tag'}, $this->{'_id'}, # 0,1,2
2750 $parent
2751 0 ▶ ? ( $parent, $parent->{'_tag'}, $parent->{'id'} )
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 0 0 0
2752 : () # 3,4,5
2753 );
2754 };
2755 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not $rv) { } 0 0 0
2756 0 push @errors, [ $this, "assertion at $id died: $@" ];
2757 }
2758 elsif ( !$rv ) {
2759 0 push @errors, [ $this, "assertion at $id failed" ];
2760 }
2761  
2762 # else OK
2763 }
2764 0 ▶ push @pile, grep ref($_), @{ $this->{'_content'} || next };
Condition0%0/2
$this->{'_content'} || (next)
l!l
00
MC/DC0%0/1
$this->{'_content'} || (next)
$this->{'_content'}
Truth table0%0/2
A
A= $this->{'_content'}
Aresult
11
00
2765 }
2766 0 return @errors;
2767 }
2768  
2769 ## _valid_name
2770 # validate XML style attribute names
2771 # http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-Name
2772  
2773 3679 ▶sub _valid_name {
Subroutine _valid_name: called CC=3 SCAR=11.0
2774 3679 my $self = shift;
2775 3679 ▶ my $attr = shift
Branch: 1 branch
BranchTrueFalseTotal
my $attr = shift() or Carp::croak("sub valid_name requires an attribute name") 3679 0 3679
2776 or Carp::croak("sub valid_name requires an attribute name");
2777  
2778 3679 ▶ return (0) unless ( $attr =~ /^$START_CHAR$NAME_CHAR+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^$START_CHAR$NAME_CHAR+$/ 1 3678 3679
2779  
2780 3678 return (1);
2781 }
2782  
2783  
2784 224 ▶sub element_class {
Subroutine element_class: called CC=2 SCAR=7.6
Pod documented
2785 224 ▶ $_[0]->{_element_class} || __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"_element_class"} or __PACKAGE__ 0 224 224
2786 }
2787  
2788 23 1;
2789  
2790  
2791 23 1;
2792  
2793 __END__
« TreeBuilder.pm ↑ Summary Element/traverse.pm »