View/ByCode/Renderer.pm

filter: Statementstmt 80.9% 259 / 320 Branchbran 52.0% 51 / 98 Conditioncond 41.1% 30 / 73 MC/DCmcdc 18.9% 7 / 37 Subroutinesub 76.4% 39 / 51 Podpod 0.0% 0 / 19 totaltotal 64.5% 386 / 598 CC 79 SCAR 29.8
CC
79
Cov
69%
load
90.0
_yield
56.0
_render
38.0
CRAP 19.7
↑ Summary View/ByCode.pm »
1 package Catalyst::View::ByCode::Renderer;
2 7 $Catalyst::View::ByCode::Renderer::VERSION = '0.28';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Devel::Declare();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Catalyst::View::ByCode::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # use HTML::Entities; ### TODO: think about -- but pollutes our namespaces
12  
13 7 our @EXPORT_OK = qw(clear_markup init_markup get_markup);
14  
15 7 our @EXPORT = qw(template block block_content
16 load
17 yield
18 params
19 attr
20 class id on
21 stash c _
22 doctype boilerplate
23 nbsp
24 );
25 7 our %EXPORT_TAGS = (
26 markup => [ qw(clear_markup init_markup get_markup) ],
27 default => [ @EXPORT ],
28 );
29  
30 our @IS_KNOWN = (
31 # HTML5 tags not defined in HTML::Tagset
32 qw( article aside audio
33 bdi bdo
34 canvas
35 data datalist details dialog
36 figcaption figure footer
37 header
38 keygen
39 main mark markup menu menuitem meter
40 nav
41 output
42 progress
43 rd rp rt ruby
44 section source summary
45 time track
46 video ),
47 7 grep { m{\A \w}xms }
48 keys(%HTML::Tagset::isKnown)
49 );
50  
51 our %EMPTY_ELEMENT = (
52 (
53 7 map { ($_=>1) }
54 qw(source) ### FIXME: more needed!!!
55 ),
56 %HTML::Tagset::emptyElement
57 );
58  
59 #
60 # define variables -- get local() ized at certain positions
61 #
62 7 our @m; # whole content: initialized with &init_markup()
63 7 our @top = ( \@m ); # contains open tags
64 7 our $stash; # current stash
65 7 our $c; # current context
66 7 our $view; # ByCode View instance
67 7 our $block_content; # code for executing &content()
68  
69 #
70 # some constants
71 #
72 7 our $NEED_ESCAPE = qr{[\"<>&\x{0000}-\x{001f}\x{007f}-\x{ffff}]};
73  
74 #
75 # some tags get changed by simply renaming them
76 #
77 # 'html tag' 'sub name'
78 7 our %change_tags = ('select' => 'choice',
79 'link' => 'link_tag',
80 'tr' => 'trow',
81 'td' => 'tcol',
82 'sub' => 'subscript',
83 'sup' => 'superscript',
84 'meta' => 'meta_tag', # Moose needs &meta()...
85 'q' => 'quote',
86 's' => 'strike',
87 'map' => 'map_tag',
88 );
89  
90 ######################################## IMPORT
91 #
92 # just importing this module...
93 #
94 17 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
95 17 my $module = shift; # eat off 'Catalyst::View::ByCode::Renderer';
96  
97 17 my $calling_package = caller;
98  
99 17 my $default_export = grep {$_ eq ':default'} @_;
100  
101 #
102 # do Exporter's Job on Catalyst::View::ByCode::Renderer's @EXPORT
103 #
104 17 $module->export_to_level(1, $module, grep {!ref $_} @_);
105  
106 #
107 # overwrite (or create) &import in calling_package which
108 # - auto-imports all block() directives
109 # - adds a Devel::Declare-setup for every block() directive
110 #
111 17 ▶ if ($default_export && !UNIVERSAL::can($calling_package, '_import')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_export and not UNIVERSAL::can($calling_package, "_import")) 12 5 17
Condition66%2/3
$default_export and not UNIVERSAL::can($calling_package, "_import")
!ll&&!rl&&r
5012
MC/DC50%1/2
$default_export and not UNIVERSAL::can($calling_package, "_import")
$default_export not UNIVERSAL::can($calling_package, "_import")
Truth table66%2/3
A and not(B)
A= $default_exportB= not UNIVERSAL::can($calling_package, "_import")
ABresult
0X0
100
111
112 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 # save original -- in doubt use Exporter::import
115 12 local *_old_import = (*{"$calling_package\::import"}{CODE})
116 0 ? *{"$calling_package\::import"}
117 12 ▶ : *{"Exporter::import"};
Branch: 1 branch
BranchTrueFalseTotal
*{"${calling_package}::import";}{'CODE'} ? : 0 12 12
118  
119 12 *{"$calling_package\::_import"} = *_old_import;
120 12 *{"$calling_package\::import"} = \&overloaded_import;
121 }
122  
123 #
124 # build HTML Tag-subs into caller's namespace
125 #
126 17 ▶ _construct_functions($calling_package)
Branch: 1 branch
BranchTrueFalseTotal
if $default_export 12 5 17
127 if ($default_export);
128  
129 #
130 # create *OUT and *RAW in calling package to allow 'print' to work
131 # 'print OUT' works, Components use a 'select OUT' to allow 'print' alone
132 #
133 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 17 ▶ if ($default_export || !scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_export or not scalar @_) 12 5 17
Condition66%2/3
$default_export or not scalar @_
l!l&&r!l&&!r
1205
MC/DC50%1/2
$default_export or not scalar @_
$default_export not scalar @_
Truth table66%2/3
A or not(B)
A= $default_exportB= not scalar @_
ABresult
1X1
011
000
135 12 tie *{"$calling_package\::OUT"}, $module, 1; # escaped: OUT
136 12 tie *{"$calling_package\::RAW"}, $module, 0; # unescaped: RAW
137 12 tie *{"$calling_package\::STDOUT"}, $module, 1; # escaped: STDOUT
138  
139 # stupid hack to make -w happy ;-)
140 12 my $dummy0 = *{"$calling_package\::OUT"};
141 12 my $dummy1 = *{"$calling_package\::RAW"};
142 12 my $dummy2 = *{"$calling_package\::STDOUT"};
143 }
144 }
145  
146 #
147 # our importing packages' import() routine...
148 #
149 5 ▶sub overloaded_import {
Subroutine overloaded_import: called CC=4 SCAR=13.9
Pod undocumented
150 5 my $imported_package = $_[0];
151 5 my $calling_package = caller;
152  
153 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 5 ▶ if (scalar(@{"$imported_package\::EXPORT_BLOCK"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{"${imported_package}::EXPORT_BLOCK";}) 5 0 5
156 #
157 # process every recorded block() directive
158 #
159 5 my %declare;
160  
161 5 foreach my $symbol (@{"$imported_package\::EXPORT_BLOCK"}) {
162 ### FIXME: aliasing makes trouble in case of overwriting !!!!!
163 5 *{"$calling_package\::$symbol"} = *{"$imported_package\::$symbol"};
164 # *{"$calling_package\::$symbol"} = eval qq{ sub { goto $imported_package\::$symbol } };
165 # *{"$calling_package\::$symbol"} = eval qq{ sub { $imported_package\::$symbol(\@_) } };
166  
167 5 $declare{$symbol} = {
168 const => Catalyst::View::ByCode::Declare::tag_parser
169 };
170 }
171  
172 5 ▶ if (scalar(keys(%declare))) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %declare) 5 0 5
173 5 Devel::Declare->setup_for($calling_package, \%declare);
174 }
175 }
176  
177 #
178 # proceed with the original import
179 #
180 5 goto &{"$imported_package\::_import"};
181 }
182  
183 ######################################## FILE HANDLE MANAGEMENT
184 #
185 # IN/OUT stuff using a tied thing
186 #
187 36 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
188 36 my $class = shift; # my class (Catalyst::View::ByCode::Renderer)
189 36 my $handle = shift; # escaping on or off -- use this scalar as a handle
190 # and its value to decide escaping
191 # -- see PRINT below
192  
193 36 return bless \$handle, $class;
194 }
195  
196 7 ▶sub PRINT {
Subroutine PRINT: called CC=4 SCAR=13.9
197 7 my $handle = shift;
198  
199 7 push @{$top[-1]},
200 map {
201 7 blessed($_) && $_->can('render')
202 ? $_->render()
203 : $$handle
204 7 ▶ ? do { my $text = "$_";
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) && $_->can('render') ? : 2 5 7
$$handle ? : 2 3 5
Condition66%2/3
builtin::blessed($_) and $_->can("render")
!ll&&!rl&&r
502
MC/DC50%1/2
builtin::blessed($_) and $_->can("render")
builtin::blessed($_) $_->can("render")
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->can("render")
ABresult
0X0
100
111
205 2 $text =~ s{($NEED_ESCAPE)}{'&#' . ord($1) . ';'}oexmsg;
206 2 $text; }
207 : "$_"
208 }
209 @_;
210 7 return;
211 }
212  
213 0 ▶sub PRINTF { $_[0]->PRINT(sprintf(@_[1..$#_])) }
Subroutine PRINTF: not called CC=1 SCAR=6.9
214  
215 ######################################## MARKUP
216 #
217 #
218 #
219 33 ▶sub clear_markup {
Subroutine clear_markup: called CC=1 SCAR=0.0
Pod undocumented
220 33 @m = ();
221 33 @top = ( \@m );
222 33 undef $c;
223 33 undef $stash;
224 33 undef $view;
225 }
226  
227 28 ▶sub init_markup {
Subroutine init_markup: called CC=3 SCAR=11.0
Pod undocumented
228 28 clear_markup();
229  
230 28 $view = shift;
231 28 $c = shift;
232 28 ▶ $stash = $c && $c->can('stash')
Branch: 1 branch
BranchTrueFalseTotal
$Catalyst::View::ByCode::Renderer::c && $Catalyst::View::ByCode::Renderer::c->can('stash') ? : 1 27 28
Condition66%2/3
$Catalyst::View::ByCode::Renderer::c and $Catalyst::View::ByCode::Renderer::c->can("stash")
!ll&&!rl&&r
2701
MC/DC50%1/2
$Catalyst::View::ByCode::Renderer::c and $Catalyst::View::ByCode::Renderer::c->can("stash")
$Catalyst::View::ByCode::Renderer::c $Catalyst::View::ByCode::Renderer::c->can("stash")
Truth table66%2/3
A and B
A= $Catalyst::View::ByCode::Renderer::cB= $Catalyst::View::ByCode::Renderer::c->can("stash")
ABresult
0X0
100
111
233 ? $c->stash
234 : {}; # primitive fallback
235 }
236  
237 61 ▶sub get_markup { _render(@m) }
Subroutine get_markup: called CC=1 SCAR=0.0
Pod undocumented
238  
239 92 ▶sub _render {
Subroutine _render: called CC=22 SCAR=36.4
240 7 ▶ no warnings 'uninitialized'; # we might have undef sometimes
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 join ('',
243 map {
244 92 ref($_) eq 'ARRAY'
245 # a Tag is [ 'tag', {attrs}, content, ... ]
246 128 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 31 97 128
247 31 my $attr = $_->[1];
248  
249 $_->[0]
250 # tag structure is named => <tag ...>
251 ? "<$_->[0]" .
252 # render attribute(s)
253 join('',
254 map {
255 31 my $k = $_;
256 31 my $v = $attr->{$k};
257  
258 31 ▶ if (!defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $v) { } 2 29 31
elsif ($k eq 'autofocus' or $k eq 'checked' or $k eq 'disabled' or $k eq 'formnovalidate' or $k eq 'hidden' or $k eq 'inert' or $k eq 'multiple' or $k eq 'novalidate' or $k eq 'readonly' or $k eq 'selected' or $k eq 'required') { } 0 29 29
259 2 " $k";
260 ▶ } elsif ($k eq 'autofocus' ||
Condition33%10/30
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple" or $k eq "novalidate" or $k eq "readonly" or $k eq "selected" or $k eq "required"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple" or $k eq "novalidate" or $k eq "readonly" or $k eq "selected"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple" or $k eq "novalidate" or $k eq "readonly"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple" or $k eq "novalidate"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked" or $k eq "disabled"
l!l&&r!l&&!r
0029
$k eq "autofocus" or $k eq "checked"
l!l&&r!l&&!r
0029
MC/DC0%0/11
$k eq "autofocus" or $k eq "checked" or $k eq "disabled" or $k eq "formnovalidate" or $k eq "hidden" or $k eq "inert" or $k eq "multiple" or $k eq "novalidate" or $k eq "readonly" or $k eq "selected" or $k eq "required"
$k eq "autofocus" $k eq "checked" $k eq "disabled" $k eq "formnovalidate" $k eq "hidden" $k eq "inert" $k eq "multiple" $k eq "novalidate" $k eq "readonly" $k eq "selected" $k eq "required"
Truth table8%1/12
A or B or C or D or E or F or G or H or I or J or K
A= $k eq "autofocus"B= $k eq "checked"C= $k eq "disabled"D= $k eq "formnovalidate"E= $k eq "hidden"F= $k eq "inert"G= $k eq "multiple"H= $k eq "novalidate"I= $k eq "readonly"J= $k eq "selected"K= $k eq "required"
ABCDEFGHIJKresult
1XXXXXXXXXX1
01XXXXXXXXX1
001XXXXXXXX1
0001XXXXXXX1
00001XXXXXX1
000001XXXXX1
0000001XXXX1
00000001XXX1
000000001XX1
0000000001X1
000000000011
000000000000
261 $k eq 'checked' ||
262 $k eq 'disabled' ||
263 $k eq 'formnovalidate' ||
264 $k eq 'hidden' ||
265 $k eq 'inert' ||
266 $k eq 'multiple' ||
267 $k eq 'novalidate' ||
268 $k eq 'readonly' ||
269 $k eq 'selected' ||
270 $k eq 'required') {
271 # special handling for magic names that require magic values
272 0 ▶ $v ? qq{ $k="$k"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
273 } else {
274 # not a special attribute name
275 29 ▶ if (ref $v eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'SCALAR') { } 1 28 29
elsif (ref $v) { } 4 24 28
276 1 $v = $$v;
277 } elsif (ref $v) {
278 # handle ref values differently
279 $v = ref $v eq 'ARRAY'
280 2 ▶ ? join(' ', @{$v})
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 1 1 2
281 : ref $v eq 'HASH'
282 ? join(';',
283 1 map { my $k = $_;
284 1 $k =~ s{([A-Z])|_}{-\l$1}oxmsg;
285 1 "$k:$v->{$_}" }
286 4 ▶ keys %{$v})
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 2 2 4
ref $v eq 'CODE' ? : 1 0 1
287 : ref $v eq 'CODE'
288 ? $v->()
289 : "$v";
290 4 $v =~ s{($NEED_ESCAPE)}{'&#' . ord($1) . ';'}oexmsg;
291 } else {
292 24 $v =~ s{($NEED_ESCAPE)}{'&#' . ord($1) . ';'}oexmsg;
293 }
294  
295 # convert key into dash-separaed version,
296 # dataId -> data-id, data_id => data-id
297 29 $k =~ s{([A-Z])|_}{-\l$1}oxmsg;
298  
299 # compose attr="value"
300 29 qq{ $k="$v"};
301 }
302 }
303 sort # not needed but nice for testing/guessing
304 28 ▶ keys %{$attr}
Branch: 1 branch
BranchTrueFalseTotal
exists $Catalyst::View::ByCode::Renderer::EMPTY_ELEMENT{$_->[0]} ? : 0 28 28
305 ) .
306  
307 # closing tag or content?
308 (exists($EMPTY_ELEMENT{$_->[0]})
309 ? ' />'
310 : '>' .
311 28 _render(@{$_}[2 .. $#$_]) .
312 "</$_->[0]>")
313  
314 # tag is unnamed -- just render content
315 31 ▶ : _render(@{$_}[2 .. $#$_])
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] ? : 28 3 31
316 }
317  
318 # everything else is stringified
319 : "$_"
320 } @_);
321 }
322  
323 ######################################## EXPORTED FUNCTIONS
324 #
325 # a template definition instead of sub RUN {}
326 #
327 1 ▶sub template(&) {
Subroutine template: called CC=1 SCAR=0.1
Pod undocumented
328 1 my $package = caller;
329  
330 1 my $code = shift;
331 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
332 1 ▶ *{"$package\::RUN"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
333 1 push @{$top[-1]}, [ '', {} ];
334  
335 1 push @top, $top[-1]->[-1];
336  
337 1 my $text = $code->();
338 1 ▶ if (ref($text) && UNIVERSAL::can($text, 'render')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text and UNIVERSAL::can($text, 'render')) { } 0 1 1
Condition33%1/3
ref $text and UNIVERSAL::can($text, "render")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $text and UNIVERSAL::can($text, "render")
ref $text UNIVERSAL::can($text, "render")
Truth table33%1/3
A and B
A= ref $textB= UNIVERSAL::can($text, "render")
ABresult
0X0
100
111
339 0 push @{$top[-1]}, $text->render;
340 } else {
341 7 ▶ no warnings 'uninitialized'; # we might see undef values
Subroutine BEGIN: called CC=1 SCAR=0.0
342 0 $text =~ s{($NEED_ESCAPE)}{'&#' . ord($1) . ';'}oexmsg;
343 1 push @{$top[-1]}, $text;
344 }
345  
346 1 pop @top;
347 1 return;
348 1 };
349  
350 1 return;
351 }
352  
353 #
354 # a block definition
355 #
356 2 ▶sub block($&;@) {
Subroutine block: called CC=1 SCAR=0.0
Pod undocumented
357 2 my $name = shift;
358 2 my $code = shift;
359  
360 2 my $package = caller;
361  
362 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
363 7 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
364  
365 #
366 # generate a sub in our namespace
367 #
368 2 ▶ *{"$package\::$name"} = sub(;&@) {
Subroutine __ANON__: called CC=2 SCAR=6.9
369 1 local $block_content = $_[0];
370  
371 1 push @{$top[-1]}, [ '', { @_[1 .. $#_] } ];
372  
373 1 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 1 0 1
374 1 push @top, $top[-1]->[-1];
375 1 push @{$top[-1]}, $code->();
376 1 pop @top;
377 }
378  
379 1 return;
380 2 };
381  
382 #
383 # mark this sub as a special exportable
384 #
385 2 ${"$package\::EXPORT_BLOCK"}{$name} = 1;
386 }
387  
388 #
389 # execute a block's content
390 #
391 0 ▶sub block_content {
Subroutine block_content: not called CC=2 SCAR=17.9
Pod undocumented
392 0 ▶ push @{$top[-1]}, $block_content->(@_) if ($block_content);
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::View::ByCode::Renderer::block_content 0 0 0
393 0 return;
394 }
395  
396 #
397 # a simple shortcut for multiple param(name => ..., value => ...) sequences
398 #
399 0 ▶sub params {
Subroutine params: not called CC=2 SCAR=17.9
Pod undocumented
400 0 my %params = @_;
401  
402 0 ▶ while (my ($name, $value) = each %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 0 0 0
403 0 push @{$top[-1]}, [ 'param', { name => $name, value => $value } ];
404 }
405  
406 0 return;
407 }
408  
409 #
410 # a simple shortcut for css/js handling
411 # usage:
412 # load js => '/url/to/file.js';
413 # load css => '/url/to/file.js';
414 #
415 # load <<Controller_name>> => file_name [.js]
416 #
417 ### FIXME: build more logic into load() -- accumulate calls
418 ### and resolve as late as possible
419 #
420 0 ▶sub load {
Subroutine load: not called CC=9 SCAR=45.0
Pod undocumented
421 0 my $kind = shift;
422  
423 0 ▶ return if (!$kind || ref($kind));
Branch: 1 branch
BranchTrueFalseTotal
if not $kind or ref $kind 0 0 0
Condition0%0/3
not $kind or ref $kind
l!l&&r!l&&!r
000
MC/DC0%0/2
not $kind or ref $kind
not $kind ref $kind
Truth table0%0/3
not(A) or B
A= not $kindB= ref $kind
ABresult
1X1
011
000
424  
425 0 ▶ if ($kind eq 'css') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'css') { } 0 0 0
elsif ($kind eq 'js') { } 0 0 0
elsif (my $controller = $Catalyst::View::ByCode::Renderer::c->controller($kind) and $kind eq 'Js' || $kind eq 'Css') { } 0 0 0
426 #
427 # simple static CSS inserted just here and now
428 #
429 0 push @{$top[-1]},
430 0 map { [ 'link',
431 {
432 rel => 'stylesheet',
433 type => 'text/css',
434 href => $_
435 }
436 ] } @_;
437 } elsif ($kind eq 'js') {
438 #
439 # simple static JS inserted just here and now
440 #
441 0 push @{$top[-1]},
442 0 map { [ 'script',
443 {
444 type => 'text/javascript',
445 src => $_
446 }
447 ] } @_;
448 ▶ } elsif ((my $controller = $c->controller($kind)) &&
Condition0%0/6
my $controller = $Catalyst::View::ByCode::Renderer::c->controller($kind) and $kind eq "Js" || $kind eq "Css"
!ll&&!rl&&r
000
$kind eq "Js" or $kind eq "Css"
l!l&&r!l&&!r
000
MC/DC0%0/3
my $controller = $Catalyst::View::ByCode::Renderer::c->controller($kind) and $kind eq "Js" || $kind eq "Css"
my $controller = $Catalyst::View::ByCode::Renderer::c->controller($kind) $kind eq "Js" $kind eq "Css"
Truth table0%0/4
A and B or C
A= my $controller = $Catalyst::View::ByCode::Renderer::c->controller($kind)B= $kind eq "Js"C= $kind eq "Css"
ABCresult
0XX0
1000
11X1
1011
449 ($kind eq 'Js' || $kind eq 'Css')) {
450 ### FIXME: are Hardcoded controller names wise???
451 #
452 # some other kind of load operation we have a controller for
453 #
454 # $c->log->debug("LOAD: kind=$kind, ref(controller)=" . ref($controller));
455  
456 0 ▶ if ($kind eq 'Css') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'Css') { } 0 0 0
457 0 push @{$top[-1]},
458 [ 'link',
459 {
460 rel => 'stylesheet',
461 type => 'text/css',
462 href =>$c->uri_for($controller->action_for('default'), @_)
463 }
464 ];
465 } else {
466 0 push @{$top[-1]},
467 [ 'script',
468 {
469 type => 'text/javascript',
470 src => $c->uri_for($controller->action_for('default'), @_)
471 }
472 ];
473 }
474 }
475  
476 0 return;
477 }
478  
479 #
480 # a special sub-rendering command like Rails ;-)
481 #
482 # yield \&name_of_a_sub;
483 # yield a_named_yield;
484 # yield 'content';
485 # yield; # same as 'content'
486 # yield 'path/to/template.pl'
487 # ### TODO: yield package::subname
488 # ### TODO: yield +package::package::package::subname
489 #
490 0 ▶sub yield(;*@) {
Subroutine yield: not called CC=5 SCAR=34.0
Pod undocumented
491 0 ▶ my $yield_name = shift || 'content';
Condition0%0/2
shift() || 'content'
l!l
00
MC/DC0%0/1
shift() || 'content'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
492  
493 0 ▶ $c->log->debug("yield '$yield_name' executing...") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::View::ByCode::Renderer::c->debug 0 0 0
494  
495 _yield(exists($c->stash->{yield}->{$yield_name})
496 0 ▶ ? $c->stash->{yield}->{$yield_name}
Branch: 2 branches
BranchTrueFalseTotal
_yield(exists $Catalyst::View::ByCode::Renderer::c->stash->{"yield"}{$yield_name} ? $Catalyst::View::ByCode::Renderer::c->stash->{"yield"}{$yield_name} : $yield_name) or $Catalyst::View::ByCode::Renderer::c->log->info("could not yield '${yield_name}'") 0 0 0
exists $Catalyst::View::ByCode::Renderer::c->stash->{'yield'}{$yield_name} ? : 0 0 0
497 : $yield_name)
498 or $c->log->info("could not yield '$yield_name'");
499  
500 0 return;
501 }
502  
503 # helper for recursive resolution
504 0 ▶sub _yield {
Subroutine _yield: not called CC=7 SCAR=40.3
505 0 my $thing = shift;
506  
507 0 ▶ if (!$thing) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $thing) { } 0 0 0
elsif (ref $thing eq 'ARRAY') { } 0 0 0
elsif (ref $thing eq 'CODE') { } 0 0 0
elsif (not ref $thing) { } 0 0 0
508 0 return;
509 } elsif (ref($thing) eq 'ARRAY') {
510 0 my $result;
511 0 ▶ while (my $x = shift(@{$thing})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = shift @{$thing;}) 0 0 0
512 0 ▶ _yield($x) and $result = 1;
Branch: 1 branch
BranchTrueFalseTotal
_yield($x) and $result = 1 0 0 0
513 }
514 0 return $result;
515 } elsif (ref($thing) eq 'CODE') {
516 0 $thing->();
517 0 return 1;
518 } elsif (!ref($thing)) {
519 0 return _yield($view->_compile_template($c, $thing));
520 }
521 }
522  
523 #
524 # get/set attribute(s) of latest open tag
525 #
526 2 ▶sub attr {
Subroutine attr: called CC=3 SCAR=11.1
Pod undocumented
527 # FIXME: better discovery of set/get !defined wantarray (?)
528
529 2 ▶ return $top[-1]->[1]->{$_[0]} if scalar @_ == 1 && defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and defined wantarray 0 2 2
Condition66%2/3
scalar @_ == 1 and defined wantarray
!ll&&!rl&&r
110
MC/DC0%0/2
scalar @_ == 1 and defined wantarray
scalar @_ == 1 defined wantarray
Truth table66%2/3
A and B
A= scalar @_ == 1B= defined wantarray
ABresult
0X0
100
111
530  
531 7 ▶ no warnings; # avoid odd no of elements in hash
Subroutine BEGIN: called CC=1 SCAR=0.0
532 2 %{ $top[-1]->[1] } = ( %{ $top[-1]->[1] }, @_ );
533 2 return;
534 }
535  
536 #
537 # set a class inside a tag
538 #
539 11 ▶sub class {
Subroutine class: called CC=10 SCAR=23.3
Pod undocumented
540 11 ▶ my @args = @_
Branch: 1 branch
BranchTrueFalseTotal
my(@args) = or return 11 0 11
541 or return;
542  
543 #
544 # class 'huhu'; - set 'huhu' (replacing previous name)
545 # class 'huhu zzz'; - set 'huhu' and 'zzz' (replacing previous name/s)
546 # class '-bar'; - remove 'bar'
547 # class '-bar baz'; - remove 'bar' and 'baz'
548 # class '+foo'; - add 'foo'
549 # class '+foo moo' - add 'foo' and 'moo'
550 # class '+foo -bar baz' - add 'foo', remove 'bar' and 'baz'
551 # class '+foo','-bar','baz' - add 'foo', remove 'bar' and 'baz'
552 # class qw(+foo -bar baz) - same thing.
553 #
554 11 ▶ my $class_name = $top[-1]->[1]->{class} || '';
Condition100%2/2
$Catalyst::View::ByCode::Renderer::top[-1][1]{'class'} || ''
l!l
56
MC/DC100%1/1
$Catalyst::View::ByCode::Renderer::top[-1][1]{'class'} || ''
$Catalyst::View::ByCode::Renderer::top[-1][1]{'class'}
Truth table100%2/2
A
A= $Catalyst::View::ByCode::Renderer::top[-1][1]{'class'}
Aresult
11
00
555 8 my %class = map {($_ => 1)}
556 11 grep {$_}
557 split(qr{\s+}xms, $class_name);
558  
559 11 my $operation = 0; # -1 = sub, 0 = set, +1 = add
560 11 ▶ foreach my $name (grep {length} map {split qr{\s+}xms} grep {!ref && defined && length} @args) {
Condition33%2/6
!ref($_) && defined $_ && length $_
!ll&&!rl&&r
0012
not ref $_ and defined $_
!ll&&!rl&&r
0012
MC/DC0%0/3
!ref($_) && defined $_ && length $_
not ref $_ defined $_ length $_
Truth table25%1/4
not(A) and B && C
A= not ref $_B= defined $_C= length $_
ABCresult
0XX0
10X0
1100
1111
561 16 ▶ if ($name =~ s{\A([-+])}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/\A([-+])//msx) 6 10 16
562 6 ▶ $operation = $1 eq '-' ? -1 : +1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 3 3 6
563 }
564 16 ▶ if ($operation < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($operation < 0) { } 3 13 16
elsif ($operation > 0) { } 6 7 13
565 3 delete $class{$name};
566 } elsif ($operation > 0) {
567 6 $class{$name} = 1;
568 } else {
569 7 %class = ($name => 1);
570 7 $operation = +1;
571 }
572 }
573  
574 11 $top[-1]->[1]->{class} = join(' ', sort keys(%class));
575 11 return;
576 }
577  
578 #
579 # set an ID
580 #
581 1 ▶sub id { $top[-1]->[1]->{id} = $_[0]; return; }
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
582  
583 #
584 # define a javascript-handler
585 #
586 0 ▶sub on { $top[-1]->[1]->{"on$_[0]"} = join('', @_[1..$#_]); return; }
Subroutine on: not called CC=1 SCAR=6.9
Pod undocumented
587  
588 #
589 # simple getters
590 #
591 0 ▶sub stash { $stash }
Subroutine stash: not called CC=1 SCAR=6.9
Pod undocumented
592 0 ▶sub c { $c }
Subroutine c: not called CC=1 SCAR=6.9
Pod undocumented
593  
594 #
595 # generate a proper doctype line
596 #
597 0 ▶sub doctype {
Subroutine doctype: not called CC=3 SCAR=24.8
Pod undocumented
598 0 my $kind = join(' ', @_);
599  
600 # see http://hsivonen.iki.fi/doctype/ for details on these...
601 0 my @doctype_finder = (
602 [qr(html(?:\W*5)) => 'html5'],
603 [qr(html) => 'html5'],
604  
605 [qr(html(?:\W*4[0-9.]*)?\W*s) => 'html4_strict'],
606 [qr(html(?:\W*4[0-9.]*)?\W*[tl]) => 'html4_loose'],
607  
608 [qr(xhtml\W*1\W*1) => 'xhtml1_1'],
609 [qr(xhtml(?:\W*1[0-9.]*)?\W*s) => 'xhtml1_strict'],
610 [qr(xhtml(?:\W*1[0-9.]*)?\W*[tl]) => 'xhtml1_trans'],
611 [qr(xhtml) => 'xhtml1'],
612 );
613  
614 0 my %doctype_for = (
615 default => q{<!DOCTYPE html>},
616 html5 => q{<!DOCTYPE html>},
617 html4 => q{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">},
618 html4_strict => q{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" } .
619 q{"http://www.w3.org/TR/html4/strict.dtd">},
620 html4_loose => q{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" } .
621 q{"http://www.w3.org/TR/html4/loose.dtd">},
622 xhtml1_1 => q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" } .
623 q{"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">},
624 xhtml1 => q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" } .
625 q{"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">},
626 xhtml1_strict=> q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" } .
627 q{"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">},
628 xhtml1_trans => q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" } .
629 q{"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">},
630 );
631  
632 0 my $doctype = 'default';
633 0 foreach my $d (@doctype_finder) {
634 0 ▶ if ($kind =~ m{\A $d->[0]}xmsi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind =~ /\A $d->[0]/imsx) 0 0 0
635 0 $doctype = $d->[1];
636 0 last;
637 }
638 }
639  
640 0 push @{$top[-1]}, $doctype_for{$doctype};
641 }
642  
643 2 ▶sub boilerplate(;&) {
Subroutine boilerplate: called CC=2 SCAR=6.9
Pod undocumented
644 2 my $code = shift;
645  
646 2 push @{$top[-1]}, <<HTML;
647 <!--[if lt IE 7 ]> <html class="no-js ie6" lang="en"> <![endif]-->
648 <!--[if IE 7 ]> <html class="no-js ie7" lang="en"> <![endif]-->
649 <!--[if IE 8 ]> <html class="no-js ie8" lang="en"> <![endif]-->
650 <!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
651 HTML
652  
653 2 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 1 1 2
654 1 $code->();
655 }
656  
657 2 push @{$top[-1]}, '</html>';
658 }
659  
660 ######################################## Locale stuff
661 #
662 # get a localized version of something
663 #
664 {
665 7 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
666 0 ▶sub _ { return $c->localize(@_) }
Subroutine _: not called CC=1 SCAR=6.9
667 }
668  
669 0 ▶sub nbsp { "\x{00a0}" } # bad hack in the moment...
Subroutine nbsp: not called CC=1 SCAR=1.2
Pod undocumented
670  
671 #
672 # define a function for every tag into a given namespace
673 #
674 12 ▶sub _construct_functions {
Subroutine _construct_functions: called CC=3 SCAR=11.0
675 12 my $namespace = shift;
676  
677 7 ▶ no warnings 'redefine'; # in case of a re-compile.
Subroutine BEGIN: called CC=1 SCAR=0.0
678  
679 12 my %declare;
680  
681 # tags with content are treated the same as tags without content
682 12 foreach my $tag_name (@IS_KNOWN) {
683 1656 ▶ my $sub_name = $change_tags{$tag_name}
Condition66%2/3
$Catalyst::View::ByCode::Renderer::change_tags{$tag_name} || $tag_name
l!l&&r!l&&!r
12015360
MC/DC0%0/2
$Catalyst::View::ByCode::Renderer::change_tags{$tag_name} || $tag_name
$Catalyst::View::ByCode::Renderer::change_tags{$tag_name} $tag_name
Truth table66%2/3
A || B
A= $Catalyst::View::ByCode::Renderer::change_tags{$tag_name}B= $tag_name
ABresult
1X1
011
000
684 || $tag_name;
685  
686 # install a tag-named sub in caller's namespace
687 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
688 1656 ▶ *{"$namespace\::$sub_name"} = sub (;&@) {
Subroutine __ANON__: called CC=5 SCAR=16.1
689 17 push @{$top[-1]}, [ $tag_name, { @_[1 .. $#_] } ];
690  
691 17 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 17 0 17
692 17 push @top, $top[-1]->[-1];
693
694 #### TODO: find out why ->render does not work for HTML::FormFu !!!
695
696 17 my $text = $_[0]->(@_);
697 17 ▶ if (ref $text && UNIVERSAL::can($text, 'render')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $text and UNIVERSAL::can($text, 'render')) { } 1 16 17
elsif (ref $text eq 'SCALAR') { } 1 15 16
Condition100%3/3
ref $text and UNIVERSAL::can($text, "render")
!ll&&!rl&&r
1511
MC/DC100%2/2
ref $text and UNIVERSAL::can($text, "render")
ref $text UNIVERSAL::can($text, "render")
Truth table100%3/3
A and B
A= ref $textB= UNIVERSAL::can($text, "render")
ABresult
0X0
100
111
698 1 push @{$top[-1]}, $text->render;
699 } elsif (ref $text eq 'SCALAR') {
700 1 push @{$top[-1]}, $$text;
701 } else {
702 7 ▶ no warnings 'uninitialized'; # we might see undef values
Subroutine BEGIN: called CC=1 SCAR=0.0
703 0 $text =~ s{($NEED_ESCAPE)}{'&#' . ord($1) . ';'}oexmsg;
704 15 push @{$top[-1]}, $text;
705 }
706
707 17 pop @top;
708 }
709  
710 ### TODO: can we call _render() here and save text instead of a structure?
711 ### would convert [ tag => {attr}, content ] to <tag attr>content</tag>
712 # $top[-1] = _render($top[-1]);
713  
714 17 return;
715 1656 };
716 7 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
717  
718 # remember me to generate a magic tag-parser that applies extra magic
719 1656 $declare{$sub_name} = {
720 const => Catalyst::View::ByCode::Declare::tag_parser
721 };
722 }
723  
724 # add logic for block definitions
725 $declare{block} = {
726 12 const => Catalyst::View::ByCode::Declare::block_parser
727 };
728  
729 # install all tag-parsers collected above
730 12 Devel::Declare->setup_for($namespace, \%declare);
731 }
732  
733 7 1;
↑ Summary View/ByCode.pm »