| 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 | 11 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
parse_css: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 28 | 11 | my $source = shift; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 11 | my %classes; | |||||||||||||
| 31 | my %ids; | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 11 | $self->_parse_css($source, \%classes, \%ids); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 11 | return (\%classes, \%ids); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | sub _parse_css { | ||||||||||||||
| 39 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_parse_css: called CC=14 SCAR=26.8
| |||||||||||||||
| 40 | 12 | my ($source, $classes, $ids) = @_; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 12 | $source =~s|/\*.*?\*/||gs; # remove comments | |||||||||||||
| 43 | |||||||||||||||
| 44 | 12 | ▶ | while ($source =~ m/(.*?)($nested)/gs) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 23 | my ($selector, $selector_body) = ($1, $2); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 23 | ▶ | 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 | 22 | eval { | |||||||||||||
| 57 | 22 | foreach (parse_selector($selector)) { | |||||||||||||
| 58 | 26 | ▶ | next unless (ref $_ eq 'ARRAY'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 26 | foreach my $rule (@{$_}) { | |||||||||||||
| 60 | 26 | ▶ | if (exists $rule->{class}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | # Bug, selector for .foo.bar return class foo.bar | ||||||||||||||
| 62 | # so split this selector by "." | ||||||||||||||
| 63 | 17 | foreach my $classname (split /\./, $rule->{class}) { | |||||||||||||
| 64 | $classes->{ | ||||||||||||||
| 65 | exists ($rule->{element}) ? $rule->{element} : "global" | ||||||||||||||
| 66 | 19 | ▶ | }{ $classname }++; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | 26 | ▶ | 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 | 22 | ▶ | 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__ | ||||||||||||||
| 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 | ||||||||||||||