lib/CSS/LESS/Filter.pm

filter: Statementstmt 96.7% 90 / 93 Branchbran 77.5% 45 / 58 Conditioncond 60.7% 17 / 28 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 81.1% 168 / 207 CC 45 SCAR 28.9
CC
45
Cov
85%
_apply
28.6
process
16.5
add
4.2
CRAP 18.1
↑ Summary
1 package CSS::LESS::Filter;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.04';
9  
10 16 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
11 16 my $class = shift;
12  
13 16 ▶ my $parser = Parse::RecDescent->new($class->_less_grammar)
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "Parse::RecDescent"->new($class->_less_grammar) or die "Bad Grammar" 16 0 16
14 or die "Bad Grammar";
15  
16 16 my $self = bless { parser => $parser, filters => [] }, $class;
17  
18 16 ▶ $self->add(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 16 16
19  
20 16 $self;
21 }
22  
23 16 ▶sub process {
Subroutine process: called CC=16 SCAR=28.0
Pod documented
24 16 my ($self, $less, $opts) = @_;
25  
26 16 ▶ my $mode = ($opts || {})->{mode} || 'warn';
Condition100%4/4
+($opts || {})->{'mode'} || 'warn'
l!l
412
$opts || {}
l!l
412
MC/DC100%1/1
+($opts || {})->{'mode'} || 'warn'
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
27  
28 16 $_->[2] = 0 for @{$self->{filters}}; # clear "used" flag.
29  
30 16 my $res = $self->_apply($self->_parse($less), "");
31  
32 16 for (@{$self->{filters}}) {
33 16 ▶ next if $_->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_->[2] 9 7 16
34 7 ▶ if ($mode eq 'warn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "warn") 3 4 7
35 3 carp "Filter '$_->[0]' ($_->[1]) is not used at all.";
36 3 next;
37 }
38 4 ▶ if ($mode eq 'append') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "append") 4 0 4
39 4 my ($id, $filter) = ($_->[0], $_->[1]);
40 4 ▶ if (ref $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id) 0 4 4
41 0 carp "Can't append ambiguous '$id' ($filter).";
42 0 next;
43 }
44 4 my $type = substr($id, -1);
45 4 my $depth =()= $id =~ /\{/g;
46 4 my $closing_braces = '}' x $depth;
47 4 ▶ my $value = !ref $filter ? $filter : $filter->('');
Branch: 1 branch
BranchTrueFalseTotal
!ref($filter) ? : 3 1 4
48 4 ▶ $res .= "\n" if length $res && substr($res, -1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if length $res and substr($res, -1) ne "\n" 0 4 4
Condition66%2/3
length $res and substr($res, -1) ne "\n"
!ll&&!rl&&r
220
MC/DC0%0/2
length $res and substr($res, -1) ne "\n"
length $res substr($res, -1) ne "\n"
Truth table66%2/3
A and B
A= length $resB= substr($res, -1) ne "\n"
ABresult
0X0
100
111
49 4 ▶ if ($id eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($id eq '') { } 1 3 4
elsif ($type eq '{') { } 1 2 3
50 1 $res .= "$value\n";
51 }
52 elsif ($type eq '{') { # ruleset
53 1 $res .= "$id\n$value";
54 1 ▶ $res .= "\n" if substr($res, -1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($res, -1) ne "\n" 0 1 1
55 1 $res .= "$closing_braces\n";
56 }
57 else { # declaration, at rule
58 2 ▶ $closing_braces = " $closing_braces" if $closing_braces;
Branch: 1 branch
BranchTrueFalseTotal
if $closing_braces 1 1 2
59 2 $res .= "$id $value;$closing_braces\n";
60 }
61 }
62 }
63  
64 16 $res;
65 }
66  
67 16 ▶sub add {
Subroutine add: called CC=4 SCAR=14.3
Pod documented
68 16 my $self = shift;
69  
70 16 ▶ croak "Odd number of args" if @_ && @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and @_ % 2 0 16 16
Condition33%1/3
@_ and @_ % 2
!ll&&!rl&&r
0160
MC/DC0%0/2
@_ and @_ % 2
@_ @_ % 2
Truth table33%1/3
A and B
A= @_B= @_ % 2
ABresult
0X0
100
111
71  
72 16 ▶ while(my ($id, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 16 16 32
73 16 push @{$self->{filters}}, [$id, $value];
74 }
75  
76 16 $self;
77 }
78  
79 16 ▶sub _parse {
Subroutine _parse: called CC=1 SCAR=0.0
80 16 my ($self, $less) = @_;
81  
82 16 $self->{parser}->less($less);
83 }
84  
85 22 ▶sub _apply {
Subroutine _apply: called CC=25 SCAR=33.5
86 22 my ($self, $parts, $id) = @_;
87  
88 22 my $str = '';
89 22 for my $part (@$parts) {
90 40 ▶ if (!ref $part) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $part) { } 22 18 40
elsif (ref $part eq ref {}) { } 18 0 18
91 22 $str .= $part;
92 }
93 elsif (ref $part eq ref {}) {
94 18 ▶ if (ref $part->{value} eq ref []) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $part->{'value'} eq ref []) { } 6 12 18
95 6 ▶ my $new_id = (length $id ? "$id " : "") . "$part->{key} {";
Branch: 1 branch
BranchTrueFalseTotal
length $id ? : 0 6 6
96 6 $new_id =~ s/\s+/ /gs;
97 6 my $ruleset .= $part->{key} . $part->{brace_open};
98 6 my $inside = $self->_apply($part->{value}, $new_id);
99  
100 6 for (@{$self->{filters}}) {
101 6 ▶ next if $_->[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq "" 0 6 6
102 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_->[0] and $new_id eq $_->[0] or ref $_->[0] eq ref qr// and $new_id =~ /$_->[0]/) 2 4 6
Condition55%5/9
not ref $_->[0] and $new_id eq $_->[0] or ref $_->[0] eq ref qr// and $new_id =~ /$_->[0]/
l!l&&r!l&&!r
204
not ref $_->[0] and $new_id eq $_->[0]
!ll&&!rl&&r
042
ref $_->[0] eq ref qr// and $new_id =~ /$_->[0]/
!ll&&!rl&&r
400
MC/DC25%1/4
not ref $_->[0] and $new_id eq $_->[0] or ref $_->[0] eq ref qr// and $new_id =~ /$_->[0]/
not ref $_->[0] $new_id eq $_->[0] ref $_->[0] eq ref qr// $new_id =~ /$_->[0]/
Truth table28%2/7
not(A) and B or C and D
A= not ref $_->[0]B= $new_id eq $_->[0]C= ref $_->[0] eq ref qr//D= $new_id =~ /$_->[0]/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
103 (!ref $_->[0] and $new_id eq $_->[0]) or
104 (ref $_->[0] eq ref qr// and $new_id =~ /$_->[0]/)
105 ) {
106 2 ▶ if (!ref $_->[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_->[1]) { } 1 1 2
elsif (ref $_->[1] eq ref sub { } ) { } 1 0 1
107 1 $inside = $_->[1];
108 }
109 0 ▶ elsif (ref $_->[1] eq ref sub {}) {
Subroutine __ANON__: not called CC=1 SCAR=0.0
110 1 $inside = $_->[1]->($inside);
111 }
112 2 $_->[2] = 1;
113 }
114 }
115 6 ▶ next unless defined $inside;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inside 0 6 6
116 6 $ruleset .= $inside;
117 6 $ruleset .= $part->{brace_close};
118 6 $str .= $ruleset;
119 }
120 else {
121 12 (my $sep = $part->{sep}) =~ s/\s+//gs;
122 12 ▶ my $cur_id = (length $id ? "$id " : "") . "$part->{key}$sep";
Branch: 1 branch
BranchTrueFalseTotal
length $id ? : 6 6 12
123 12 for (@{$self->{filters}}) {
124 12 ▶ next if $_->[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq "" 1 11 12
125 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_->[0] and $cur_id eq $_->[0] or ref $_->[0] eq ref qr// and $cur_id =~ /$_->[0]/) 8 3 11
Condition55%5/9
not ref $_->[0] and $cur_id eq $_->[0] or ref $_->[0] eq ref qr// and $cur_id =~ /$_->[0]/
l!l&&r!l&&!r
803
not ref $_->[0] and $cur_id eq $_->[0]
!ll&&!rl&&r
038
ref $_->[0] eq ref qr// and $cur_id =~ /$_->[0]/
!ll&&!rl&&r
300
MC/DC25%1/4
not ref $_->[0] and $cur_id eq $_->[0] or ref $_->[0] eq ref qr// and $cur_id =~ /$_->[0]/
not ref $_->[0] $cur_id eq $_->[0] ref $_->[0] eq ref qr// $cur_id =~ /$_->[0]/
Truth table28%2/7
not(A) and B or C and D
A= not ref $_->[0]B= $cur_id eq $_->[0]C= ref $_->[0] eq ref qr//D= $cur_id =~ /$_->[0]/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
126 (!ref $_->[0] and $cur_id eq $_->[0]) or
127 (ref $_->[0] eq ref qr// and $cur_id =~ /$_->[0]/)
128 ) {
129 8 ▶ if (!ref $_->[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_->[1]) { } 6 2 8
elsif (ref $_->[1] eq ref sub { } ) { } 2 0 2
130 6 $part->{value} = $_->[1];
131 }
132 0 ▶ elsif (ref $_->[1] eq ref sub {}) {
Subroutine __ANON__: not called CC=1 SCAR=0.0
133 2 $part->{value} = $_->[1]->($part->{value});
134 }
135 8 $_->[2] = 1;
136 }
137 }
138 12 ▶ next unless defined $part->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part->{"value"} 3 9 12
139 9 $str .= join '', @$part{qw/key sep value semicolon/};
140 }
141 }
142 else {
143 0 warn "illegal format: $part";
144 }
145 }
146  
147 22 $str;
148 }
149  
150 16 ▶sub _less_grammar { return <<'GRAMMAR'
Subroutine _less_grammar: called CC=1 SCAR=0.0
151 {
152 use Data::Dump 'dump';
153 *debug = sub {};
154 if ($ENV{CSS_LESS_FILTER_DEBUG}) {
155 *debug = sub { warn(shift, ': ', dump @_) }
156 }
157 }
158  
159 less: <skip: ''> (at_rule | ruleset | declaration | mixin | selectors | comments | sp | unknown)(s)
160 { $return = $item[2] }
161  
162 comments: less_comment | css_comment
163 { debug($thisline, @item); $return = $item[1] }
164  
165 less_comment: /\/\/.*$/m
166 { debug($thisline, @item); $return = $item[1] }
167  
168 css_comment: /\/\*[^*]*\*+([^\/*][^*]*\*+)*\//
169 { debug($thisline, @item); $return = $item[1] }
170  
171 ruleset: selectors brace_open (
172 comments | ruleset | function | declaration | at_rule |
173 selectors | semicolon | sp
174 )(s?) brace_close
175 { debug($thisline, @item); $return = { key => $item[1], brace_open => $item[2], value => $item[3], brace_close => $item[4] } }
176  
177 at_rule: at_keyword sp (string | url | ident | parens | sp)(s) semicolon
178 { debug($thisline, @item); $return = {key => $item[1], sep => $item[2], value => join('', @{$item[3]}), semicolon => $item[4]} }
179  
180 selectors: (function|selector) (function | selector | parens | comments | ',' | sp)(s?)
181 { debug($thisline, @item); $return = join '', $item[1], @{$item[2]} }
182  
183 at_keyword: '@' ident
184 { debug($thisline, @item); $return = join '', @item[1..2] }
185  
186 selector: namespace_prefix(?) (
187 ident
188 | id_selector
189 | class_selector
190 | at_keyword
191 | '&'
192 | '%'
193 | pseudo_class
194 | pseudo_element
195 | attribute_selector
196 | child_selector
197 | adjacent_selector
198 | universal_selector
199 | parens # for interpolation
200 | interpolated_variable
201 | color
202 | percent
203 | important
204 )
205 { debug($thisline, @item); $return = join '', @{$item[1]}, $item[2] }
206  
207 namespace_prefix: (ident | '*')(?) '|'
208 { debug($thisline, @item); $return = join '', @{$item[1]}, $item[2] }
209  
210 id_selector: '#' ident
211 { debug($thisline, @item); $return = join '', @item[1..2] }
212  
213 class_selector: '.' ident
214 { debug($thisline, @item); $return = join '', @item[1..2] }
215  
216 pseudo_class:
217 ':' ident (parens)(?)
218 { debug($thisline, @item); $return = join '', @item[1..2], @{$item[3]} }
219 | ':' ident
220 { debug($thisline, @item); $return = join '', @item[1..2] }
221  
222 pseudo_element: '::' ident
223 { debug($thisline, @item); $return = join '', @item[1..2] }
224  
225 universal_selector: '*'
226 { debug($thisline, @item); $return = $item[1] }
227  
228 child_selector: '>'
229 { debug($thisline, @item); $return = $item[1] }
230  
231 adjacent_selector: '+'
232 { debug($thisline, @item); $return = $item[1] }
233  
234 attribute_selector: /\[.+?\]/
235 { debug($thisline, @item); $return = $item[1] }
236  
237 ident: (word | interpolated_variable | escape)(s)
238 { debug($thisline, @item); $return = join '', @{$item[1]} }
239  
240 interpolated_variable:
241 '@{' word '}'
242 { debug($thisline, @item); $return = join '', @item[1..3] }
243 | '@@' word
244 { debug($thisline, @item); $return = join '', @item[1..2] }
245  
246 mixin: selectors semicolon
247 { debug($thisline, @item); $return = join '', @item[1..2] }
248  
249 brace_open: sp '{' sp
250 { debug($thisline, @item); $return = join '', @item[1..3] }
251  
252 brace_close: sp '}' sp
253 { debug($thisline, @item); $return = join '', @item[1..3] }
254  
255 semicolon: sp ';' sp
256 { debug($thisline, @item); $return = join '', @item[1..3] }
257  
258 colon: sp ':' sp
259 { debug($thisline, @item); $return = join '', @item[1..3] }
260  
261 declaration: (property|variable) colon values (semicolon)(?)
262 { debug($thisline, @item); $return = {key => $item[1], sep => $item[2], value => $item[3], semicolon => join '', @{$item[4]}} }
263  
264 property: /[\*]?/ ident
265 { debug($thisline, @item); $return = join'', @item[1..2] }
266  
267 unicode_range: /U\+[0-9a-fA-F?]{1,6}/
268 { debug($thisline, @item); $return = $item[1] }
269  
270 iefilter: 'progid:DXImageTransform.Microsoft.' ident parens
271 { debug($thisline, @item); $return = join'', @item[1..3] }
272  
273 javascript: /~?`[^`]+?`/s
274 { debug($thisline, @item); $return = $item[1] }
275  
276 values: (value | comments | /[, ]/ )(s)
277 { debug($thisline, @item); $return = join '', @{$item[1]} }
278  
279 value: (
280 string | url | variable | color | iefilter | javascript
281 | unicode_range | expression | percent
282 | px | num | function | ident | important
283 | sp | unknown
284 )(s)
285 { debug($thisline, @item); $return = join'', @{$item[1]} }
286  
287 variable: at_keyword
288 { debug($thisline, @item); $return = $item[1] }
289  
290 important: sp '!' sp 'important'
291 { debug($thisline, @item); $return = join'', @item[1..4] }
292  
293 url_string: (/[!#$%&*\-\[:\/\.\?=\]~,]|[a-zA-Z0-9_]/ | nonascii | escape)(s)
294 { debug($thisline, @item); $return = join'', @{$item[1]} }
295  
296 url: 'url(' sp (url_string | string) sp ')'
297 { debug($thisline, @item); $return = join'', @item[1..5] }
298  
299 function: selector parens
300 { debug($thisline, @item); $return = join '', @item[1..2] }
301  
302 num: /[0-9\.\-]+[a-z]*/
303 { debug($thisline, @item); $return = $item[1] }
304  
305 op: /\s*[\+\*\/\-,<>=]\s*/
306 { debug($thisline, @item); $return = $item[1] }
307  
308 url: /url\([^)]+?\)/
309 { debug($thisline, @item); $return = $item[1] }
310  
311 paren_open: '('
312 { debug($thisline, @item); $return = $item[1] }
313  
314 paren_close: ')'
315 { debug($thisline, @item); $return = $item[1] }
316  
317 arg: parens | variable | op | color | px | percent | num | function | string | ident | colon | attribute_selector
318 { debug($thisline, @item); $return = $item[1] }
319  
320 parens: paren_open (arg | /[;, ]/ | sp)(s?) paren_close
321 { debug($thisline, @item); $return = join '', $item[1], @{$item[2]}, $item[3] }
322 expression: (variable | percent | px | num | op | parens)(s)
323 { debug($thisline, @item); $return = join '', @{$item[1]} }
324  
325 px: /[0-9]+px/
326 { debug($thisline, @item); $return = $item[1] }
327  
328 percent: /[0-9\.\-]+%/
329 { debug($thisline, @item); $return = $item[1] }
330  
331 color: /#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/
332 { debug($thisline, @item); $return = $item[1] }
333  
334 string: string1 | string2
335 { debug($thisline, @item); $return = $item[1] }
336  
337 string1: /~?"/ ( /[^\n\r\f\\"]/ | escaped_nl | escape )(s?) /"/
338 { debug($thisline, @item); $return = join '', $item[1], @{$item[2]}, $item[3] }
339  
340 string2: /~?'/ ( /[^\n\r\f\\']/ | escaped_nl | escape )(s?) /'/
341 { debug($thisline, @item); $return = join '', $item[1], @{$item[2]}, $item[3] }
342  
343 unicode: '\\' /[0-9a-fA-F]{1,6}(\r\n|[ \n\r\f\t])?/
344 { debug($thisline, @item); $return = join '', @item[1..2] }
345  
346 word: /[-]?/ nmstart nmchar(s?)
347 { debug($thisline, @item); $return = join '', @item[1..2], @{$item[3]} }
348  
349 nmstart: /[_a-zA-Z]/ | nonascii | escape
350 { debug($thisline, @item); $return = $item[1] }
351  
352 name: nmchar(s)
353 { debug($thisline, @item); $return = join '', @{$item[1]} }
354  
355 nmchar: /[_a-zA-Z0-9\-]/ | nonascii | escape
356 { debug($thisline, @item); $return = $item[1] }
357  
358 nonascii: /[^\0-\237]/
359 { debug($thisline, @item); $return = $item[1] }
360  
361 escape:
362 unicode
363 { debug($thisline, @item); $return = $item[1] }
364 | '\\' /[^\n\r\f0-9a-fA-F]/
365 { debug($thisline, @item); $return = join '', @item[1..2] }
366  
367 escaped_nl: '\\' nl
368 { debug($thisline, @item); $return = join '', @item[1..2] }
369  
370 nl: "\n" | "\r\n" | "\r" | "\f"
371 { debug($thisline, @item); $return = $item[1] }
372  
373 sp: /[ \t\r\n]*/
374 { debug($thisline, @item); $return = $item[1] }
375  
376 unknown: /./s
377 { warn dump @item; $return = $item[1] }
378  
379 GRAMMAR
380 16 }
381  
382 6 1;
383  
384 __END__
↑ Summary