blib/lib/Markup/TreeNode.pm

filter: Statementstmt 15.9% 23 / 144 Branchbran 6.2% 3 / 48 Conditioncond 12.0% 3 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 25.0% 4 / 16 Podpod 84.6% 11 / 13 totaltotal 16.8% 44 / 261 CC 40 SCAR 34.3
CC
40
Cov
13%
insert
90.0
previous_node
42.0
drop
42.0
CRAP 30.8
↑ Summary
1 package Markup::TreeNode;
2 1 $VERSION = '1.1.6';
3
4 ####################################################
5 # This module is protected under the terms of the
6 # GNU GPL. Please see
7 # http://www.opensource.org/licenses/gpl-license.php
8 # for more information.
9 ####################################################
10
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 require Exporter;
15
16 1 our @ISA = qw(Exporter);
17 1 our $empty = '(empty)';
18
19 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
20 1 my $invocant = shift();
21 1 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
22 1 $class = bless {
23 element_type => 'tag',
24 tagname => '',
25 attr => { },
26 level => 0,
27 parent => $empty,
28 child_num => 0,
29 children => [ ],
30 text => ''
31 }, $class;
32 1 $class->init (@_);
33 1 return $class;
34 }
35
36 1 ▶sub init {
Subroutine init: called CC=7 SCAR=25.7
Pod undocumented
37 1 my $self = shift();
38 1 my %args = @_;
39
40 1 foreach (keys %args) {
41 # enforce integrity
42 2 ▶ if ($_ eq 'parent' && $args{$_} ne $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "parent" and $args{$_} ne $Markup::TreeNode::empty) 0 2 2
Condition33%1/3
$_ eq "parent" and $args{$_} ne $Markup::TreeNode::empty
!ll&&!rl&&r
200
MC/DC0%0/2
$_ eq "parent" and $args{$_} ne $Markup::TreeNode::empty
$_ eq "parent" $args{$_} ne $Markup::TreeNode::empty
Truth table33%1/3
A and B
A= $_ eq "parent"B= $args{$_} ne $Markup::TreeNode::empty
ABresult
0X0
100
111
43 0 $self->attach_parent($args{$_});
44 0 next;
45 }
46
47 # enforce integrity
48 0 ▶ if ($_ eq 'children' && scalar(@{$args{$_}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "children" and scalar @{$args{$_};}) 0 2 2
Condition50%1/2
$_ eq "children" and scalar @{$args{$_};}
!ll
20
MC/DC0%0/1
$_ eq "children" and scalar @{$args{$_};}
$_ eq "children"
Truth table50%1/2
A
A= $_ eq "children"
Aresult
00
11
49 0 $self->attach_children($args{$_});
50 0 next;
51 }
52
53 2 ▶ if (exists $self->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$_}) { } 2 0 2
54 2 $self->{$_} = $args{$_};
55 }
56 else {
57 0 croak ("unrecognized node option $_");
58 }
59 }
60 }
61
62 0 ▶sub attach_parent {
Subroutine attach_parent: not called CC=2 SCAR=17.9
Pod documented
63 0 my ($self, $parent) = @_;
64
65 0 $self->{'parent'} = $parent;
66 # if setting parent, add us to [bottom of] parent children
67 0 ▶ my $child_count = scalar(@{$self->{'parent'}->{'children'} || []});
Condition0%0/2
$self->{'parent'}{'children'} || []
l!l
00
MC/DC0%0/1
$self->{'parent'}{'children'} || []
$self->{'parent'}{'children'}
Truth table0%0/2
A
A= $self->{'parent'}{'children'}
Aresult
11
00
68 0 $self->{'parent'}->{'children'}->[$child_count] = $self;
69 0 $self->{'child_num'} = $child_count;
70
71 0 return $self;
72 }
73
74 0 ▶sub attach_child {
Subroutine attach_child: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self, $child) = @_;
76 0 my $child_count = scalar(@{ $self->{'children'} });
77
78 0 $self->{'children'}->[$child_count] = $child;
79 # if setting child, add us as parent of child
80 0 $child->{'parent'} = $self;
81 0 $child->{'child_num'} = $child_count;
82
83 0 return $self;
84 }
85
86 0 ▶sub attach_child_before {
Subroutine attach_child_before: not called CC=2 SCAR=17.9
Pod documented
87 0 my ($self, $child) = @_;
88 0 my $child_count = scalar(@{ $self->{'children'} });
89
90 0 ▶ for (my $i = 0; $i < $child_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $child_count) 0 0 0
91 0 $self->{'children'}->[($i + 1)] = $self->{'children'}->[$i];
92 0 $self->{'children'}->[($i + 1)]->{'child_num'}++;
93 }
94
95 0 $self->{'children'}->[0] = $child;
96 # if setting child, add us as parent of child
97 0 $child->{'parent'} = $self;
98 0 $child->{'child_num'} = 0;
99
100 0 return $self;
101 }
102
103
104 0 ▶sub attach_children {
Subroutine attach_children: not called CC=3 SCAR=24.8
Pod documented
105 0 my ($self, $childref) = @_;
106 0 my $cnt = 0;
107
108 0 $self->{'children'} = $childref;
109 # if setting children, add us as parent of all children
110 0 foreach (@{ $self->{'children'} }) {
111 0 ▶ if (!UNIVERSAL::isa($_, 'Markup::TreeNode')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($_, "Markup::TreeNode")) 0 0 0
112 0 croak ("$_ is not a recognized child");
113 }
114
115 0 $_->{'parent'} = $self;
116 0 $_->{'child_num'} = $cnt++;
117 }
118
119 0 return $self;
120 }
121
122 0 ▶sub get_text {
Subroutine get_text: not called CC=3 SCAR=24.8
Pod documented
123 0 my $self = shift();
124
125 0 ▶ if ($self->{'element_type'} eq '-->text') { return $self->{'text'}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"element_type"} eq "-->text") 0 0 0
126
127 0 my $next_node = $self->next_node();
128
129 0 ▶ return (($next_node->{'element_type'} eq '-->text') ? $next_node->{'text'} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$next_node->{'element_type'} eq '-->text' ? : 0 0 0
130 }
131
132 0 ▶sub next_node {
Subroutine next_node: not called CC=2 SCAR=17.9
Pod documented
133 0 my $self = shift();
134
135 0 ▶ if (scalar(@{ $self->{'children'} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"children"};}) 0 0 0
136 0 return $self->{'children'}->[0];
137 }
138
139 0 ▶ my $recurse = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
140 0 my ($me, $myself) = @_;
141 0 ▶ if ($myself->{'parent'} ne $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($myself->{"parent"} ne $Markup::TreeNode::empty) 0 0 0
142 0 ▶ if ($myself->{'child_num'} < (scalar(@{ $myself->{'parent'}->{'children'} || [] }) - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($myself->{"child_num"} < scalar(@{$myself->{"parent"}{"children"} or [];}) - 1) 0 0 0
Condition0%0/2
$myself->{'parent'}{'children'} || []
l!l
00
MC/DC0%0/1
$myself->{'parent'}{'children'} || []
$myself->{'parent'}{'children'}
Truth table0%0/2
A
A= $myself->{'parent'}{'children'}
Aresult
11
00
143 0 return ($myself->{'parent'}->{'children'}->[($myself->{'child_num'} + 1)]);
144 }
145
146 0 return ($me->($me, $myself->{'parent'}));
147 }
148
149 0 return undef;
150 0 };
151
152 0 return ($recurse->($recurse, $self));
153 }
154
155 0 ▶sub previous_node {
Subroutine previous_node: not called CC=6 SCAR=37.4
Pod documented
156 0 my $self = shift();
157
158 0 ▶ if ($self->{'parent'} ne $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"} ne $Markup::TreeNode::empty) 0 0 0
159 0 ▶ if (($self->{'child_num'} > 0) && (scalar(@{ $self->{'parent'}->{'children'} }) >= 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"child_num"} > 0 and scalar @{$self->{"parent"}{"children"};} >= 1) 0 0 0
Condition0%0/3
$self->{"child_num"} > 0 and scalar @{$self->{"parent"}{"children"};} >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"child_num"} > 0 and scalar @{$self->{"parent"}{"children"};} >= 1
$self->{"child_num"} > 0 scalar @{$self->{"parent"}{"children"};} >= 1
Truth table0%0/3
A and B
A= $self->{"child_num"} > 0B= scalar @{$self->{"parent"}{"children"};} >= 1
ABresult
0X0
100
111
160 0 my $ret = $self->{'parent'}->{'children'}->[($self->{'child_num'} - 1)];
161 0 ▶ while (scalar(@{$ret->{'children'} || []})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$ret->{"children"} or [];}) 0 0 0
Condition0%0/2
$ret->{'children'} || []
l!l
00
MC/DC0%0/1
$ret->{'children'} || []
$ret->{'children'}
Truth table0%0/2
A
A= $ret->{'children'}
Aresult
11
00
162 0 $ret = $ret->{'children'}->[(scalar(@{$ret->{'children'}}) - 1)];
163 }
164 0 return ($ret);
165 }
166
167 0 return ($self->{'parent'});
168 }
169
170 0 return undef;
171 }
172
173 0 ▶sub drop {
Subroutine drop: not called CC=6 SCAR=37.4
Pod documented
174 0 my $self = shift();
175 0 my $parent = $self->{'parent'};
176
177 0 ▶ return ($self) if ($parent eq $empty);
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $Markup::TreeNode::empty 0 0 0
178
179 0 splice @{ $parent->{'children'} }, $self->{'child_num'}--, 1;
180
181 0 ▶ if ($self->{'child_num'} < (scalar(@{ $parent->{'children'} || [] })) && $self->{'child_num'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"child_num"} < scalar @{$parent->{"children"} or [];} and $self->{"child_num"} > 0) 0 0 0
Condition0%0/5
$self->{"child_num"} < scalar @{$parent->{"children"} or [];} and $self->{"child_num"} > 0
!ll&&!rl&&r
000
$parent->{'children'} || []
l!l
00
MC/DC0%0/2
$self->{"child_num"} < scalar @{$parent->{"children"} or [];} and $self->{"child_num"} > 0
$self->{"child_num"} < scalar @{$parent->{"children"} or [];} $self->{"child_num"} > 0
MC/DC0%0/1
$parent->{'children'} || []
$parent->{'children'}
Truth table0%0/3
A and B
A= $self->{"child_num"} < scalar @{$parent->{"children"} or [];}B= $self->{"child_num"} > 0
ABresult
0X0
100
111
Truth table0%0/2
A
A= $parent->{'children'}
Aresult
11
00
182 0 ▶ for (my $i = $self->{'child_num'}; $i < scalar(@{ $parent->{'children'} }); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$parent->{"children"};}) 0 0 0
183 0 $parent->{'children'}->[$i]->{'child_num'} = $i;
184 }
185 }
186
187 0 $self->{'parent'} = $empty;
188
189 0 return ($self);
190 }
191
192 0 ▶sub replace {
Subroutine replace: not called CC=2 SCAR=17.9
Pod documented
193 0 my ($self, $node) = @_;
194
195 0 ▶ if (!UNIVERSAL::isa($node, 'Markup::TreeNode')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($node, "Markup::TreeNode")) 0 0 0
196 0 croak ("Node is not a Markup::TreeNode");
197 }
198
199 0 $self->insert($node, 'after');
200 0 return ($self->drop());
201 }
202
203 0 ▶sub insert {
Subroutine insert: not called CC=9 SCAR=45.0
Pod documented
204 0 my ($self, $node, $position) = @_;
205 0 ▶ $position = 'after' if (!$position);
Branch: 1 branch
BranchTrueFalseTotal
unless $position 0 0 0
206 0 my $child_num;
207
208 0 ▶ if (($position ne 'after') && ($position ne 'before')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position ne "after" and $position ne "before") 0 0 0
Condition0%0/3
$position ne "after" and $position ne "before"
!ll&&!rl&&r
000
MC/DC0%0/2
$position ne "after" and $position ne "before"
$position ne "after" $position ne "before"
Truth table0%0/3
A and B
A= $position ne "after"B= $position ne "before"
ABresult
0X0
100
111
209 0 croak ("Unknown position '$position'");
210 }
211
212 0 ▶ if (!UNIVERSAL::isa($node, 'Markup::TreeNode')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($node, "Markup::TreeNode")) 0 0 0
213 0 croak ("Node is not a Markup::TreeNode");
214 }
215
216 0 $child_num = $self->{'child_num'} + ($position eq 'after'); # yes, I know what that means :)
217 0 ▶ $child_num = 0 if ($child_num < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $child_num < 0 0 0 0
218
219 0 ▶ if ($self->{'parent'} eq $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"} eq $Markup::TreeNode::empty) 0 0 0
220 0 return ($self->attach_child($node));
221 }
222
223 0 for ($self->{'parent'}->{'children'}) {
224 0 my $oglen = scalar(@{ $_ });
225 0 ▶ for (my $i = $oglen; $i >= $child_num; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $child_num) 0 0 0
226 0 $_->[($i + 1)] = $_->[$i];
227 0 $_->[($i + 1)]->{'child_num'}++;
228 }
229
230 0 splice (@{$_}, ++$oglen);
231
232 0 $_->[$child_num] = $node;
233 0 $_->[$child_num]->{'parent'} = $self->{'parent'};
234 0 $_->[$child_num]->{'child_num'} = $child_num;
235 0 return ($_->[$child_num]);
236 }
237 }
238
239 0 ▶sub copy_of {
Subroutine copy_of: not called CC=4 SCAR=30.0
Pod documented
240 0 my $self = shift();
241 0 my ($newbie => %options); # if you don't know you betta' axe somebody!
242
243 0 foreach (keys %{ $self }) {
244 0 $options{$_} = $self->{$_};
245 }
246
247 0 for ($self->{'children'}) {
248 0 my $a = scalar(@{$_});
249 0 ▶ for (my $i = 0; $i < $a; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $a) 0 0 0
250 0 $options{'children'}->[$i] = $_->[$i]->copy_of();
251 }
252 }
253
254 0 return ($self->new(%options));
255 }
256
257 1 1;
258
259 __END__
↑ Summary