Plift/Handler/Wrap.pm

filter: Statementstmt 100% 35 / 35 Branchbran 72.7% 16 / 22 Conditioncond 65.2% 15 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 74.0% 74 / 100 CC 21 SCAR 30.3
CC
21
Cov
82%
wrap
24.5
BEGIN
1.0
BEGIN
1.0
CRAP 20.8
↑ Summary Plift/Handler/Include.pm »
1 package Plift::Handler::Wrap;
2  
3 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 10 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
8 10 my ($self, $engine) = @_;
9  
10 $engine->add_handler({
11 name => 'wrap',
12 tag => 'x-wrap',
13 attribute => 'data-plift-wrap',
14 6 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 6 $self->wrap(@_);
17 }
18 })
19 10 }
20  
21  
22 6 ▶sub wrap {
Subroutine wrap: called CC=21 SCAR=32.0
Pod undocumented
23 6 my ($self, $element, $ctx) = @_;
24 6 my $node = $element->get(0);
25  
26 6 my $is_tag = $node->localname eq 'x-wrap';
27 6 ▶ my $template_name = $is_tag ? $node->getAttribute('template')
Branch: 1 branch
BranchTrueFalseTotal
$is_tag ? : 4 2 6
28 : $node->getAttribute('data-plift-wrap');
29  
30 6 ▶ $template_name ||= 'layout';
Condition50%1/2
$template_name ||= "layout"
l!l
60
MC/DC0%0/1
$template_name ||= "layout"
$template_name
Truth table50%1/2
A
A= $template_name
Aresult
11
00
31  
32 # params
33 6 my %params = ( at => '#content' );
34  
35 6 foreach (qw/ at if unless replace content /) {
36  
37 30 ▶ my $value = $node->getAttribute("data-$_") || $node->getAttribute($_);
Condition100%3/3
$node->getAttribute("data-$_") || $node->getAttribute($_)
l!l&&r!l&&!r
2424
MC/DC100%2/2
$node->getAttribute("data-$_") || $node->getAttribute($_)
$node->getAttribute("data-$_") $node->getAttribute($_)
Truth table100%3/3
A || B
A= $node->getAttribute("data-$_")B= $node->getAttribute($_)
ABresult
1X1
011
000
38  
39 30 ▶ unless ($is_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_tag) 10 20 30
40 10 $node->removeAttribute("data-$_");
41 10 $node->removeAttribute($_);
42 }
43  
44 30 ▶ $params{$_} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 6 24 30
45 }
46  
47 # contitional abort
48 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"if"} and not $ctx->get($params{"if"}) or defined $params{"unless"} and $ctx->get($params{"unless"})) 1 5 6
Condition55%5/9
defined $params{"if"} and not $ctx->get($params{"if"}) or defined $params{"unless"} and $ctx->get($params{"unless"})
l!l&&r!l&&!r
015
defined $params{"if"} and not $ctx->get($params{"if"})
!ll&&!rl&&r
600
defined $params{"unless"} and $ctx->get($params{"unless"})
!ll&&!rl&&r
501
MC/DC25%1/4
defined $params{"if"} and not $ctx->get($params{"if"}) or defined $params{"unless"} and $ctx->get($params{"unless"})
defined $params{"if"} not $ctx->get($params{"if"}) defined $params{"unless"} $ctx->get($params{"unless"})
Truth table28%2/7
A and not(B) or C and D
A= defined $params{"if"}B= not $ctx->get($params{"if"})C= defined $params{"unless"}D= $ctx->get($params{"unless"})
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
49 (defined $params{if} && !$ctx->get($params{if}))
50 || (defined $params{unless} && $ctx->get($params{unless}))
51 ) {
52  
53 1 ▶ $element->replace_with($element->children) if $is_tag;
Branch: 1 branch
BranchTrueFalseTotal
if $is_tag 1 0 1
54 1 return;
55 }
56  
57 # load template
58 5 my $dom = $ctx->process_template($template_name);
59  
60 # $dom elements comes unbound of document, insert after element
61 5 $dom->insert_after($element);
62  
63 # find wrapper
64 5 my $wrapper = $dom->find($params{at});
65 5 ▶ $wrapper = $dom->filter($params{at}) if $wrapper->size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wrapper->size == 0 0 5 5
66  
67 5 ▶ confess "wrap error: can't find wrapper element (with id '".$params{at}."') on:\n".$dom->as_html
Branch: 1 branch
BranchTrueFalseTotal
unless $wrapper->size > 0 0 5 5
68 unless $wrapper->size > 0;
69  
70 # wrap element
71  
72 5 my $is_xtag = $element->tagname =~ /^x-/;
73 5 ▶ if ($params{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'replace'}) { } 2 3 5
74  
75 2 ▶ $wrapper->replace_with($is_xtag || $params{content} ? $element->contents : $element);
Branch: 1 branch
BranchTrueFalseTotal
$is_xtag || $params{'content'} ? : 2 0 2
Condition66%2/3
$is_xtag or $params{"content"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$is_xtag or $params{"content"}
$is_xtag $params{"content"}
Truth table66%2/3
A or B
A= $is_xtagB= $params{"content"}
ABresult
1X1
011
000
76 }
77 else {
78  
79 3 ▶ $wrapper->append($is_xtag || $params{content} ? $element->contents->{nodes} : $element->{nodes});
Branch: 1 branch
BranchTrueFalseTotal
$is_xtag || $params{'content'} ? : 3 0 3
Condition66%2/3
$is_xtag or $params{"content"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$is_xtag or $params{"content"}
$is_xtag $params{"content"}
Truth table66%2/3
A or B
A= $is_xtagB= $params{"content"}
ABresult
1X1
011
000
80 }
81  
82 5 ▶ $element->remove if $is_xtag || $params{content};
Branch: 1 branch
BranchTrueFalseTotal
if $is_xtag or $params{"content"} 5 0 5
Condition66%2/3
$is_xtag or $params{"content"}
l!l&&r!l&&!r
320
MC/DC0%0/2
$is_xtag or $params{"content"}
$is_xtag $params{"content"}
Truth table66%2/3
A or B
A= $is_xtagB= $params{"content"}
ABresult
1X1
011
000
83 }
84  
85  
86  
87  
88  
89 9 1;
90  
91 __END__
↑ Summary Plift/Handler/Include.pm »