blib/lib/HTML/Packer.pm

filter: Statementstmt 97.0% 229 / 236 Branchbran 87.5% 105 / 120 Conditioncond 71.7% 28 / 39 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 100% 34 / 34 Podpod 50.0% 4 / 8 totaltotal 89.6% 414 / 462 CC 74 SCAR 20.9
CC
74
Cov
92%
minify
25.9
__ANON__
18.2
__ANON__
6.0
CRAP 8.1
↑ Summary
1 package HTML::Packer;
2  
3 11 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Regexp::RegGrp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Digest::SHA qw(sha256_base64 sha384_base64 sha512_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # -----------------------------------------------------------------------------
12  
13 7 our $VERSION = '2.11';
14  
15 7 our @BOOLEAN_ACCESSORS = (
16 'remove_comments',
17 'remove_comments_aggressive',
18 'remove_newlines',
19 'no_compress_comment',
20 'html5',
21 );
22  
23 7 our @JAVASCRIPT_OPTS = ( 'clean', 'obfuscate', 'shrink', 'best' );
24 7 our @CSS_OPTS = ( 'minify', 'pretty' );
25 7 our @CSP_OPTS = ( 'sha256', 'sha384', 'sha512' );
26  
27 7 our $REQUIRED_JAVASCRIPT_PACKER = '1.002001';
28 7 our $REQUIRED_CSS_PACKER = '1.000001';
29  
30 7 our @SAVE_SPACE_ELEMENTS = (
31 'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'button', 'cite',
32 'del', 'dfn', 'em', 'font', 'i', 'input', 'ins', 'kbd', 'label', 'q',
33 's', 'samp', 'select', 'small', 'strike', 'strong', 'sub', 'sup', 'u', 'var'
34 );
35  
36 7 our @VOID_ELEMENTS = (
37 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
38 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
39 );
40  
41 # Some regular expressions are from HTML::Clean
42  
43 7 our $COMMENT = '((?>\s*))(<!--(?:.*?)?-->)((?>\s*))';
44 7 our $COMMENT_SAFE = '((?>\s*))(<!--(?:(?![#\[]| google_ad_section_).*?)?-->)((?>\s*))';
45  
46 7 our $PACKER_COMMENT = '<!--\s*HTML::Packer\s*(\w+)\s*-->';
47  
48 7 our $DOCTYPE = '<\!DOCTYPE[^>]*>';
49  
50 7 our $DONT_CLEAN = '(<\s*(pre|code|textarea|script|style)[^>]*>)(.*?)(<\s*\/\2[^>]*>)';
51  
52 our $WHITESPACES = [
53 {
54 regexp => qr/^\s*/s,
55 replacement => ''
56 },
57 {
58 regexp => qr/\s*$/s,
59 replacement => ''
60 },
61 {
62 regexp => '^\s*',
63 replacement => '',
64 modifier => 'm'
65 },
66 {
67 regexp => '[^\S\n]*$',
68 replacement => '',
69 modifier => 'm'
70 },
71 {
72 regexp => qr/(?<=>)[^<>]*(?=<)/sm,
73 300 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 300 my $match = $_[0]->{match};
75  
76 300 $match =~ s/[^\S\n]{2,}/ /sg;
77 300 $match =~ s/\s*\n+\s*/\n/sg;
78  
79 300 return $match;
80 }
81 },
82 {
83 regexp => '<\s*(\/)?\s*',
84 466 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 466 return sprintf( '<%s', $_[0]->{submatches}->[0] );
86 },
87 modifier => 's'
88 },
89 {
90 regexp => '\s*(\/)?\s*>',
91 463 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 466 return sprintf( '%s>', $_[0]->{submatches}->[0] );
93 },
94 7 modifier => 's'
95 }
96 ];
97  
98 our $NEWLINES_TAGS = [
99 {
100 regexp => '(\s*)(<\s*\/?\s*(?:' . join( '|', @SAVE_SPACE_ELEMENTS ) . ')\b[^>]*>)(\s*)',
101 113 ▶ replacement => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
102 116 ▶ return sprintf( '%s%s%s', $_[0]->{submatches}->[0] ? ' ' : '', $_[0]->{submatches}->[1], $_[0]->{submatches}->[2] ? ' ' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{'submatches'}[0] ? : 82 30 112
$_[0]{'submatches'}[2] ? : 56 56 112
103 },
104 7 modifier => 'is'
105 }
106 ];
107  
108 our $NEWLINES = [
109 {
110 regexp => '(.)\n(.)',
111 94 ▶ replacement => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
112 94 my ( $pre, $post ) = @{$_[0]->{submatches}};
113  
114 94 my $ret;
115  
116 94 ▶ if ( $pre eq '>' and $post eq '<' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pre eq '>' and $post eq '<') { } 63 30 93
elsif ($pre eq '-' and $post =~ /[\w]/) { } 1 29 30
Condition66%2/3
$pre eq ">" and $post eq "<"
!ll&&!rl&&r
30063
MC/DC50%1/2
$pre eq ">" and $post eq "<"
$pre eq ">" $post eq "<"
Truth table66%2/3
A and B
A= $pre eq ">"B= $post eq "<"
ABresult
0X0
100
111
117 64 $ret = $pre . $post;
118 }
119 ▶ elsif ( $pre eq '-' and $post =~ /[\w]/ ) {
Condition66%2/3
$pre eq "-" and $post =~ /[\w]/
!ll&&!rl&&r
2901
MC/DC50%1/2
$pre eq "-" and $post =~ /[\w]/
$pre eq "-" $post =~ /[\w]/
Truth table66%2/3
A and B
A= $pre eq "-"B= $post =~ /[\w]/
ABresult
0X0
100
111
120 2 $ret = $pre . $post;
121 }
122 else {
123 30 $ret = $pre . ' ' . $post;
124 }
125  
126 94 return $ret;
127 }
128 }
129 7 ];
130  
131 7 our @REGGRPS = ( 'newlines', 'newlines_tags', 'whitespaces', 'void_elements' );
132  
133 7 our $GLOBAL_REGGRP = 'global';
134  
135 ##########################################################################################
136  
137 {
138 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 7 foreach my $field ( @BOOLEAN_ACCESSORS ) {
141 35 ▶ next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'HTML::Packer::' . $field;}{"CODE"} 0 35 35
142  
143 35 ▶ *{ __PACKAGE__ . '::' . $field} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
144 476 my ( $self, $value ) = @_;
145  
146 476 ▶ $self->{'_' . $field} = $value ? 1 : undef if ( defined( $value ) );
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 88 387 475
$value ? : 83 5 88
147  
148 476 return $self->{'_' . $field};
149 35 };
150 }
151  
152 7 foreach my $reggrp ( @REGGRPS, $GLOBAL_REGGRP ) {
153 35 ▶ next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'HTML::Packer::reggrp_' . $reggrp;}{"CODE"} 0 35 35
154  
155 35 ▶ *{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
156 275 my ( $self ) = shift;
157  
158 275 return $self->{ '_reggrp_' . $reggrp };
159 35 };
160 }
161 }
162  
163 95 ▶sub do_javascript {
Subroutine do_javascript: called CC=4 SCAR=13.9
Pod documented
164 95 my ( $self, $value ) = @_;
165  
166 95 ▶ if ( defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 35 59 94
167 36 ▶ if ( grep( $value eq $_, @JAVASCRIPT_OPTS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($value eq $_), @HTML::Packer::JAVASCRIPT_OPTS)) { } 32 3 35
elsif (not $value) { } 1 2 3
168 33 $self->{_do_javascript} = $value;
169 }
170 elsif ( ! $value ) {
171 2 $self->{_do_javascript} = undef;
172 }
173 }
174  
175 95 return $self->{_do_javascript};
176 }
177  
178 72 ▶sub do_stylesheet {
Subroutine do_stylesheet: called CC=4 SCAR=13.9
Pod documented
179 72 my ( $self, $value ) = @_;
180  
181 72 ▶ if ( defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 13 58 71
182 14 ▶ if ( grep( $value eq $_, @CSS_OPTS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($value eq $_), @HTML::Packer::CSS_OPTS)) { } 10 3 13
elsif (not $value) { } 1 2 3
183 11 $self->{_do_stylesheet} = $value;
184 }
185 elsif ( ! $value ) {
186 2 $self->{_do_stylesheet} = undef;
187 }
188 }
189  
190 72 return $self->{_do_stylesheet};
191 }
192  
193 58 ▶sub do_csp {
Subroutine do_csp: called CC=4 SCAR=13.9
Pod documented
194 58 my ( $self, $value ) = @_;
195  
196 58 ▶ if ( defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 7 50 57
197 8 ▶ if ( grep( $value eq $_, @CSP_OPTS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($value eq $_), @HTML::Packer::CSP_OPTS)) { } 5 2 7
elsif (not $value) { } 1 1 2
198 6 $self->{_do_csp} = $value;
199 }
200 elsif ( ! $value ) {
201 2 $self->{_do_csp} = undef;
202 }
203 }
204  
205 58 return $self->{_do_csp};
206 }
207  
208 # these variables are used in the closures defined in the init function
209 # below - we have to use globals as using $self within the closures leads
210 # to a reference cycle and thus memory leak, and we can't scope them to
211 # the init method as they may change. they are set by the minify sub
212 7 our $remove_comments;
213 7 our $remove_comments_aggressive;
214 7 our $remove_newlines;
215 7 our $html5;
216 7 our $do_javascript;
217 7 our $do_stylesheet;
218 7 our $do_csp;
219 7 our $js_packer;
220 7 our $css_packer;
221 7 our %csp;
222 7 our $reggrp_ws;
223  
224 40 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
225 40 my $class = shift;
226 40 my $self = {};
227  
228 40 bless( $self, $class );
229  
230 40 $self->{whitespaces}->{reggrp_data} = $WHITESPACES;
231 40 $self->{newlines}->{reggrp_data} = $NEWLINES;
232 40 $self->{newlines_tags}->{reggrp_data} = $NEWLINES_TAGS;
233 $self->{global}->{reggrp_data} = [
234 {
235 regexp => $DOCTYPE,
236 2 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
237 1 return '<!--~' . $_[0]->{store_index} . '~-->';
238 },
239 2 ▶ store => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 1 my $doctype = $_[0]->{match};
241  
242 1 $doctype =~ s/\s+/ /gsm;
243  
244 1 return $doctype;
245 }
246 },
247 {
248 # this is using a variable that won't be initialized until after we have
249 # called ->minify so we endup calling ->init again (see FIXME)
250 regexp => $remove_comments_aggressive ? $COMMENT : $COMMENT_SAFE,
251 37 ▶ replacement => sub {
Subroutine __ANON__: called CC=5 SCAR=17.4
252 return $remove_comments ? (
253 $remove_newlines ? ' ' : (
254 ( $_[0]->{submatches}->[0] =~ /\n/s or $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : ''
255 )
256 36 ▶ ) : '<!--~' . $_[0]->{store_index} . '~-->';
Branch: 3 branches
BranchTrueFalseTotal
$HTML::Packer::remove_comments ? : 33 3 36
$HTML::Packer::remove_newlines ? : 28 5 33
$_[0]{'submatches'}[0] =~ /\n/s || $_[0]{'submatches'}[2] =~ /\n/s ? : 5 0 5
Condition33%1/3
$_[0]{"submatches"}[0] =~ /\n/s or $_[0]{"submatches"}[2] =~ /\n/s
l!l&&r!l&&!r
500
MC/DC0%0/2
$_[0]{"submatches"}[0] =~ /\n/s or $_[0]{"submatches"}[2] =~ /\n/s
$_[0]{"submatches"}[0] =~ /\n/s $_[0]{"submatches"}[2] =~ /\n/s
Truth table33%1/3
A or B
A= $_[0]{"submatches"}[0] =~ /\n/sB= $_[0]{"submatches"}[2] =~ /\n/s
ABresult
1X1
011
000
257 },
258 37 ▶ store => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
259 my $ret = $remove_comments ? '' : (
260 ( ( not $remove_newlines and $_[0]->{submatches}->[0] =~ /\n/s ) ? "\n" : '' ) .
261 $_[0]->{submatches}->[1] .
262 36 ▶ ( ( not $remove_newlines and $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : '' )
Branch: 3 branches
BranchTrueFalseTotal
$HTML::Packer::remove_comments ? : 33 3 36
!$HTML::Packer::remove_newlines && $_[0]{'submatches'}[0] =~ /\n/s ? : 2 1 3
!$HTML::Packer::remove_newlines && $_[0]{'submatches'}[2] =~ /\n/s ? : 1 2 3
Condition83%5/6
not $HTML::Packer::remove_newlines and $_[0]{"submatches"}[0] =~ /\n/s
!ll&&!rl&&r
102
not $HTML::Packer::remove_newlines and $_[0]{"submatches"}[2] =~ /\n/s
!ll&&!rl&&r
111
MC/DC50%1/2
not $HTML::Packer::remove_newlines and $_[0]{"submatches"}[0] =~ /\n/s
not $HTML::Packer::remove_newlines $_[0]{"submatches"}[0] =~ /\n/s
MC/DC100%2/2
not $HTML::Packer::remove_newlines and $_[0]{"submatches"}[2] =~ /\n/s
not $HTML::Packer::remove_newlines $_[0]{"submatches"}[2] =~ /\n/s
Truth table66%2/3
not(A) and B
A= not $HTML::Packer::remove_newlinesB= $_[0]{"submatches"}[0] =~ /\n/s
ABresult
0X0
100
111
Truth table100%3/3
not(A) and B
A= not $HTML::Packer::remove_newlinesB= $_[0]{"submatches"}[2] =~ /\n/s
ABresult
0X0
100
111
263 );
264  
265 36 return $ret;
266 }
267 },
268 {
269 regexp => $DONT_CLEAN,
270 61 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 60 return '<!--~' . $_[0]->{store_index} . '~-->';
272 },
273 60 ▶ store => sub {
Subroutine __ANON__: called CC=18 SCAR=29.0
274 60 my ( $opening, undef, $content, $closing ) = @{$_[0]->{submatches}};
275  
276 60 ▶ if ( $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) { } 60 0 60
277 60 ▶ my $opening_script_re = '<\s*script' . ( $html5 ? '[^>]*>' : '[^>]*(?:java|ecma)script[^>]*>' );
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Packer::html5 ? : 9 51 60
278 60 ▶ my $opening_style_re = '<\s*style' . ( $html5 ? '[^>]*>' : '[^>]*text\/css[^>]*>' );
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Packer::html5 ? : 9 51 60
279 60 my $js_type_re = q{type=['"]((((application|text)/){0,1}(x-){0,1}(java|ecma)script)|module)['"]};
280  
281 60 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($opening =~ /$opening_script_re/i and $opening =~ /$js_type_re/i || !($opening =~ /type/i)) { } 36 24 60
elsif ($opening =~ /$opening_style_re/i) { } 16 8 24
Condition66%4/6
$opening =~ /$opening_script_re/i and $opening =~ /$js_type_re/i || !($opening =~ /type/i)
!ll&&!rl&&r
24036
$opening =~ /$js_type_re/i or not $opening =~ /type/i
l!l&&r!l&&!r
3510
MC/DC33%1/3
$opening =~ /$opening_script_re/i and $opening =~ /$js_type_re/i || !($opening =~ /type/i)
$opening =~ /$opening_script_re/i $opening =~ /$js_type_re/i not $opening =~ /type/i
Truth table75%3/4
A and B or not(C)
A= $opening =~ /$opening_script_re/iB= $opening =~ /$js_type_re/iC= not $opening =~ /type/i
ABCresult
0XX0
1000
11X1
1011
282 $opening =~ /$opening_script_re/i
283 && ( $opening =~ /$js_type_re/i || $opening !~ /type/i )
284 ) {
285 36 ▶ $opening =~ s/ type="(text\/)?(java|ecma)script"//i if ( $html5 );
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Packer::html5 3 33 36
286  
287 36 ▶ if ( $js_packer and $do_javascript ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Packer::js_packer and $HTML::Packer::do_javascript) 27 9 36
Condition66%2/3
$HTML::Packer::js_packer and $HTML::Packer::do_javascript
!ll&&!rl&&r
0927
MC/DC50%1/2
$HTML::Packer::js_packer and $HTML::Packer::do_javascript
$HTML::Packer::js_packer $HTML::Packer::do_javascript
Truth table66%2/3
A and B
A= $HTML::Packer::js_packerB= $HTML::Packer::do_javascript
ABresult
0X0
100
111
288 27 $js_packer->minify( \$content, { compress => $do_javascript } );
289  
290 27 ▶ unless ( $html5 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HTML::Packer::html5) 25 2 27
291 25 $content = '/*<![CDATA[*/' . $content . '/*]]>*/';
292 }
293 }
294  
295 36 ▶ if ( $do_csp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Packer::do_csp) 9 27 36
296 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
297 9 push @{ $csp{'script-src'} }, &{ "${do_csp}_base64" } ( $content );
298 }
299 }
300 elsif ( $opening =~ /$opening_style_re/i ) {
301 16 ▶ $opening =~ s/ type="text\/css"//i if ( $html5 );
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Packer::html5 6 10 16
302  
303 16 ▶ if ( $css_packer and $do_stylesheet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Packer::css_packer and $HTML::Packer::do_stylesheet) 7 9 16
Condition66%2/3
$HTML::Packer::css_packer and $HTML::Packer::do_stylesheet
!ll&&!rl&&r
097
MC/DC50%1/2
$HTML::Packer::css_packer and $HTML::Packer::do_stylesheet
$HTML::Packer::css_packer $HTML::Packer::do_stylesheet
Truth table66%2/3
A and B
A= $HTML::Packer::css_packerB= $HTML::Packer::do_stylesheet
ABresult
0X0
100
111
304 7 $css_packer->minify( \$content, { compress => $do_stylesheet } );
305 7 ▶ $content = "\n" . $content if ( $do_stylesheet eq 'pretty' );
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Packer::do_stylesheet eq "pretty" 2 5 7
306 }
307  
308 16 ▶ if ( $do_csp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Packer::do_csp) 9 7 16
309 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
310 9 push @{ $csp{'style-src'} }, &{ "${do_csp}_base64" } ( $content );
311 }
312 }
313 }
314 else {
315 0 $content = '';
316 }
317  
318 60 $reggrp_ws->exec( \$opening );
319 60 $reggrp_ws->exec( \$closing );
320  
321 60 return $opening . $content . $closing;
322 },
323 40 ▶ modifier => 'ism'
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Packer::remove_comments_aggressive ? : 2 37 39
324 }
325 ];
326  
327 $self->{void_elements}->{reggrp_data} = [
328 {
329 regexp => '<\s*((?:' . join( '|', @VOID_ELEMENTS ) . ')\b[^>]*)\s*\/>',
330 2 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
331 2 return '<' . $_[0]->{submatches}->[0] . '>';
332 },
333 40 modifier => 'ism'
334 }
335 ];
336  
337 40 foreach ( @HTML::Packer::REGGRPS ) {
338 157 $self->{ '_reggrp_' . $_ } = Regexp::RegGrp->new( { reggrp => $self->{$_}->{reggrp_data} } );
339 }
340  
341 $self->{ '_reggrp_' . $GLOBAL_REGGRP } = Regexp::RegGrp->new(
342 {
343 reggrp => $self->{$GLOBAL_REGGRP}->{reggrp_data},
344 40 restore_pattern => qr/<!--~(\d+)~-->/
345 }
346 );
347  
348 39 return $self;
349 }
350  
351 51 ▶sub minify {
Subroutine minify: called CC=23 SCAR=32.6
Pod undocumented
352 51 my ( $self, $input, $opts );
353  
354 51 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->$HTML::Packer::_isa(__PACKAGE__)) { } 51 0 51
Condition33%1/3
ref $_[0] and $_[0]->$HTML::Packer::_isa(__PACKAGE__)
!ll&&!rl&&r
0051
MC/DC0%0/2
ref $_[0] and $_[0]->$HTML::Packer::_isa(__PACKAGE__)
ref $_[0] $_[0]->$HTML::Packer::_isa(__PACKAGE__)
Truth table33%1/3
A and B
A= ref $_[0]B= $_[0]->$HTML::Packer::_isa(__PACKAGE__)
ABresult
0X0
100
111
355 ref( $_[0] ) and
356 $_[0]->$_isa( __PACKAGE__ )
357 ) {
358 0 $self = __PACKAGE__->init();
359  
360 0 ▶ shift( @_ ) unless ( ref( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
361  
362 0 ( $input, $opts ) = @_;
363 }
364 else {
365 51 ( $self, $input, $opts ) = @_;
366 }
367  
368 51 ▶ if ( ref( $input ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "SCALAR") 0 51 51
369 0 carp( 'First argument must be a scalarref!' );
370 0 return undef;
371 }
372  
373 51 my $html;
374 51 my $cont = 'void';
375  
376 51 ▶ if ( defined( wantarray ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 10 41 51
377 10 ▶ my $tmp_input = ref( $input ) ? ${$input} : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 10 0 10
378  
379 10 $html = \$tmp_input;
380 10 $cont = 'scalar';
381 }
382 else {
383 41 ▶ $html = ref( $input ) ? $input : \$input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 41 0 41
384 }
385  
386 51 ▶ if ( ref( $opts ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq "HASH") 51 0 51
387 51 foreach my $field ( @BOOLEAN_ACCESSORS ) {
388 255 ▶ $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$field} 80 175 255
389 }
390  
391 51 ▶ $self->do_javascript( $opts->{do_javascript} ) if ( defined( $opts->{do_javascript} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"do_javascript"} 29 22 51
392 51 ▶ $self->do_stylesheet( $opts->{do_stylesheet} ) if ( defined( $opts->{do_stylesheet} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"do_stylesheet"} 8 43 51
393 51 ▶ $self->do_csp( $opts->{do_csp} ) if ( defined( $opts->{do_csp} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"do_csp"} 7 44 51
394 }
395  
396 51 ▶ if ( not $self->no_compress_comment() and ${$html} =~ /$PACKER_COMMENT/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->no_compress_comment and ${$html;} =~ /$PACKER_COMMENT/s) 1 50 51
Condition100%3/3
not $self->no_compress_comment and ${$html;} =~ /$PACKER_COMMENT/s
!ll&&!rl&&r
2481
MC/DC100%2/2
not $self->no_compress_comment and ${$html;} =~ /$PACKER_COMMENT/s
not $self->no_compress_comment ${$html;} =~ /$PACKER_COMMENT/s
Truth table100%3/3
not(A) and B
A= not $self->no_compress_commentB= ${$html;} =~ /$PACKER_COMMENT/s
ABresult
0X0
100
111
397 1 my $compress = $1;
398 1 ▶ if ( $compress eq '_no_compress_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress eq "_no_compress_") 1 0 1
399 0 ▶ return ( $cont eq 'scalar' ) ? ${$html} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cont eq 'scalar' ? : 0 1 1
400 }
401 }
402  
403 # (re)initialize variables used in the closures
404 50 ▶ $remove_comments = $self->remove_comments || $self->remove_comments_aggressive;
Condition100%3/3
$self->remove_comments || $self->remove_comments_aggressive
l!l&&r!l&&!r
34115
MC/DC100%2/2
$self->remove_comments || $self->remove_comments_aggressive
$self->remove_comments $self->remove_comments_aggressive
Truth table100%3/3
A || B
A= $self->remove_commentsB= $self->remove_comments_aggressive
ABresult
1X1
011
000
405 50 $remove_comments_aggressive = $self->remove_comments_aggressive;
406 50 $remove_newlines = $self->remove_newlines;
407 50 $html5 = $self->html5;
408 50 $do_javascript = $self->do_javascript;
409 50 $do_stylesheet = $self->do_stylesheet;
410 50 $do_csp = $self->do_csp;
411 50 $js_packer = $self->javascript_packer;
412 50 $css_packer = $self->css_packer;
413 50 $reggrp_ws = $self->reggrp_whitespaces;
414  
415 # blank out the CSP hash before populating it again
416 50 %csp = ();
417  
418 # FIXME: hacky way to get around ->init being called before ->minify
419 50 ▶ $self = ref( $self )->init if $remove_comments_aggressive;
Branch: 1 branch
BranchTrueFalseTotal
if $remove_comments_aggressive 1 49 50
420  
421 50 $self->reggrp_global()->exec( $html );
422 50 $self->reggrp_whitespaces()->exec( $html );
423 50 ▶ if ( $self->remove_newlines() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->remove_newlines) 34 16 50
424 34 $self->reggrp_newlines_tags()->exec( $html );
425 34 $self->reggrp_newlines()->exec( $html );
426 }
427 50 ▶ if ( $self->html5() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->html5) 6 44 50
428 6 $self->reggrp_void_elements()->exec( $html );
429 }
430  
431 50 $self->reggrp_global()->restore_stored( $html );
432  
433 50 ▶ return ${$html} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 10 40 50
434 }
435  
436 51 ▶sub javascript_packer {
Subroutine javascript_packer: called CC=3 SCAR=11.0
Pod undocumented
437 51 my $self = shift;
438  
439 51 ▶ unless ( $self->{_checked_javascript_packer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_checked_javascript_packer"}) 37 14 51
440 37 ▶ eval "use JavaScript::Packer $REQUIRED_JAVASCRIPT_PACKER;";
Subroutine BEGIN: called CC=1 SCAR=0.0
441  
442 37 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 37 0 37
443 37 $self->{_javascript_packer} = eval {
444 37 JavaScript::Packer->init();
445 };
446 }
447  
448 37 $self->{_checked_javascript_packer} = 1;
449 }
450  
451 51 return $self->{_javascript_packer};
452 }
453  
454 51 ▶sub css_packer {
Subroutine css_packer: called CC=3 SCAR=11.0
Pod undocumented
455 51 my $self = shift;
456  
457 51 ▶ unless ( $self->{_checked_css_packer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_checked_css_packer"}) 37 14 51
458 37 ▶ eval "use CSS::Packer $REQUIRED_CSS_PACKER;";
Subroutine BEGIN: called CC=1 SCAR=0.0
459  
460 37 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 37 0 37
461 37 $self->{_css_packer} = eval {
462 37 CSS::Packer->init();
463 };
464 }
465  
466 37 $self->{_checked_css_packer} = 1;
467 }
468  
469 51 return $self->{_css_packer};
470 }
471  
472 7 ▶sub csp {
Subroutine csp: called CC=3 SCAR=11.0
Pod documented
473 7 my $self = shift;
474  
475 7 ▶ return 'script-src' => [ ], 'style-src' => [ ] unless $do_csp and %csp;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_csp and %csp 3 4 7
Condition100%3/3
$do_csp and %csp
!ll&&!rl&&r
214
MC/DC100%2/2
$do_csp and %csp
$do_csp %csp
Truth table100%3/3
A and B
A= $do_cspB= %csp
ABresult
0X0
100
111
476  
477 return
478 4 'script-src' => [ map "'$do_csp-$_='", @{ $csp{'script-src'} } ],
479 4 'style-src' => [ map "'$do_csp-$_='", @{ $csp{'style-src'} } ],
480 ;
481 }
482  
483 7 1;
484  
485 __END__
↑ Summary