HTML.pm

filter: Statementstmt 96.5% 166 / 172 Branchbran 66.6% 28 / 42 Conditioncond 63.1% 12 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 38 / 38 Podpod 25.0% 1 / 4 totaltotal 86.7% 248 / 286 CC 31 SCAR 12.9
CC
31
Cov
88%
__ANON__
10.1
__ANON__
6.4
build_template
4.6
CRAP 3.6
« HTML/Compiler.pm ↑ Summary HTML/Tag.pm »
1 package DSL::HTML;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Exporter::Declare::Magic qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 import
10 export
11 default_export
12 gen_export
13 gen_default_export
14 7 };
15  
16 7 ▶use DSL::HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use DSL::HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use DSL::HTML::Rendering;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 our $VERSION = '0.006';
22  
23 9 ▶sub after_import {
Subroutine after_import: called CC=1 SCAR=0.0
Pod undocumented
24 9 my $class = shift;
25 9 my ( $importer, $specs ) = @_;
26  
27 9 inject_meta($importer);
28 }
29  
30 12 ▶sub inject_meta {
Subroutine inject_meta: called CC=2 SCAR=6.9
Pod undocumented
31 12 my ($importer) = @_;
32  
33 12 ▶ return $importer->DSL_HTML
Branch: 1 branch
BranchTrueFalseTotal
if $importer->can("DSL_HTML") 2 10 12
34 if $importer->can('DSL_HTML');
35  
36 10 my $meta = {};
37  
38 {
39 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 10 ▶ *{"$importer\::DSL_HTML"} = sub { $meta };
Subroutine __ANON__: called CC=1 SCAR=0.0
41 }
42  
43 10 return $meta;
44 }
45  
46 7 ▶default_export import {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=6 SCAR=18.6
47 3 my $class = shift;
48  
49 3 my $caller = caller;
50 3 my $imeta = inject_meta($caller);
51 3 my $meta = $class->DSL_HTML;
52  
53 3 ▶ my @want = @_ ? @_ : keys %$meta;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
54  
55 3 for my $template (@want) {
56 8 ▶ if ( $imeta->{$template} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imeta->{$template}) 0 8 8
57 0 carp "'$template' already defined in class '$caller', not replacing";
58 0 next;
59 }
60  
61 8 ▶ $imeta->{$template} = $meta->{$template}
Condition33%1/3
$meta->{$template} || carp("'${template}' is not defined by '${class}'")
l!l&&r!l&&!r
800
MC/DC0%0/2
$meta->{$template} || carp("'${template}' is not defined by '${class}'")
$meta->{$template} carp("'${template}' is not defined by '${class}'")
Truth table33%1/3
A || B
A= $meta->{$template}B= carp("'${template}' is not defined by '${class}'")
ABresult
1X1
011
000
62 || carp "'$template' is not defined by '$class'";
63 }
64  
65 3 ▶ return 1 if $caller->can('build_template');
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can("build_template") 2 1 3
66  
67 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 *{"$caller\::build_template"} = \&build_template;
69  
70 1 return 1;
71 7 }
72  
73 7 ▶default_export template dsl_html {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=4 SCAR=14.7
74 14 my $name = shift;
75 14 ▶ die "Template name is required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 14 14
76 14 my ( $params, $block );
77 14 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 14 0 14
78 14 $block = pop @_;
79 14 $params = {};
80 }
81 else {
82 0 $params = {@_};
83 0 $block = delete $params->{block};
84 }
85  
86 14 my $template = DSL::HTML::Template->new( $name, $params, $block );
87 14 ▶ return $template if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 14 14
88  
89 14 caller->DSL_HTML->{$name} = $template;
90 7 }
91  
92 7 ▶default_export tag dsl_html {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=10 SCAR=23.1
93 43 my $name = shift;
94 43 ▶ croak "tag name is required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 43 43
95 43 my ( $params, $block );
96 43 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 29 14 43
97 29 $block = pop @_;
98 29 $params = {};
99 }
100 else {
101 14 $params = {@_};
102 14 $block = delete $params->{block};
103 }
104  
105 43 check_nesting('tag');
106 43 my $rendering = DSL::HTML::Rendering->current;
107  
108 43 my $tag;
109 43 ▶ if ( $name =~ m/^head$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ /^head$/i) { } 4 39 43
elsif ($name =~ /^body$/i) { } 2 37 39
elsif ($name =~ /^html$/i) { } 0 37 37
110 4 $tag = $rendering->head;
111 }
112 elsif ( $name =~ m/^body$/i ) {
113 2 $tag = $rendering->body;
114 }
115 elsif ( $name =~ m/^html$/i ) {
116 0 $tag = $rendering->root;
117 }
118 else {
119 37 $tag = HTML::Element->new( $name, %$params );
120 37 $rendering->insert($tag);
121 }
122  
123 43 $rendering->push_tag($tag);
124 43 my @result;
125 43 my $success = eval {
126 43 @result = $block->($tag);
127 43 1;
128 };
129 43 my $error = $@;
130 43 $rendering->pop_tag($tag);
131 43 ▶ die $error unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 43 43
132  
133 43 ▶ $tag->push_content(@result)
Branch: 1 branch
BranchTrueFalseTotal
if @result and not ref $result[0] and not $tag->content_list 25 18 43
Condition100%6/6
@result and not ref $result[0] and not $tag->content_list
!ll&&!rl&&r
15325
@result and not ref $result[0]
!ll&&!rl&&r
12328
MC/DC100%3/3
@result and not ref $result[0] and not $tag->content_list
@result not ref $result[0] not $tag->content_list
Truth table100%4/4
A and not(B) and not(C)
A= @resultB= not ref $result[0]C= not $tag->content_list
ABCresult
0XX0
10X0
1100
1111
134 if @result && !ref $result[0] && !$tag->content_list;
135  
136 43 return;
137 7 }
138  
139 7 ▶default_export get_template {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.0
140 2 my $name = pop;
141 2 ▶ my $from = $_[0] || caller;
Condition66%2/3
$_[0] || &CORE::GLOBAL::caller()
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[0] || &CORE::GLOBAL::caller()
$_[0] &CORE::GLOBAL::caller()
Truth table66%2/3
A || B
A= $_[0]B= &CORE::GLOBAL::caller()
ABresult
1X1
011
000
142 2 return $from->DSL_HTML->{$name};
143 7 }
144  
145 7 default_export 'build_template';
146  
147 4 ▶sub build_template {
Subroutine build_template: called CC=4 SCAR=15.2
Pod documented
148 4 my ( $template, @args ) = @_;
149 4 my $caller = caller;
150  
151 4 ▶ $template->compile(@args)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($template) and $template->isa("DSL::HTML::Template") 0 4 4
Condition33%1/3
builtin::blessed($template) and $template->isa("DSL::HTML::Template")
!ll&&!rl&&r
400
MC/DC0%0/2
builtin::blessed($template) and $template->isa("DSL::HTML::Template")
builtin::blessed($template) $template->isa("DSL::HTML::Template")
Truth table33%1/3
A and B
A= builtin::blessed($template)B= $template->isa("DSL::HTML::Template")
ABresult
0X0
100
111
152 if blessed($template)
153 && $template->isa('DSL::HTML::Template');
154  
155 croak "No such template '$template'"
156 4 ▶ unless $caller->DSL_HTML->{$template};
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->DSL_HTML->{$template} 0 4 4
157  
158 4 return $caller->DSL_HTML->{$template}->compile(@args);
159 }
160  
161 7 ▶default_export include {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.1
162 6 my ( $template, @args ) = @_;
163 6 my $caller = caller;
164  
165 6 check_nesting('include');
166  
167 my $tmp =
168 blessed($template)
169 ? $template
170 6 ▶ : $caller->DSL_HTML->{$template};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($template) ? : 0 6 6
171  
172 6 ▶ croak "No such template '$template'"
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 0 6 6
173 unless $tmp;
174  
175 6 DSL::HTML::Rendering->current->include( $tmp, @args );
176  
177 6 return;
178 7 }
179  
180 7 ▶default_export text {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
181 2 my ($content) = @_;
182 2 check_nesting('text');
183 2 DSL::HTML::Rendering->current->peek_tag->push_content("$content");
184 2 return;
185 7 }
186  
187 7 ▶default_export css {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
188 8 check_nesting('css');
189 8 DSL::HTML::Rendering->current->add_css(@_);
190 8 return;
191 7 }
192  
193 7 ▶default_export js {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
194 10 check_nesting('js');
195 10 DSL::HTML::Rendering->current->add_js(@_);
196 10 return;
197 7 }
198  
199 7 ▶default_export attr {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=4 SCAR=13.9
200 4 check_nesting('attr');
201 4 my $tag = DSL::HTML::Rendering->current->peek_tag;
202 4 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
203 4 ▶ my $attrs = @_ < 2 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 ? : 2 2 4
204 4 $tag->attr( $_ => $attrs->{$_} ) for keys %$attrs;
205 4 return;
206 7 }
207  
208 7 ▶default_export add_class {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
209 2 check_nesting('add_class');
210  
211 2 my $tag = DSL::HTML::Rendering->current->peek_tag;
212 2 ▶ my $existing = $tag->attr('class') || "";
Condition50%1/2
$tag->attr('class') || ''
l!l
20
MC/DC0%0/1
$tag->attr('class') || ''
$tag->attr('class')
Truth table50%1/2
A
A= $tag->attr('class')
Aresult
11
00
213  
214 2 my %seen;
215 2 my $new = join " ", sort grep { !$seen{$_}++ } @_, split /\s+/, $existing;
216 2 $tag->attr( class => $new );
217  
218 2 return;
219 7 }
220  
221 7 ▶default_export del_class {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
222 2 check_nesting('del_class');
223  
224 2 my $tag = DSL::HTML::Rendering->current->peek_tag;
225 2 ▶ my $existing = $tag->attr('class') || "";
Condition50%1/2
$tag->attr('class') || ''
l!l
20
MC/DC0%0/1
$tag->attr('class') || ''
$tag->attr('class')
Truth table50%1/2
A
A= $tag->attr('class')
Aresult
11
00
226  
227 2 my %seen = map { $_ => 1 } @_;
228 2 my $new = join " ", sort grep { !$seen{$_}++ } split /\s+/, $existing;
229 2 $tag->attr( class => $new );
230  
231 2 return;
232 7 }
233  
234 77 ▶sub check_nesting {
Subroutine check_nesting: called CC=2 SCAR=8.1
Pod undocumented
235 77 my ($sub) = @_;
236 77 ▶ return if DSL::HTML::Rendering->current;
Branch: 1 branch
BranchTrueFalseTotal
if "DSL::HTML::Rendering"->current 77 0 77
237 0 croak "No template stack found, '$sub()' must have been called outside of a template.";
238 }
239  
240 7 1;
241  
242 __END__
« HTML/Compiler.pm ↑ Summary HTML/Tag.pm »