blib/lib/CSS/Packer.pm

filter: Statementstmt 94.3% 182 / 193 Branchbran 75.0% 57 / 76 Conditioncond 59.2% 16 / 27 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 94.7% 18 / 19 Podpod 33.3% 1 / 3 totaltotal 83.5% 280 / 335 CC 46 SCAR 25.8
CC
46
Cov
86%
minify
29.2
__ANON__
6.0
compress
5.2
CRAP 13.2
↑ Summary
1 package CSS::Packer;
2  
3 5 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Regexp::RegGrp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '2.09';
10  
11 5 our @COMPRESS = ( 'minify', 'pretty' );
12 5 our $DEFAULT_COMPRESS = 'pretty';
13  
14 5 our @BOOLEAN_ACCESSORS = (
15 'no_compress_comment',
16 'remove_copyright'
17 );
18  
19 5 our @COPYRIGHT_ACCESSORS = (
20 'copyright',
21 'copyright_comment'
22 );
23  
24 5 our $COPYRIGHT_COMMENT = '\/\*((?>[^*]|\*[^/])*(?:license|copyright)(?>[^*]|\*[^/])*)\*\/';
25  
26 5 our $DICTIONARY = {
27 'STRING1' => qr~"(?>(?:(?>[^"\\]+)|\\.|\\"|\\\s)*)"~,
28 'STRING2' => qr~'(?>(?:(?>[^'\\]+)|\\.|\\'|\\\s)*)'~
29 };
30  
31 5 our $WHITESPACES = '\s+';
32  
33 5 our $RULE = '([^{};]+)\{([^{}]*)\}';
34  
35 5 our $URL = 'url\(\s*(' . $DICTIONARY->{STRING1} . '|' . $DICTIONARY->{STRING2} . '|[^\'"\s]+?)\s*\)';
36  
37 5 our $IMPORT = '\@import\s+(' . $DICTIONARY->{STRING1} . '|' . $DICTIONARY->{STRING2} . '|' . $URL . ')([^;]*);';
38  
39 5 our $AT_RULE = '\@\S+([^{}]+)\{((?:' . $IMPORT . '|' . $RULE . '|' . $WHITESPACES . ')+)\}';
40  
41 5 our $DECLARATION = '((?>[^;:]+)):(?<=:)((?>[^;]*))(?:;(?!(charset|base64))|\s*$)';
42  
43 5 our $COMMENT = '(\/\*[^*]*\*+([^/][^*]*\*+)*\/)';
44  
45 5 our $PACKER_COMMENT = '\/\*\s*CSS::Packer\s*(\w+)\s*\*\/';
46  
47 5 our $CHARSET = '^(\@charset)\s+(' . $DICTIONARY->{STRING1} . '|' . $DICTIONARY->{STRING2} . ');';
48  
49 5 our @REGGRPS = ( 'whitespaces', 'url', 'import', 'declaration', 'rule', 'content_value', 'mediarules', 'global' );
50  
51 # --------------------------------------------------------------------------- #
52  
53 {
54 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 5 foreach my $reggrp ( @REGGRPS ) {
57 40 ▶ next if defined *{ __PACKAGE__ . '::reggrp_' . $reggrp }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'CSS::Packer::reggrp_' . $reggrp;}{"CODE"} 0 40 40
58  
59 40 ▶ *{ __PACKAGE__ . '::reggrp_' . $reggrp } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 148 my ( $self ) = shift;
61  
62 148 return $self->{ '_reggrp_' . $reggrp };
63 40 };
64 }
65  
66 5 foreach my $field ( @BOOLEAN_ACCESSORS ) {
67 10 ▶ next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'CSS::Packer::' . $field;}{"CODE"} 0 10 10
68  
69 10 ▶ *{ __PACKAGE__ . '::' . $field} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
70 77 my ( $self, $value ) = @_;
71  
72 77 ▶ $self->{'_' . $field} = $value ? 1 : undef if ( defined( $value ) );
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 11 66 77
$value ? : 7 4 11
73  
74 77 return $self->{'_' . $field};
75 10 };
76 }
77  
78 5 foreach my $field ( @COPYRIGHT_ACCESSORS ) {
79 10 ▶ $field = '_' . $field if ( $field eq 'copyright_comment' );
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "copyright_comment" 5 5 10
80 10 ▶ next if defined *{ __PACKAGE__ . '::' . $field }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'CSS::Packer::' . $field;}{"CODE"} 0 10 10
81  
82 10 ▶ *{ __PACKAGE__ . '::' . $field} = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
83 88 my ( $self, $value ) = @_;
84  
85 88 ▶ if ( defined( $value ) and not ref( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 34 54 88
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
54034
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
86 34 $value =~ s/^\s*|\s*$//gs;
87 34 $self->{'_' . $field} = $value;
88 }
89  
90 88 my $ret = '';
91  
92 88 ▶ if ( $self->{'_' . $field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{'_' . $field}) 13 75 88
93 13 $ret = '/* ' . $self->{'_' . $field} . ' */' . "\n";
94 }
95  
96 88 return $ret;
97 10 };
98 }
99 }
100  
101 57 ▶sub compress {
Subroutine compress: called CC=5 SCAR=16.4
Pod documented
102 57 my ( $self, $value ) = @_;
103  
104 57 ▶ if ( defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 21 36 57
105 21 ▶ if ( grep( $value eq $_, @COMPRESS ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($value eq $_), @CSS::Packer::COMPRESS)) { } 20 1 21
elsif (not $value) { } 0 1 1
106 20 $self->{_compress} = $value;
107 }
108 elsif ( ! $value ) {
109 0 $self->{_compress} = undef;
110 }
111 }
112  
113 57 ▶ $self->{_compress} ||= $DEFAULT_COMPRESS;
Condition66%2/3
$self->{"_compress"} ||= $CSS::Packer::DEFAULT_COMPRESS
l!l&&r!l&&!r
5340
MC/DC0%0/2
$self->{"_compress"} ||= $CSS::Packer::DEFAULT_COMPRESS
$self->{"_compress"} $CSS::Packer::DEFAULT_COMPRESS
Truth table66%2/3
A ||= B
A= $self->{"_compress"}B= $CSS::Packer::DEFAULT_COMPRESS
ABresult
1X1
011
000
114  
115 57 return $self->{_compress};
116 }
117  
118 # these variables are used in the closures defined in the init function
119 # below - we have to use globals as using $self within the closures leads
120 # to a reference cycle and thus memory leak, and we can't scope them to
121 # the init method as they may change. they are set by the minify sub
122 5 our $reggrp_url;
123 5 our $reggrp_declaration;
124 5 our $reggrp_mediarules;
125 5 our $reggrp_content_value;
126 5 our $global_compress;
127 5 our $indent = '';
128  
129 17 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
130 17 my $class = shift;
131 17 my $self = {};
132  
133 17 bless( $self, $class );
134  
135 $self->{content_value}->{reggrp_data} = [
136 {
137 regexp => $DICTIONARY->{STRING1}
138 },
139 {
140 regexp => $DICTIONARY->{STRING2}
141 },
142 {
143 regexp => qr~([\w-]+)\(\s*([\w-]+)\s*\)~,
144 1 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 1 return $_[0]->{submatches}->[0] . '(' . $_[0]->{submatches}->[1] . ')';
146 }
147 },
148 {
149 17 regexp => $WHITESPACES,
150 replacement => ''
151 }
152 ];
153  
154 $self->{whitespaces}->{reggrp_data} = [
155 {
156 17 regexp => $WHITESPACES,
157 replacement => ''
158 }
159 ];
160  
161 $self->{url}->{reggrp_data} = [
162 {
163 regexp => $URL,
164 5 ▶ replacement => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 5 my $url = $_[0]->{submatches}->[0];
166  
167 5 return 'url(' . $url . ')';
168 }
169 }
170 17 ];
171  
172 $self->{import}->{reggrp_data} = [
173 {
174 regexp => $IMPORT,
175 5 ▶ replacement => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
176 5 my $submatches = $_[0]->{submatches};
177 5 my $url = $submatches->[0];
178 5 my $mediatype = $submatches->[2];
179  
180 5 my $compress = $global_compress;
181 5 $reggrp_url->exec( \$url );
182  
183 5 $mediatype =~ s/^\s*|\s*$//gs;
184 5 $mediatype =~ s/\s*,\s*/,/gsm;
185  
186 5 ▶ return '@import ' . $url . ( $mediatype ? ( ' ' . $mediatype ) : '' ) . ';' . ( $compress eq 'pretty' ? "\n" : '' );
Branch: 2 branches
BranchTrueFalseTotal
$mediatype ? : 0 5 5
$compress eq 'pretty' ? : 2 3 5
187 }
188 }
189 17 ];
190  
191 $self->{declaration}->{reggrp_data} = [
192 {
193 regexp => $DECLARATION,
194 114 ▶ replacement => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
195 114 my $submatches = $_[0]->{submatches};
196 114 my $key = $submatches->[0];
197 114 my $value = $submatches->[1];
198  
199 114 my $compress = $global_compress;
200  
201 114 $key =~ s/^\s*|\s*$//gs;
202 114 $value =~ s/^\s*|\s*$//gs;
203  
204 114 ▶ if ( $key eq 'content' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'content') { } 11 103 114
205 11 $reggrp_content_value->exec( \$value );
206 }
207 else {
208 103 $value =~ s/\s*,\s*/,/gsm;
209 103 $value =~ s/\s+/ /gsm;
210 }
211  
212 114 ▶ return '' if ( not $key or $value eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not $key or $value eq "" 0 114 114
Condition33%1/3
not $key or $value eq ""
l!l&&r!l&&!r
00114
MC/DC0%0/2
not $key or $value eq ""
not $key $value eq ""
Truth table33%1/3
not(A) or B
A= not $keyB= $value eq ""
ABresult
1X1
011
000
213  
214 114 ▶ return $indent . $key . ':' . $value . ';' . ( $compress eq 'pretty' ? "\n" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$compress eq 'pretty' ? : 71 43 114
215 }
216 }
217 17 ];
218  
219 $self->{rule}->{reggrp_data} = [
220 {
221 regexp => $RULE,
222 49 ▶ replacement => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
223 49 my $submatches = $_[0]->{submatches};
224 49 my $selector = $submatches->[0];
225 49 my $declaration = $submatches->[1];
226  
227 49 my $compress = $global_compress;
228  
229 49 $selector =~ s/^\s*|\s*$//gs;
230 49 $selector =~ s/\s*,\s*/,/gsm;
231 49 $selector =~ s/\s+/ /gsm;
232  
233 49 $declaration =~ s/^\s*|\s*$//gs;
234  
235 49 $reggrp_declaration->exec( \$declaration );
236  
237 49 ▶ my $store = $selector . '{' . ( $compress eq 'pretty' ? "\n" : '' ) . $declaration . '}' .
Branch: 2 branches
BranchTrueFalseTotal
$compress eq 'pretty' ? : 24 25 49
$compress eq 'pretty' ? : 24 25 49
238 ( $compress eq 'pretty' ? "\n" : '' );
239  
240 49 ▶ $store = '' unless ( $selector or $declaration );
Branch: 1 branch
BranchTrueFalseTotal
unless $selector or $declaration 0 49 49
Condition33%1/3
$selector or $declaration
l!l&&r!l&&!r
4900
MC/DC0%0/2
$selector or $declaration
$selector $declaration
Truth table33%1/3
A or B
A= $selectorB= $declaration
ABresult
1X1
011
000
241  
242 49 return $store;
243 }
244 }
245 17 ];
246  
247 $self->{mediarules}->{reggrp_data} = [
248 17 @{$self->{import}->{reggrp_data}},
249 17 @{$self->{rule}->{reggrp_data}},
250 17 @{$self->{whitespaces}->{reggrp_data}}
251 ];
252  
253 $self->{global}->{reggrp_data} = [
254 {
255 regexp => $CHARSET,
256 0 ▶ replacement => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
257 0 my $submatches = $_[0]->{submatches};
258  
259 0 my $compress = $global_compress;
260  
261 0 ▶ return $submatches->[0] . " " . $submatches->[1] . ( $compress eq 'pretty' ? "\n" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$compress eq 'pretty' ? : 0 0 0
262 }
263 },
264 {
265 regexp => $AT_RULE,
266 3 ▶ replacement => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
267 3 my $original = $_[0]->{match};
268 3 my ($selector) = ( $original =~ /\@(\S+)/ );
269  
270 3 my $submatches = $_[0]->{submatches};
271 3 my $mediatype = $submatches->[0];
272 3 my $mediarules = $submatches->[1];
273  
274 3 my $compress = $global_compress;
275  
276 3 $mediatype =~ s/^\s*|\s*$//gs;
277 3 $mediatype =~ s/\s*,\s*/,/gsm;
278  
279 3 $reggrp_mediarules->exec( \$mediarules );
280  
281 3 ▶ return "\@$selector " . $mediatype . '{' . ( $compress eq 'pretty' ? "\n" : '' ) .
Branch: 2 branches
BranchTrueFalseTotal
$compress eq 'pretty' ? : 0 3 3
$compress eq 'pretty' ? : 0 3 3
282 $mediarules . '}' . ( $compress eq 'pretty' ? "\n" : '' );
283 }
284 },
285 17 @{$self->{mediarules}->{reggrp_data}}
286 ];
287  
288  
289 map {
290 17 $self->{ '_reggrp_' . $_ } = Regexp::RegGrp->new(
291 {
292 reggrp => $self->{$_}->{reggrp_data}
293 }
294 136 );
295 } @REGGRPS;
296  
297 17 return $self;
298 }
299  
300 30 ▶sub minify {
Subroutine minify: called CC=24 SCAR=33.7
Pod undocumented
301 30 my ( $self, $input, $opts );
302  
303 30 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq __PACKAGE__) { } 30 0 30
Condition33%1/3
ref $_[0] and ref $_[0] eq __PACKAGE__
!ll&&!rl&&r
0030
MC/DC0%0/2
ref $_[0] and ref $_[0] eq __PACKAGE__
ref $_[0] ref $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq __PACKAGE__
ABresult
0X0
100
111
304 ref( $_[0] ) and
305 ref( $_[0] ) eq __PACKAGE__
306 ) {
307 0 $self = __PACKAGE__->init();
308  
309 0 ▶ shift( @_ ) unless ( ref( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
310  
311 0 ( $input, $opts ) = @_;
312 }
313 else {
314 30 ( $self, $input, $opts ) = @_;
315 }
316  
317 30 ▶ if ( ref( $input ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "SCALAR") 0 30 30
318 0 carp( 'First argument must be a scalarref!' );
319 0 return undef;
320 }
321  
322 30 my $css = \'';
323 30 my $cont = 'void';
324  
325 30 ▶ if ( defined( wantarray ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 5 25 30
326 5 ▶ my $tmp_input = ref( $input ) ? ${$input} : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 5 0 5
327  
328 5 $css = \$tmp_input;
329 5 $cont = 'scalar';
330 }
331 else {
332 25 ▶ $css = ref( $input ) ? $input : \$input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 25 0 25
333 }
334  
335 30 ▶ if ( ref( $opts ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq "HASH") 27 3 30
336 27 foreach my $field ( @BOOLEAN_ACCESSORS ) {
337 54 ▶ $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$field} 7 47 54
338 }
339  
340 27 foreach my $field ( 'compress', 'copyright' ) {
341 54 ▶ $self->$field( $opts->{$field} ) if ( defined( $opts->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$field} 16 38 54
342 }
343  
344 27 ▶ $indent = $opts->{indent} && ( !$opts->{compress} || $opts->{compress} eq 'pretty' ) ? ' ' x $opts->{indent} : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'indent'} && (!$opts->{'compress'} || $opts->{'compress'} eq 'pretty') ? : 2 25 27
Condition50%3/6
$opts->{"indent"} and !$opts->{"compress"} || $opts->{"compress"} eq "pretty"
!ll&&!rl&&r
2502
not $opts->{"compress"} or $opts->{"compress"} eq "pretty"
l!l&&r!l&&!r
020
MC/DC33%1/3
$opts->{"indent"} and !$opts->{"compress"} || $opts->{"compress"} eq "pretty"
$opts->{"indent"} not $opts->{"compress"} $opts->{"compress"} eq "pretty"
Truth table50%2/4
A and not(B) or C
A= $opts->{"indent"}B= not $opts->{"compress"}C= $opts->{"compress"} eq "pretty"
ABCresult
0XX0
1000
11X1
1011
345 }
346  
347 # (re)initialize variables used in the closures
348 30 $reggrp_url = $self->reggrp_url;
349 30 $reggrp_declaration = $self->reggrp_declaration;
350 30 $reggrp_mediarules = $self->reggrp_mediarules;
351 30 $reggrp_content_value = $self->reggrp_content_value;
352 30 $global_compress = $self->compress;
353  
354 30 my $copyright_comment = '';
355  
356 30 ▶ if ( ${$css} =~ /$COPYRIGHT_COMMENT/ism ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$css;} =~ /$CSS::Packer::COPYRIGHT_COMMENT/ism) 4 26 30
357 4 $copyright_comment = $1;
358 }
359 # Resets copyright_comment() if there is no copyright comment
360 30 $self->_copyright_comment( $copyright_comment );
361  
362 30 ▶ if ( not $self->no_compress_comment() and ${$css} =~ /$PACKER_COMMENT/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->no_compress_comment and ${$css;} =~ /$CSS::Packer::PACKER_COMMENT/) 2 28 30
Condition100%3/3
not $self->no_compress_comment and ${$css;} =~ /$CSS::Packer::PACKER_COMMENT/
!ll&&!rl&&r
3252
MC/DC100%2/2
not $self->no_compress_comment and ${$css;} =~ /$CSS::Packer::PACKER_COMMENT/
not $self->no_compress_comment ${$css;} =~ /$CSS::Packer::PACKER_COMMENT/
Truth table100%3/3
not(A) and B
A= not $self->no_compress_commentB= ${$css;} =~ /$CSS::Packer::PACKER_COMMENT/
ABresult
0X0
100
111
363 2 my $compress = $1;
364 2 ▶ if ( $compress eq '_no_compress_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress eq "_no_compress_") 2 0 2
365 0 ▶ return ( $cont eq 'scalar' ) ? ${$css} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cont eq 'scalar' ? : 0 2 2
366 }
367  
368 0 $self->compress( $compress );
369 }
370  
371 28 ${$css} =~ s/$COMMENT/ /gsm;
372  
373 28 $self->reggrp_global()->exec( $css );
374  
375 28 ▶ if ( not $self->remove_copyright() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->remove_copyright) 26 2 28
376 26 ▶ ${$css} = ( $self->copyright() || $self->_copyright_comment() ) . ${$css};
Condition100%3/3
$self->copyright || $self->_copyright_comment
l!l&&r!l&&!r
2222
MC/DC100%2/2
$self->copyright || $self->_copyright_comment
$self->copyright $self->_copyright_comment
Truth table100%3/3
A || B
A= $self->copyrightB= $self->_copyright_comment
ABresult
1X1
011
000
377 }
378  
379 28 ▶ return ${$css} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 5 23 28
380 }
381  
382 5 1;
383  
384 __END__
↑ Summary