Style.pm

filter: Statementstmt 85.7% 60 / 70 Branchbran 75.0% 12 / 16 Conditioncond 80.7% 21 / 26 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 81.1% 112 / 138 CC 19 SCAR 23.9
CC
19
Cov
83%
style
17.8
__from_css
6.0
_from_css
2.0
CRAP 10.9
↑ Summary foo.pl »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Mojo::DOM::Role::Style::Value;
5  
6 # use Scalar::Util qw/blessed/;
7  
8 use overload
9 5 ▶ '""' => sub { ${$_[0]}[0] },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
10 6 ▶ '%{}' => sub { ${$_[0]}[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 4 fallback => 1;
12  
13 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
14 11 my ($class, $css, $hash) = @_;
15 11 return bless [$css, $hash], $class;
16 }
17  
18 package Mojo::DOM::Role::Style;
19  
20 # ABSTRACT: Adds a style method to Mojo::DOM
21  
22 4 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 ▶sub style {
Subroutine style: called CC=17 SCAR=28.8
Pod documented
26 16 my $self = shift;
27  
28 16 ▶ my $css = $self->attr('style') // '';
Condition100%2/2
$self->attr("style") // ""
l!l
151
MC/DC100%1/1
$self->attr("style") // ""
$self->attr("style")
Truth table100%2/2
A
A= $self->attr("style")
Aresult
11
00
29 16 my ($h, $k) = _from_css($css);
30  
31 # Getter - no args
32 16 ▶ return Mojo::DOM::Role::Style::Value->new($css, $h) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 11 5 16
33  
34 # Single string arg - get one property value
35 5 ▶ if (@_ == 1 && !ref $_[0] && defined $_[0] && $_[0] !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0] and defined $_[0] and not $_[0] =~ /:/u) 1 4 5
Condition88%8/9
@_ == 1 and not ref $_[0] and defined $_[0] and not $_[0] =~ /:/u
!ll&&!rl&&r
401
@_ == 1 and not ref $_[0] and defined $_[0]
!ll&&!rl&&r
311
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
122
MC/DC75%3/4
@_ == 1 and not ref $_[0] and defined $_[0] and not $_[0] =~ /:/u
@_ == 1 not ref $_[0] defined $_[0] not $_[0] =~ /:/u
Truth table80%4/5
A and not(B) and C and not(D)
A= @_ == 1B= not ref $_[0]C= defined $_[0]D= not $_[0] =~ /:/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
36 1 return $h->{$_[0]};
37 }
38  
39 4 ▶ if (@_ == 1 && !ref $_[0] && defined $_[0] && $_[0] =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0] and defined $_[0] and $_[0] =~ /:/u) 0 4 4
Condition66%6/9
@_ == 1 and not ref $_[0] and defined $_[0] and $_[0] =~ /:/u
!ll&&!rl&&r
400
@_ == 1 and not ref $_[0] and defined $_[0]
!ll&&!rl&&r
310
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
121
MC/DC0%0/4
@_ == 1 and not ref $_[0] and defined $_[0] and $_[0] =~ /:/u
@_ == 1 not ref $_[0] defined $_[0] $_[0] =~ /:/u
Truth table60%3/5
A and not(B) and C and D
A= @_ == 1B= not ref $_[0]C= defined $_[0]D= $_[0] =~ /:/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
40 0 $self->attr(style => $_[0]);
41 0 return $self
42 }
43  
44 # Undef - remove style attribute entirely
45 4 ▶ if (@_ == 1 && !defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not defined $_[0]) 1 3 4
Condition100%3/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
121
MC/DC100%2/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
46 1 $self->attr({style => undef});
47 1 return $self;
48 }
49  
50 # Hashref - merge into existing style
51 3 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH") 2 1 3
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
102
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
52 2 $h->{$_} = $_[0]->{$_} for keys %{$_[0]};
53 2 my $new = _to_css($h, $k);
54 2 $self->attr(style => $new);
55 # return Mojo::DOM::Role::Style::Value->new($new, $h);
56 2 return $self
57 }
58  
59 # Flat list - replace entire style
60 1 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) 1 0 1
61 1 my @keys = do { my %seen; grep { !$seen{$_}++ } @_[ grep { !($_ % 2) } 0 .. $#_ ] };
62 1 my $new = _to_css({@_}, \@keys);
63 1 $self->attr(style => $new);
64 # return Mojo::DOM::Role::Style::Value->new($new, {@_});
65 1 return $self;
66 }
67 }
68  
69 3 ▶sub _to_css {
Subroutine _to_css: called CC=1 SCAR=0.0
70 3 my ($h, $k) = @_;
71 3 $k = [ uniq(@$k, keys %$h) ];
72 3 return join ';', map { $_ . ':' . $h->{$_} } @$k;
73 }
74  
75 0 ▶sub __from_css {
Subroutine __from_css: not called CC=2 SCAR=17.9
76 0 my $css = shift;
77 0 ▶ return ({}, []) unless $css;
Branch: 1 branch
BranchTrueFalseTotal
unless $css 0 0 0
78  
79 0 my $k = [ map { /^([^:]+?)\s*:/; $1 } split /\s*;\s*/, $css ];
80 0 my $h = { map { split /\s*:\s*/, $_, 2 } split /\s*;\s*/, $css };
81  
82 0 return ($h, $k);
83 }
84  
85 16 ▶sub _from_css {
Subroutine _from_css: called CC=2 SCAR=6.9
86 16 my $css = shift;
87 16 ▶ return ({}, []) unless $css;
Branch: 1 branch
BranchTrueFalseTotal
unless $css 1 15 16
88  
89 15 my @declarations = split /\s*;\s*(?![^(]*\))/, $css;
90  
91 15 my $k = [ map { /^([^:]+?)\s*:/; $1 } @declarations ];
92 15 my $h = { map { split /\s*:\s*/, $_, 2 } @declarations };
93  
94 15 return ($h, $k);
95 }
96  
97 4 1;
98  
99 =encoding utf8
100  
101 =head1 NAME
102  
103 Mojo::DOM::Role::Style - Manage inline CSS styles on Mojo::DOM elements
104  
105 =head1 SYNOPSIS
106  
107 use Mojo::DOM;
108  
109 my $dom = Mojo::DOM->new('<div style="color:red;font-size:12pt">hello</div>')
110 ->with_roles('+Style');
111  
112 # Get the full style - stringifies to the CSS string
113 my $style = $dom->at('div')->style;
114 say $style; # "color:red;font-size:12pt"
115 say $style->{'color'}; # "red"
116  
117 # Get a single property
118 my $color = $dom->at('div')->style('color');
119  
120 # Replace with a raw CSS string
121 $dom->at('div')->style('color:blue;font-size:14pt');
122  
123 # Replace with a flat list
124 $dom->at('div')->style(color => 'blue', 'font-size' => '14pt');
125  
126 # Merge into the existing style
127 $dom->at('div')->style({'font-weight' => 'bold'});
128  
129 # Remove the style attribute entirely
130 $dom->at('div')->style(undef);
131  
132 =head1 DESCRIPTION
133  
134 L<Mojo::DOM::Role::Style> is a role that adds a convenience method for
135 reading and manipulating the C<style> attribute of L<Mojo::DOM> elements.
136  
137 =head1 RETURN VALUE
138  
139 Getter calls with no arguments return a L<Mojo::DOM::Role::Style::Value>
140 object. This object stringifies to the CSS attribute string, and can be
141 dereferenced as a hash to access individual properties.
142  
143 my $style = $dom->at('div')->style;
144 say $style; # "color:red;font-size:12pt"
145 say $style->{'color'}; # "red"
146  
147 Single-property getter calls return a plain string. All setter calls return
148 the element itself for chaining.
149  
150 =head1 METHODS
151  
152 L<Mojo::DOM::Role::Style> implements the following methods.
153  
154 =head2 style
155  
156 my $style = $dom->at('div')->style;
157 my $val = $dom->at('div')->style('color');
158 $dom = $dom->at('div')->style('color:red;font-size:12pt');
159 $dom = $dom->at('div')->style(color => 'red', 'font-size' => '12pt');
160 $dom = $dom->at('div')->style({'font-weight' => 'bold'});
161 $dom = $dom->at('div')->style(undef);
162  
163 Get or set the inline style of this element.
164  
165 With no arguments, returns a L<Mojo::DOM::Role::Style::Value> representing
166 the current style.
167  
168 # "red"
169 my $style = $dom->at('p')->style;
170 say $style->{'color'};
171  
172 With a single string argument that is a property name, returns the value of
173 that CSS property, or C<undef> if it is not set.
174  
175 # "red"
176 $dom->at('p')->style('color');
177  
178 With a single string argument that is a raw CSS declaration string, replaces
179 the entire inline style and returns the element for chaining.
180  
181 # <p style="color:red;font-size:12pt">hi</p>
182 $dom->at('p')->style('color:red;font-size:12pt');
183  
184 With an even-length flat list, replaces the entire inline style with the
185 given property-value pairs. Key order is preserved as supplied. Returns the
186 element for chaining.
187  
188 # <p style="color:blue;font-size:14pt">hi</p>
189 $dom->at('p')->style(color => 'blue', 'font-size' => '14pt');
190  
191 With a hash reference, merges the given properties into the existing style,
192 adding new properties and overwriting existing ones. Returns the element for
193 chaining.
194  
195 # <p style="color:red;font-weight:bold">hi</p>
196 $dom->at('p')->style({'font-weight' => 'bold'});
197  
198 With C<undef>, removes the C<style> attribute entirely and returns the
199 element for chaining.
200  
201 # <p>hi</p>
202 $dom->at('p')->style(undef)->attr('id', 'intro');
203  
204 =head1 SEE ALSO
205  
206 L<Mojo::DOM>, L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
207  
208 =head1 COPYRIGHT AND LICENSE
209  
210 Copyright (c) 2021 Simone Cesano.
211  
212 This is free software, you may use it and distribute it under the same terms
213 as Perl itself.
214  
215 =head1 AUTHOR
216  
217 Simone Cesano
218  
219 =cut
↑ Summary foo.pl »