Valiant/HTML/Util/TagBuilder.pm

filter: Statementstmt 84.1% 201 / 239 Branchbran 78.2% 108 / 138 Conditioncond 50.0% 16 / 32 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 64.8% 24 / 37 Podpod 70.0% 7 / 10 totaltotal 77.0% 362 / 470 CC 109 SCAR 26.1
CC
109
Cov
79%
content_tag
32.1
del
15.4
ins
15.4
CRAP 13.6
« Valiant/HTML/Tag.pm ↑ Summary Valiant/Error.pm »
1 package Valiant::HTML::Util::TagBuilder;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Sub::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use overload
8 2 ▶ bool => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
9 0 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 5 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $ATTRIBUTE_SEPARATOR = ' ';
13 5 our %SUBHASH_ATTRIBUTES = map { $_ => 1} qw(data aria);
14 5 our %ARRAY_ATTRIBUTES = map { $_ => 1 } qw(class);
15 5 our %HTML_VOID_ELEMENTS = map { $_ => 1 } qw(area base br col circle embed hr img input keygen link meta param source track wbr);
16 5 our %BOOLEAN_ATTRIBUTES = map { $_ => 1 } qw(
17 allowfullscreen allowpaymentrequest async autofocus autoplay checked compact controls declare default
18 defaultchecked defaultmuted defaultselected defer disabled enabled formnovalidate hidden indeterminate
19 inert ismap itemscope loop multiple muted nohref nomodule noresize noshade novalidate nowrap open
20 pauseonexit playsinline readonly required reversed scoped seamless selected sortable truespeed
21 typemustmatch visible);
22  
23 5 our %HTML_CONTENT_ELEMENTS = map { $_ => 1 } qw(
24 a abbr acronym address apple article aside audio
25 b basefont bdi bdo big blockquote body button
26 canvas caption center cite code colgroup
27 data datalist dd del details dfn dialog dir div dl dt
28 em
29 fieldset figcaption figure font footer form frame frameset
30 head header hgroup h1 h2 h3 h4 5 h6 html
31 i iframe ins
32 kbd label legend li
33 main map mark menu menuitem meter
34 nav noframes noscript
35 object ol optgroup option output
36 p picture pre progress
37 q
38 rp rt ruby
39 s samp script section select small span strike strong style sub summary sup svg
40 table tbody td template textarea tfoot th thead time title tt tr
41 u ul
42 var video);
43  
44 5 has view => (
45 is => 'ro',
46 required => 1,
47 handles => [qw(safe raw escape_html safe_concat)],
48 );
49  
50 5 ▶sub import { shift->_install_tags }
Subroutine import: called CC=1 SCAR=0.0
51  
52 sub _install_tags {
53 5 ▶ my $class = shift;
Subroutine _install_tags: called CC=5 SCAR=19.7
54  
55 5 foreach my $e (keys %HTML_VOID_ELEMENTS) {
56 80 ▶ next if "${class}::_tags"->can($e);
Branch: 1 branch
BranchTrueFalseTotal
if ("${class}::_tags")->can($e) 0 80 80
57 80 my $full_name = $class . "::_tags::$e";
58 $full_name = Sub::Util::set_subname $full_name => sub {
59 0 ▶ my ($self, $attrs) = @_;
Subroutine embed: not called CC=2 SCAR=9.2
Subroutine area: called CC=2 SCAR=9.2
60 0 ▶ $attrs = +{} unless $attrs;
Branch: 2 branches
BranchTrueFalseTotal
unless $attrs 0 0 0
unless $attrs 5 129 134
61  
62 0 return $self->{tb}->tag($e, $attrs);
63 80 };
64 80 Moo::_Utils::_install_tracked("${class}::_tags", $e, $full_name);
65 }
66  
67 5 foreach my $e (keys %HTML_CONTENT_ELEMENTS) {
68 555 ▶ next if "${class}::_tags"->can($e);
Branch: 1 branch
BranchTrueFalseTotal
if ("${class}::_tags")->can($e) 0 555 555
69 555 my $full_name = $class . "::_tags::$e";
70 $full_name = Sub::Util::set_subname $full_name => sub {
71 0 ▶ my $self = shift;
Subroutine del: not called CC=7 SCAR=27.3
Subroutine output: called CC=7 SCAR=27.3
72 0 ▶ my $attrs = ((ref($_[0])||'') eq 'HASH') ? shift : +{};
Branch: 2 branches
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 0 0 0
(ref $_[0] || '') eq 'HASH' ? : 219 21 240
Condition50%2/4
ref $_[0] || ''
l!l
00
ref $_[0] || ''
l!l
22119
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
73 0 my $content = shift;
74 0 ▶ my @args = (((ref($content)||'') eq 'CODE') || ((ref($content)||'') eq 'ARRAY')) ?
Branch: 2 branches
BranchTrueFalseTotal
(ref $content || '') eq 'CODE' || (ref $content || '') eq 'ARRAY' ? : 0 0 0
(ref $content || '') eq 'CODE' || (ref $content || '') eq 'ARRAY' ? : 48 192 240
Condition35%5/14
(ref $content || "") eq "CODE" or (ref $content || "") eq "ARRAY"
l!l&&r!l&&!r
000
ref $content || ''
l!l
00
ref $content || ''
l!l
00
(ref $content || "") eq "CODE" or (ref $content || "") eq "ARRAY"
l!l&&r!l&&!r
480192
ref $content || ''
l!l
48192
ref $content || ''
l!l
0192
MC/DC0%0/2
(ref $content || "") eq "CODE" or (ref $content || "") eq "ARRAY"
ref $content ref $content
MC/DC50%1/2
(ref $content || "") eq "CODE" or (ref $content || "") eq "ARRAY"
ref $content ref $content
Truth table0%0/3
A or B
A= ref $contentB= ref $content
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= ref $contentB= ref $content
ABresult
1X1
011
000
75 ($e, $attrs, $content) :
76 ($e, $content, $attrs);
77  
78 0 return $self->{tb}->content_tag(@args);
79 555 };
80 555 Moo::_Utils::_install_tracked("${class}::_tags", $e, $full_name);
81 }
82 }
83  
84 sub is_content_tag {
85 0 ▶ my ($self, $name) = @_;
Subroutine is_content_tag: not called CC=2 SCAR=17.9
Pod undocumented
86 0 ▶ return $HTML_CONTENT_ELEMENTS{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Valiant::HTML::Util::TagBuilder::HTML_CONTENT_ELEMENTS{$name} ? : 0 0 0
87 }
88  
89 sub is_void_tag {
90 0 ▶ my ($self, $name) = @_;
Subroutine is_void_tag: not called CC=2 SCAR=17.9
Pod undocumented
91 0 ▶ return $HTML_VOID_ELEMENTS{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Valiant::HTML::Util::TagBuilder::HTML_VOID_ELEMENTS{$name} ? : 0 0 0
92 }
93  
94 sub tags {
95 314 ▶ my $self = shift;
Subroutine tags: called CC=1 SCAR=0.0
Pod documented
96 314 my $class = ref $self;
97 314 return bless +{ tb=>$self }, "${class}::_tags";
98 }
99  
100 sub _omit_tag {
101 463 ▶ my ($self, $attrs) = @_;
Subroutine _omit_tag: called CC=5 SCAR=16.2
102 463 ▶ return 0 unless exists $attrs->{omit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"omit"} 457 6 463
103 6 my $omit_tag = delete $attrs->{omit};
104 6 ▶ if(ref($omit_tag) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $omit_tag eq "CODE") 2 4 6
105 2 ▶ return $omit_tag->($self->view) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&$omit_tag($self->view) ? : 2 0 2
106 }
107 4 ▶ return $omit_tag ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$omit_tag ? : 4 0 4
108 }
109  
110 sub _process_cb_attr {
111 559 ▶ my ($self, $attr_val) = @_;
Subroutine _process_cb_attr: called CC=2 SCAR=6.9
112 559 ▶ return $attr_val->($self->view) if ref($attr_val) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attr_val eq "CODE" 5 554 559
113 554 return $attr_val;
114 }
115  
116 sub tag {
117 158 ▶ my ($self, $name, $attrs) = (@_, +{});
Subroutine tag: called CC=12 SCAR=26.1
Pod documented
118 158 ▶ carp "'$name' is not a valid VOID HTML element" unless $HTML_VOID_ELEMENTS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Valiant::HTML::Util::TagBuilder::HTML_VOID_ELEMENTS{$name} 0 158 158
119  
120 # Handle given / when / when_default
121 158 ▶ if(exists $attrs->{when}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"when"}) 2 156 158
122 2 my $when = $self->_process_cb_attr(delete $attrs->{when});
123 2 ▶ return $self->raw('') unless $when eq $self->{_given}{val};
Branch: 1 branch
BranchTrueFalseTotal
unless $when eq $self->{"_given"}{"val"} 1 1 2
124 1 $self->{_given}{gone} = 1;
125 }
126 157 ▶ if(exists $attrs->{when_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"when_default"}) 0 157 157
127 0 ▶ return $self->raw('') if $self->{_given}{gone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_given"}{"gone"} 0 0 0
128 0 delete $attrs->{when_default};
129 }
130  
131 # Handle 'map'
132 157 ▶ if(my $repeat_proto = $self->_process_cb_attr(delete $attrs->{map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $repeat_proto = $self->_process_cb_attr(delete $attrs->{"map"})) 1 156 157
133 1 my $idx = 0;
134 1 my @repeated_content = ();
135 1 ▶ my $repeat = ref($repeat_proto) eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $repeat_proto eq 'ARRAY' ? : 1 0 1
136 Valiant::HTML::Util::Collection->new(@$repeat_proto)
137 : $repeat_proto;
138 1 ▶ my @code_placeholder_attrs = grep { (ref($attrs->{$_})||'') eq 'CODE' } keys %$attrs;
Condition50%1/2
ref $attrs->{$_} || ''
l!l
01
MC/DC0%0/1
ref $attrs->{$_} || ''
ref $attrs->{$_}
Truth table50%1/2
A
A= ref $attrs->{$_}
Aresult
11
00
139 1 my @template_placeholder_attrs = grep { $attrs->{$_} =~m/\{:.*?\}/ } keys %$attrs;
140 1 ▶ while(my $next = $repeat->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $repeat->next) 3 1 4
141 my %expanded_attrs = (
142 %$attrs,
143 0 (map { my $key = $_; local $_ = $next; $key => $attrs->{$key}->($self->view, $next, $idx) } @code_placeholder_attrs),
144 3 (map { $_ => $self->sf($next, $attrs->{$_}) } @template_placeholder_attrs),
145 );
146 3 push @repeated_content, $self->tag($name, \%expanded_attrs);
147 3 $idx++;
148 }
149 1 ▶ $repeat->reset if $repeat->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $repeat->can("reset") 1 0 1
150 1 my $repeated_content = $self->safe_concat(@repeated_content);
151 1 return $repeated_content;
152 }
153 156 ▶ return $self->raw('') if $self->_omit_tag($attrs);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_omit_tag($attrs) 3 153 156
154 153 return my $tag = $self->raw("<${name}@{[ $self->_tag_options(%{$attrs}) ]}/>");
155 }
156  
157 sub content_tag {
158 329 ▶ my $self = shift;
Subroutine content_tag: called CC=32 SCAR=34.7
Pod documented
159 329 my $name = shift;
160 329 ▶ carp "'$name' is not a valid HTML content element" unless $HTML_CONTENT_ELEMENTS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Valiant::HTML::Util::TagBuilder::HTML_CONTENT_ELEMENTS{$name} 0 329 329
161  
162 329 my ($code, $block);
163 329 ▶ if(ref($_[-1]) eq 'CODE') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[-1] eq 'CODE') { } 67 262 329
elsif (ref $_[-1] eq 'ARRAY') { } 1 261 262
elsif (ref \$_[-1] eq 'SCALAR') { } 21 240 261
elsif (ref \$_[0] eq 'SCALAR') { } 212 28 240
164 67 $code = pop(@_);
165 } elsif(ref($_[-1]) eq 'ARRAY') {
166 1 $block = $self->safe_concat(@{ pop @_ });
167 } elsif(ref(\$_[-1]) eq 'SCALAR') {
168 21 $block = pop @_;
169 } elsif(ref(\$_[0]) eq 'SCALAR') {
170 212 $block = shift @_;
171 }
172  
173 329 my $attrs = +{};
174 329 ▶ if(ref($_[-1]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[-1] eq 'HASH') { } 308 21 329
elsif (ref $_[0] eq 'HASH') { } 4 17 21
175 308 $attrs = pop(@_);
176 } elsif(ref($_[0]) eq 'HASH') {
177 4 $attrs = shift(@_);
178 }
179  
180 329 ▶ $block = $self->safe_concat(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 28 301 329
181  
182 329 my $content = $self->raw('');
183  
184 # Handle 'if'
185 329 ▶ return $content if exists($attrs->{if}) && !$self->_process_cb_attr(delete $attrs->{if});
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"if"} and not $self->_process_cb_attr(delete $attrs->{"if"}) 2 327 329
Condition100%3/3
exists $attrs->{"if"} and not $self->_process_cb_attr(delete $attrs->{"if"})
!ll&&!rl&&r
32522
MC/DC100%2/2
exists $attrs->{"if"} and not $self->_process_cb_attr(delete $attrs->{"if"})
exists $attrs->{"if"} not $self->_process_cb_attr(delete $attrs->{"if"})
Truth table100%3/3
A and not(B)
A= exists $attrs->{"if"}B= not $self->_process_cb_attr(delete $attrs->{"if"})
ABresult
0X0
100
111
186 # Handle 'with'
187 327 my @args = ($self->view);
188 327 ▶ push @args, $self->_process_cb_attr(delete $attrs->{with}) if exists $attrs->{with};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"with"} 2 325 327
189  
190 # Handle given / when / when_default
191 327 ▶ if(exists $attrs->{when}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"when"}) 15 312 327
192 15 my $when = $self->_process_cb_attr(delete $attrs->{when});
193 15 ▶ return $content unless $when eq $self->{_given}{val};
Branch: 1 branch
BranchTrueFalseTotal
unless $when eq $self->{"_given"}{"val"} 11 4 15
194 4 $self->{_given}{gone} = 1;
195 }
196 316 ▶ if(exists $attrs->{when_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"when_default"}) 6 310 316
197 6 ▶ return $content if $self->{_given}{gone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_given"}{"gone"} 5 1 6
198 1 delete $attrs->{when_default};
199 }
200 311 ▶ local $self->{_given} = { val => $self->_process_cb_attr(delete $attrs->{given})} if exists $attrs->{given};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"given"} 6 305 311
201  
202 # Handle 'map'
203 311 ▶ if(my $repeat_proto = $self->_process_cb_attr(delete $attrs->{map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $repeat_proto = $self->_process_cb_attr(delete $attrs->{"map"})) 4 307 311
204 4 my $idx = 0;
205 4 my @repeated_content = ();
206 4 ▶ my $repeat = ref($repeat_proto) eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $repeat_proto eq 'ARRAY' ? : 3 1 4
207 Valiant::HTML::Util::Collection->new(@$repeat_proto)
208 : $repeat_proto;
209 4 ▶ my @code_placeholder_attrs = grep { (ref($attrs->{$_})||'') eq 'CODE' } keys %$attrs;
Condition100%2/2
ref $attrs->{$_} || ''
l!l
13
MC/DC100%1/1
ref $attrs->{$_} || ''
ref $attrs->{$_}
Truth table100%2/2
A
A= ref $attrs->{$_}
Aresult
11
00
210 4 my @template_placeholder_attrs = grep { $attrs->{$_} =~m/\{:.*?\}/ } keys %$attrs;
211 4 ▶ while(my $next = $repeat->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $repeat->next) 12 4 16
212 my %expanded_attrs = (
213 %$attrs,
214 3 (map { my $key = $_; local $_ = $next; $key => $attrs->{$key}->($self->view, $next, $idx) } @code_placeholder_attrs),
215 12 (map { $_ => $self->sf($next, $attrs->{$_}) } @template_placeholder_attrs),
216 );
217 12 ▶ if(defined $code) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $code) { } 9 3 12
elsif (defined $block) { } 0 3 3
218 9 ▶ push @repeated_content, $self->content_tag($name, \%expanded_attrs, sub { $code->(@_, $next, $idx++) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
219 } elsif(defined $block) {
220 0 push @repeated_content, $self->content_tag($name, \%expanded_attrs, $block);
221 } else {
222 3 push @repeated_content, $self->content_tag($name, \%expanded_attrs, $content);
223 }
224 }
225 4 ▶ $repeat->reset if $repeat->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $repeat->can("reset") 4 0 4
226 4 my $repeated_content = $self->safe_concat(@repeated_content);
227 4 return $repeated_content;
228 }
229
230 307 ▶ if(defined $code) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $code) { } 62 245 307
elsif (defined $block) { } 244 1 245
231  
232 # Handle 'repeat'
233 62 ▶ if(my $repeat_proto = $self->_process_cb_attr(delete $attrs->{repeat})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $repeat_proto = $self->_process_cb_attr(delete $attrs->{"repeat"})) 2 60 62
234 2 my $idx = 0;
235 2 my @repeated_content = ();
236 2 ▶ my $repeat = ref($repeat_proto) eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $repeat_proto eq 'ARRAY' ? : 1 1 2
237 Valiant::HTML::Util::Collection->new(@$repeat_proto)
238 : $repeat_proto;
239 2 ▶ while(my $next = $repeat->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $repeat->next) 6 2 8
240 6 push @repeated_content, $code->($self->view, $next, $idx++);
241 }
242 2 ▶ $repeat->reset if $repeat->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $repeat->can("reset") 2 0 2
243 2 $content = $self->safe_concat(@repeated_content);
244 }
245 #prepare content
246 62 ▶ if(! "$content") {
Branch: 1 branch
BranchTrueFalseTotal
unless ("$content") 60 2 62
247 60 my @return = $code->(@args);
248 60 $content = $self->safe_concat(@return);
249 }
250 } elsif(defined $block) {
251 244 $content = $self->safe_concat($block);
252 }
253  
254 307 ▶ return $content if $self->_omit_tag($attrs);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_omit_tag($attrs) 3 304 307
255 304 return my $tag = $self->raw("<${name}@{[ $self->_tag_options(%{$attrs}) ]}>${content}</${name}>");
256 }
257  
258 # switch, repeat, map
259  
260 sub sf {
261 14 ▶ my $self = shift;
Subroutine sf: called CC=7 SCAR=20.7
Pod undocumented
262 14 ▶ if(Scalar::Util::blessed $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 12 2 14
263 12 my ($src_object, $format) = @_;
264 12 ▶ $format =~ s/\{(.*?)\:([^}]+)\}/ $src_object->can($2) ? ($1 ? sprintf($1,$src_object->$2) : $src_object->$2) : carp("Source object '@{[ ref $src_object ]}' has no method '$2'") /gex;
Branch: 2 branches
BranchTrueFalseTotal
$src_object->can($2) ? : 24 0 24
$1 ? : 0 24 24
265 12 return $self->safe($format);
266 } else {
267 2 my ($format, %args) = @_;
268 2 my $collapse = delete $args{collapse};
269 2 ▶ $format =~ s/\{(.*?)\:([^}]+)\}/ exists($args{$2})? ($1 ? sprintf($1,$args{$2}) : $args{$2}) : carp("Source data has no value '$2'") /gex;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{$2} ? : 4 0 4
$1 ? : 0 4 4
270 2 ▶ if($collapse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collapse) 0 2 2
271 0 $format =~s/\s+/ /gsm;
272 }
273 2 return $self->safe($format);
274 }
275 };
276  
277 sub join_tags {
278 82 ▶ my $self = shift;
Subroutine join_tags: called CC=1 SCAR=0.0
Pod documented
279 82 return $self->safe_concat(@_);
280 }
281  
282 0 ▶sub to_string { return shift->{tag_info} || '' }
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{'tag_info'} || ''
l!l
00
MC/DC0%0/1
+(shift())->{'tag_info'} || ''
+(shift())->{'tag_info'}
Truth table0%0/2
A
A= +(shift())->{'tag_info'}
Aresult
11
00
283  
284 # helpers
285  
286 2 ▶sub text { return shift->safe_concat(@_) }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
287  
288 sub link_to {
289 3 ▶ my $self = shift;
Subroutine link_to: called CC=4 SCAR=14.0
Pod documented
290 3 ▶ my $url = shift || carp 'link_to requires a url';
Condition33%1/3
shift() || carp('link_to requires a url')
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || carp('link_to requires a url')
shift() carp('link_to requires a url')
Truth table33%1/3
A || B
A= shift()B= carp('link_to requires a url')
ABresult
1X1
011
000
291 3 ▶ my $attrs = (ref($_[0]) eq 'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 2 3
292 3 ▶ my $content = @_ ? shift : $url;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 1 3
293 3 $attrs->{href} = $url;
294 3 return $self->content_tag('a', $attrs, $content);
295 }
296  
297 # private
298  
299 sub _tag_options {
300 457 ▶ my $self = shift;
Subroutine _tag_options: called CC=10 SCAR=23.0
301 457 my (%attrs) = @_;
302 457 ▶ return '' unless %attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless %attrs 82 375 457
303 375 my @attrs = ('');
304 375 foreach my $attr (sort keys %attrs) {
305 995 ▶ if($BOOLEAN_ATTRIBUTES{$attr}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($Valiant::HTML::Util::TagBuilder::BOOLEAN_ATTRIBUTES{$attr}) { } 73 922 995
elsif ($Valiant::HTML::Util::TagBuilder::SUBHASH_ATTRIBUTES{$attr}) { } 2 920 922
elsif ($Valiant::HTML::Util::TagBuilder::ARRAY_ATTRIBUTES{$attr}) { } 102 818 920
306 73 ▶ push @attrs, $attr if $attrs{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{$attr} 60 13 73
307 } elsif($SUBHASH_ATTRIBUTES{$attr}) {
308 2 foreach my $subkey (sort keys %{$attrs{$attr}}) {
309 4 push @attrs, $self->_tag_option("${attr}-@{[ _dasherize($subkey) ]}", $attrs{$attr}{$subkey});
310 }
311 } elsif($ARRAY_ATTRIBUTES{$attr}) {
312 102 ▶ my $class = ((ref($attrs{$attr})||'') eq 'ARRAY') ? join(' ', @{$attrs{$attr}}) : $attrs{$attr};
Branch: 1 branch
BranchTrueFalseTotal
(ref $attrs{$attr} || '') eq 'ARRAY' ? : 3 99 102
Condition100%2/2
ref $attrs{$attr} || ''
l!l
399
MC/DC100%1/1
ref $attrs{$attr} || ''
ref $attrs{$attr}
Truth table100%2/2
A
A= ref $attrs{$attr}
Aresult
11
00
313 102 push @attrs, $self->_tag_option($attr, $class);
314 } else {
315 818 push @attrs, $self->_tag_option($attr, $attrs{$attr});
316 }
317 }
318 375 return join $ATTRIBUTE_SEPARATOR, @attrs;
319 }
320  
321 sub _tag_option {
322 924 ▶ my $self = shift;
Subroutine _tag_option: called CC=2 SCAR=7.0
323 924 my ($attr, $value) = @_;
324 924 ▶ return qq[${attr}="@{[ $self->escape_html(( defined($value) ? $value : '' )) ]}"];
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 924 0 924
325 }
326  
327 sub _dasherize {
328 4 ▶ my $value = shift;
Subroutine _dasherize: called CC=1 SCAR=0.0
329 4 my $copy = $value;
330 4 $copy =~s/_/-/g;
331 4 return $copy;
332 }
333  
334 package Valiant::HTML::Util::TagBuilder::_tags;
335  
336 use overload
337 0 ▶ bool => sub {1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
338 0 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
339 5 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
340  
341 sub to_string {
342 0 ▶ my $self = shift;
Subroutine to_string: not called CC=1 SCAR=6.9
343 0 return $self->{tb}->to_string;
344 }
345  
346 0 ▶sub join_tags { my $self = shift; $self->{tb}->join_tags(@_); return $self }
Subroutine join_tags: not called CC=1 SCAR=6.9
347 0 ▶sub text { my $self = shift; $self->{tb}->text(@_); return $self }
Subroutine text: not called CC=1 SCAR=6.9
348 0 ▶sub tag { my $self = shift; $self->{tb}->tag(@_); return $self }
Subroutine tag: not called CC=1 SCAR=6.9
349 0 ▶sub content_tag { my $self = shift; $self->{tb}->content_tag(@_); return $self }
Subroutine content_tag: not called CC=1 SCAR=6.9
350  
351 5 1;
352  
353  
354 =head1 NAME
355  
356 Valiant::HTML::Util::TagBuilder - Utility class to generate HTML tags
357  
358 =head1 SYNOPSIS
359  
360 use Valiant::HTML::Util::TagBuilder;
361 my $tag_builder = Valiant::HTML::Util::TagBuilder->new(view => $view);
362 my $tag = $tag_builder->tag('div', { class => 'container' });
363  
364 =head1 DESCRIPTION
365  
366 L<Valiant::HTML::Util::TagBuilder> is a utility class for generating HTML tags. It wraps
367 a view or template object which must provide methods for html escaping and for marking
368 strings as safe for display.
369  
370 =head1 ATTRIBUTES
371  
372 This class has the following initialization attributes
373  
374 =head2 view
375  
376 Object, Required. This should be an object that provides methods for creating escaped
377 strings for HTML display. Many template systems provide a way to mark strings as safe
378 for display, such as L<Mojo::Template>. You will need to add the following proxy methods
379 to your view / template to adapt it for use in creating safe strings that work in the way
380 it expects. If you're view doesn't need this you can just use L<Valiant::HTML::Util::View>.
381  
382 =over
383  
384 =item raw
385  
386 given a string return a single tagged object which is marked as safe for display. Do not do any HTML
387 escaping on the string. This is used when you want to pass strings straight to display and that you
388 know is safe. Be careful with this to avoid HTML injection attacks.
389  
390 =item safe
391  
392 given a string return a single tagged object which is marked as safe for display. First HTML escape the
393 string as safe unless its already been done (no double escaping).
394  
395 =item safe_concat
396  
397 Same as C<safe> but instead works an an array of strings (or mix of strings and safe string objects) and
398 concatenates them all into one big safe marked string.
399  
400 =item html_escape
401  
402 Given a string return string that has been HTML escaped.
403  
404 =item read_attribute
405  
406 Given an attribute name return the value that the view has defined for it.
407  
408 =item attribute_exists
409  
410 Given an attribute name return true if the view has defined a value for it.
411  
412 =back
413  
414 Both C<raw>, C<safe> and C<safe_concat> should return a 'tagged' object which is specific to your view or
415 template system. However this object must 'stringify' to the safe version of the string to be displayed. See
416 L<Valiant::HTML::SafeString> for example API. We use L<Valiant::HTML::SafeString> internally to provide
417 safe escaping if you're view doesn't do automatic escaping, as many older template systems like Template
418 Toolkit.
419  
420 =head1 METHODS
421  
422 =head2 new
423  
424 Create a new instance of the TagBuilder.
425  
426 my $tag_builder = Valiant::HTML::Util::TagBuilder->new(view => $view);
427  
428 =head2 tags
429  
430 Returns a reference to a blessed hash that provides shortcut methods for all HTML tags.
431  
432 my $tags = $tag_builder->tags;
433 my $img_tag = $tags->img({src => '/path/to/image.jpg'});
434 # <img src="/path/to/image.jpg" />
435
436 my $div_tag = $tags->div({id=>'top}, "Content");
437 # <div id="top">Content<div>
438  
439 =head2 tag
440  
441 Generates a HTML tag of the specified type and with the specified attributes.
442  
443 =head2 content_tag
444  
445 Generates a HTML content tag of the specified type, with the specified attributes, and with the specified content.
446  
447 my $tag = $tag_builder->content_tag('p', { class => 'lead' }, 'Lorem ipsum dolor sit amet');
448  
449 The content can also be generated by a code block, as shown in the following example.
450  
451 my $tag = $tag_builder->content_tag('ul', { class => 'list-group' }, sub {
452 $tag_builder->content_tag('li', 'Item 1') .
453 $tag_builder->content_tag('li', 'Item 2') .
454 $tag_builder->content_tag('li', 'Item 3')
455 });
456  
457 =head2 join_tags
458  
459 Joins multiple tags together and returns them as a single string.
460  
461 my $tags = $tag_builder->join_tags(
462 $tag_builder->tag('div', { class => 'container' }),
463 $tag_builder->content_tag('p', 'Lorem ipsum dolor sit amet')
464 );
465  
466 =head2 text
467  
468 Generates a safe string of text.
469  
470 my $text = $tag_builder->text('Lorem ipsum dolor sit amet');
471  
472 =head2 link_to
473  
474 Helper method to generate a link tag.
475  
476 my $link = $tag_builder->link_to($url, \%attrs, $content);
477  
478 C<$url> is the URL to link to and is required. Both C<%attrs> and c<$content>
479 are optional. If C<$content> is not provided, the link text will be the URL.
480  
481 =head2 to_string
482  
483 Returns the generated HTML tag as a string.
484  
485 my $tag = $tag_builder->tag('div', { class => 'container' });
486 my $tag_string = $tag->to_string;
487  
488 =head1 PROXY METHODS
489  
490 The following methods are proxied from the enclosed view object. You should
491 refer to your view for more.
492  
493 =head2 safe
494  
495 =head2 raw
496  
497 =head2 escape_html
498  
499 =head2 safe_concat
500  
501 =head1 LOGIC AND FLOW CONTROL
502  
503 L<Valiant::HTML::Util::TagBuilder> builds in some basic logic and control flow to
504 make it easier to use in small places where a full on template system would be too much
505 but you don't want ugly string concatenation. This system works by adding a handful of
506 custom html attributes to your tag declarations. These custom tags are removed from
507 the final output.
508  
509 =head2 omit
510  
511 my $t = $tag_builder->tags;
512 say $t->hr({omit=>1}) +
513 $t->div({omit=>1}, 'Hello World!');
514  
515 If the value is true the tag is removed from the final output. However any content
516 is preserved.
517  
518 Value can be a scalar which will be evaluated as a boolean, or a coderef which will be called
519 and passed the current C<$view> as an argument).
520  
521 say $t->hr({omit => sub ($view) { 1 }}); # is empty
522  
523 =head if
524  
525 my $bool = 1;
526 say $t->div({id=>'one', if=>$bool}, sub {
527 my ($view) = @_;
528 $t->p('hello');
529 }): # '<div id="one"><p>hello</p></div>';
530  
531 $bool = 0;
532 say $t->div({id=>'one', if=>$bool}, sub {
533 my ($view) = @_;
534 $t->p('hello');
535 }): # '';
536  
537 If the processed value of the tag is false, the tag and any of its contents are removed from
538 the output. Value can be a scalar value or a coderef (which gets the C<$view> as its one
539 argument).
540  
541 =head with
542  
543 Create content with a new local context.
544  
545 say $t->div({id=>'one', with=>'two'}, sub {
546 my ($view, $var) = @_;
547 $t->p($var);
548 }); # '<div id="one"><p>two</p></div>';
549  
550 Useful if you need a local value in your template
551  
552 =head2 repeat
553  
554 =head2 map
555  
556 Used to loop over a tags contents (for content tags) or the tag itself (for both content and
557 empty tags). Examples:
558  
559 say $t->div({id=>'one', repeat=>[1,2,3]}, sub {
560 my ($view, $item, $idx) = @_;
561 $t->p("hello[$idx] $item");
562 }); # '<div id="one"><p>hello[0] 1</p><p>hello[1] 2</p><p>hello[2] 3</p></div>';
563  
564 say $t->div({id=>sub {"one_${_}"}, map=>[1,2,3]}, sub {
565 my ($view) = @_;
566 $t->p('hello');
567 }); # '<div id="one_1"><p>hello</p></div><div id="one_2"><p>hello</p></div><div id="one_3"><p>hello</p></div>';
568  
569 Values for the looping attributes can be an arrayref, an object that does '->next' (for example a L<DBIx::Class>
570 resultset) or a coderef that receives the current view object and returns either of the above.
571  
572 If your object also does C<reset> that method will be called automatically after the last loop item.
573  
574 In the case of C<map> if you want to modify the attributes of the enclosing tag you can use coderefs for
575 those attributes. They will be called with the current view object and current loop item value and index. When also
576 localize C<$_> to be to the current loop item value for ease of use.
577  
578 say $t->hr({ id => sub { my ($view, $val, $i) = @_; "rule_${val}" }, map => [1,2,3] });
579  
580 returns
581  
582 <hr id='rule_2' /><hr id='rule_2' /><hr id='rule_3' />
583  
584 and this does the same:
585  
586 say $t->hr({ id => sub { "rule_${_}" }, map =>[1,2,3] });
587  
588 =head2 given / when
589  
590 Given / When 'switchlike' conditional. Example:
591  
592 say $t->div({id=>'one', given=>'one'}, sub {
593 my ($view) = @_;
594 $t->p({when=>'one'}, "hello one"),
595 $t->p({when=>'two'}, "hello two"),
596 $t->hr({when=>'three'}),
597 $t->p({when_default=>1}, "hello four")
598 }); # '<div id="one"><p>hello one</p></div>';
599  
600 When a content tag has the C<given> attribute, its content must be a coderef which will get the current
601 C<$view> as its one argument. Inside the coderef we will render a tag with a matching C<when> attribute
602 or if there are no matches a tag with the C<when_default> attribute.
603  
604 The value of C<given=> may be a scalar or a coderef. If its a coderef we will call it with the current
605 view object and expect a scalar.
606  
607 =head1 AUTHOR
608  
609 See L<Valiant>
610  
611 =head1 SEE ALSO
612  
613 L<Valiant>, L<Valiant::HTML::FormBuilder>
614  
615 =head1 AUTHOR
616
617 See L<Valiant>
618  
619 =head1 COPYRIGHT & LICENSE
620
621 See L<Valiant>
622  
623 =cut
« Valiant/HTML/Tag.pm ↑ Summary Valiant/Error.pm »