blib/lib/HTML/Widgets/NavMenu.pm

filter: Statementstmt 99.0% 326 / 329 Branchbran 93.1% 95 / 102 Conditioncond 91.3% 42 / 46 MC/DCmcdc 88.8% 24 / 27 Subroutinesub 100% 54 / 54 Podpod 100% 4 / 4 totaltotal 96.9% 545 / 562 SCAR 42.6
CC
71
Cov
97%
_get_leading_path_of_coords
12.6
_get_next_coords
7.0
_get_up_coords
6.0
CRAP 71.1
↑ Summary blib/lib/HTML/Widgets/NavMenu/Url.pm »
1 package HTML::Widgets::NavMenu;
2 14 $HTML::Widgets::NavMenu::VERSION = '1.0901';
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package HTML::Widgets::NavMenu::Error;
9 14 $HTML::Widgets::NavMenu::Error::VERSION = '1.0901';
10 14 ▶use parent "HTML::Widgets::NavMenu::Object";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package HTML::Widgets::NavMenu::Error::Redirect;
13 14 $HTML::Widgets::NavMenu::Error::Redirect::VERSION = '1.0901';
14 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 @ISA = ("HTML::Widgets::NavMenu::Error");
17  
18 sub CGIpm_perform_redirect
19 {
20 2 ▶ my $self = shift;
Subroutine CGIpm_perform_redirect: called CC=1 SCAR=0.0
21  
22 2 my $cgi = shift;
23  
24 2 print $cgi->redirect( $cgi->script_name() . $self->{-redirect_path} );
25 2 exit;
26 }
27  
28 package HTML::Widgets::NavMenu::NodeDescription;
29 14 $HTML::Widgets::NavMenu::NodeDescription::VERSION = '1.0901';
30 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 14 ▶use parent qw(HTML::Widgets::NavMenu::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 14 __PACKAGE__->mk_acc_ref( [qw(host host_url title label direct_url url_type)] );
35  
36 sub _init
37 {
38 219 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=2 SCAR=6.9
39  
40 219 ▶ while ( my ( $k, $v ) = each(%$args) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 1314 219 1533
41 {
42 1314 $self->$k($v);
43 }
44  
45 219 return 0;
46 }
47  
48 14 1;
49  
50 package HTML::Widgets::NavMenu::LeadingPath::Component;
51 14 $HTML::Widgets::NavMenu::LeadingPath::Component::VERSION = '1.0901';
52 14 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 14 @ISA = (qw(HTML::Widgets::NavMenu::NodeDescription));
55  
56 package HTML::Widgets::NavMenu::Iterator::GetCurrentlyActive;
57 14 $HTML::Widgets::NavMenu::Iterator::GetCurrentlyActive::VERSION = '1.0901';
58 14 ▶use parent 'HTML::Widgets::NavMenu::Iterator::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 14 __PACKAGE__->mk_acc_ref(
61 [
62 qw(
63 _item_found
64 _leading_path_coords
65 _ret_coords
66 _temp_coords
67 _tree
68 )
69 ]
70 );
71  
72 sub _init
73 {
74 52 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
75 52 my $args = shift;
76  
77 52 $self->SUPER::_init($args);
78  
79 52 $self->_tree( $args->{'tree'} );
80  
81 52 $self->_item_found(0);
82  
83 52 return 0;
84 }
85  
86 sub get_initial_node
87 {
88 52 ▶ my $self = shift;
Subroutine get_initial_node: called CC=1 SCAR=0.0
89  
90 52 return $self->_tree;
91 }
92  
93 sub item_matches
94 {
95 270 ▶ my $self = shift;
Subroutine item_matches: called CC=3 SCAR=11.0
96 270 my $item = $self->top();
97 270 my $url = $item->_node()->url();
98 270 my $nav_menu = $self->nav_menu();
99 270 ▶ return ( ( $item->_accum_state()->{'host'} eq $nav_menu->current_host() )
Condition100%6/6
$item->_accum_state->{'host'} eq $nav_menu->current_host && (defined $url && $url eq $nav_menu->path_info)
!ll&&!rl&&r
3021327
defined $url and $url eq $nav_menu->path_info
!ll&&!rl&&r
5016327
MC/DC100%3/3
$item->_accum_state->{'host'} eq $nav_menu->current_host && (defined $url && $url eq $nav_menu->path_info)
$item->_accum_state->{'host'} eq $nav_menu->current_host defined $url $url eq $nav_menu->path_info
Truth table100%4/4
A && B and C
A= $item->_accum_state->{'host'} eq $nav_menu->current_hostB= defined $urlC= $url eq $nav_menu->path_info
ABCresult
0XX0
10X0
1100
1111
100 && ( defined($url) && ( $url eq $nav_menu->path_info() ) ) );
101 }
102  
103 sub does_item_expand
104 {
105 243 ▶ my $self = shift;
Subroutine does_item_expand: called CC=1 SCAR=0.0
106 243 my $item = $self->top();
107 243 return $item->_node()->capture_expanded();
108 }
109  
110 sub node_start
111 {
112 270 ▶ my $self = shift;
Subroutine node_start: called CC=3 SCAR=11.0
113  
114 270 ▶ if ( $self->item_matches() )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->item_matches) { } 27 243 270
elsif ($self->does_item_expand) { } 23 220 243
115 {
116 27 my @coords = @{ $self->get_coords() };
117 27 $self->_ret_coords( [@coords] );
118 27 $self->_temp_coords( [ @coords, (-1) ] );
119 27 $self->top()->_node()->mark_as_current();
120 27 $self->_item_found(1);
121 }
122 elsif ( $self->does_item_expand() )
123 {
124 23 my @coords = @{ $self->get_coords() };
125 23 $self->_leading_path_coords( [@coords] );
126 }
127 }
128  
129 sub node_end
130 {
131 270 ▶ my $self = shift;
Subroutine node_end: called CC=3 SCAR=11.0
132 270 ▶ if ( $self->_item_found() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_item_found) 61 209 270
133 {
134 # Skip the first node, because the coords refer
135 # to the nodes below it.
136 61 my $idx = pop( @{ $self->_temp_coords() } );
137 61 ▶ if ( $idx >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 34 27 61
138 {
139 34 my $node = $self->top()->_node();
140 34 $node->update_based_on_sub( $node->get_nth_sub($idx) );
141 }
142 }
143 }
144  
145 sub node_should_recurse
146 {
147 488 ▶ my $self = shift;
Subroutine node_should_recurse: called CC=1 SCAR=0.0
148 488 return ( !$self->_item_found() );
149 }
150  
151 sub get_final_coords
152 {
153 52 ▶ my $self = shift;
Subroutine get_final_coords: called CC=1 SCAR=0.0
154  
155 52 return $self->_ret_coords();
156 }
157  
158 sub _get_leading_path_coords
159 {
160 52 ▶ my $self = shift;
Subroutine _get_leading_path_coords: called CC=2 SCAR=6.9
161  
162 52 ▶ return ( $self->_ret_coords() || $self->_leading_path_coords() );
Condition100%3/3
$self->_ret_coords || $self->_leading_path_coords
l!l&&r!l&&!r
27916
MC/DC100%2/2
$self->_ret_coords || $self->_leading_path_coords
$self->_ret_coords $self->_leading_path_coords
Truth table100%3/3
A || B
A= $self->_ret_coordsB= $self->_leading_path_coords
ABresult
1X1
011
000
163 }
164  
165 package HTML::Widgets::NavMenu;
166  
167 14 ▶use parent 'HTML::Widgets::NavMenu::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 14 ▶use HTML::Widgets::NavMenu::Url ();
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 14 require HTML::Widgets::NavMenu::Iterator::NavMenu;
172 14 require HTML::Widgets::NavMenu::Iterator::SiteMap;
173 14 require HTML::Widgets::NavMenu::Tree::Node;
174 14 require HTML::Widgets::NavMenu::Predicate;
175  
176 14 __PACKAGE__->mk_acc_ref(
177 [
178 qw(
179 _current_coords
180 coords_stop
181 current_host
182 _hosts
183 _no_leading_dot
184 _leading_path_coords
185 path_info
186 _traversed_tree
187 _tree_contents
188 _ul_classes
189 )
190 ]
191 );
192  
193 sub _init
194 {
195 56 ▶ my $self = shift;
Subroutine _init: called CC=5 SCAR=16.1
196  
197 56 my %args = (@_);
198  
199 56 $self->_register_path_info( \%args );
200  
201 54 $self->_hosts( $args{hosts} );
202 54 $self->_tree_contents( $args{tree_contents} );
203  
204 $self->current_host( $args{current_host} )
205 54 ▶ or die "Current host was not specified.";
Branch: 1 branch
BranchTrueFalseTotal
$self->current_host($args{"current_host"}) or die "Current host was not specified." 1 53 54
206  
207 53 ▶ $self->_ul_classes( $args{'ul_classes'} || [] );
Condition100%2/2
$args{'ul_classes'} || []
l!l
2726
MC/DC100%1/1
$args{'ul_classes'} || []
$args{'ul_classes'}
Truth table100%2/2
A
A= $args{'ul_classes'}
Aresult
11
00
208  
209 $self->_no_leading_dot(
210 53 ▶ exists( $args{'no_leading_dot'} ) ? $args{'no_leading_dot'} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'no_leading_dot'} ? : 1 52 53
211  
212 53 ▶ $self->coords_stop( $args{coords_stop} || 0 );
Condition50%1/2
$args{'coords_stop'} || 0
l!l
053
MC/DC0%0/1
$args{'coords_stop'} || 0
$args{'coords_stop'}
Truth table50%1/2
A
A= $args{'coords_stop'}
Aresult
11
00
213  
214 53 return 0;
215 }
216  
217 sub _get_nav_menu_traverser_args
218 {
219 276 ▶ my $self = shift;
Subroutine _get_nav_menu_traverser_args: called CC=1 SCAR=0.0
220  
221 return {
222 276 'nav_menu' => $self,
223 'ul_classes' => $self->_ul_classes(),
224 };
225 }
226  
227 sub _get_nav_menu_traverser
228 {
229 228 ▶ my $self = shift;
Subroutine _get_nav_menu_traverser: called CC=1 SCAR=0.0
230  
231 228 return HTML::Widgets::NavMenu::Iterator::NavMenu->new(
232 $self->_get_nav_menu_traverser_args() );
233 }
234  
235 sub _get_current_coords
236 {
237 188 ▶ my $self = shift;
Subroutine _get_current_coords: called CC=1 SCAR=0.0
238  
239 # This is to make sure $self->_current_coords() is generated.
240 188 $self->_get_traversed_tree();
241  
242 188 return [ @{ $self->_current_coords() } ];
243 }
244  
245 sub _register_path_info
246 {
247 56 ▶ my $self = shift;
Subroutine _register_path_info: called CC=4 SCAR=13.9
248 56 my $args = shift;
249  
250 56 my $path_info = $args->{path_info};
251  
252 56 my $redir_path = undef;
253  
254 56 ▶ if ( $path_info eq "" )
Branch: 2 branches
BranchTrueFalseTotal
if ($path_info eq '') { } 1 55 56
elsif ($path_info =~ m[//$]u) { } 1 54 55
255 {
256 1 $redir_path = "";
257 }
258 elsif ( $path_info =~ m/\/\/$/ )
259 {
260 1 my $path = $path_info;
261 1 $path =~ s{\/+$}{};
262 1 $redir_path = $path;
263 }
264  
265 56 ▶ if ( defined($redir_path) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redir_path) 2 54 56
266 {
267 2 my $error = HTML::Widgets::NavMenu::Error::Redirect->new();
268  
269 2 $error->{'-redirect_path'} = ( $redir_path . "/" );
270 2 $error->{'msg'} = "Need to redirect";
271  
272 2 die $error;
273 }
274  
275 54 $path_info =~ s!^\/!!;
276  
277 54 $self->path_info($path_info);
278  
279 54 return 0;
280 }
281  
282 sub _is_slash_terminated
283 {
284 1162 ▶ my $string = shift;
Subroutine _is_slash_terminated: called CC=2 SCAR=6.9
285 1162 ▶ return ( ( $string =~ /\/$/ ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$string =~ m[/$]u ? : 796 366 1162
286 }
287  
288 sub _text_to_url_obj
289 {
290 775 ▶ my $text = shift;
Subroutine _text_to_url_obj: called CC=2 SCAR=6.9
291 775 ▶ my $url =
Condition100%3/3
_is_slash_terminated($text) || $text eq ''
l!l&&r!l&&!r
501171103
MC/DC100%2/2
_is_slash_terminated($text) || $text eq ''
_is_slash_terminated($text) $text eq ''
Truth table100%3/3
A || B
A= _is_slash_terminated($text)B= $text eq ''
ABresult
1X1
011
000
292 HTML::Widgets::NavMenu::Url->new( $text,
293 ( _is_slash_terminated($text) || ( $text eq "" ) ), "server", );
294 775 return $url;
295 }
296  
297 sub _get_relative_url
298 {
299 387 ▶ my $from_text = shift;
Subroutine _get_relative_url: called CC=1 SCAR=0.0
300 387 my $to_text = shift(@_);
301 387 my $no_leading_dot = shift;
302  
303 387 my $from_url = _text_to_url_obj($from_text);
304 387 my $to_url = _text_to_url_obj($to_text);
305 387 my $ret =
306 $from_url->_get_relative_url( $to_url, _is_slash_terminated($from_text),
307 $no_leading_dot, );
308 387 return $ret;
309 }
310  
311 sub _get_full_abs_url
312 {
313 39 ▶ my ( $self, $args ) = @_;
Subroutine _get_full_abs_url: called CC=1 SCAR=0.0
314  
315 39 my $host = $args->{host};
316 39 my $host_url = $args->{host_url};
317  
318 39 return ( $self->_hosts->{$host}->{base_url} . $host_url );
319 }
320  
321 sub get_cross_host_rel_url_ref
322 {
323 447 ▶ my ( $self, $args ) = @_;
Subroutine get_cross_host_rel_url_ref: called CC=6 SCAR=17.9
Pod documented
324  
325 447 my $host = $args->{host};
326 447 my $host_url = $args->{host_url};
327 447 my $url_type = $args->{url_type};
328 447 my $url_is_abs = $args->{url_is_abs};
329  
330 447 ▶ if ($url_is_abs)
Branch: 4 branches
BranchTrueFalseTotal
if ($url_is_abs) { } 15 432 447
elsif ($host ne $self->current_host or $url_type eq 'full_abs') { } 39 393 432
elsif ($url_type eq 'rel') { } 386 7 393
elsif ($url_type eq 'site_abs') { } 6 1 7
331 {
332 15 return $host_url;
333 }
334 ▶ elsif ( ( $host ne $self->current_host() ) || ( $url_type eq "full_abs" ) )
Condition100%3/3
$host ne $self->current_host or $url_type eq "full_abs"
l!l&&r!l&&!r
2217393
MC/DC100%2/2
$host ne $self->current_host or $url_type eq "full_abs"
$host ne $self->current_host $url_type eq "full_abs"
Truth table100%3/3
A or B
A= $host ne $self->current_hostB= $url_type eq "full_abs"
ABresult
1X1
011
000
335 {
336 39 return $self->_get_full_abs_url($args);
337 }
338 elsif ( $url_type eq "rel" )
339 {
340 # TODO : convert to a method.
341 386 return _get_relative_url( $self->path_info(), $host_url,
342 $self->_no_leading_dot() );
343 }
344 elsif ( $url_type eq "site_abs" )
345 {
346 6 return ( $self->_hosts->{$host}->{trailing_url_base} . $host_url );
347 }
348 else
349 {
350 1 die "Unknown url_type \"$url_type\"!\n";
351 }
352 }
353  
354 sub get_cross_host_rel_url
355 {
356 13 ▶ my $self = shift;
Subroutine get_cross_host_rel_url: called CC=1 SCAR=0.0
Pod documented
357  
358 13 return $self->get_cross_host_rel_url_ref( {@_} );
359 }
360  
361 sub _get_url_to_item
362 {
363 434 ▶ my $self = shift;
Subroutine _get_url_to_item: called CC=2 SCAR=6.9
364 434 my $item = shift;
365  
366 return $self->get_cross_host_rel_url_ref(
367 {
368 434 ▶ 'host' => $item->_accum_state()->{'host'},
Condition100%2/2
$item->_node->url || ''
l!l
257177
MC/DC100%1/1
$item->_node->url || ''
$item->_node->url
Truth table100%2/2
A
A= $item->_node->url
Aresult
11
00
369 'host_url' => ( $item->_node->url() || "" ),
370 'url_type' => $item->get_url_type(),
371 'url_is_abs' => $item->_node->url_is_abs(),
372 }
373 );
374 }
375  
376 sub _gen_blank_nav_menu_tree_node
377 {
378 353 ▶ my $self = shift;
Subroutine _gen_blank_nav_menu_tree_node: called CC=1 SCAR=0.0
379  
380 353 return HTML::Widgets::NavMenu::Tree::Node->new();
381 }
382  
383 sub _create_predicate
384 {
385 38 ▶ my ( $self, $args ) = @_;
Subroutine _create_predicate: called CC=1 SCAR=0.0
386  
387 38 return HTML::Widgets::NavMenu::Predicate->new( 'spec' => $args->{'spec'}, );
388 }
389  
390 sub _create_new_nav_menu_item
391 {
392 353 ▶ my ( $self, $args ) = @_;
Subroutine _create_new_nav_menu_item: called CC=3 SCAR=11.0
393  
394 353 my $sub_contents = $args->{sub_contents};
395  
396 353 my $new_item = $self->_gen_blank_nav_menu_tree_node();
397  
398 353 $new_item->set_values_from_hash_ref($sub_contents);
399  
400 353 ▶ if ( exists( $sub_contents->{'expand'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sub_contents->{"expand"}) 38 315 353
401 {
402 my $expand_val = $self->_create_predicate(
403 {
404 38 'spec' => $sub_contents->{'expand'},
405 }
406 )->evaluate(
407 'path_info' => $self->path_info(),
408 'current_host' => $self->current_host(),
409 );
410 38 ▶ if ($expand_val)
Branch: 1 branch
BranchTrueFalseTotal
if ($expand_val) 18 20 38
411 {
412 18 $new_item->expand($expand_val);
413 }
414 }
415  
416 353 return $new_item;
417 }
418  
419 sub _render_tree_contents
420 {
421 353 ▶ my $self = shift;
Subroutine _render_tree_contents: called CC=3 SCAR=11.0
422 353 my $sub_contents = shift;
423  
424 353 my $path_info = $self->path_info();
425  
426 353 my $new_item =
427 $self->_create_new_nav_menu_item( { sub_contents => $sub_contents }, );
428  
429 353 ▶ if ( exists( $sub_contents->{subs} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sub_contents->{"subs"}) 127 226 353
430 {
431 127 foreach my $sub_contents_sub ( @{ $sub_contents->{subs} } )
432 {
433 301 $new_item->add_sub(
434 $self->_render_tree_contents( $sub_contents_sub, ) );
435 }
436 }
437 353 return $new_item;
438 }
439  
440 sub gen_site_map
441 {
442 6 ▶ my $self = shift;
Subroutine gen_site_map: called CC=1 SCAR=0.0
Pod documented
443  
444 6 my $iterator = HTML::Widgets::NavMenu::Iterator::SiteMap->new(
445 {
446 'nav_menu' => $self,
447 }
448 );
449  
450 6 $iterator->traverse();
451  
452 6 return $iterator->get_results();
453 }
454  
455 sub _get_next_coords
456 {
457 52 ▶ my $self = shift;
Subroutine _get_next_coords: called CC=7 SCAR=19.5
458  
459 52 ▶ my @coords = @{ shift || $self->_get_current_coords };
Branch: 1 branch
BranchTrueFalseTotal
shift() or $self->_get_current_coords 2 50 52
460  
461 52 my @branches = ( $self->_get_traversed_tree() );
462  
463 52 my @dest_coords;
464  
465 my $i;
466  
467 52 ▶ for ( $i = 0 ; $i < scalar(@coords) ; ++$i )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @coords) 43 52 95
468 {
469 43 $branches[ $i + 1 ] = $branches[$i]->get_nth_sub( $coords[$i] );
470 }
471  
472 52 ▶ if ( $branches[$i]->_num_subs() )
Branch: 1 branch
BranchTrueFalseTotal
if ($branches[$i]->_num_subs) { } 30 22 52
473 {
474 30 @dest_coords = ( @coords, 0 );
475 }
476 else
477 {
478 22 ▶ for ( --$i ; $i >= 0 ; --$i )
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 29 3 32
479 {
480 29 ▶ if ( $branches[$i]->_num_subs() > ( $coords[$i] + 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($branches[$i]->_num_subs > $coords[$i] + 1) 19 10 29
481 {
482 19 @dest_coords = ( @coords[ 0 .. ( $i - 1 ) ], $coords[$i] + 1 );
483 19 last;
484 }
485 }
486 22 ▶ if ( $i == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i == -1) 3 19 22
487 {
488 3 return;
489 }
490 }
491  
492 49 return \@dest_coords;
493 }
494  
495 sub _get_prev_coords
496 {
497 49 ▶ my $self = shift;
Subroutine _get_prev_coords: called CC=4 SCAR=13.9
498  
499 49 ▶ my @coords = @{ shift || $self->_get_current_coords() };
Branch: 1 branch
BranchTrueFalseTotal
shift() or $self->_get_current_coords 2 47 49
500  
501 49 ▶ if ( scalar(@coords) == 0 )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @coords == 0) { } 19 30 49
elsif ($coords[$#coords] > 0) { } 23 7 30
502 {
503 19 return;
504 }
505 elsif ( $coords[$#coords] > 0 )
506 {
507 # Get the previous leaf
508 23 my @previous_leaf =
509 ( @coords[ 0 .. ( $#coords - 1 ) ], $coords[$#coords] - 1 );
510  
511 # Continue in this leaf to the end.
512 23 my $new_coords = $self->_get_most_advanced_leaf( \@previous_leaf );
513  
514 23 return $new_coords;
515 }
516 else
517 {
518 7 return [ @coords[ 0 .. ( $#coords - 1 ) ] ];
519 }
520 }
521  
522 sub _get_up_coords
523 {
524 130 ▶ my $self = shift;
Subroutine _get_up_coords: called CC=6 SCAR=18.0
525  
526 130 ▶ my @coords = @{ shift || $self->_get_current_coords };
Branch: 1 branch
BranchTrueFalseTotal
shift() or $self->_get_current_coords 45 85 130
527  
528 130 ▶ if ( scalar(@coords) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @coords == 0) { } 19 111 130
529 {
530 19 return;
531 }
532 else
533 {
534 111 ▶ if ( ( @coords == 1 ) && ( $coords[0] > 0 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@coords == 1 and $coords[0] > 0) 81 30 111
Condition100%3/3
@coords == 1 and $coords[0] > 0
!ll&&!rl&&r
28281
MC/DC100%2/2
@coords == 1 and $coords[0] > 0
@coords == 1 $coords[0] > 0
Truth table100%3/3
A and B
A= @coords == 1B= $coords[0] > 0
ABresult
0X0
100
111
535 {
536 81 my $coords_stop = $self->coords_stop();
537 81 my $ret = [0];
538 81 ▶ if ($coords_stop)
Branch: 1 branch
BranchTrueFalseTotal
if ($coords_stop) 0 81 81
539 {
540 0 $ret = [];
541 }
542 81 return $ret;
543 }
544 30 pop(@coords);
545 30 return \@coords;
546 }
547 }
548  
549 sub _get_top_coords
550 {
551 46 ▶ my $self = shift;
Subroutine _get_top_coords: called CC=5 SCAR=16.1
552  
553 46 ▶ my @coords = @{ shift || $self->_get_current_coords() };
Branch: 1 branch
BranchTrueFalseTotal
shift() or $self->_get_current_coords 45 1 46
554  
555 46 ▶ if ( ( !@coords ) || ( ( @coords == 1 ) && ( $coords[0] == 0 ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not @coords or @coords == 1 and $coords[0] == 0) { } 21 25 46
Condition100%6/6
not @coords or @coords == 1 and $coords[0] == 0
l!l&&r!l&&!r
19225
@coords == 1 and $coords[0] == 0
!ll&&!rl&&r
8172
MC/DC100%3/3
not @coords or @coords == 1 and $coords[0] == 0
not @coords @coords == 1 $coords[0] == 0
Truth table100%4/4
not(A) or B and C
A= not @coordsB= @coords == 1C= $coords[0] == 0
ABCresult
1XX1
0111
00X0
0100
556 {
557 21 return;
558 }
559 else
560 {
561 25 return [0];
562 }
563 }
564  
565 sub _is_skip
566 {
567 97 ▶ my $self = shift;
Subroutine _is_skip: called CC=1 SCAR=0.0
568 97 my $coords = shift;
569  
570 97 my $iterator = $self->_get_nav_menu_traverser();
571  
572 97 my $ret = $iterator->find_node_by_coords($coords);
573  
574 97 my $item = $ret->{item};
575  
576 97 return $item->_node()->skip();
577 }
578  
579 sub _get_coords_while_skipping_skips
580 {
581 93 ▶ my $self = shift;
Subroutine _get_coords_while_skipping_skips: called CC=4 SCAR=13.9
582  
583 93 my $callback = shift;
584 93 my $coords = shift(@_);
585 93 ▶ if ( !$coords )
Branch: 1 branch
BranchTrueFalseTotal
unless ($coords) 92 1 93
586 {
587 92 $coords = $self->_get_current_coords();
588 }
589  
590 93 my $do_once = 1;
591  
592 93 ▶ while ( $do_once || $self->_is_skip($coords) )
Branch: 1 branch
BranchTrueFalseTotal
if ($do_once or $self->_is_skip($coords)) 97 93 190
Condition100%3/3
$do_once or $self->_is_skip($coords)
l!l&&r!l&&!r
93493
MC/DC100%2/2
$do_once or $self->_is_skip($coords)
$do_once $self->_is_skip($coords)
Truth table100%3/3
A or B
A= $do_onceB= $self->_is_skip($coords)
ABresult
1X1
011
000
593 {
594 97 $coords = $callback->( $self, $coords );
595 }
596 continue
597 {
598 97 $do_once = 0;
599 }
600  
601 93 return $coords;
602 }
603  
604 sub _get_most_advanced_leaf
605 {
606 23 ▶ my $self = shift;
Subroutine _get_most_advanced_leaf: called CC=3 SCAR=11.0
607  
608 # We accept as a parameter the vector of coordinates
609 23 my $coords_ref = shift;
610  
611 23 my @coords = @{$coords_ref};
612  
613 # Get a reference to the contents HDS (= hierarchial data structure)
614 23 my $branch = $self->_get_traversed_tree();
615  
616 # Get to the current branch by advancing to the offset
617 23 foreach my $c (@coords)
618 {
619 # Advance to the next level which is at index $c
620 26 $branch = $branch->get_nth_sub($c);
621 }
622  
623 # As long as there is something deeper
624 23 ▶ while ( my $num_subs = $branch->_num_subs() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $num_subs = $branch->_num_subs) 1 23 24
625 {
626 1 my $index = $num_subs - 1;
627  
628 # We are going to return it, so store it
629 1 push @coords, $index;
630  
631 # Recurse into the sub-branch
632 1 $branch = $branch->get_nth_sub($index);
633 }
634  
635 23 return \@coords;
636 }
637  
638  
639 # The traversed_tree is the tree that is calculated from the tree given
640 # by the user and some other parameters such as the host and path_info.
641 # It is passed to the NavMenu::Iterator::* classes as argument.
642 sub _get_traversed_tree
643 {
644 629 ▶ my $self = shift;
Subroutine _get_traversed_tree: called CC=2 SCAR=6.9
645  
646 629 ▶ if ( !$self->_traversed_tree() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_traversed_tree) 52 577 629
647 {
648 52 my $gen_retval = $self->_gen_traversed_tree();
649 52 $self->_traversed_tree( $gen_retval->{'tree'} );
650 52 $self->_current_coords( $gen_retval->{'current_coords'} );
651 52 $self->_leading_path_coords( $gen_retval->{'leading_path_coords'} );
652 }
653 629 return $self->_traversed_tree();
654 }
655  
656 sub _gen_traversed_tree
657 {
658 52 ▶ my $self = shift;
Subroutine _gen_traversed_tree: called CC=3 SCAR=11.0
659  
660 52 my $tree = $self->_render_tree_contents( $self->_tree_contents(), );
661  
662 52 my $find_coords_iterator =
663 HTML::Widgets::NavMenu::Iterator::GetCurrentlyActive->new(
664 {
665 'nav_menu' => $self,
666 'tree' => $tree,
667 }
668 );
669  
670 52 $find_coords_iterator->traverse();
671  
672 52 ▶ my $current_coords = $find_coords_iterator->get_final_coords() || [];
Condition100%2/2
$find_coords_iterator->get_final_coords || []
l!l
2725
MC/DC100%1/1
$find_coords_iterator->get_final_coords || []
$find_coords_iterator->get_final_coords
Truth table100%2/2
A
A= $find_coords_iterator->get_final_coords
Aresult
11
00
673 52 ▶ my $leading_path_coords =
Condition100%2/2
$find_coords_iterator->_get_leading_path_coords || []
l!l
3616
MC/DC100%1/1
$find_coords_iterator->_get_leading_path_coords || []
$find_coords_iterator->_get_leading_path_coords
Truth table100%2/2
A
A= $find_coords_iterator->_get_leading_path_coords
Aresult
11
00
674 $find_coords_iterator->_get_leading_path_coords() || [];
675  
676 # The root should always be expanded because:
677 # 1. If one of the leafs was marked as expanded so will its ancestors
678 # and eventually the root.
679 # 2. If nothing was marked as expanded, it should still be marked as
680 # expanded so it will expand.
681 52 $tree->expand();
682  
683 return {
684 52 'tree' => $tree,
685 'current_coords' => $current_coords,
686 'leading_path_coords' => $leading_path_coords,
687 };
688 }
689  
690 sub _get_leading_path_of_coords
691 {
692 135 ▶ my $self = shift;
Subroutine _get_leading_path_of_coords: called CC=11 SCAR=25.3
693 135 my $coords = shift;
694  
695 135 my $coords_stop = $self->coords_stop();
696  
697 135 ▶ if ( !$coords_stop )
Branch: 1 branch
BranchTrueFalseTotal
unless ($coords_stop) 135 0 135
698 {
699 135 ▶ if ( !@$coords )
Branch: 1 branch
BranchTrueFalseTotal
unless (@$coords) 10 125 135
700 {
701 10 $coords = [0];
702 }
703 }
704 135 if (0) # ( $coords->[0] == 0 )
705 {
706 $coords = [ @$coords[ 1 .. $#$coords ] ];
707 }
708  
709 135 my @leading_path;
710 135 my $iterator = $self->_get_nav_menu_traverser();
711  
712 COORDS_LOOP:
713 135 while (1)
714 {
715 219 my $ret = $iterator->find_node_by_coords($coords);
716  
717 219 my $item = $ret->{item};
718  
719 219 my $node = $item->_node();
720  
721 # This is a workaround for the root link.
722 219 ▶ my $host_url = ( defined( $node->url() ) ? ( $node->url() ) : "" );
Branch: 1 branch
BranchTrueFalseTotal
defined $node->url ? : 219 0 219
723 219 my $host = $item->_accum_state()->{'host'};
724  
725 219 ▶ my $url_type = (
Branch: 1 branch
BranchTrueFalseTotal
$node->url_is_abs ? : 7 212 219
726 $node->url_is_abs()
727 ? "full_abs"
728 : $item->get_url_type()
729 );
730  
731 219 push @leading_path,
732 HTML::Widgets::NavMenu::LeadingPath::Component->new(
733 {
734 'host' => $host,
735 'host_url' => $host_url,
736 'title' => $node->title(),
737 'label' => $node->text(),
738 'direct_url' => $self->_get_url_to_item($item),
739 'url_type' => $url_type,
740 }
741 );
742  
743 219 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($coords_stop ? scalar @$coords == 0 : scalar @$coords == 1 && $coords->[0] == 0) 135 84 219
$coords_stop ? : 0 219 219
Condition100%3/3
scalar @$coords == 1 and $coords->[0] == 0
!ll&&!rl&&r
2064135
MC/DC100%2/2
scalar @$coords == 1 and $coords->[0] == 0
scalar @$coords == 1 $coords->[0] == 0
Truth table100%3/3
A and B
A= scalar @$coords == 1B= $coords->[0] == 0
ABresult
0X0
100
111
744 $coords_stop
745 ? ( scalar(@$coords) == 0 )
746 : ( ( scalar(@$coords) == 1 ) && ( $coords->[0] == 0 ) )
747 )
748 {
749 135 last COORDS_LOOP;
750 }
751 }
752 continue
753 {
754 84 $coords = $self->_get_up_coords($coords);
755 }
756  
757 135 my $p = [ reverse(@leading_path) ];
758 135 ▶ if ($coords_stop)
Branch: 1 branch
BranchTrueFalseTotal
if ($coords_stop) 0 135 135
759 {
760 0 ▶ while ( ( @$p > 1 and $p->[0]->host_url eq $p->[1]->host_url ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@$p > 1 and $p->[0]->host_url eq $p->[1]->host_url) 0 0 0
Condition0%0/3
@$p > 1 and $p->[0]->host_url eq $p->[1]->host_url
!ll&&!rl&&r
000
MC/DC0%0/2
@$p > 1 and $p->[0]->host_url eq $p->[1]->host_url
@$p > 1 $p->[0]->host_url eq $p->[1]->host_url
Truth table0%0/3
A and B
A= @$p > 1B= $p->[0]->host_url eq $p->[1]->host_url
ABresult
0X0
100
111
761 {
762 0 shift @$p;
763 }
764 }
765 135 return $p;
766 }
767  
768 sub _get_leading_path
769 {
770 44 ▶ my $self = shift;
Subroutine _get_leading_path: called CC=1 SCAR=0.0
771 44 return $self->_get_leading_path_of_coords( $self->_leading_path_coords() );
772 }
773  
774 sub render
775 {
776 44 ▶ my $self = shift;
Subroutine render: called CC=1 SCAR=0.0
Pod documented
777 44 my %args = (@_);
778  
779 44 return $self->_render_generic(
780 { %args, _iter_method => '_get_nav_menu_traverser', } );
781 }
782  
783 sub _is_top_coords
784 {
785 260 ▶ my ( $self, $coords ) = @_;
Subroutine _is_top_coords: called CC=4 SCAR=13.9
786  
787 return (
788 260 ▶ defined($coords)
Branch: 3 branches
BranchTrueFalseTotal
defined $coords ? : 201 59 260
@$coords == 0 ? : 80 121 201
@$coords == 1 ? : 97 24 121
789 ? (
790 ( @$coords == 0 ) ? 1
791 : ( @$coords == 1 ) ? ( $coords->[0] == 0 )
792 : ''
793 )
794 : 1
795 );
796 }
797  
798 sub _render_generic
799 {
800 44 ▶ my $self = shift;
Subroutine _render_generic: called CC=5 SCAR=16.1
801 44 my $args = shift;
802  
803 44 my $method = $args->{_iter_method};
804  
805 44 my $iterator = $self->$method();
806 44 $iterator->traverse();
807 44 my $html = $iterator->get_results();
808  
809 44 my %nav_links;
810 my %nav_links_obj;
811  
812 44 my %links_proto = (
813 'prev' => scalar(
814 $self->_get_coords_while_skipping_skips( \&_get_prev_coords )
815 ),
816 'next' => scalar(
817 $self->_get_coords_while_skipping_skips( \&_get_next_coords )
818 ),
819 'up' => scalar( $self->_get_up_coords() ),
820 'top' => scalar( $self->_get_top_coords() ),
821 );
822  
823 44 ▶ while ( my ( $link_rel, $coords ) = each(%links_proto) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %links_proto) 176 44 220
824 {
825 # This is so we would avoid coordinates that point to the
826 # root ($coords == []).
827 176 ▶ if ( $self->_is_top_coords( $self->_current_coords() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_top_coords($self->_current_coords) and $self->_is_top_coords($coords)) 82 94 176
Condition100%3/3
$self->_is_top_coords($self->_current_coords) and $self->_is_top_coords($coords)
!ll&&!rl&&r
92282
MC/DC100%2/2
$self->_is_top_coords($self->_current_coords) and $self->_is_top_coords($coords)
$self->_is_top_coords($self->_current_coords) $self->_is_top_coords($coords)
Truth table100%3/3
A and B
A= $self->_is_top_coords($self->_current_coords)B= $self->_is_top_coords($coords)
ABresult
0X0
100
111
828 and $self->_is_top_coords($coords) )
829 {
830 82 undef($coords);
831 }
832 176 ▶ if ( defined($coords) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $coords) 91 85 176
833 {
834 91 my $obj = $self->_get_leading_path_of_coords($coords)->[-1];
835  
836 91 $nav_links_obj{$link_rel} = $obj;
837 91 $nav_links{$link_rel} = $obj->direct_url();
838 }
839 }
840  
841 44 my $js_code = "";
842  
843 return {
844 44 'html' => $html,
845 'leading_path' => $self->_get_leading_path(),
846 'nav_links' => \%nav_links,
847 'nav_links_obj' => \%nav_links_obj,
848 };
849 }
850  
851 14 1;
852  
853 __END__
↑ Summary blib/lib/HTML/Widgets/NavMenu/Url.pm »