blib/lib/Mojolicious/Plugin/FontAwesomeHelpers.pm

filter: Statementstmt 99.0% 105 / 106 Branchbran 94.1% 64 / 68 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 94.6% 195 / 206 CC 39 SCAR 24.7
CC
39
Cov
96%
_fa_class
22.0
extract_flags
7.0
_icon
6.0
CRAP 11.9
↑ Summary
1 package Mojolicious::Plugin::FontAwesomeHelpers;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use version; our $VERSION = version->declare('v0.1.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use subs qw(extract_block extract_flags);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
11 1 my $self = shift;
12 1 my $app = shift;
13  
14 1 $app->helper('icon' => \&_icon);
15 1 $app->helper('fa_icon' => \&_icon);
16 1 $app->helper('fa.icon' => \&_icon);
17  
18 1 $app->helper('stacked_icon' => \&_stack);
19 1 $app->helper('fa_stack' => \&_stack);
20 1 $app->helper('fa.stack' => \&_stack);
21  
22 1 ▶ $app->helper('fa.class' => sub { shift; _fa_class(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
23 }
24  
25 5 ▶sub _stack {
Subroutine _stack: called CC=4 SCAR=13.9
26 5 my $c = shift;
27 5 my $content_class = _fa_class('fa-stack', @_);
28  
29 5 my $content;
30 5 ▶ if (@_ % 2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 5 0 5
31 5 my $block = extract_block \@_;
32 5 ▶ $content = $block ? $block->() : pop;
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 1 4 5
33 }
34 else {
35 0 Carp::croak "content is required in the form of text or a block";
36 }
37  
38 5 my %html_options = @_;
39 5 ▶ $content_class .= " $html_options{class}" if $html_options{class};
Branch: 1 branch
BranchTrueFalseTotal
if $html_options{"class"} 1 4 5
40 5 $html_options{class} = $content_class;
41  
42 5 return $c->tag('span', class => $content_class, %html_options, $content);
43 }
44  
45 9 ▶sub _icon {
Subroutine _icon: called CC=6 SCAR=17.9
46 9 my $c = shift;
47 9 my $icon = shift;
48 9 my $content_class = _fa_class($icon, @_);
49  
50 9 my $text;
51 9 ▶ if (@_ % 2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) 4 5 9
52 4 my $block = extract_block \@_;
53 4 ▶ $text = $block ? $block->() : pop;
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 2 2 4
54 }
55  
56 9 my %html_options = @_;
57 9 ▶ $content_class .= " $html_options{class}" if $html_options{class};
Branch: 1 branch
BranchTrueFalseTotal
if $html_options{"class"} 1 8 9
58 9 $html_options{class} = $content_class;
59 9 ▶ $html_options{'aria-hidden'} //= 'true';
Condition50%1/2
$html_options{"aria-hidden"} //= "true"
l!l
09
MC/DC0%0/1
$html_options{"aria-hidden"} //= "true"
$html_options{"aria-hidden"}
Truth table50%1/2
A
A= $html_options{"aria-hidden"}
Aresult
11
00
60  
61 9 my $html = $c->tag('i', %html_options);
62 9 ▶ $html = $c->b($html . " $text") if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 4 5 9
63  
64 9 return $html;
65 }
66  
67 35 ▶sub _fa_class {
Subroutine _fa_class: called CC=22 SCAR=30.9
68 35 my $icon = shift;
69 35 ▶ my $class = blessed($icon) ? _try($icon, 'fa_class') : $icon;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($icon) ? : 1 34 35
70 35 my %options = extract_flags \@_;
71  
72 35 ▶ $class .= " fa-$options{-size}" if $options{-size};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-size"} 5 30 35
73 35 ▶ $class .= " fa-pull-$options{-pull}" if $options{-pull};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-pull"} 1 34 35
74 35 ▶ $class .= " fa-rotate-$options{-rotate}" if $options{-rotate};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-rotate"} 1 34 35
75 35 ▶ $class .= " fa-stack-$options{-stack}" if $options{-stack};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-stack"} 1 34 35
76  
77 35 ▶ my $swap_opacity = defined $options{-opacity} && $options{-opacity} eq 'swap';
Condition66%2/3
defined $options{'-opacity'} && $options{'-opacity'} eq 'swap'
!ll&&!rl&&r
3401
MC/DC50%1/2
defined $options{'-opacity'} && $options{'-opacity'} eq 'swap'
defined $options{'-opacity'} $options{'-opacity'} eq 'swap'
Truth table66%2/3
A && B
A= defined $options{'-opacity'}B= $options{'-opacity'} eq 'swap'
ABresult
0X0
100
111
78 35 ▶ $class .= " fa-swap-opacity" if $swap_opacity;
Branch: 1 branch
BranchTrueFalseTotal
if $swap_opacity 1 34 35
79  
80 35 ▶ my $auto_width = defined $options{-width} && $options{-width} eq 'auto';
Condition66%2/3
defined $options{'-width'} && $options{'-width'} eq 'auto'
!ll&&!rl&&r
3401
MC/DC50%1/2
defined $options{'-width'} && $options{'-width'} eq 'auto'
defined $options{'-width'} $options{'-width'} eq 'auto'
Truth table66%2/3
A && B
A= defined $options{'-width'}B= $options{'-width'} eq 'auto'
ABresult
0X0
100
111
81 35 ▶ $class .= " fa-width-auto" if $auto_width;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_width 1 34 35
82 35 ▶ $class .= " fa-inverse" if $options{-inverse};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-inverse"} 1 34 35
83 35 ▶ $class .= " fa-bounce" if $options{-bounce};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-bounce"} 1 34 35
84 35 ▶ $class .= " fa-shake" if $options{-shake};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-shake"} 1 34 35
85  
86  
87 35 ▶ if (my $spin = $options{-spin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spin = $options{"-spin"}) 5 30 35
88 5 ▶ if ($spin eq 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spin eq 1) { } 1 4 5
elsif (ref $spin eq 'ARRAY') { } 1 3 4
89 1 $class .= " fa-spin";
90 }
91 elsif (ref $spin eq 'ARRAY') {
92 1 $class .= " " . join(' ' => map {"fa-spin-$_"} @$spin);
93 }
94 else {
95 3 $class .= " fa-spin-$spin";
96 }
97 }
98  
99 35 ▶ if (my $flip = $options{-flip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flip = $options{"-flip"}) 2 33 35
100 2 ▶ if ($flip eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flip eq 1) { } 1 1 2
101 1 $class .= " fa-flip";
102 }
103 else {
104 1 $class .= " fa-flip-$flip";
105 }
106 }
107  
108 35 ▶ if ($options{-beat} && $options{-fade}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'-beat'} and $options{'-fade'}) { } 1 34 35
Condition100%3/3
$options{"-beat"} and $options{"-fade"}
!ll&&!rl&&r
3311
MC/DC100%2/2
$options{"-beat"} and $options{"-fade"}
$options{"-beat"} $options{"-fade"}
Truth table100%3/3
A and B
A= $options{"-beat"}B= $options{"-fade"}
ABresult
0X0
100
111
109 1 $class .= " fa-beat-fade";
110 }
111 else {
112 34 ▶ $class .= " fa-beat" if $options{-beat};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-beat"} 1 33 34
113 34 ▶ $class .= " fa-fade" if $options{-fade};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-fade"} 1 33 34
114 }
115  
116 35 $class;
117 }
118  
119 # Utils
120  
121 1 ▶sub _try {
Subroutine _try: called CC=3 SCAR=11.4
122 1 my ($value, $method_name) = @_;
123 1 ▶ return unless blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 1 1
124  
125 1 ▶ if (my $method = $value->can('fa_class')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $value->can("fa_class")) 1 0 1
126 1 return $value->$method();
127 }
128 }
129  
130 9 ▶sub extract_block {
Subroutine extract_block: called CC=2 SCAR=6.9
131 9 my $arrayref = shift;
132 9 ▶ return delete $arrayref->[-1] if ref $arrayref->[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arrayref->[-1] eq "CODE" 3 6 9
133 6 return undef;
134 }
135  
136 35 ▶sub extract_flags {
Subroutine extract_flags: called CC=7 SCAR=19.5
137 35 my $arrayref = shift;
138 35 my %flags = ();
139  
140 ITEM:
141 35 ▶ for (my $i = 0; $i < @$arrayref; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arrayref) 44 15 59
142 44 my $item = $arrayref->[$i];
143 44 ▶ if ($item =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^-/u) 15 29 44
144 15 $flags{$item} = $arrayref->[$i + 1];
145 15 splice @$arrayref, $i => 2;
146 15 ▶ last ITEM unless @$arrayref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arrayref 12 3 15
147 3 $i -= 2;
148 3 next ITEM;
149 }
150 29 ▶ if ($item =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^:/u) 9 20 29
151 9 $item =~ s/^:/-/;
152 9 $flags{"$item"} = 1;
153 9 splice @$arrayref, $i => 1;
154 9 ▶ last ITEM unless @$arrayref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arrayref 8 1 9
155 1 $i--;
156 }
157 }
158  
159 35 ▶ wantarray ? %flags : \%flags;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 35 0 35
160 }
161  
162 1 1;
163  
164 =encoding utf8
165  
166 =head1 NAME
167  
168 Mojolicious::Plugin::FontAwesomeHelpers - Mojolicious helpers for Font Awesome icons
169  
170 =head1 SYNOPSIS
171  
172 # template.html.ep
173 %= icon 'fas fa-chevron' # => <i class="fas fa-chevron" aria-hidden="true"></i>
174 %= icon 'fas fa-chevron', 'Back' # => <i class="fas fa-chevron" aria-hidden="true"></i> Back
175  
176 # ViewModel.pm
177 package ViewModel {
178 # ... other methods
179 sub fa_class { 'fas fa-circle' }
180 }
181  
182 # another_template.html.ep
183 %= icon ViewModel->new # => <i class="fas fa-circle" aria-hidden="true"></i>
184 %= icon ViewModel->new, 'A Circle' # => <i class="fas fa-circle" aria-hidden="true"></i> A Circle
185  
186 =head1 HELPERS
187  
188 =head2 icon
189  
190 %= icon 'fas fa-chevron' # => <i class="fas fa-chevron" aria-hidden="true"></i>
191 %= icon 'fas fa-chevron', 'Back' # => <i class="fas fa-chevron" aria-hidden="true"></i> Back
192  
193 %= icon ViewModel->new # => <i class="fas fa-circle" aria-hidden="true"></i>
194 %= icon ViewModel->new, 'A Circle' # => <i class="fas fa-circle" aria-hidden="true"></i> A Circle
195  
196 %= icon 'fas fa-chevron', (id => 'back', class => 'fa-solid'), 'Back'
197 # => <i id="back", class="fas fa-chevron fa-solid" aria-hidden="true"></i> Back
198  
199 %= icon 'fas fa-chevron' => begin
200 <b>Back</b>
201 %= end
202 # => <i class="fas fa-chevron aria-hidden="true"></i> <b>Back</b>
203  
204 Renders a Font Awesome icon using an C<i> tag. It will also handle accessibility
205 concerns.
206  
207 It can be used with two string arguments specifying the icon style and name
208 respectively. A third argument can be given to specify text that should be rendered
209 adjacent to the icon.
210  
211 If the first argument is an object that implements a C<fa_class> method then the
212 return value of that method will be used to specify the icon and a second argument,
213 if provided, will be rendered as text adjacent to the icon.
214  
215 All other arguments will be rendered as HTML attributes on the C<i> tag.
216  
217 =head2 fa_icon
218  
219 %= fa_icon 'fas fa-circle' # => "<i class="fas fa-circle" aria-hidden="true"></i>
220  
221 An alias of L</"icon">.
222  
223 =head2 fa->icon
224  
225 %= $c->fa->icon('fas fa-circle') # => "<i class="fas fa-circle" aria-hidden="true"></i>
226  
227 An alias of L</"icon">.
228  
229 =head2 stacked_icon
230  
231 %= stacked_icon -size => '2x' => begin
232 %= icon 'fa-solid fa-square', -stack => '2x'
233 %= icon 'fab fa-twitter', -stack => '1x', :inverse
234 %= end
235  
236 =head2 fa_stack
237  
238 An alias of L</"stacked_icon">
239  
240 =head2 fa->stack
241  
242 An alias of L</"stacked_icon">
243  
244 =head2 fa->class
245  
246 %= $c->fa->class('fas fa-circle') # => "fas fa-circle"
247 %= $c->fa->class('fas fa-circle', -size => 'sm') # => "fas fa-circle fa-sm"
248  
249 %= $c->fa->class(ViewModel->new) # => "fas fa-circle"
250 %= $c->fa->class(ViewModel->new, -size => 'sm') # => "fas fa-circle fa-sm"
251  
252 =head3 Options
253  
254 =head4 -size
255  
256 =head4 -rotate
257  
258 =head4 -flip
259  
260 =head4 -stack
261  
262 =head4 -inverse
263  
264 =head4 -beat
265  
266 =head4 -fade
267  
268 =head4 -beat & -fade
269  
270 =head4 -bounce
271  
272 =head4 -shake
273  
274 =head4 -spin
275  
276 =head4 -pull
277  
278 =head4 -width
279  
280 =head4 -opacity
281  
282 =cut
↑ Summary