| 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
| |||||||||||||||
| 45 | 25 | my ($selector, $selector_body) = ($1, $2); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 25 | ▶ | if ($selector =~/\s*\@media/s) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 1 | ▶ | if ($selector_body =~m /\{(.+)\}/s) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 59 | 28 | foreach my $rule (@{$_}) { | |||||||||||||
| 60 | 28 | ▶ | if (exists $rule->{class}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | 28 | ▶ | if (exists $rule->{id}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | $ids->{ | ||||||||||||||
| 71 | exists ($rule->{element}) ? $rule->{element} : "global" | ||||||||||||||
| 72 | 9 | ▶ | }{ $rule->{id} }++; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | }; | ||||||||||||||
| 77 | 24 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | # log selector parse failure | ||||||||||||||
| 79 | 0 | ERROR "Can't parse selector: \"$selector\""; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | 2 | 1; | |||||||||||||
| 86 | |||||||||||||||
| 87 | __END__ | ||||||||||||||