Plift/Handler/Include.pm

filter: Statementstmt 100% 25 / 25 Branchbran 85.7% 12 / 14 Conditioncond 86.6% 13 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 2 totaltotal 88.0% 59 / 67 CC 14 SCAR 25.2
CC
14
Cov
93%
include
14.1
BEGIN
1.0
register
1.0
CRAP 12.5
« Plift/Handler/Wrap.pm ↑ Summary Plift/Context.pm »
1 package Plift::Handler::Include;
2  
3 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5  
6 10 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
7 10 my ($self, $engine) = @_;
8  
9 10 $engine->add_handler({
10 name => 'include',
11 tag => 'x-include',
12 attribute => 'data-plift-include',
13 handler => \&include
14 })
15 }
16  
17  
18 28 ▶sub include {
Subroutine include: called CC=14 SCAR=26.5
Pod undocumented
19 28 my ($element, $ctx) = @_;
20 28 my $node = $element->get(0);
21  
22 28 my $is_tag = $node->localname eq 'x-include';
23 28 ▶ my $template_name = $is_tag ? $node->getAttribute('template')
Branch: 1 branch
BranchTrueFalseTotal
$is_tag ? : 20 8 28
24 : $node->getAttribute('data-plift-include');
25  
26 28 ▶ my $if = $node->getAttribute('if') || $node->getAttribute('data-if');
Condition66%2/3
$node->getAttribute('if') || $node->getAttribute('data-if')
l!l&&r!l&&!r
2026
MC/DC50%1/2
$node->getAttribute('if') || $node->getAttribute('data-if')
$node->getAttribute('if') $node->getAttribute('data-if')
Truth table66%2/3
A || B
A= $node->getAttribute('if')B= $node->getAttribute('data-if')
ABresult
1X1
011
000
27 28 ▶ my $unless = $node->getAttribute('unless') || $node->getAttribute('data-unless');
Condition66%2/3
$node->getAttribute('unless') || $node->getAttribute('data-unless')
l!l&&r!l&&!r
2026
MC/DC50%1/2
$node->getAttribute('unless') || $node->getAttribute('data-unless')
$node->getAttribute('unless') $node->getAttribute('data-unless')
Truth table66%2/3
A || B
A= $node->getAttribute('unless')B= $node->getAttribute('data-unless')
ABresult
1X1
011
000
28  
29 # no template name
30 28 ▶ unless ($template_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template_name) 1 27 28
31  
32 1 my $error = "include error: missing template name\nsyntax:\n";
33 1 ▶ $error .= $is_tag ? '<x-include template="<template_name>"'
Branch: 1 branch
BranchTrueFalseTotal
$is_tag ? : 0 1 1
34 : sprintf('<%s data-plift-include="<template_name>">', $element->tagname);
35  
36 1 die $error;
37 }
38  
39 # contitional remove
40 27 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $if and not $ctx->get($if) or defined $unless and $ctx->get($unless)) 2 25 27
Condition100%9/9
defined $if and not $ctx->get($if) or defined $unless and $ctx->get($unless)
l!l&&r!l&&!r
1125
defined $if and not $ctx->get($if)
!ll&&!rl&&r
2511
defined $unless and $ctx->get($unless)
!ll&&!rl&&r
2411
MC/DC100%4/4
defined $if and not $ctx->get($if) or defined $unless and $ctx->get($unless)
defined $if not $ctx->get($if) defined $unless $ctx->get($unless)
Truth table71%5/7
A and not(B) or C and D
A= defined $ifB= not $ctx->get($if)C= defined $unlessD= $ctx->get($unless)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
41 (defined $if && !$ctx->get($if))
42 || (defined $unless && $ctx->get($unless))
43 ) {
44  
45 2 ▶ $element->remove if $is_tag;
Branch: 1 branch
BranchTrueFalseTotal
if $is_tag 2 0 2
46 2 return;
47 }
48  
49 # remove our attributes
50 25 ▶ unless ($is_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_tag) 7 18 25
51 7 $node->removeAttribute($_) for
52 qw/ data-plift-include data-if data-unless if unless/;
53 }
54  
55 25 my $dom = $ctx->process_template( $template_name );
56  
57 # replace or append
58 25 ▶ if ($is_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_tag) { } 18 7 25
59  
60 18 $element->replace_with($dom);
61 }
62 else {
63  
64 7 $element->append($dom);
65 }
66  
67 }
68  
69  
70  
71  
72  
73 9 1;
74 __END__
« Plift/Handler/Wrap.pm ↑ Summary Plift/Context.pm »