| File: | blib/lib/CSS/Adaptor.pm |
| Coverage: | 100.0% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package CSS::Adaptor; | |||||
| 2 | ||||||
| 3 | 2 2 2 | 11 5 15 | use strict; | |||
| 4 | 2 2 2 | 14 9 14 | use warnings; | |||
| 5 | ||||||
| 6 | sub new { | |||||
| 7 | 7 | 23 | my ($class) = @_; | |||
| 8 | 7 | 24 | my $self = bless {}, $class; | |||
| 9 | ||||||
| 10 | 7 | 44 | return $self; | |||
| 11 | } | |||||
| 12 | ||||||
| 13 | sub format_stylesheet { | |||||
| 14 | 3 | 7 | my ($self, $stylesheet) = @_; | |||
| 15 | ||||||
| 16 | 3 | 6 | my @blobs; | |||
| 17 | ||||||
| 18 | 3 3 | 5 67 | for (@{$stylesheet->{items}}){ | |||
| 19 | ||||||
| 20 | 6 | 27 | push @blobs, $self->format_atrule($_) if ref $_ eq 'CSS::AtRule'; | |||
| 21 | 6 | 29 | push @blobs, $self->format_ruleset($_) if ref $_ eq 'CSS::Ruleset'; | |||
| 22 | } | |||||
| 23 | ||||||
| 24 | 3 | 30 | return join "", @blobs; | |||
| 25 | } | |||||
| 26 | ||||||
| 27 | sub format_atrule { | |||||
| 28 | 2 | 6 | my ($self, $atrule) = @_; | |||
| 29 | ||||||
| 30 | 2 | 13 | return "\@$atrule->{name} $atrule->{value};\n"; | |||
| 31 | } | |||||
| 32 | ||||||
| 33 | sub format_ruleset { | |||||
| 34 | 1 | 2 | my ($self, $ruleset) = @_; | |||
| 35 | ||||||
| 36 | 1 1 1 | 3 3 3 | my $selectors = join ', ', map {$self->format_selector($_)} @{$ruleset->{selectors}}; | |||
| 37 | 1 1 1 | 2 2 4 | my $declarations = join '; ', map {$self->format_declaration($_)} @{$ruleset->{declarations}}; | |||
| 38 | ||||||
| 39 | 1 | 6 | return $selectors.' { '.$declarations." }\n" ; | |||
| 40 | } | |||||
| 41 | ||||||
| 42 | sub format_selector { | |||||
| 43 | 2 | 6 | my ($self, $selector) = @_; | |||
| 44 | ||||||
| 45 | 2 | 10 | return $selector->{name}; | |||
| 46 | } | |||||
| 47 | ||||||
| 48 | sub format_declaration { | |||||
| 49 | 1 | 9 | my ($self, $declaration) = @_; | |||
| 50 | ||||||
| 51 | 1 | 3 | my $val = $declaration->{simple_value}; | |||
| 52 | 1 | 8 | $val =~ s/^\s*(.*?)\s*$/$1/; | |||
| 53 | ||||||
| 54 | 1 | 5 | my $prop = $declaration->{property}; | |||
| 55 | 1 | 4 | $prop =~ s/^\s*(.*?)\s*$/$1/; | |||
| 56 | ||||||
| 57 | 1 | 6 | return "$prop: $val"; | |||
| 58 | } | |||||
| 59 | ||||||
| 60 | 1; | |||||
| 61 | ||||||