Watcher/Parser.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 89.6% 69 / 77 CC 14 SCAR 23.1
CC
14
Cov
91%
_parse_css
14.6
BEGIN
1.0
BEGIN
1.0
CRAP 10.0
« Watcher/ParserLess.pm ↑ Summary Watcher.pm »
1 package CSS::Watcher::Parser;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use CSS::Selector::Parser qw/parse_selector/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my $nested; # regexp for "{ }"
11 $nested = qr/
12 \{
13 (
14 [^{}]
15 |
16 (??{ $nested })
17 )*
18 \}
19 2 /x;
20  
21 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 6 my $class = shift;
23 6 return bless ({}, $class);
24 }
25  
26 13 ▶sub parse_css {
Subroutine parse_css: called CC=1 SCAR=0.0
Pod undocumented
27 13 my $self = shift;
28 13 my $source = shift;
29  
30 13 my %classes;
31 my %ids;
32  
33 13 $self->_parse_css($source, \%classes, \%ids);
34
35 13 return (\%classes, \%ids);
36 }
37  
38 14 ▶sub _parse_css {
Subroutine _parse_css: called CC=14 SCAR=26.8
39 14 my $self = shift;
40 14 my ($source, $classes, $ids) = @_;
41  
42 14 $source =~s|/\*.*?\*/||gs; # remove comments
43  
44 14 ▶ while ($source =~ m/(.*?)($nested)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /(.*?)($nested)/gs) 25 14 39
45 25 my ($selector, $selector_body) = ($1, $2);
46  
47 25 ▶ if ($selector =~/\s*\@media/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($selector =~ /\s*\@media/s) { } 1 24 25
elsif ($selector =~ /\s*\@/s) { } 0 24 24
48 1 ▶ if ($selector_body =~m /\{(.+)\}/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector_body =~ /\{(.+)\}/s) 1 0 1
49 1 $self->_parse_css($1, $classes, $ids);
50 }
51 1 next;
52 } elsif ($selector =~/\s*\@/s) {
53 # ignore @keyframes, etc @..
54 0 next;
55 }
56 24 eval {
57 24 foreach (parse_selector($selector)) {
58 28 ▶ next unless (ref $_ eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 0 28 28
59 28 foreach my $rule (@{$_}) {
60 28 ▶ if (exists $rule->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"class"}) 19 9 28
61 # Bug, selector for .foo.bar return class foo.bar
62 # so split this selector by "."
63 19 foreach my $classname (split /\./, $rule->{class}) {
64 $classes->{
65 exists ($rule->{element}) ? $rule->{element} : "global"
66 21 ▶ }{ $classname }++;
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{'element'} ? : 10 11 21
67 }
68 }
69 28 ▶ if (exists $rule->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"id"}) 9 19 28
70 $ids->{
71 exists ($rule->{element}) ? $rule->{element} : "global"
72 9 ▶ }{ $rule->{id} }++;
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{'element'} ? : 2 7 9
73 }
74 }
75 }
76 };
77 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
78 # log selector parse failure
79 0 ERROR "Can't parse selector: \"$selector\"";
80 }
81 }
82 }
83  
84  
85 2 1;
86  
87 __END__
« Watcher/ParserLess.pm ↑ Summary Watcher.pm »