blib/lib/CSS/Packer.pm

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