View/HTML.pm

filter: Statementstmt 98.8% 164 / 166 Branchbran 94.2% 66 / 70 Conditioncond 62.5% 15 / 24 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 23 / 23 Podpod 100% 4 / 4 totaltotal 92.0% 279 / 303 CC 46 SCAR 15.6
CC
46
Cov
94%
expandPathSimple
8.1
_processListItemConfig
7.0
expandAllComplex
7.0
CRAP 4.8
« View/ASCII.pm ↑ Summary View/DHTML.pm »
1  
2 package Tree::Simple::View::HTML;
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.20';
8  
9 4 ▶use parent 'Tree::Simple::View';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Tree::Simple::View::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use constant OPEN_TAG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use constant CLOSE_TAG => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use constant EXPANDED => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 my(%tags) =
18 (
19 xhtml => { OL => 'ol', UL => 'ul', LI => 'li', STYLE => q{ style='}, CLASS => q{ class='} },
20 html => { OL => 'OL', UL => 'UL', LI => 'LI', STYLE => q{ STYLE='}, CLASS => q{ CLASS='} },
21 );
22  
23 ## public methods
24  
25 8 ▶sub expandPathSimple {
Subroutine expandPathSimple: called CC=8 SCAR=20.9
Pod documented
26 8 my ($self, $tree, $current_path, @path) = @_;
27 8 my @results;
28 # if we were not called from this routine, and
29 # include trunk has been turned on then, this is
30 # the first time we have been called, so ...
31 8 ▶ if ($self->{include_trunk} && (caller(1))[3] !~ /expandPathSimple$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'include_trunk'} and not +(caller 1)[3] =~ /expandPathSimple$/) { } 1 7 8
Condition100%3/3
$self->{"include_trunk"} and not +(caller 1)[3] =~ /expandPathSimple$/
!ll&&!rl&&r
431
MC/DC100%2/2
$self->{"include_trunk"} and not +(caller 1)[3] =~ /expandPathSimple$/
$self->{"include_trunk"} not +(caller 1)[3] =~ /expandPathSimple$/
Truth table100%3/3
A and not(B)
A= $self->{"include_trunk"}B= not +(caller 1)[3] =~ /expandPathSimple$/
ABresult
0X0
100
111
32 1 push @results => '<UL>';
33 1 push @results => ('<LI>' . $tree->getNodeValue() . '</LI>');
34 # now recurse but dont change any of the args,
35 # (if we are supposed to that is, based on the path)
36 1 ▶ push @results => ($self->expandPathSimple($tree, @path))
Branch: 1 branch
BranchTrueFalseTotal
if defined $current_path and $tree->getNodeValue eq $current_path 1 0 1
Condition33%1/3
defined $current_path and $tree->getNodeValue eq $current_path
!ll&&!rl&&r
001
MC/DC0%0/2
defined $current_path and $tree->getNodeValue eq $current_path
defined $current_path $tree->getNodeValue eq $current_path
Truth table33%1/3
A and B
A= defined $current_pathB= $tree->getNodeValue eq $current_path
ABresult
0X0
100
111
37 if (defined $current_path && $tree->getNodeValue() eq $current_path);
38 1 push @results => '</UL>';
39 }
40 else {
41 7 push @results => '<UL>';
42 7 foreach my $child ($tree->getAllChildren()) {
43 22 ▶ if (defined $current_path && $self->_compareNodeToPath($current_path, $child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_path and $self->_compareNodeToPath($current_path, $child)) { } 4 18 22
Condition100%3/3
defined $current_path and $self->_compareNodeToPath($current_path, $child)
!ll&&!rl&&r
8104
MC/DC100%2/2
defined $current_path and $self->_compareNodeToPath($current_path, $child)
defined $current_path $self->_compareNodeToPath($current_path, $child)
Truth table100%3/3
A and B
A= defined $current_pathB= $self->_compareNodeToPath($current_path, $child)
ABresult
0X0
100
111
44 4 push @results => ('<LI>' . $child->getNodeValue() . '</LI>');
45 4 push @results => ($self->expandPathSimple($child, @path));
46 }
47 else {
48 18 push @results => ('<LI>' . $child->getNodeValue() . '</LI>');
49 }
50 }
51 7 push @results => '</UL>';
52 }
53 8 return (join "\n" => @results);
54 }
55  
56 10 ▶sub expandPathComplex {
Subroutine expandPathComplex: called CC=4 SCAR=14.0
Pod documented
57 10 my ($self, $tree, $config, $current_path, @path) = @_;
58 # get the config
59 10 my ($list_func, $list_item_func) = $self->_processConfig($config);
60  
61 # use the helper function to recurse
62 20 ▶ my $_expandPathComplex = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
63 20 my ($self_func, $list_func, $list_item_func, $tree, $current_path, @path) = @_;
64 20 my @results = ($list_func->(OPEN_TAG));
65 20 foreach my $child ($tree->getAllChildren()) {
66 64 ▶ if (defined $current_path && $self->_compareNodeToPath($current_path, $child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_path and $self->_compareNodeToPath($current_path, $child)) { } 10 54 64
Condition100%3/3
defined $current_path and $self->_compareNodeToPath($current_path, $child)
!ll&&!rl&&r
243010
MC/DC100%2/2
defined $current_path and $self->_compareNodeToPath($current_path, $child)
defined $current_path $self->_compareNodeToPath($current_path, $child)
Truth table100%3/3
A and B
A= defined $current_pathB= $self->_compareNodeToPath($current_path, $child)
ABresult
0X0
100
111
67 10 ▶ unless ($child->isLeaf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isLeaf) { } 0 10 10
68 10 push @results => ($list_item_func->($child, EXPANDED));
69 10 push @results => ($self_func->($self_func, $list_func, $list_item_func, $child, @path));
70 }
71 else {
72 0 push @results => ($list_item_func->($child));
73 }
74 }
75 else {
76 54 push @results => ($list_item_func->($child));
77 }
78 }
79 20 push @results => ($list_func->(CLOSE_TAG));
80 20 return (join "\n" => @results);
81 10 };
82  
83 10 my @results;
84 10 ▶ if ($self->{include_trunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'include_trunk'}) { } 2 8 10
85 2 push @results => ($list_func->(OPEN_TAG));
86 2 ▶ if (defined $current_path && $self->_compareNodeToPath($current_path, $tree)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_path and $self->_compareNodeToPath($current_path, $tree)) { } 2 0 2
Condition33%1/3
defined $current_path and $self->_compareNodeToPath($current_path, $tree)
!ll&&!rl&&r
002
MC/DC0%0/2
defined $current_path and $self->_compareNodeToPath($current_path, $tree)
defined $current_path $self->_compareNodeToPath($current_path, $tree)
Truth table33%1/3
A and B
A= defined $current_pathB= $self->_compareNodeToPath($current_path, $tree)
ABresult
0X0
100
111
87 2 push @results => ($list_item_func->($tree, EXPANDED));
88 2 push @results => $_expandPathComplex->($_expandPathComplex, $list_func, $list_item_func, $tree, @path);
89 }
90 else {
91 0 push @results => ($list_item_func->($tree));
92 }
93 2 push @results => ($list_func->(CLOSE_TAG));
94 }
95 else {
96 8 push @results => $_expandPathComplex->($_expandPathComplex, $list_func, $list_item_func, $tree, $current_path, @path);
97 }
98  
99 10 return (join "\n" => @results);
100 }
101  
102 2 ▶sub expandAllSimple {
Subroutine expandAllSimple: called CC=3 SCAR=11.0
Pod documented
103 2 my ($self) = @_;
104  
105 2 my @results = ('<UL>');
106 2 my $root_depth = $self->{tree}->getDepth() + 1;
107 2 my $last_depth = -1;
108 31 ▶ my $traversal_sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
109 31 my ($t) = @_;
110 31 my $current_depth = $t->getDepth();
111 31 ▶ push @results => ('</UL>' x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 8 23 31
112 31 push @results => ('<LI>' . $t->getNodeValue() . '</LI>');
113 31 ▶ push @results => '<UL>' unless $t->isLeaf();
Branch: 1 branch
BranchTrueFalseTotal
unless $t->isLeaf 11 20 31
114 31 $last_depth = $current_depth;
115 2 };
116 2 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 1 2
117 2 $self->{tree}->traverse($traversal_sub);
118 2 $last_depth -= $root_depth;
119 2 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 1 2
120 2 push @results => ('</UL>' x ($last_depth + 1));
121 2 return (join "\n" => @results);
122 }
123  
124 sub expandAllComplex
125 11 ▶{
Subroutine expandAllComplex: called CC=7 SCAR=19.5
Pod documented
126 11 my($self, $config) = @_;
127 11 ▶ my($html5) = $$config{html5} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'html5'} ? : 2 9 11
128 11 my($last_depth) = -1;
129 11 my($list_func, $list_item_func) = $self->_processConfig($config);
130 10 my($root_depth) = $self->{tree}->getDepth + 1;
131  
132 10 my(@results);
133  
134 10 ▶ if ($html5)
Branch: 1 branch
BranchTrueFalseTotal
if ($html5) { } 2 8 10
135 {
136 2 @results = ();
137 2 my($css, $expanded_css, $node_formatter) = $self->_processListItemConfig(%$config);
138 2 $css = $expanded_css;
139 my($pre_sub) = sub
140 13 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
141 13 my($t) = @_;
142 13 ▶ my($node_value) = $node_formatter ? $node_formatter->($t) : $t->getNodeValue;
Branch: 1 branch
BranchTrueFalseTotal
$node_formatter ? : 0 13 13
143  
144 13 ▶ push @results, "<$tags{html}{UL}$css>" if ($t->isFirstChild);
Branch: 1 branch
BranchTrueFalseTotal
if $t->isFirstChild 6 7 13
145 13 push @results, "<$tags{html}{LI}$css>$node_value";
146 2 };
147 my($post_sub) = sub
148 13 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
149 13 my($t) = @_;
150  
151 13 push @results, "</$tags{html}{LI}>";
152 13 ▶ push @results, "</$tags{html}{UL}>" if ($t->isLastChild);
Branch: 1 branch
BranchTrueFalseTotal
if $t->isLastChild 6 7 13
153 2 };
154  
155 2 ▶ if ($self->{include_trunk})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_trunk"}) 1 1 2
156 {
157 1 push @results, $list_func->(OPEN_TAG);
158  
159 1 $pre_sub->($self->{tree});
160 }
161  
162 2 $self->{tree}->traverse($pre_sub, $post_sub);
163  
164 2 ▶ if ($self->{include_trunk})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_trunk"}) 1 1 2
165 {
166 1 $post_sub->($self->{tree});
167  
168 1 push @results, $list_func->(CLOSE_TAG);
169 }
170 }
171 else
172 {
173 my($traversal_sub) = sub
174 122 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
175 122 my($t) = @_;
176 122 my($current_depth) = $t->getDepth;
177  
178 122 ▶ push @results, ($list_func->(CLOSE_TAG) x ($last_depth - $current_depth) ) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 32 90 122
179  
180 122 ▶ if ($t->isLeaf)
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isLeaf) { } 80 42 122
181 {
182 80 push @results, ($list_item_func->($t) );
183 }
184 else
185 {
186 42 push @results, $list_item_func->($t, EXPANDED);
187 }
188  
189 122 ▶ push @results, $list_func->(OPEN_TAG) unless $t->isLeaf;
Branch: 1 branch
BranchTrueFalseTotal
unless $t->isLeaf 42 80 122
190  
191 122 $last_depth = $current_depth;
192 8 };
193  
194 8 push @results, $list_func->(OPEN_TAG);
195  
196 8 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 2 6 8
197 8 $self->{tree}->traverse($traversal_sub);
198  
199 8 $last_depth -= $root_depth;
200  
201 8 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 2 6 8
202  
203 8 push @results, ($list_func->(CLOSE_TAG) x ($last_depth + 1) );
204 }
205  
206 10 return (join "\n" => @results);
207  
208 } # End of expandAllComplex.
209  
210 ## private methods
211  
212 # process configurations
213  
214 33 ▶sub _processConfig {
Subroutine _processConfig: called CC=5 SCAR=16.5
215 33 my ($self, $config) = @_;
216 33 my %config = %{$config};
217  
218 # Make sure the tag style is always set to something we know &
219 # set tags to be the hashref of tags we want to save extra indirection later
220 33 ▶ if ( !exists $config{ tag_style } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $config{'tag_style'}) { } 23 10 33
elsif (not exists $tags{$config{'tag_style'}}) { } 1 9 10
221 23 $config{ tags } = $tags{ html };
222 }
223 elsif ( !exists( $tags{ $config{ tag_style } }) ) {
224 1 throw Tree::Simple::View::CompilationFailed "Unknown tag_style $config{ tag_style }";
225 }
226 else {
227 9 $config{ tags } = $tags{ $config{ tag_style } };
228 }
229  
230 32 ▶ my $list_func = $self->_buildListFunction(%config)
Condition33%1/3
$self->_buildListFunction(%config) || 'Tree::Simple::View::CompilationFailed'->throw(q[List function didn't compile], $@)
l!l&&r!l&&!r
3200
MC/DC0%0/2
$self->_buildListFunction(%config) || 'Tree::Simple::View::CompilationFailed'->throw(q[List function didn't compile], $@)
$self->_buildListFunction(%config) 'Tree::Simple::View::CompilationFailed'->throw(q[List function didn't compile], $@)
Truth table33%1/3
A || B
A= $self->_buildListFunction(%config)B= 'Tree::Simple::View::CompilationFailed'->throw(q[List function didn't compile], $@)
ABresult
1X1
011
000
231 || throw Tree::Simple::View::CompilationFailed "List function didn't compile", $@;
232 32 ▶ my $list_item_func = $self->_buildListItemFunction(%config)
Condition33%1/3
$self->_buildListItemFunction(%config) || 'Tree::Simple::View::CompilationFailed'->throw(q[List item function didn't compile], $@)
l!l&&r!l&&!r
3200
MC/DC0%0/2
$self->_buildListItemFunction(%config) || 'Tree::Simple::View::CompilationFailed'->throw(q[List item function didn't compile], $@)
$self->_buildListItemFunction(%config) 'Tree::Simple::View::CompilationFailed'->throw(q[List item function didn't compile], $@)
Truth table33%1/3
A || B
A= $self->_buildListItemFunction(%config)B= 'Tree::Simple::View::CompilationFailed'->throw(q[List item function didn't compile], $@)
ABresult
1X1
011
000
233 || throw Tree::Simple::View::CompilationFailed "List item function didn't compile", $@;
234  
235 32 return ($list_func, $list_item_func);
236 }
237  
238 ## code strings to be evaluated
239  
240 4 ▶use constant LIST_FUNCTION_CODE_STRING => q|
Subroutine BEGIN: called CC=1 SCAR=0.0
241 sub {
242 my ($tag_type) = @_;
243 return '<' . $config{tags}->{$list_type} . ${list_css} . '>' if ($tag_type == OPEN_TAG);
244 return '</' . $config{tags}->{$list_type} .'>' if ($tag_type == CLOSE_TAG);
245 }
246 4 |;
247  
248 4 ▶use constant LIST_ITEM_FUNCTION_CODE_STRING => q|;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 sub {
250 my($t, $is_expanded) = @_;
251 my($item_css) = $list_item_css;
252 $item_css = $expanded_item_css if ($is_expanded && $expanded_item_css);
253 my($node_value) = $node_formatter ? $node_formatter->($t) : $t->getNodeValue;
254  
255 return "<${$config{tags} }{LI}$item_css>$node_value</${$config{tags} }{LI}>";
256 }
257 4 |;
258  
259 ## list config processing
260 32 ▶sub _processListConfig {
Subroutine _processListConfig: called CC=6 SCAR=17.9
261 32 my ($self, %config) = @_;
262  
263 32 my $list_type = 'UL';
264 32 ▶ $list_type = (($config{list_type} eq 'unordered') ? 'UL' : 'OL') if exists $config{list_type};
Branch: 2 branches
BranchTrueFalseTotal
if exists $config{"list_type"} 16 16 32
$config{'list_type'} eq 'unordered' ? : 2 14 16
265  
266 32 my $list_css = '';
267 32 ▶ if (exists $config{list_css}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'list_css'}) { } 6 26 32
elsif (exists $config{'list_css_class'}) { } 9 17 26
268 # make sure we have a proper ';' at the end
269 # of the CSS code here, it is needed by the
270 # DHTML subclass when we add the display property
271 # to it, no other element requires this so far,
272 # but if it did, this same idiom could be reused
273 6 my $_list_css = $config{list_css};
274 6 ▶ $_list_css .= ';' unless ($_list_css =~ /\;$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $_list_css =~ /\;$/ 3 3 6
275 6 $list_css = $config{tags}->{STYLE} . "${_list_css}'";
276 }
277 elsif (exists $config{list_css_class}) {
278 9 $list_css = $config{tags}->{CLASS} . $config{list_css_class} . "'";
279 }
280 # otherwise do nothing and stick with default
281  
282 32 return ($list_type, $list_css);
283 }
284  
285 32 ▶sub _buildListFunction {
Subroutine _buildListFunction: called CC=1 SCAR=0.0
286 32 my ($self, %config) = @_;
287 # process the configuration directives
288 32 my ($list_type, $list_css) = $self->_processListConfig(%config);
289 # now compile the subroutine in the current environment
290 32 return eval $self->LIST_FUNCTION_CODE_STRING;
291 }
292  
293 ## list item config processing
294  
295 34 ▶sub _processListItemConfig {
Subroutine _processListItemConfig: called CC=7 SCAR=19.5
296 34 my ($self, %config) = @_;
297  
298 34 my $list_item_css = '';
299 34 ▶ if (exists $config{list_item_css}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'list_item_css'}) { } 6 28 34
elsif (exists $config{'list_item_css_class'}) { } 9 19 28
300 6 $list_item_css = $config{tags}->{STYLE} . $config{list_item_css} . "'";
301 }
302 elsif (exists $config{list_item_css_class}) {
303 9 $list_item_css = $config{tags}->{CLASS} . $config{list_item_css_class} . "'";
304 }
305 # otherwise do nothing and stick with default
306  
307 34 my $expanded_item_css = '';
308 34 ▶ if (exists $config{expanded_item_css}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'expanded_item_css'}) { } 6 28 34
elsif (exists $config{'expanded_item_css_class'}) { } 9 19 28
309 6 $expanded_item_css = $config{tags}->{STYLE} . $config{expanded_item_css} . "'";
310 }
311 elsif (exists $config{expanded_item_css_class}) {
312 9 $expanded_item_css = $config{tags}->{CLASS} . $config{expanded_item_css_class} . "'";
313 }
314 # otherwise do nothing and stick with default
315  
316 34 my $node_formatter;
317 $node_formatter = $config{node_formatter}
318 34 ▶ if (exists $config{node_formatter} && ref($config{node_formatter}) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if exists $config{"node_formatter"} and ref $config{"node_formatter"} eq "CODE" 15 19 34
Condition66%2/3
exists $config{"node_formatter"} and ref $config{"node_formatter"} eq "CODE"
!ll&&!rl&&r
19015
MC/DC50%1/2
exists $config{"node_formatter"} and ref $config{"node_formatter"} eq "CODE"
exists $config{"node_formatter"} ref $config{"node_formatter"} eq "CODE"
Truth table66%2/3
A and B
A= exists $config{"node_formatter"}B= ref $config{"node_formatter"} eq "CODE"
ABresult
0X0
100
111
319  
320 34 return ($list_item_css, $expanded_item_css, $node_formatter);
321 }
322  
323 20 ▶sub _buildListItemFunction {
Subroutine _buildListItemFunction: called CC=1 SCAR=0.0
324 20 my ($self, %config) = @_;
325 # process the configuration directives
326 20 my ($list_item_css, $expanded_item_css, $node_formatter) = $self->_processListItemConfig(%config);
327 # now compile the subroutine in the current environment
328 20 return eval $self->LIST_ITEM_FUNCTION_CODE_STRING;
329 }
330  
331 4 1;
332  
333 __END__
« View/ASCII.pm ↑ Summary View/DHTML.pm »