blib/lib/Mojolicious/Plugin/Plift.pm

filter: Statementstmt 98.2% 57 / 58 Branchbran 80.0% 16 / 20 Conditioncond 46.1% 12 / 26 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 74.4% 96 / 129 CC 21 SCAR 26.4
CC
21
Cov
82%
_render
23.4
register
2.0
_build_plift
2.0
CRAP 14.0
↑ Summary
1 package Mojolicious::Plugin::Plift;
2  
3 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Plift;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Util qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = "0.02";
8  
9 2 __PACKAGE__->attr([qw/ plift config /]);
10  
11  
12 2 ▶sub register {
Subroutine register: called CC=2 SCAR=7.1
Pod documented
13 2 my ( $self, $app, $config ) = @_;
14  
15 2 ▶ $self->config($config || {});
Condition50%1/2
$config || {}
l!l
20
MC/DC0%0/1
$config || {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
16 2 $self->plift($self->_build_plift($config));
17  
18 0 ▶ $app->helper( plift => sub { $self->plift });
Subroutine __ANON__: not called CC=1 SCAR=1.2
19  
20 $app->renderer->add_handler(
21 10 ▶ plift => sub { $self->_render(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23 }
24  
25  
26 10 ▶sub _render {
Subroutine _render: called CC=17 SCAR=31.5
27 10 my ( $self, $renderer, $c, $output, $options ) = @_;
28  
29 # setup plift
30 10 my $plift = $self->plift;
31 10 $plift->paths( $renderer->paths );
32  
33 my $template =
34 defined $options->{inline} ? \($options->{inline})
35 : defined $options->{template} && $plift->has_template($options->{template}) ? $options->{template}
36 10 ▶ : \($renderer->get_data_template($options));
Branch: 2 branches
BranchTrueFalseTotal
defined $options->{'inline'} ? : 3 7 10
defined $options->{'template'} && $plift->has_template($options->{'template'}) ? : 7 0 7
Condition33%1/3
defined $options->{"template"} and $plift->has_template($options->{"template"})
!ll&&!rl&&r
007
MC/DC0%0/2
defined $options->{"template"} and $plift->has_template($options->{"template"})
defined $options->{"template"} $plift->has_template($options->{"template"})
Truth table33%1/3
A and B
A= defined $options->{"template"}B= $plift->has_template($options->{"template"})
ABresult
0X0
100
111
37  
38 # TODO prevent render deep recursion when for exception pages when plift
39 # is the default handler
40 10 ▶ return unless defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 10 10
41 10 ▶ return if ref $template && !defined $$template;
Branch: 1 branch
BranchTrueFalseTotal
if ref $template and not defined $$template 0 10 10
Condition66%2/3
ref $template and not defined $$template
!ll&&!rl&&r
730
MC/DC0%0/2
ref $template and not defined $$template
ref $template not defined $$template
Truth table66%2/3
A and not(B)
A= ref $templateB= not defined $$template
ABresult
0X0
100
111
42  
43 # snippet_namespaces
44 my $snippet_namespaces = $c->stash->{'plift.snippet_namespaces'}
45 10 ▶ || $self->config->{'snippet_namespaces'}
Condition40%2/5
$c->stash->{'plift.snippet_namespaces'} || $self->config->{'snippet_namespaces'} || [ref($c->app) . '::Snippet']
l!l
010
$c->stash->{"plift.snippet_namespaces"} or $self->config->{"snippet_namespaces"}
l!l&&r!l&&!r
0010
MC/DC0%0/2
$c->stash->{'plift.snippet_namespaces'} || $self->config->{'snippet_namespaces'} || [ref($c->app) . '::Snippet']
$c->stash->{"plift.snippet_namespaces"} $self->config->{"snippet_namespaces"}
Truth table33%1/3
A or B
A= $c->stash->{"plift.snippet_namespaces"}B= $self->config->{"snippet_namespaces"}
ABresult
1X1
011
000
46 || [(ref $c->app).'::Snippet'];
47  
48 # resolve data
49 10 my $stash = $c->stash;
50 10 ▶ my $data_key = $stash->{'plift.data_key'} || $self->config->{data_key};
Condition33%1/3
$stash->{'plift.data_key'} || $self->config->{'data_key'}
l!l&&r!l&&!r
0010
MC/DC0%0/2
$stash->{'plift.data_key'} || $self->config->{'data_key'}
$stash->{'plift.data_key'} $self->config->{'data_key'}
Truth table33%1/3
A || B
A= $stash->{'plift.data_key'}B= $self->config->{'data_key'}
ABresult
1X1
011
000
51 10 ▶ my $data = defined $data_key ? $stash->{$data_key} : $stash;
Branch: 1 branch
BranchTrueFalseTotal
defined $data_key ? : 0 10 10
52  
53 10 my $plift_tpl = $plift->template($template, {
54 encoding => $renderer->encoding,
55 paths => $renderer->paths,
56 helper => $c,
57 data => $data,
58 snippet_namespaces => $snippet_namespaces
59 });
60  
61 # delete 'layout' metadata set by previous (e.g. inner content) template
62 10 my $metadata = $plift_tpl->metadata;
63 10 delete $metadata->{layout};
64  
65 # render
66 10 my $document = $plift_tpl->render;
67  
68 # meta.layout
69 $stash->{layout} = $metadata->{layout}
70 10 ▶ if defined $metadata->{layout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $metadata->{"layout"} 1 9 10
71  
72 # insert inner content
73 10 ▶ if (defined $stash->{'mojo.content'}->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stash->{"mojo.content"}{"content"}) 2 8 10
74  
75 my $wrapper_selector = $stash->{'plift.wrapper_selector'}
76 2 ▶ || $self->config->{wrapper_selector} || '#content';
Condition40%2/5
$stash->{'plift.wrapper_selector'} || $self->config->{'wrapper_selector'} || '#content'
l!l
02
$stash->{"plift.wrapper_selector"} or $self->config->{"wrapper_selector"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$stash->{'plift.wrapper_selector'} || $self->config->{'wrapper_selector'} || '#content'
$stash->{"plift.wrapper_selector"} $self->config->{"wrapper_selector"}
Truth table33%1/3
A or B
A= $stash->{"plift.wrapper_selector"}B= $self->config->{"wrapper_selector"}
ABresult
1X1
011
000
77  
78 $document->find($wrapper_selector)
79 2 ->append($stash->{'mojo.content'}->{content});
80 }
81  
82 # pass the rendered result back to the renderer
83 10 ▶ $$output = defined $c->res->body && length $c->res->body
Branch: 1 branch
BranchTrueFalseTotal
defined $c->res->body && length $c->res->body ? : 1 9 10
Condition66%2/3
defined $c->res->body and length $c->res->body
!ll&&!rl&&r
091
MC/DC50%1/2
defined $c->res->body and length $c->res->body
defined $c->res->body length $c->res->body
Truth table66%2/3
A and B
A= defined $c->res->bodyB= length $c->res->body
ABresult
0X0
100
111
84 ? $c->res->body : decode 'UTF-8', $document->as_html;
85 }
86  
87  
88 2 ▶sub _build_plift {
Subroutine _build_plift: called CC=2 SCAR=6.9
89 2 my $self = shift;
90 2 my $cfg = $self->config;
91  
92 my $plift = Plift->new(
93 2 ▶ plugins => $cfg->{plugins} || []
Condition50%1/2
$cfg->{'plugins'} || []
l!l
02
MC/DC0%0/1
$cfg->{'plugins'} || []
$cfg->{'plugins'}
Truth table50%1/2
A
A= $cfg->{'plugins'}
Aresult
11
00
94 );
95  
96 # x-link
97 $plift->add_handler({
98 name => 'link-tag',
99 tag => 'x-link',
100 2 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
101 2 my ($el, $c) = @_;
102 2 my $node = $el->get(0);
103 2 my $path;
104  
105 2 ▶ if ($node->hasAttribute('to')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttribute("to")) 1 1 2
106  
107 1 $path = $node->getAttribute('to');
108 1 $node->removeAttribute('to');
109 }
110  
111 2 $node->setAttribute('href', $c->url_for($path));
112 2 $node->setNodeName('a');
113 }
114 2 });
115  
116 # x-csrf-field
117 $plift->add_handler({
118 name => 'csrf-field-tag',
119 tag => 'x-csrf-field',
120 2 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
121 2 my ($el, $c) = @_;
122 2 my $node = $el->get(0);
123  
124 2 $node->setNodeName('input');
125 2 $node->setAttribute('value', $c->helper->csrf_token) ;
126 2 $node->setAttribute('type', 'hidden') ;
127 2 ▶ $node->setAttribute('name', 'csrf_token')
Branch: 1 branch
BranchTrueFalseTotal
unless $node->hasAttribute("name") 1 1 2
128 unless $node->hasAttribute('name');
129 }
130 2 });
131  
132  
133 2 $plift;
134 }
135  
136  
137  
138 2 1;
139 __END__
↑ Summary