| 1 | package CSS::Scopifier; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Prepends CSS selectors to apply scope/context | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = 0.03; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use CSS::Tiny 1.19; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | extends 'CSS::Tiny'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub scopify { | ||||||||||||||||||||||||||||
| 15 | 2 | ▶ | my ($self, $selector, @arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
scopify: called CC=9 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my %opt = ref($arg[0]) ? %{$arg[0]} : @arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | die "scopify() requires string selector argument" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$selector and not ref $selector
MC/DC0%0/2
$selector and not ref $selector
$selector
not ref $selector
Truth table33%1/3
A and not(B)
A= $selectorB= not ref $selector
| |||||||||||||||||||||||||||||
| 19 | unless ($selector && ! ref($selector)); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Merge specified selectors into the root/scoped selector. Useful | ||||||||||||||||||||||||||||
| 22 | # with merge => ['html','body'] if scopifying a base css file | ||||||||||||||||||||||||||||
| 23 | 2 | my $root_sel; | |||||||||||||||||||||||||||
| 24 | 2 | ▶ | if($opt{merge}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my @list = ref $opt{merge} ? @{$opt{merge}} : ($opt{merge}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | $root_sel = {}; | |||||||||||||||||||||||||||
| 27 | 1 | %$root_sel = ( %$root_sel, %$_ ) for ( | |||||||||||||||||||||||||||
| 28 | 1 | map { delete $self->{$_} } | |||||||||||||||||||||||||||
| 29 | 2 | grep { exists $self->{$_} } | |||||||||||||||||||||||||||
| 30 | @list | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | $self->{"$selector $_"} = delete $self->{$_} for (keys %$self); | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | $self->{$selector} = $root_sel if ($root_sel); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | return 1; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Redefining read_string() until CSS::Tiny bug [rt.cpan.org #87261] is fixed | ||||||||||||||||||||||||||||
| 41 | sub read_string { | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $self = ref $_[0] ? shift : bless {}, shift; | ||||||||||||||||||||||||||
|
Subroutine
read_string: called CC=9 SCAR=22.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Flatten whitespace and remove /* comment */ style comments | ||||||||||||||||||||||||||||
| 45 | 2 | my $string = shift; | |||||||||||||||||||||||||||
| 46 | 2 | $string =~ tr/\n\t/ /; | |||||||||||||||||||||||||||
| 47 | 2 | $string =~ s!/\*.*?\*\/!!g; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Split into styles | ||||||||||||||||||||||||||||
| 50 | 2 | foreach ( grep { /\S/ } split /(?<=\})/, $string ) { | |||||||||||||||||||||||||||
| 51 | 8 | ▶ | unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | return $self->_error( "Invalid or unexpected style data '$_'" ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Split in such a way as to support grouped styles | ||||||||||||||||||||||||||||
| 56 | 8 | my $style = $1; | |||||||||||||||||||||||||||
| 57 | 8 | my $properties = $2; | |||||||||||||||||||||||||||
| 58 | 8 | $style =~ s/\s{2,}/ /g; | |||||||||||||||||||||||||||
| 59 | 8 | my @styles = grep { s/\s+/ /g; 1; } grep { /\S/ } split /\s*,\s*/, $style; | |||||||||||||||||||||||||||
| 60 | 8 | ▶ | foreach ( @styles ) { $self->{$_} ||= {} } | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$_} ||= {}
MC/DC100%1/1
$self->{$_} ||= {}
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Split into properties | ||||||||||||||||||||||||||||
| 63 | 8 | foreach ( grep { /\S/ } split /\;/, $properties ) { | |||||||||||||||||||||||||||
| 64 | 12 | ▶ | unless ( /^\s*(\*?[\w._-]+)\s*:\s*(.*?)\s*$/ ) { #<-- updated regex to support starting with '*' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return $self->_error( "Invalid or unexpected property '$_' in style '$style'" ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 12 | foreach ( @styles ) { $self->{$_}->{lc $1} = $2 } | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $self | ||||||||||||||||||||||||||||
| 72 | 2 | } | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||