blib/lib/HTML/DeferableCSS.pm

filter: Statementstmt 93.3% 154 / 165 Branchbran 85.7% 48 / 56 Conditioncond 72.2% 13 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 94.4% 34 / 36 Podpod 100% 6 / 6 totaltotal 88.7% 260 / 293 CC 38 SCAR 14.3
CC
38
Cov
90%
deferred_link_html
9.0
href
8.5
_build_css_files
7.0
CRAP 4.2
↑ Summary
1 package HTML::DeferableCSS;
2  
3 # ABSTRACT: Simplify management of stylesheets in your HTML
4  
5 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo 1.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Devel::StrictMode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::ShareDir 1.112 qw/ dist_file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MooX::TypeTiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use List::Util 1.45 qw/ first uniqstr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Types::Path::Tiny qw/ Dir File Path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Types::Common::Numeric qw/ PositiveOrZeroInt /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Types::Common::String qw/ NonEmptySimpleStr SimpleStr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Types::Standard qw/ Bool CodeRef HashRef Maybe Tuple /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # RECOMMEND PREREQ: Type::Tiny::XS
20  
21 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our $VERSION = 'v0.4.2';
24  
25  
26  
27 has aliases => (
28 is => 'ro',
29 isa => STRICT ? HashRef [Maybe[SimpleStr]] : HashRef,
30 required => 1,
31 27 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
32 27 ▶ return { map { $_ => $_ } @{$_[0]} } if ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 1 26 27
33 26 return $_[0];
34 },
35 4 );
36  
37  
38 4 has css_root => (
39 is => 'ro',
40 isa => Dir,
41 coerce => 1,
42 required => 1,
43 );
44  
45  
46 4 has url_base_path => (
47 is => 'ro',
48 isa => SimpleStr,
49 default => '/',
50 );
51  
52  
53 4 has prefer_min => (
54 is => 'ro',
55 isa => Bool,
56 default => 1,
57 );
58  
59  
60 4 has css_files => (
61 is => 'lazy',
62 isa => STRICT
63 ? HashRef [ Tuple [ Maybe[Path], Maybe[NonEmptySimpleStr], PositiveOrZeroInt ] ]
64 : HashRef,
65 builder => 1,
66 coerce => 1,
67 );
68  
69 4 ▶use constant PATH => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 ▶use constant NAME => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 ▶use constant SIZE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 28 ▶sub _build_css_files {
Subroutine _build_css_files: called CC=7 SCAR=19.5
74 28 my ($self) = @_;
75  
76 28 my $root = $self->css_root;
77 28 my $min = $self->prefer_min;
78  
79 28 my %files;
80 28 for my $name (keys %{ $self->aliases }) {
81 38 my $base = $self->aliases->{$name};
82 38 ▶ if (!$base) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $base) { } 6 32 38
elsif ($base =~ m[^(\w+:)?//]) { } 4 28 32
83 6 $files{$name} = [ undef, undef, 0 ];
84 }
85 elsif ($base =~ m{^(\w+:)?//}) {
86 4 $files{$name} = [ undef, $base, 0 ];
87 }
88 else {
89 28 ▶ $base = $name if $base eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $base eq 1 5 23 28
90 28 $base =~ s/(?:\.min)?\.css$//;
91 28 ▶ my @bases = $min
Branch: 1 branch
BranchTrueFalseTotal
$min ? : 25 3 28
92 ? ( "${base}.min.css", "${base}.css", $base )
93 : ( "${base}.css", "${base}.min.css", $base );
94  
95 28 ▶ my $file = first { $_->exists } map { path( $root, $_ ) } @bases;
Subroutine __ANON__: called CC=1 SCAR=0.0
96 28 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 4 24 28
97 4 $self->log->( error => "alias '$name' refers to a non-existent file" );
98 0 next;
99 }
100 # PATH NAME SIZE
101 24 $files{$name} = [ $file, $file->relative($root)->stringify, $file->stat->size ];
102 }
103 }
104  
105 24 return \%files;
106 }
107  
108  
109 4 has cdn_links => (
110 is => 'ro',
111 isa => STRICT ? HashRef [NonEmptySimpleStr] : HashRef,
112 predicate => 1,
113 );
114  
115  
116 4 has use_cdn_links => (
117 is => 'lazy',
118 isa => Bool,
119 builder => 'has_cdn_links',
120 );
121  
122  
123 4 has inline_max => (
124 is => 'ro',
125 isa => PositiveOrZeroInt,
126 default => 1024,
127 );
128  
129  
130 4 has defer_css => (
131 is => 'ro',
132 isa => Bool,
133 default => 1,
134 );
135  
136  
137 4 has include_noscript => (
138 is => 'lazy',
139 isa => Bool,
140 builder => 'defer_css',
141 );
142  
143  
144 has preload_script => (
145 is => 'lazy',
146 isa => File,
147 coerce => 1,
148 0 ▶ builder => sub { dist_file( qw/ HTML-DeferableCSS cssrelpreload.min.js / ) },
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_preload_script: called CC=1 SCAR=1.2
149 4 );
150  
151  
152 has link_template => (
153 is => 'ro',
154 isa => CodeRef,
155 21 ▶ builder => sub {
Subroutine _build_link_template: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
156 18 ▶ return sub { sprintf('<link rel="stylesheet" href="%s">', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 21 },
158 4 );
159  
160  
161  
162 has preload_template => (
163 is => 'lazy',
164 isa => CodeRef,
165 0 ▶ builder => sub {
Subroutine _build_preload_template: not called CC=2 SCAR=9.7
166 0 my ($self) = @_;
167 0 ▶ if ($self->simple) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->simple) { } 0 0 0
if ($self->simple) { } 1 1 2
168 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 1 sprintf('<link rel="preload" as="style" href="%s">' .
170 '<link rel="stylesheet" href="%s" media="print" onload="this.media=\'all\';this.onload=null;">',
171 $_[0], $_[0])
172 }
173 0 }
174 else {
175 0 ▶ return sub { sprintf('<link rel="preload" as="style" href="%s" onload="this.onload=null;this.rel=\'stylesheet\'">', $_[0]) };
Subroutine __ANON__: called CC=1 SCAR=1.2
176 }
177 },
178 4 );
179  
180  
181 4 has asset_id => (
182 is => 'ro',
183 isa => NonEmptySimpleStr,
184 predicate => 1,
185 );
186  
187  
188 has log => (
189 is => 'ro',
190 isa => CodeRef,
191 26 ▶ builder => sub {
Subroutine _build_log: called CC=1 SCAR=0.0
192 8 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
193 8 my ($level, $message) = @_;
194 8 ▶ croak $message if ($level eq 'error');
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "error" 6 2 8
195 2 carp $message;
196 26 };
197 },
198 4 );
199  
200  
201 4 has simple => (
202 is => 'ro',
203 isa => Bool,
204 default => 0,
205 );
206  
207  
208 5 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod documented
209 5 my ($self) = @_;
210  
211 5 my $files = $self->css_files;
212  
213 3 ▶ scalar(keys %$files) or
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$files or return $self->log->("error", "no aliases") 2 1 3
214 return $self->log->( error => "no aliases" );
215  
216 2 return 1;
217 }
218  
219  
220 28 ▶sub href {
Subroutine href: called CC=8 SCAR=21.3
Pod documented
221 28 my ($self, $name, $file) = @_;
222 28 ▶ $file //= $self->_get_file($name) or return;
Branch: 1 branch
BranchTrueFalseTotal
$file //= $self->_get_file($name) or return 28 0 28
Condition66%2/3
$file //= $self->_get_file($name)
l!l&&r!l&&!r
11170
MC/DC0%0/2
$file //= $self->_get_file($name)
$file $self->_get_file($name)
Truth table66%2/3
A //= B
A= $fileB= $self->_get_file($name)
ABresult
1X1
011
000
223 28 ▶ if (defined $file->[PATH]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file->[0]) { } 20 8 28
224 20 my $href = $self->url_base_path . $file->[NAME];
225 20 ▶ $href .= '?' . $self->asset_id if $self->has_asset_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_asset_id 2 18 20
226 20 ▶ if ($self->use_cdn_links && $self->has_cdn_links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_cdn_links and $self->has_cdn_links) 1 19 20
Condition66%2/3
$self->use_cdn_links and $self->has_cdn_links
!ll&&!rl&&r
1901
MC/DC50%1/2
$self->use_cdn_links and $self->has_cdn_links
$self->use_cdn_links $self->has_cdn_links
Truth table66%2/3
A and B
A= $self->use_cdn_linksB= $self->has_cdn_links
ABresult
0X0
100
111
227 1 ▶ return $self->cdn_links->{$name} // $href;
Condition33%1/3
$self->cdn_links->{$name} // $href
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->cdn_links->{$name} // $href
$self->cdn_links->{$name} $href
Truth table33%1/3
A // B
A= $self->cdn_links->{$name}B= $href
ABresult
1X1
011
000
228 }
229 19 return $href;
230 }
231 else {
232 8 return $file->[NAME];
233 }
234 }
235  
236  
237 21 ▶sub link_html {
Subroutine link_html: called CC=2 SCAR=6.9
Pod documented
238 21 my ( $self, $name, $file ) = @_;
239 21 ▶ if (my $href = $self->href( $name, $file )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $self->href($name, $file)) { } 17 4 21
240 17 return $self->link_template->($href);
241 }
242 else {
243 4 return "";
244 }
245 }
246  
247  
248 9 ▶sub inline_html {
Subroutine inline_html: called CC=5 SCAR=16.3
Pod documented
249 9 my ( $self, $name, $file ) = @_;
250 9 ▶ $file //= $self->_get_file($name) or return;
Branch: 1 branch
BranchTrueFalseTotal
$file //= $self->_get_file($name) or return 9 0 9
Condition66%2/3
$file //= $self->_get_file($name)
l!l&&r!l&&!r
450
MC/DC0%0/2
$file //= $self->_get_file($name)
$file $self->_get_file($name)
Truth table66%2/3
A //= B
A= $fileB= $self->_get_file($name)
ABresult
1X1
011
000
251 9 ▶ if (my $path = $file->[PATH]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $file->[0]) { } 8 1 9
252 8 ▶ if ($file->[SIZE]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->[2]) 6 2 8
253 6 return "<style>" . $file->[PATH]->slurp_raw . "</style>";
254 }
255 2 $self->log->( warning => "empty file '$path'" );
256 2 return "";
257 }
258 else {
259 1 $self->log->( error => "'$name' refers to a URI" );
260 0 return;
261 }
262 }
263  
264  
265 5 ▶sub link_or_inline_html {
Subroutine link_or_inline_html: called CC=5 SCAR=16.1
Pod documented
266 5 my ($self, @names ) = @_;
267 5 my $buffer = "";
268 5 foreach my $name (uniqstr @names) {
269 8 ▶ my $file = $self->_get_file($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_get_file($name) or next 8 0 8
270 8 ▶ if ( $file->[PATH] && ($file->[SIZE] <= $self->inline_max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->[0] and $file->[2] <= $self->inline_max) { } 3 5 8
Condition100%3/3
$file->[0] and $file->[2] <= $self->inline_max
!ll&&!rl&&r
323
MC/DC100%2/2
$file->[0] and $file->[2] <= $self->inline_max
$file->[0] $file->[2] <= $self->inline_max
Truth table100%3/3
A and B
A= $file->[0]B= $file->[2] <= $self->inline_max
ABresult
0X0
100
111
271 3 $buffer .= $self->inline_html($name, $file);
272 }
273 else {
274 5 $buffer .= $self->link_html($name, $file);
275 }
276 }
277 5 return $buffer;
278 }
279  
280  
281 6 ▶sub deferred_link_html {
Subroutine deferred_link_html: called CC=9 SCAR=22.0
Pod documented
282 6 my ($self, @names) = @_;
283 6 my $buffer = "";
284 6 my @deferred;
285 6 for my $name (uniqstr @names) {
286 7 ▶ my $file = $self->_get_file($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_get_file($name) or next 7 0 7
287 7 ▶ if ($file->[PATH] && $file->[SIZE] <= $self->inline_max) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->[0] and $file->[2] <= $self->inline_max) { } 1 6 7
elsif ($self->defer_css) { } 2 4 6
Condition100%3/3
$file->[0] and $file->[2] <= $self->inline_max
!ll&&!rl&&r
241
MC/DC100%2/2
$file->[0] and $file->[2] <= $self->inline_max
$file->[0] $file->[2] <= $self->inline_max
Truth table100%3/3
A and B
A= $file->[0]B= $file->[2] <= $self->inline_max
ABresult
0X0
100
111
288 1 $buffer .= $self->inline_html($name, $file);
289 }
290 elsif ($self->defer_css) {
291 2 my $href = $self->href($name, $file);
292 2 push @deferred, $href;
293 2 $buffer .= $self->preload_template->($href);
294 }
295 else {
296 4 $buffer .= $self->link_html($name, $file);
297 }
298 }
299  
300 6 ▶ if (@deferred) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deferred) 2 4 6
301  
302 $buffer .= "<noscript>" .
303 2 ▶ join("", map { $self->link_template->($_) } @deferred ) .
Branch: 1 branch
BranchTrueFalseTotal
if $self->include_noscript 1 1 2
304 "</noscript>" if $self->include_noscript;
305  
306 2 ▶ $buffer .= "<script>" .
Branch: 1 branch
BranchTrueFalseTotal
unless $self->simple 1 1 2
307 $self->preload_script->slurp_raw .
308 "</script>" unless $self->simple;
309  
310 }
311  
312 6 return $buffer;
313 }
314  
315 37 ▶sub _get_file {
Subroutine _get_file: called CC=3 SCAR=14.2
316 37 my ($self, $name) = @_;
317 37 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 37 37
318 0 $self->log->( error => "missing name" );
319 0 return;
320 }
321 37 ▶ if (my $file = $self->css_files->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->css_files->{$name}) { } 37 0 37
322 37 return $file;
323 }
324 else {
325 0 $self->log->( error => "invalid name '$name'" );
326 0 return;
327 }
328  
329 }
330  
331  
332  
333 4 1;
334  
335 __END__
↑ Summary