Plift/Context.pm

filter: Statementstmt 98.5% 271 / 275 Branchbran 84.3% 113 / 134 Conditioncond 65.5% 19 / 29 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 94.4% 34 / 36 Podpod 21.0% 4 / 19 totaltotal 87.8% 449 / 511 CC 84 SCAR 23.1
CC
84
Cov
92%
_render_directives
24.1
get
17.8
_dispatch_handlers
9.0
CRAP 10.1
« Plift/Handler/Include.pm ↑ Summary Plift/Handler/Render.pm »
1 package Plift::Context;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use XML::LibXML::jQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use JSON 'to_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Encode 'encode';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Ref::Util qw/ is_hashref is_blessed_ref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 10 has 'helper', is => 'ro';
13 10 has 'wrapper', is => 'ro';
14 10 has 'template', is => 'ro', required => 1;
15 10 has 'encoding', is => 'ro', default => 'UTF-8';
16 10 has 'loop_var', is => 'ro', default => 'loop';
17 10 has 'metadata_key', is => 'ro', default => 'meta';
18 0 ▶has 'handlers', is => 'ro', default => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 10 has 'active_handlers', is => 'rw';
20 10 has 'inactive_handlers', is => 'rw';
21 10 has 'internal_id_attribute', is => 'ro', default => 'data-plift-id';
22 10 has '_load_template', is => 'ro', required => 1, init_arg => 'load_template';
23 10 has '_load_snippet', is => 'ro', required => 1, init_arg => 'load_snippet';
24 10 has '_run_hooks', is => 'ro', required => 1, init_arg => 'run_hooks';
25  
26  
27 10 has 'document', is => 'rw', init_arg => undef;
28 10 has 'is_rendering', is => 'rw', init_arg => undef, default => 0;
29 10 has 'is_aborted', is => 'rw', init_arg => undef, default => '';
30  
31 32 ▶has '_data_stack', is => 'ro', init_arg => 'data_stack', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 32 ▶has '_directive_stack', is => 'ro', init_arg => undef, default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34  
35 33 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=19.9
36 33 my $self = shift;
37  
38 33 my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/;
39 33 ▶ Carp::croak "Undefined subroutine &${package}::$method called"
Branch: 1 branch
BranchTrueFalseTotal
unless is_blessed_ref($self) and $self->isa(__PACKAGE__) 0 33 33
Condition33%1/3
is_blessed_ref($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
0033
MC/DC0%0/2
is_blessed_ref($self) and $self->isa(__PACKAGE__)
is_blessed_ref($self) $self->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= is_blessed_ref($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
40 unless is_blessed_ref $self && $self->isa(__PACKAGE__);
41  
42 33 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 32 1 33
43  
44 1 ▶ Carp::croak qq{Can't locate object method "$method" via package "$package"}
Branch: 1 branch
BranchTrueFalseTotal
unless $self->helper and $self->helper->can($method) 0 1 1
Condition33%1/3
$self->helper and $self->helper->can($method)
!ll&&!rl&&r
001
MC/DC0%0/2
$self->helper and $self->helper->can($method)
$self->helper $self->helper->can($method)
Truth table33%1/3
A and B
A= $self->helperB= $self->helper->can($method)
ABresult
0X0
100
111
45 unless $self->helper && $self->helper->can($method);
46  
47 1 $self->helper->$method(@_);
48 }
49  
50 32 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
51 32 my $self = shift;
52  
53 # coerse array indo hash
54 32 foreach my $attr (qw/ active_handlers inactive_handlers/) {
55  
56 64 ▶ $self->$attr({ map { $_ => 1 } @{ $self->$attr } })
Branch: 1 branch
BranchTrueFalseTotal
if $self->$attr 2 62 64
57 if $self->$attr;
58 }
59 }
60  
61 32 ▶sub metadata {
Subroutine metadata: called CC=2 SCAR=6.9
Pod undocumented
62 32 my $self = shift;
63 32 my $key = $self->metadata_key;
64 32 my $data = $self->data;
65 32 ▶ $data->{$key} = {} unless exists $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$key} 24 8 32
66 32 $data->{$key};
67 }
68  
69  
70 277 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
Pod undocumented
71 277 my $self = shift;
72 277 my $stack = $self->_data_stack;
73 277 ▶ push @$stack, +{} if @$stack == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack == 0 23 254 277
74 277 $stack->[-1];
75 }
76  
77 39 ▶sub _push_stack {
Subroutine _push_stack: called CC=2 SCAR=7.0
78 39 my ($self, $data_point) = @_;
79 39 ▶ my $data = $self->get($data_point) || {};
Condition50%1/2
$self->get($data_point) || {}
l!l
390
MC/DC0%0/1
$self->get($data_point) || {}
$self->get($data_point)
Truth table50%1/2
A
A= $self->get($data_point)
Aresult
11
00
80 39 push @{$self->_data_stack}, $data;
81 39 $self;
82 }
83  
84 39 ▶sub _pop_stack {
Subroutine _pop_stack: called CC=1 SCAR=0.0
85 39 my ($self) = @_;
86 39 pop @{$self->_data_stack};
87 39 $self;
88 }
89  
90 71 ▶sub directives {
Subroutine directives: called CC=2 SCAR=6.9
Pod undocumented
91 71 my $self = shift;
92 71 my $stack = $self->_directive_stack;
93 71 ▶ push @$stack, +{
Branch: 1 branch
BranchTrueFalseTotal
if @$stack == 0 26 45 71
94 directives => [],
95 selector => '',
96 } if @$stack == 0;
97 71 $stack->[-1];
98 }
99  
100 50 ▶sub rewind_directive_stack {
Subroutine rewind_directive_stack: called CC=6 SCAR=17.9
Pod undocumented
101 50 my ($self, $element) = @_;
102  
103 # rewind all
104 50 ▶ unless (defined $element) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $element) 25 25 50
105  
106 25 my $directive_stack = $self->_directive_stack;
107 25 ▶ pop @$directive_stack while (@$directive_stack > 1);
Branch: 1 branch
BranchTrueFalseTotal
@$directive_stack > 1 and do { pop @$directive_stack } 4 25 29
108 25 return;
109 }
110  
111 # rewind until parent is found
112 # pop stack until we find a parent or reach the root of stack
113 25 my $stack = $self->_directive_stack;
114 25 ▶ while (@$stack > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack > 1) 16 11 27
115  
116 16 my $parent = $element->parent;
117 16 my $parent_selector = $stack->[-1]->{selector};
118  
119 # remove modifiers
120 16 $self->_parse_matchspec_modifiers($parent_selector);
121  
122 16 ▶ while ($parent->get(0)->nodeType != 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->get(0)->nodeType != 9) 31 2 33
123  
124 31 ▶ return if $parent->filter($parent_selector)->size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->filter($parent_selector)->size == 1 14 17 31
125  
126 17 $parent = $parent->parent;
127 }
128  
129 2 pop @$stack;
130 }
131 }
132  
133 7 ▶sub push_at {
Subroutine push_at: called CC=1 SCAR=0.0
Pod undocumented
134 7 my ($self, $selector, $data_point) = @_;
135  
136 7 my $inner_directives = [];
137 7 $self->at($selector => { $data_point => $inner_directives });
138 7 push @{$self->_directive_stack}, {
139 selector => $selector,
140 directives => $inner_directives
141 };
142  
143 # p $self->_directive_stack;
144  
145 7 $self;
146 }
147  
148 1 ▶sub pop_at {
Subroutine pop_at: called CC=1 SCAR=0.0
Pod undocumented
149 1 my $self = shift;
150 1 pop @{$self->_directive_stack};
151 1 $self;
152 }
153  
154  
155  
156 26 ▶sub internal_id {
Subroutine internal_id: called CC=3 SCAR=11.0
Pod undocumented
157 26 my ($self, $node) = @_;
158  
159 26 ▶ $node = $node->get(0)
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("XML::LibXML::jQuery") 1 25 26
160 if $node->isa('XML::LibXML::jQuery');
161  
162 26 ▶ unless ($node->hasAttribute($self->internal_id_attribute)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->hasAttribute($self->internal_id_attribute)) 26 0 26
163 26 $node->setAttribute($self->internal_id_attribute, $node->unique_key);
164 }
165  
166 26 $node->getAttribute($self->internal_id_attribute);
167 }
168  
169 26 ▶sub selector_for {
Subroutine selector_for: called CC=1 SCAR=0.0
Pod undocumented
170 26 my ($self, $element) = @_;
171 26 my $id = $self->internal_id($element);
172 26 sprintf '*[%s="%s"]', $self->internal_id_attribute, $id;
173 }
174  
175  
176  
177  
178 43 ▶sub at {
Subroutine at: called CC=4 SCAR=14.2
Pod documented
179 43 my $self = shift;
180 43 my $directives = $self->directives->{directives};
181 43 ▶ if (my $reftype = ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reftype = ref $_[0]) { } 3 40 43
182  
183 3 ▶ push @$directives, @{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
if $reftype eq "ARRAY" 3 0 3
184 if $reftype eq 'ARRAY';
185  
186 0 ▶ push @$directives, %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
if $reftype eq "HASH" 0 3 3
187 if $reftype eq 'HASH';
188 }
189 else {
190 40 push @$directives, @_;
191 }
192  
193 43 $self;
194 }
195  
196  
197  
198 8 ▶sub set {
Subroutine set: called CC=5 SCAR=16.2
Pod documented
199 8 my $self = shift;
200  
201 8 ▶ confess "set() what?"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 8 8
202 unless defined $_[0];
203  
204 8 my $data = $self->data;
205  
206 # set(hashref)
207 8 ▶ if (my $reftype = ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reftype = ref $_[0]) 3 5 8
208  
209 3 ▶ confess "Invalid parameter given to set(data): data must be a hashref."
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype eq "HASH" 0 3 3
210 unless $reftype eq 'HASH';
211  
212 # copy data
213 $data->{$_} = $_[0]->{$_}
214 3 for keys %{$_[0]};
215  
216 3 return $self;
217 }
218  
219 # set(key, value)
220 5 $data->{$_[0]} = $_[1];
221  
222 5 $self;
223 }
224  
225  
226 200 ▶sub get {
Subroutine get: called CC=17 SCAR=28.8
Pod documented
227 200 my ($self, $reference) = @_;
228  
229 200 my $data = $self->data;
230 200 ▶ return $data if $reference eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $reference eq "." 0 200 200
231  
232 200 my @keys = split /\./, $reference;
233  
234 # empty key
235 die "invalid reference '$reference'"
236 200 ▶ if grep { !defined } @keys;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @keys 0 200 200
237  
238 # traverse data, valid reference formats:
239 # - foo
240 # - foo.bar
241 # - foo.0
242 # - foo.0.bar
243  
244 200 my $current_path = '';
245 200 ▶ while (defined (my $key = shift @keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @keys)) 276 197 473
246  
247 # undefined data
248 276 ▶ confess "get('$reference') error: '$current_path' is undefined."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 276 276
249 unless defined $data;
250  
251 # cant traverse non-ref data
252 276 ▶ die "get('$reference') error: can't traverse key '$key': '$current_path' is a non-ref value."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 1 275 276
253 unless ref $data;
254  
255  
256 275 my $next_data;
257  
258 # hash key
259 275 ▶ if (ref $data eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 230 45 275
elsif (ref $data eq 'ARRAY') { } 41 4 45
elsif (is_blessed_ref($data)) { } 4 0 4
elsif (ref $data) { } 0 0 0
260  
261 230 $next_data = $data->{$key};
262 }
263  
264 # array: numeric keys only
265 elsif (ref $data eq 'ARRAY') {
266  
267 41 ▶ confess "get('$reference') error: '$current_path' is an array and '$key' is not a numeric index."
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^\-?\d+$/ 1 40 41
268 unless $key =~ /^\-?\d+$/;
269  
270 40 $next_data = $data->[$key];
271 }
272  
273 elsif (is_blessed_ref $data) {
274  
275 4 ▶ if ($data->can($key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->can($key)) { } 2 2 4
elsif (is_hashref($data) and exists $data->{$key}) { } 1 1 2
276 2 $next_data = $data->$key;
277 }
278 ▶ elsif (is_hashref $data && exists $data->{$key}) {
Condition66%2/3
is_hashref($data) and exists $data->{$key}
!ll&&!rl&&r
011
MC/DC50%1/2
is_hashref($data) and exists $data->{$key}
is_hashref($data) exists $data->{$key}
Truth table66%2/3
A and B
A= is_hashref($data)B= exists $data->{$key}
ABresult
0X0
100
111
279 1 $next_data = $data->{$key};
280 }
281 else {
282 1 die sprintf("get('%s') error: '%s' is an '%s' instance and '%s' is not a existing method or property.",
283 $reference, $current_path, ref $data, $key);
284 }
285 }
286  
287 elsif (ref $data) {
288  
289 0 die sprintf "get('%s') error: can't traverse key '%s': '%s' is a unsupported ref value (%s).",
290 $reference, $key, $current_path, ref $data;
291 }
292  
293 # next data is code, replace by its rv
294 273 ▶ $next_data = $next_data->($self, $data)
Branch: 1 branch
BranchTrueFalseTotal
if ref $next_data eq "CODE" 6 267 273
295 if ref $next_data eq 'CODE';
296  
297 273 $data = $next_data;
298  
299 # append path
300 273 ▶ $current_path .= length $current_path ? ".$key" : $key;
Branch: 1 branch
BranchTrueFalseTotal
length $current_path ? : 73 200 273
301 }
302  
303  
304 197 ▶ $data = '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 195 197
305 197 return $data;
306 }
307  
308 1 ▶sub abort { shift->is_aborted(1) }
Subroutine abort: called CC=1 SCAR=0.0
Pod undocumented
309  
310 57 ▶sub process_template {
Subroutine process_template: called CC=1 SCAR=0.0
Pod undocumented
311 57 my ($self, $template_name) = @_;
312  
313 # TODO this is an action at a distance... find a better solution
314 # localize current_file and current_path
315 # load_template sets it, process_element can recurse into load_template
316 # thats for related paths to work
317 57 local $self->{current_file} = $self->{current_file};
318 57 local $self->{current_path} = $self->{current_path};
319  
320 57 my $element = $self->load_template($template_name);
321 57 $self->_run_hooks->('after_load_template', [$self, $element, $template_name]);
322 57 $self->_run_hooks->('before_process_template', [$self, $element, $template_name]);
323 57 $self->process_element($element);
324 56 $self->_run_hooks->('after_process_template', [$self, $element, $template_name]);
325  
326 56 $element;
327 }
328  
329  
330 # load a template from the paths contained in the _load_template closure
331 57 ▶sub load_template {
Subroutine load_template: called CC=1 SCAR=0.0
Pod undocumented
332 57 my ($self, $name) = @_;
333 57 $self->_load_template->($self, $name);
334 }
335  
336 57 ▶sub process_element {
Subroutine process_element: called CC=5 SCAR=16.1
Pod undocumented
337 57 my ($self, $element) = @_;
338  
339 # match elements
340 0 ▶ my $callback = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
341 0 $self->_dispatch_handlers(@_, $element->_new_nodes([$_[1]]));
342 57 };
343  
344 # get handlers
345 57 my @handlers = @{ $self->handlers };
346 57 ▶ @handlers = grep { exists $self->active_handlers->{$_->{name}} } @handlers
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->active_handlers 2 55 57
347 if defined $self->active_handlers;
348  
349 57 ▶ @handlers = grep { !exists $self->inactive_handlers->{$_->{name}} } @handlers
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->inactive_handlers 2 55 57
350 if defined $self->inactive_handlers;
351  
352 # build xpath
353 57 my $find_xpath = join ' | ', map { $_->{xpath} } @handlers;
354 57 my $filter_xpath = $find_xpath;
355 57 $filter_xpath =~ s{\.//}{./}g;
356  
357 57 foreach my $node (
358 57 @{ $element->xfilter($filter_xpath)->{nodes} },
359 57 @{ $element->xfind($find_xpath)->{nodes} }
360 ) {
361  
362 67 ▶ last if $self->is_aborted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_aborted 0 67 67
363 67 $self->_dispatch_handlers($node, $element->_new_nodes([$node]));
364 }
365  
366 # printf STDERR "# afer filter: %s\n", $element->as_html;
367 }
368  
369 67 ▶sub _dispatch_handlers {
Subroutine _dispatch_handlers: called CC=9 SCAR=22.0
370 67 my ($self, $node, $el) = @_;
371 67 my $tagname = $node->localname;
372  
373 67 foreach my $handler (@{ $self->handlers }) {
374  
375 332 ▶ last if $self->is_aborted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_aborted 0 332 332
376  
377 # dispatch by tagname
378 332 my $handler_match = 0;
379 332 ▶ if ($handler->{tag} && scalar grep { $_ eq $tagname } @{$handler->{tag}}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($handler->{'tag'} and scalar grep({$_ eq $tagname;} @{$$handler{'tag'};})) { } 39 293 332
elsif ($handler->{'attribute'}) { } 231 62 293
Condition100%2/2
$handler->{"tag"} and scalar grep({$_ eq $tagname;} @{$$handler{"tag"};})
!ll
66266
MC/DC100%1/1
$handler->{"tag"} and scalar grep({$_ eq $tagname;} @{$$handler{"tag"};})
$handler->{"tag"}
Truth table100%2/2
A
A= $handler->{"tag"}
Aresult
00
11
380  
381 39 $handler_match = 1;
382 }
383  
384 # dispatch by attribute
385 elsif ($handler->{attribute}) {
386  
387 231 foreach my $attr (@{$handler->{attribute}}) {
388  
389 360 ▶ if ($node->hasAttribute($attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttribute($attr)) 28 332 360
390  
391 28 $handler_match = 1;
392 28 last;
393 }
394 }
395 }
396  
397 # dispatch
398 332 ▶ $handler->{sub}->($el, $self)
Branch: 1 branch
BranchTrueFalseTotal
if $handler_match 67 265 332
399 if $handler_match;
400 }
401 }
402  
403  
404 26 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
405 26 my ($self, $data) = @_;
406  
407 26 ▶ @{ $self->_data_stack } = ( $data )
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 5 21 26
408 if defined $data;
409  
410 # already rendering
411 26 ▶ die "Can't call render() now. We are already rendering."
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_rendering 0 26 26
412 if $self->is_rendering;
413  
414 26 $self->is_rendering(1);
415 26 $self->is_aborted(0);
416  
417 # vivify metadata
418 26 my $meta = $self->metadata;
419  
420 # process template
421 26 my $element = $self->process_template($self->template);
422  
423 # aborted
424 25 ▶ return $element->document if $self->is_aborted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_aborted 0 25 25
425  
426 # apply wrapper
427 25 ▶ if ($self->wrapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wrapper) 1 24 25
428  
429 1 my $wrapper = $self->process_template($self->wrapper);
430 1 $wrapper->insert_after($element);
431 1 $wrapper->find('#content')->append($element);
432 1 $element = $wrapper;
433 }
434  
435 # rewind directive stack, then render
436 25 $self->rewind_directive_stack;
437 25 $self->_run_hooks->('before_render_directives', [$self, $element]);
438 25 $self->_render_directives($element, $self->directives->{directives});
439 25 $self->_run_hooks->('after_render_directives', [$self, $element]);
440  
441 # TODO output filters
442  
443 # remove internal id attribute
444 25 $element->xfind(sprintf '//*[@%s]', $self->internal_id_attribute)
445 ->remove_attr($self->internal_id_attribute);
446  
447 # return the document
448 25 $self->is_rendering(0);
449 25 $element->document;
450 }
451  
452 65 ▶sub _render_directives {
Subroutine _render_directives: called CC=24 SCAR=31.8
453 65 my ($self, $el, $directives) = @_;
454  
455 65 ▶ for (my $i = 0; $i < @$directives; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$directives) 136 64 200
456  
457 136 ▶ last if $self->is_aborted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_aborted 1 135 136
458  
459 135 my $match_spec = $directives->[$i];
460  
461 # modifiers
462 135 my $mod = $self->_parse_matchspec_modifiers($match_spec);
463  
464 135 my ($selector, $attribute) = split '@', $match_spec;
465 135 my $action = $directives->[$i+1];
466  
467 135 ▶ my $target_element = $selector eq '.' ? $el : $el->find($selector);
Branch: 1 branch
BranchTrueFalseTotal
$selector eq '.' ? : 1 134 135
468 135 ▶ $target_element = $el->filter($selector) if $target_element->size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $target_element->size == 0 3 132 135
469 135 ▶ next unless $target_element->size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_element->size > 0 0 135 135
470  
471 # Scalar
472 135 ▶ if (!ref $action) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $action) { } 117 18 135
elsif (ref $action eq 'ARRAY') { } 1 17 18
elsif (ref $action eq 'HASH') { } 15 2 17
elsif (ref $action eq 'CODE') { } 2 0 2
473  
474 117 my $value = $self->get($action);
475  
476 117 ▶ $target_element->remove unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 117 117
477  
478 # to_json
479 117 ▶ $value = to_json($value, { convert_blessed => 1 }) if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 2 115 117
480  
481 # encode
482 117 $value = encode 'UTF-8', $value;
483  
484 117 ▶ if (defined $attribute && $attribute ne 'HTML') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attribute and $attribute ne 'HTML') { } 39 78 117
Condition100%3/3
defined $attribute and $attribute ne "HTML"
!ll&&!rl&&r
69939
MC/DC100%2/2
defined $attribute and $attribute ne "HTML"
defined $attribute $attribute ne "HTML"
Truth table100%3/3
A and B
A= defined $attributeB= $attribute ne "HTML"
ABresult
0X0
100
111
485  
486 # TODO append prepend attribute
487 39 $target_element->attr($attribute, $value);
488 }
489 else {
490  
491 # render Text by default (i.e. escaped HTML)
492 78 ▶ $value = [$target_element->{document}->createTextNode($value)]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute and $attribute eq "HTML" 69 9 78
Condition66%2/3
defined $attribute and $attribute eq "HTML"
!ll&&!rl&&r
6909
MC/DC50%1/2
defined $attribute and $attribute eq "HTML"
defined $attribute $attribute eq "HTML"
Truth table66%2/3
A and B
A= defined $attributeB= $attribute eq "HTML"
ABresult
0X0
100
111
493 unless defined $attribute && $attribute eq 'HTML';
494  
495 # replace contents by default
496 $target_element->contents->remove
497 78 ▶ unless $mod->{prepend} || $mod->{append};
Branch: 1 branch
BranchTrueFalseTotal
unless $mod->{"prepend"} or $mod->{"append"} 74 4 78
Condition100%3/3
$mod->{"prepend"} or $mod->{"append"}
l!l&&r!l&&!r
2274
MC/DC100%2/2
$mod->{"prepend"} or $mod->{"append"}
$mod->{"prepend"} $mod->{"append"}
Truth table100%3/3
A or B
A= $mod->{"prepend"}B= $mod->{"append"}
ABresult
1X1
011
000
498  
499 78 ▶ $mod->{prepend} ? $target_element->prepend($value)
Branch: 1 branch
BranchTrueFalseTotal
$mod->{'prepend'} ? : 2 76 78
500 : $target_element->append($value);
501 }
502 }
503  
504 # ArrayRef
505 elsif (ref $action eq 'ARRAY') {
506  
507 1 $self->_render_directives($target_element, $action);
508 }
509  
510 # HashRef
511 elsif (ref $action eq 'HASH') {
512  
513 15 my ($new_data_root, $new_directives) = %$action;
514  
515 15 my $new_data = $self->get($new_data_root);
516  
517 # loop render
518 15 ▶ if (defined $new_data && ref $new_data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_data and ref $new_data eq 'ARRAY') { } 12 3 15
Condition66%2/3
defined $new_data and ref $new_data eq "ARRAY"
!ll&&!rl&&r
0312
MC/DC50%1/2
defined $new_data and ref $new_data eq "ARRAY"
defined $new_data ref $new_data eq "ARRAY"
Truth table66%2/3
A and B
A= defined $new_dataB= ref $new_data eq "ARRAY"
ABresult
0X0
100
111
519  
520 12 my $total = @$new_data;
521 12 ▶ my $item_tpl = $mod->{replace} ? $target_element->contents
Branch: 1 branch
BranchTrueFalseTotal
$mod->{'replace'} ? : 4 8 12
522 : $target_element;
523  
524 12 ▶ for (my $i = 0; $i < @$new_data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$new_data) 36 12 48
525  
526 36 $self->_push_stack("$new_data_root.$i");
527  
528 # temporary loop var
529 36 local $self->data->{$self->loop_var} = {
530 index => $i + 1,
531 total => $total
532 };
533  
534 36 my $new_item = $item_tpl->clone;
535 36 $new_item->insert_before($target_element);
536 36 $self->_render_directives($new_item, $new_directives);
537  
538 36 $self->_pop_stack;
539 }
540  
541 12 $target_element->remove;
542 }
543 else {
544  
545 3 $self->_push_stack($new_data_root);
546 3 $self->_render_directives($target_element, $new_directives);
547 3 $self->_pop_stack;
548 }
549 }
550  
551 # CodeRef
552 elsif (ref $action eq 'CODE') {
553 # Template::Pure used this coderef to receive a value.
554 # our coderef is used to perform custom element rendering,
555 # We support evaluating a value from a coderef when a datapoint is a coderef.
556 2 $action->($target_element, $self);
557 }
558  
559 # replace
560 $target_element->replace_with($target_element->contents)
561 135 ▶ if $mod->{replace};
Branch: 1 branch
BranchTrueFalseTotal
if $mod->{"replace"} 19 116 135
562 }
563 }
564  
565 181 ▶sub _parse_matchspec_modifiers {
Subroutine _parse_matchspec_modifiers: called CC=1 SCAR=0.0
566 181 my %mod;
567  
568 # ^<match_spec>
569 181 $mod{replace} = $_[1] =~ /^\+?\^\+?/;
570  
571 # +<match_spec>
572 181 $mod{prepend} = $_[1] =~ /^\^?\+\^?/;
573  
574 # <match_spec>+
575 181 $mod{append} = $_[1] =~ /\+$/;
576  
577 # remove modifier characters
578 181 $_[1] =~ s/^[+^]+(?=[\w\.\[\*\#])//g;
579 181 $_[1] =~ s/(?<=[\w\.\#\*\]])[+]+$//g;
580  
581 181 \%mod;
582 }
583  
584  
585 3 ▶sub run_snippet {
Subroutine run_snippet: called CC=3 SCAR=11.2
Pod undocumented
586 3 my ($self, $name, $element, $params) = @_;
587  
588 # instantiate
589 3 ($name, my $action) = split /\//, $name;
590 3 my $snippet = $self->snippet($name, $params);
591  
592 # action
593 3 ▶ $action ||= 'process';
Condition50%1/2
$action ||= "process"
l!l
03
MC/DC0%0/1
$action ||= "process"
$action
Truth table50%1/2
A
A= $action
Aresult
11
00
594 3 my $method = $snippet->can($action);
595  
596 3 ▶ die "Invalid action '$action' for snippet '$name'."
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 3 3
597 unless $method;
598  
599 # run
600 3 $snippet->$method($element, $self, $params);
601 }
602  
603 3 ▶sub snippet {
Subroutine snippet: called CC=2 SCAR=7.1
Pod undocumented
604 3 my ($self, $name, $params) = @_;
605 3 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
30
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
606 3 $self->_load_snippet->($name, $params);
607 }
608  
609  
610  
611  
612  
613 10 1;
614 __END__
« Plift/Handler/Include.pm ↑ Summary Plift/Handler/Render.pm »