| 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
Condition33%1/3
is_blessed_ref($self) and $self->isa(__PACKAGE__)
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__)
| |||||||||||||||||||||||||||||
| 40 | unless is_blessed_ref $self && $self->isa(__PACKAGE__); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 33 | ▶ | return if $method eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | Carp::croak qq{Can't locate object method "$method" via package "$package"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->helper and $self->helper->can($method)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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) || {}
MC/DC0%0/1
$self->get($data_point) || {}
$self->get($data_point)
Truth table50%1/2
A
A= $self->get($data_point)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 25 | my $directive_stack = $self->_directive_stack; | |||||||||||||||||||||||||||
| 107 | 25 | ▶ | pop @$directive_stack while (@$directive_stack > 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 31 | ▶ | return if $parent->filter($parent_selector)->size == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 160 | if $node->isa('XML::LibXML::jQuery'); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 26 | ▶ | unless ($node->hasAttribute($self->internal_id_attribute)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 3 | ▶ | push @$directives, @{$_[0]} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | if $reftype eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | push @$directives, %{$_[0]} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 3 | ▶ | confess "Invalid parameter given to set(data): data must be a hashref." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | # undefined data | ||||||||||||||||||||||||||||
| 248 | 276 | ▶ | confess "get('$reference') error: '$current_path' is undefined." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 197 | ▶ | $data = '' unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 347 | if defined $self->active_handlers; | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 57 | ▶ | @handlers = grep { !exists $self->inactive_handlers->{$_->{name}} } @handlers | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%2/2
$handler->{"tag"} and scalar grep({$_ eq $tagname;} @{$$handler{"tag"};})
MC/DC100%1/1
$handler->{"tag"} and scalar grep({$_ eq $tagname;} @{$$handler{"tag"};})
$handler->{"tag"}
Truth table100%2/2
A
A= $handler->{"tag"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 28 | $handler_match = 1; | |||||||||||||||||||||||||||
| 392 | 28 | last; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # dispatch | ||||||||||||||||||||||||||||
| 398 | 332 | ▶ | $handler->{sub}->($el, $self) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 408 | if defined $data; | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | # already rendering | ||||||||||||||||||||||||||||
| 411 | 26 | ▶ | die "Can't call render() now. We are already rendering." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | # apply wrapper | ||||||||||||||||||||||||||||
| 427 | 25 | ▶ | if ($self->wrapper) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 136 | ▶ | last if $self->is_aborted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 468 | 135 | ▶ | $target_element = $el->filter($selector) if $target_element->size == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | 135 | ▶ | next unless $target_element->size > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | # Scalar | ||||||||||||||||||||||||||||
| 472 | 135 | ▶ | if (!ref $action) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 117 | my $value = $self->get($action); | |||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 117 | ▶ | $target_element->remove unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | # to_json | ||||||||||||||||||||||||||||
| 479 | 117 | ▶ | $value = to_json($value, { convert_blessed => 1 }) if ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | # encode | ||||||||||||||||||||||||||||
| 482 | 117 | $value = encode 'UTF-8', $value; | |||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | 117 | ▶ | if (defined $attribute && $attribute ne 'HTML') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $attribute and $attribute ne "HTML"
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"
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $attribute and $attribute eq "HTML"
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"
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$mod->{"prepend"} or $mod->{"append"}
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"}
| |||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 78 | ▶ | $mod->{prepend} ? $target_element->prepend($value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $new_data and ref $new_data eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | 12 | my $total = @$new_data; | |||||||||||||||||||||||||||
| 521 | 12 | ▶ | my $item_tpl = $mod->{replace} ? $target_element->contents | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | : $target_element; | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 12 | ▶ | for (my $i = 0; $i < @$new_data; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$action ||= "process"
$action
Truth table50%1/2
A
A= $action
| |||||||||||||||||||||||||||||
| 594 | 3 | my $method = $snippet->can($action); | |||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 3 | ▶ | die "Invalid action '$action' for snippet '$name'." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||
| 606 | 3 | $self->_load_snippet->($name, $params); | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 10 | 1; | |||||||||||||||||||||||||||
| 614 | __END__ | ||||||||||||||||||||||||||||