lib/CSS/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 SCAR 26.5
CC
14
Cov
91%
_parse_css
14.6
BEGIN
1.0
BEGIN
1.0
CRAP 14.1
« lib/CSS/Watcher/Monitor.pm ↑ Summary
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 sub new {
22 6 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 6 return bless ({}, $class);
24 }
25  
26 sub parse_css {
27 13 ▶ my $self = shift;
Subroutine parse_css: called CC=1 SCAR=0.0
Pod undocumented
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 sub _parse_css {
39 14 ▶ my $self = shift;
Subroutine _parse_css: called CC=14 SCAR=26.8
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__
88  
89 =head1 NAME
90  
91 CSS::Watcher::Parser - Extract classes, ids from css
92  
93 =head1 SYNOPSIS
94  
95 use CSS::Watcher::Parser;
96 my $parser = CSS::Watcher::Parser->new()
97 my ($hclasses, $hids) = parser->parse_css ('.foo, #myid {color: red}');
98  
99 =head1 DESCRIPTION
100  
101 Simple parser of css files
102  
103 =head1 AUTHOR
104  
105 Olexandr Sydorchuk (olexandr.syd@gmail.com)
106  
107 =head1 COPYRIGHT AND LICENSE
108  
109 Copyright (C) 2014 by Olexandr Sydorchuk
110  
111 This program is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself, either Perl version 5.8.2 or,
113 at your option, any later version of Perl 5 you may have available.
114  
115 =cut
« lib/CSS/Watcher/Monitor.pm ↑ Summary