blib/lib/HTML/Packer.pm

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