Valiant/HTML/Util/TagBuilder.pm

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