DOM/Value/Primitive.pm

filter: Statementstmt 93.7% 180 / 192 Branchbran 79.0% 98 / 124 Conditioncond 60.7% 62 / 102 MC/DCmcdc 43.7% 21 / 48 Subroutinesub 100% 32 / 32 Podpod 25.0% 4 / 16 totaltotal 77.2% 397 / 514 CC 104 SCAR 44.9
CC
104
Cov
81%
cssText
195.9
_serialise
13.1
getFloatValue
10.0
CRAP 89.2
« DOM/Parser.pm ↑ Summary DOM/PropertyParser.pm »
1 package CSS::DOM::Value::Primitive;
2  
3 9 $VERSION = '0.16';
4  
5 9 ▶use warnings; no warnings qw 'utf8 parenthesis';;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use CSS::DOM::Constants
10 9 ▶ <%SuffixToConst NO_MODIFICATION_ALLOWED_ERR INVALID_ACCESS_ERR>;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 use CSS::DOM::Util qw '
12 unescape
13 unescape_url
14 unescape_str escape_str
15 9 ▶ escape_ident ';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub DOES {
19 31 ▶ return 1 if $_[1] eq 'CSS::DOM::Value';
Subroutine DOES: called CC=3 SCAR=17.3
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "CSS::DOM::Value" 31 0 31
20 0 ▶ goto &UNIVERSAL'DOES if defined &UNIVERSAL'DOES;
Branch: 1 branch
BranchTrueFalseTotal
if defined &UNIVERSAL::DOES 0 0 0
21 }
22  
23 9 use constant 1.03 our $_const = { # Don’t conflict with the superclass!
24 type => 2,
25 valu => 3, # counters
26 csst => 4, name => 0,
27 ownr => 5, sepa => 1,
28 prop => 6, styl => 2,
29 indx => 7,
30 form => 8,
31 sfrm => 9, # serialisation format; used currently only by colours
32 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶{ no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 9 *EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{primitive};
36 9 our %EXPORT_TAGS = ( all => \our @EXPORT_OK );
37  
38  
39 sub new {
40 250 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
41 250 my %args = @_;
42 250 for('type','value') {
43 0 croak "The $_ argument to new ${\__PACKAGE__} is required"
44 500 ▶ unless exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$_} 0 500 500
45 }
46 250 my $self = bless[], $class;
47 @$self[type,valu,csst,ownr,prop,indx,form]
48 250 = @args{< type value css owner property index format >};
49 250 $self;
50 }
51  
52 9 my @unit_suffixes;
53 9 $unit_suffixes[CSS_PERCENTAGE ] = '%';
54 9 $unit_suffixes[CSS_EMS ] = 'em';
55 9 $unit_suffixes[CSS_EXS ] = 'ex';
56 9 $unit_suffixes[CSS_PX ] = 'px';
57 9 $unit_suffixes[CSS_CM ] = 'cm';
58 9 $unit_suffixes[CSS_MM ] = 'mm';
59 9 $unit_suffixes[CSS_IN ] = 'in';
60 9 $unit_suffixes[CSS_PT ] = 'pt';
61 9 $unit_suffixes[CSS_PC ] = 'pc';
62 9 $unit_suffixes[CSS_DEG ] = 'deg';
63 9 $unit_suffixes[CSS_RAD ] = 'rad';
64 9 $unit_suffixes[CSS_GRAD ] = 'grad';
65 9 $unit_suffixes[CSS_MS ] = 'ms';
66 9 $unit_suffixes[CSS_S ] = 's';
67 9 $unit_suffixes[CSS_HZ ] = 'Hz';
68 9 $unit_suffixes[CSS_KHZ ] = 'kHz';
69  
70 sub cssText {
71 382 ▶ my $self = shift;
Subroutine cssText: called CC=59 SCAR=52.8
Pod documented
72 382 my $old;
73 382 ▶ if(defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 280 102 382
74 280 ▶ if(defined $self->[csst]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[4]) { } 182 98 280
75 182 $old = $self->[csst]
76 }
77 98 else { for($self->[type]) {
78 98 my $val = $self->[valu];
79 $old
80 = $_ == CSS_RECT
81 ? 'rect('
82 . join(
83 ', ',
84 map $self->$_->cssText,
85 <top right bottom left>
86 )
87 .')'
88 : $_ == CSS_RGBCOLOR
89 ? ref $val eq 'ARRAY'
90 98 ▶ ? do {
Branch: 3 branches
BranchTrueFalseTotal
$_ == 24 ? : 2 96 98
$_ == 25 ? : 9 87 96
ref $val eq 'ARRAY' ? : 6 3 9
91 6 my(@val_objs,$ret)
92 = map $self->$_, <red green blue>;
93 6 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (my $form = $self->[9] and @$val < 4 || $val->[3]->getFloatValue == 1) 3 3 6
Condition100%6/6
my $form = $self->[9] and @$val < 4 || $val->[3]->getFloatValue == 1
!ll&&!rl&&r
231
@$val < 4 or $val->[3]->getFloatValue == 1
l!l&&r!l&&!r
211
MC/DC66%2/3
my $form = $self->[9] and @$val < 4 || $val->[3]->getFloatValue == 1
my $form = $self->[9] @$val < 4 $val->[3]->getFloatValue == 1
Truth table75%3/4
A and B or C
A= my $form = $self->[9]B= @$val < 4C= $val->[3]->getFloatValue == 1
ABCresult
0XX0
1000
11X1
1011
94 my $form = $$self[sfrm]
95 and
96 @$val < 4 || $$val[3]->getFloatValue==1
97 ){
98 3 ▶ if($form =~ /^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form =~ /^#/) { } 2 1 3
99 # Try to preserve original #bed/#c0ffee
100 # format if possible
101 2 my $digits = chop $form;
102 2 ▶ if($digits == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits == 1) 0 2 2
103 0 for my $val_obj(@val_objs) {
104 0 my $val = $val_obj->getFloatValue;
105 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($val_obj->primitiveType == 1) { } 0 0 0
106 $val_obj->primitiveType
107 == CSS_NUMBER
108 ){
109 0 ▶ not $val % 17 and $val == int $val
Branch: 1 branch
BranchTrueFalseTotal
not $val % 17 and $val == int $val and $val > 0 and $val < 256 or undef $ret, last 0 0 0
Condition0%0/9
not $val % 17 and $val == int $val and $val > 0 and $val < 256
!ll&&!rl&&r
000
not $val % 17 and $val == int $val and $val > 0
!ll&&!rl&&r
000
not $val % 17 and $val == int $val
!ll&&!rl&&r
000
MC/DC0%0/4
not $val % 17 and $val == int $val and $val > 0 and $val < 256
not $val % 17 $val == int $val $val > 0 $val < 256
Truth table0%0/5
not(A) and B and C and D
A= not $val % 17B= $val == int $valC= $val > 0D= $val < 256
ABCDresult
0XXX0
10XX0
110X0
11100
11111
110 and $val > 0 and $val < 256
111 # ~~~ Would it be faster simply to use
112 # a regexp?
113 or undef $ret, last;
114 0 $ret .= sprintf "%x", $val/17;
115 }
116 else { # percentage
117 0 ▶ not $val % 20 and $val == int $val
Branch: 1 branch
BranchTrueFalseTotal
not $val % 20 and $val == int $val and $val > 0 and $val < 101 or undef $ret, last 0 0 0
Condition0%0/9
not $val % 20 and $val == int $val and $val > 0 and $val < 101
!ll&&!rl&&r
000
not $val % 20 and $val == int $val and $val > 0
!ll&&!rl&&r
000
not $val % 20 and $val == int $val
!ll&&!rl&&r
000
MC/DC0%0/4
not $val % 20 and $val == int $val and $val > 0 and $val < 101
not $val % 20 $val == int $val $val > 0 $val < 101
Truth table0%0/5
not(A) and B and C and D
A= not $val % 20B= $val == int $valC= $val > 0D= $val < 101
ABCDresult
0XXX0
10XX0
110X0
11100
11111
118 and $val > 0 and $val < 101
119 # ~~~ Would it be faster simply to use
120 # a regexp?
121 or undef $ret, last;
122 0 $ret .= sprintf "%x", $val * .15;
123 }
124 }
125 }
126 2 ▶ if(!$val || $digits == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val or $digits == 2) 2 0 2
Condition33%1/3
not $val or $digits == 2
l!l&&r!l&&!r
020
MC/DC0%0/2
not $val or $digits == 2
not $val $digits == 2
Truth table33%1/3
not(A) or B
A= not $valB= $digits == 2
ABresult
1X1
011
000
127 2 for my $val_obj(@val_objs) {
128 4 my $val = $val_obj->getFloatValue;
129 4 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if ($val_obj->primitiveType == 1) { } 3 1 4
elsif ($digits == 2) { } 1 0 1
130 $val_obj->primitiveType
131 == CSS_NUMBER
132 ){
133 3 ▶ $val == int $val
Branch: 1 branch
BranchTrueFalseTotal
$val == int $val and $val > 0 and $val < 256 or undef $ret, last 0 3 3
Condition33%2/6
$val == int $val and $val > 0 and $val < 256
!ll&&!rl&&r
003
$val == int $val and $val > 0
!ll&&!rl&&r
003
MC/DC0%0/3
$val == int $val and $val > 0 and $val < 256
$val == int $val $val > 0 $val < 256
Truth table25%1/4
A and B and C
A= $val == int $valB= $val > 0C= $val < 256
ABCresult
0XX0
10X0
1100
1111
134 and $val > 0 and $val < 256
135 or undef $ret, last;
136 3 $ret .= sprintf "%02x", $val;
137 }
138 elsif($digits == 2) { # percentage
139 1 ▶ not $val % 20 and $val == int $val
Branch: 1 branch
BranchTrueFalseTotal
not $val % 20 and $val == int $val and $val > 0 and $val < 101 or undef $ret, last 0 0 0
Condition33%3/9
not $val % 20 and $val == int $val and $val > 0 and $val < 101
!ll&&!rl&&r
100
not $val % 20 and $val == int $val and $val > 0
!ll&&!rl&&r
100
not $val % 20 and $val == int $val
!ll&&!rl&&r
100
MC/DC0%0/4
not $val % 20 and $val == int $val and $val > 0 and $val < 101
not $val % 20 $val == int $val $val > 0 $val < 101
Truth table20%1/5
not(A) and B and C and D
A= not $val % 20B= $val == int $valC= $val > 0D= $val < 101
ABCDresult
0XXX0
10XX0
110X0
11100
11111
140 and $val > 0 and $val < 101
141 # ~~~ Would it be faster simply to use
142 # a regexp?
143 or undef $ret, last;
144 0 $ret .= sprintf "%02x",$val * 2.55;
145 }
146 }
147 }
148 2 ▶ $ret and substr $ret,0,0, = '#';
Branch: 1 branch
BranchTrueFalseTotal
$ret and substr($ret, 0, 0) = "#" 1 1 2
149 }
150 else { # named colour
151 1 my $rgb = (\our %Colours)->{lc $form};
152 1 ▶ $val_objs[0]->getFloatValue
Branch: 1 branch
BranchTrueFalseTotal
$val_objs[0]->getFloatValue == $rgb->[0] and $val_objs[1]->getFloatValue == $rgb->[1] and $val_objs[2]->getFloatValue == $rgb->[2] and $ret = $form 1 0 1
Condition33%2/6
$val_objs[0]->getFloatValue == $rgb->[0] and $val_objs[1]->getFloatValue == $rgb->[1] and $val_objs[2]->getFloatValue == $rgb->[2]
!ll&&!rl&&r
001
$val_objs[0]->getFloatValue == $rgb->[0] and $val_objs[1]->getFloatValue == $rgb->[1]
!ll&&!rl&&r
001
MC/DC0%0/3
$val_objs[0]->getFloatValue == $rgb->[0] and $val_objs[1]->getFloatValue == $rgb->[1] and $val_objs[2]->getFloatValue == $rgb->[2]
$val_objs[0]->getFloatValue == $rgb->[0] $val_objs[1]->getFloatValue == $rgb->[1] $val_objs[2]->getFloatValue == $rgb->[2]
Truth table25%1/4
A and B and C
A= $val_objs[0]->getFloatValue == $rgb->[0]B= $val_objs[1]->getFloatValue == $rgb->[1]C= $val_objs[2]->getFloatValue == $rgb->[2]
ABCresult
0XX0
10X0
1100
1111
153 == $$rgb[0]
154 and $val_objs[1]->getFloatValue
155 == $$rgb[1]
156 and $val_objs[2]->getFloatValue
157 == $$rgb[2]
158 and $ret = $form;
159 }
160 }
161
162 6 ▶ unless($ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 4 2 6
163 my @types
164 4 = map $_->primitiveType, @val_objs;
165 4 ▶ if($types[0] == $types[1]
Branch: 1 branch
BranchTrueFalseTotal
if ($types[0] == $types[1] and $types[0] == $types[2]) { } 2 2 4
Condition66%2/3
$types[0] == $types[1] and $types[0] == $types[2]
!ll&&!rl&&r
202
MC/DC50%1/2
$types[0] == $types[1] and $types[0] == $types[2]
$types[0] == $types[1] $types[0] == $types[2]
Truth table66%2/3
A and B
A= $types[0] == $types[1]B= $types[0] == $types[2]
ABresult
0X0
100
111
166 && $types[0] == $types[2]) {
167 2 $ret = join ", ",
168 map cssText $_, @val_objs;
169 }
170 else {
171 2 ▶ my $type = $types[
Branch: 1 branch
BranchTrueFalseTotal
$types[0] == $types[1] || $types[0] == $types[2] ? : 0 2 2
Condition33%1/3
$types[0] == $types[1] or $types[0] == $types[2]
l!l&&r!l&&!r
002
MC/DC0%0/2
$types[0] == $types[1] or $types[0] == $types[2]
$types[0] == $types[1] $types[0] == $types[2]
Truth table33%1/3
A or B
A= $types[0] == $types[1]B= $types[0] == $types[2]
ABresult
1X1
011
000
172 $types[0] == $types[1]
173 || $types[0] == $types[2]
174 ? 0
175 : 1
176 ];
177 2 ▶ $ret = join ", ", $type == CSS_NUMBER
Branch: 3 branches
BranchTrueFalseTotal
$type == 1 ? : 2 0 2
$types[$_] == 1 ? : 4 2 6
$types[$_] == 2 ? : 0 0 0
178 ? map
179 $types[$_] == CSS_NUMBER
180 ? $val_objs[$_]->getFloatValue
181 : $val_objs[$_]->getFloatValue
182 * 255/100,
183 0...2
184 : map
185 $types[$_] == CSS_PERCENTAGE
186 ? $val_objs[$_]->getFloatValue
187 : $val_objs[$_]->getFloatValue
188 * 100/255 . '%',
189 0...2;
190 }
191 4 my $alpha;
192 4 ▶ @$val >= 4 && (
Branch: 2 branches
BranchTrueFalseTotal
@$val >= 4 && ($alpha = $self->alpha->cssText) != 1 ? : 2 2 4
$val =~ /^#/ ? : 2 1 3
Condition66%2/3
@$val >= 4 and ($alpha = $self->alpha->cssText) != 1
!ll&&!rl&&r
202
MC/DC50%1/2
@$val >= 4 and ($alpha = $self->alpha->cssText) != 1
@$val >= 4 ($alpha = $self->alpha->cssText) != 1
Truth table66%2/3
A and B
A= @$val >= 4B= ($alpha = $self->alpha->cssText) != 1
ABresult
0X0
100
111
193 $alpha = $self->alpha->cssText
194 ) != 1
195 ? "rgba($ret, $alpha)"
196 : "rgb($ret)"
197 }
198 }
199 : $val =~ /^#/
200 ? $val
201 : escape_ident $val
202 : _serialise($_,$val)
203 }}
204 }
205 382 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 104 278 382
206 require CSS'DOM'Exception,
207 104 ▶ die new CSS'DOM'Exception
Branch: 1 branch
BranchTrueFalseTotal
unless my $owner = $self->[5] 4 100 104
208 NO_MODIFICATION_ALLOWED_ERR,
209 "Unowned value objects cannot be modified"
210 unless my $owner = $self->[ownr];
211 100 my $prop = $$self[prop];
212  
213 # deal with formats
214 100 ▶ if(my $format = $$self[form]) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $format = $self->[8]) { } 10 90 100
elsif (not defined $prop) { } 0 90 90
elsif (defined(my $index = $self->[7])) { } 9 81 90
elsif (my(@arsg) = $owner->property_parser->match($prop, $_[0])) { } 39 42 81
215 10 ▶ if(!our $parser) {
Branch: 1 branch
BranchTrueFalseTotal
unless (our $parser) 1 9 10
216 1 require CSS'DOM'PropertyParser;
217 1 add_property{
218 1 $parser = new CSS'DOM'PropertyParser
219 } _=>our $prop_spec = {};
220 }
221 10 our $prop_spec->{format} = $format;
222 10 ▶ if(my @args = match { our $parser } _=> shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@args) = do { our $parser }->match("_", shift())) 5 5 10
223 5 require CSS'DOM'Value;
224 5 CSS'DOM'Value'_apply_args_to_self(
225 $self, $owner, $prop,
226 @args, format => $format,
227 );
228 }
229 }
230  
231 # This is never reached, at least not when CSS::DOM’s mod-
232 # ules call the constructor:
233 elsif(!defined $prop) {
234 require CSS'DOM'Exception,
235 0 die new CSS'DOM'Exception
236 NO_MODIFICATION_ALLOWED_ERR,
237 ref($self) . " objects that do not know to which "
238 ."property they belong cannot be modified"
239 }
240  
241 # sub-values of a list
242 elsif(defined(my $index = $$self[indx])) {
243 9 my $old_list
244 = $owner->getPropertyCSSValue($prop);
245 # ~~~ What do we do if $old_list is undef?
246 # In what circumstances can
247 # that happen?
248 # ~~~ If we add an API to PropertyParser to allow
249 # for list sub-value formats, we can do away
250 # with this inefficient mess.
251 9 my $length = $old_list->length;
252 my @arsg
253 = $owner->property_parser->match(
254 $prop,
255 join $old_list->{s}, # ~~~ we probably need an
256 # API to avoid this encap viol
257 9 map(
258 $old_list->item($_)->cssText, 0..$index-1
259 ),
260 $_[0],
261 map(
262 $old_list->item($_)->cssText,
263 $index+1..$length-1
264 ),
265 );
266 9 require CSS'DOM'Value;
267 9 CSS'DOM'Value'_load_if_necessary($arsg[1]);
268 9 my $list = $arsg[1]->new(
269 owner => $owner,
270 property => $prop,
271 @arsg[2..$#arsg]
272 );
273 9 ▶ if($list->length != $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->length != $length) 0 9 9
274 # This would mean we were given a
275 # string with commas or a blank
276 # string, which are invalid.
277 0 return $old
278 }
279 9 @$self = @{ $list->item($index) };
280 }
281  
282 # property-level values
283 elsif(
284 my @arsg
285 = $owner->property_parser->match($prop, $_[0])
286 ) {
287 39 require CSS'DOM'Value;
288 39 CSS'DOM'Value'_apply_args_to_self(
289 $self, $owner, $prop, @arsg
290 );
291 }
292  
293 100 ▶ if(my $mh = $owner->modification_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mh = $owner->modification_handler) 16 84 100
294 16 &$mh();
295 }
296 }
297 378 $old;
298 }
299  
300 sub _serialise {
301 173 ▶ my ($type, $val) = @_;
Subroutine _serialise: called CC=13 SCAR=25.7
302 173 for($type) {
303 9 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
304 return
305 $_ == CSS_ATTR
306 ? 'attr(' . $val . ')'
307 : $_ == CSS_URI
308 ? 'url(' . $val. ')'
309 : $_ == CSS_RECT
310 ? die "_serialise does not support rects"
311 : $_ == CSS_RGBCOLOR
312 ? die "_serialise does not support colours"
313 : $_ == CSS_STRING
314 173 ▶ ? do {
Branch: 5 branches
BranchTrueFalseTotal
$_ == 22 ? : 5 168 173
$_ == 20 ? : 7 161 168
$_ == 24 ? : 0 161 161
$_ == 25 ? : 0 161 161
$_ == 19 ? : 10 151 161
315 10 (my $str = $val) =~ s/'/\\'/g;;
316 10 ▶ return "'$str'";
Branch: 6 branches
BranchTrueFalseTotal
$_ == 23 ? : 4 147 151
defined $val->[1] ? : 2 2 4
defined $val->[2] ? : 2 2 4
$_ == 18 ? : 1 146 147
$_ == 1 ? : 42 104 146
$unit_suffixes[$_] ? : 94 10 104
317 }
318 : $_ == CSS_COUNTER
319 ? 'counter' . 's' x defined($$val[sepa]) . '('
320 . escape_ident($$val[name])
321 . (defined $$val[sepa]
322 ? ", " . escape_str($$val[sepa])
323 : '' )
324 . (defined $$val[styl]
325 ? ", " . escape_ident($$val[styl])
326 : '' )
327 . ")"
328 : $_ == CSS_DIMENSION
329 ? $$val[0].escape_ident$$val[1]
330 : $_ == CSS_NUMBER
331 ? 0+$val
332 : $unit_suffixes[$_]
333 ? 0+$val . $unit_suffixes[$_]
334 : $val;
335 }
336  
337 }
338  
339 67 ▶sub cssValueType { CSS::DOM::Value::CSS_PRIMITIVE_VALUE }
Subroutine cssValueType: called CC=1 SCAR=0.0
Pod documented
340  
341 49 ▶sub primitiveType { shift->[type] }
Subroutine primitiveType: called CC=1 SCAR=0.0
Pod documented
342  
343 sub setFloatValue {
344 96 ▶ my ($self,$type,$val) = @'_;
Subroutine setFloatValue: called CC=7 SCAR=19.5
Pod undocumented
345  
346 require CSS'DOM'Exception,
347 96 ▶ die new CSS'DOM'Exception INVALID_ACCESS_ERR, "Invalid value type"
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 or $type == 23 or $type == 24 or $type == 25 or $type == 18 4 86 90
Condition100%12/12
$type == 0 or $type == 23 or $type == 24 or $type == 25 or $type == 18
l!l&&r!l&&!r
8286
$type == 0 or $type == 23 or $type == 24 or $type == 25
l!l&&r!l&&!r
6288
$type == 0 or $type == 23 or $type == 24
l!l&&r!l&&!r
4290
$type == 0 or $type == 23
l!l&&r!l&&!r
2292
MC/DC100%5/5
$type == 0 or $type == 23 or $type == 24 or $type == 25 or $type == 18
$type == 0 $type == 23 $type == 24 $type == 25 $type == 18
Truth table100%6/6
A or B or C or D or E
A= $type == 0B= $type == 23C= $type == 24D= $type == 25E= $type == 18
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
348 if $type == CSS_UNKNOWN || $type == CSS_COUNTER
349 || $type == CSS_RECT || $type == CSS_RGBCOLOR || $type == CSS_DIMENSION;
350  
351 # This is not particularly efficient, but I doubt anyone is actually
352 # using this API.
353 9 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
354 86 $self->cssText(my $css = _serialise($type, $val));
355 require CSS'DOM'Exception,
356 84 ▶ die new CSS'DOM'Exception INVALID_ACCESS_ERR, "Invalid value: $css"
Branch: 1 branch
BranchTrueFalseTotal
if $self->cssText ne $css 42 42 84
357 if $self->cssText ne $css;
358 _:
359 }
360  
361 sub getFloatValue {
362 44 ▶ my $self = shift;
Subroutine getFloatValue: called CC=10 SCAR=23.0
Pod documented
363  
364 # There are more types that are numbers than are not, so we
365 # invert our list.
366 44 my $type = $self->[type];
367 require CSS'DOM'Exception,
368 44 ▶ die new CSS'DOM'Exception INVALID_ACCESS_ERR, "Not a numeric value"
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22 or $type == 23 or $type == 24 or $type == 25 5 33 38
Condition95%20/21
$type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22 or $type == 23 or $type == 24 or $type == 25
l!l&&r!l&&!r
7433
$type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22 or $type == 23 or $type == 24
l!l&&r!l&&!r
6137
$type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22 or $type == 23
l!l&&r!l&&!r
4238
$type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22
l!l&&r!l&&!r
3140
$type == 0 or $type == 19 or $type == 20 or $type == 21
l!l&&r!l&&!r
2141
$type == 0 or $type == 19 or $type == 20
l!l&&r!l&&!r
1142
$type == 0 or $type == 19
l!l&&r!l&&!r
0143
MC/DC87%7/8
$type == 0 or $type == 19 or $type == 20 or $type == 21 or $type == 22 or $type == 23 or $type == 24 or $type == 25
$type == 0 $type == 19 $type == 20 $type == 21 $type == 22 $type == 23 $type == 24 $type == 25
Truth table88%8/9
A or B or C or D or E or F or G or H
A= $type == 0B= $type == 19C= $type == 20D= $type == 21E= $type == 22F= $type == 23G= $type == 24H= $type == 25
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
369 if $type == CSS_UNKNOWN || $type == CSS_STRING || $type == CSS_URI
370 || $type == CSS_IDENT || $type == CSS_ATTR || $type == CSS_COUNTER
371 || $type == CSS_RECT || $type == CSS_RGBCOLOR;
372  
373 9 ▶ no warnings"numeric";
Subroutine BEGIN: called CC=1 SCAR=0.0
374 33 ▶ 0+($type == CSS_DIMENSION ? $$self[valu][0] : $$self[valu])
Branch: 1 branch
BranchTrueFalseTotal
$type == 18 ? : 1 32 33
375 }
376  
377 9 *setStringValue = *setFloatValue;
378  
379 sub getStringValue {
380 29 ▶ my $self = shift;
Subroutine getStringValue: called CC=5 SCAR=16.1
Pod undocumented
381  
382 29 my $type = $self->[type];
383 require CSS'DOM'Exception,
384 29 ▶ die new CSS'DOM'Exception INVALID_ACCESS_ERR, "Not a string value"
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 19 or $type == 20 or $type == 21 or $type == 22 25 4 29
Condition100%9/9
$type == 19 or $type == 20 or $type == 21 or $type == 22
l!l&&r!l&&!r
3125
$type == 19 or $type == 20 or $type == 21
l!l&&r!l&&!r
2126
$type == 19 or $type == 20
l!l&&r!l&&!r
1127
MC/DC100%4/4
$type == 19 or $type == 20 or $type == 21 or $type == 22
$type == 19 $type == 20 $type == 21 $type == 22
Truth table100%5/5
A or B or C or D
A= $type == 19B= $type == 20C= $type == 21D= $type == 22
ABCDresult
1XXX1
01XX1
001X1
00011
00000
385 unless $type == CSS_STRING || $type == CSS_URI
386 || $type == CSS_IDENT || $type == CSS_ATTR;
387  
388 4 "$$self[valu]"
389 }
390  
391 # ------------- Rect interface --------------- #
392  
393 sub _autoviv_rect_value {
394 13 ▶ my($self,$index) = @_;
Subroutine _autoviv_rect_value: called CC=3 SCAR=11.0
395 13 for my $val($$self[valu][$index]) {
396 13 ▶ if(ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 12 1 13
397 12 $val = new
398 __PACKAGE__,
399 owner => $$self[ownr],
400 format => '<length>|auto',
401 @$val;
402 12 delete $$self[csst]; # prevent this from being used by cssText; hence-
403 } # forth we must use the subvalues
404 13 return $val
405 }
406 }
407  
408 4 ▶sub top { _autoviv_rect_value $_[0], 0 }
Subroutine top: called CC=1 SCAR=0.0
Pod undocumented
409 3 ▶sub right { _autoviv_rect_value $_[0], 1 }
Subroutine right: called CC=1 SCAR=0.0
Pod undocumented
410 3 ▶sub bottom { _autoviv_rect_value $_[0], 2 }
Subroutine bottom: called CC=1 SCAR=0.0
Pod undocumented
411 3 ▶sub left { _autoviv_rect_value $_[0], 3 }
Subroutine left: called CC=1 SCAR=0.0
Pod undocumented
412  
413 # ------------- RGBColor interface --------------- #
414  
415 sub _autoviv_colour_value {
416 50 ▶ my($self,$index) = @_;
Subroutine _autoviv_colour_value: called CC=9 SCAR=22.0
417 50 ▶ if(ref $$self[valu] ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->[3] ne "ARRAY") 4 46 50
418 4 ▶ if($$self[valu] =~ /^#(..|.)(..|.)(..|.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] =~ /^#(..|.)(..|.)(..|.)/) { } 3 1 4
419 3 my $x = -length($1) + 3;
420 3 $$self[sfrm] = '#' . length $1;
421 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
422 3 $$self[valu] = [
423 map([type => CSS_NUMBER, value => hex $$_ x$x], 1...3),
424 ];
425 }
426 else {
427 1 ▶ our %Colours or require "CSS/DOM/Value/Primitive/colours.pl";
Branch: 1 branch
BranchTrueFalseTotal
our %Colours or require "CSS/DOM/Value/Primitive/colours.pl" 1 0 1
428 1 my $rgb = $Colours{lc($$self[sfrm] = $$self[valu])};
429 1 $$self[valu] = [
430 map
431 [type => CSS_NUMBER, value => $_],
432 @$rgb
433 ];
434 }
435 }
436 50 for my $val($$self[valu][$index]) {
437 50 ▶ if(ref $val eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 33 17 50
elsif (not defined $val and $index == 3) { } 6 22 28
438 33 ▶ $val = new
Branch: 1 branch
BranchTrueFalseTotal
$index == 3 ? : 3 30 33
439 __PACKAGE__,
440 owner => $$self[ownr],
441 format => $index == 3 ? '<number>' : '<number>|<percentage>',
442 @$val;
443 33 delete $$self[csst];
444 }
445 ▶ elsif(!defined $val and $index == 3) { # alpha
Condition66%2/3
not defined $val and $index == 3
!ll&&!rl&&r
1106
MC/DC50%1/2
not defined $val and $index == 3
not defined $val $index == 3
Truth table66%2/3
not(A) and B
A= not defined $valB= $index == 3
ABresult
0X0
100
111
446 6 $val = new
447 __PACKAGE__,
448 owner => $$self[ownr],
449 format => '<number>',
450 type => CSS_NUMBER,
451 value => 1;
452 6 delete $$self[csst];
453 }
454 50 return $val
455 }
456 }
457  
458 14 ▶sub red { _autoviv_colour_value $_[0], 0 }
Subroutine red: called CC=1 SCAR=0.0
Pod undocumented
459 13 ▶sub green { _autoviv_colour_value $_[0], 1 }
Subroutine green: called CC=1 SCAR=0.0
Pod undocumented
460 13 ▶sub blue { _autoviv_colour_value $_[0], 2 }
Subroutine blue: called CC=1 SCAR=0.0
Pod undocumented
461 10 ▶sub alpha { _autoviv_colour_value $_[0], 3 }
Subroutine alpha: called CC=1 SCAR=0.0
Pod undocumented
462  
463 9 !()__END__()!
464  
465 =head1 NAME
466  
467 CSS::DOM::Value::Primitive - CSSPrimitiveValue class for CSS::DOM
468  
469 =head1 VERSION
470  
471 Version 0.16
472  
473 =head1 SYNOPSIS
474  
475 # ...
476  
477 =head1 DESCRIPTION
478  
479 This module implements objects that represent CSS primitive property
480 values (as opposed to lists). It
481 implements the DOM CSSPrimitiveValue, Rect, and RGBColor interfaces.
482  
483 =head1 METHODS
484  
485 If you need the constructor, it's below the object methods. Normally you
486 would get an object via L<CSS::DOM::Style's C<getPropertyCSSValue>
487 method|CSS::DOM::Style/getPropertyCSSValue>.
488  
489 =head2 CSSValue Interface
490  
491 =over 4
492  
493 =item cssText
494  
495 Returns a string representation of the attribute. Pass an argument to set
496 it.
497  
498 =item cssValueType
499  
500 Returns C<CSS::DOM::Value::CSS_PRIMITIVE_VALUE>.
501  
502 =back
503  
504 =head2 CSSPrimitiveValue Interface
505  
506 =over
507  
508 =item primitiveType
509  
510 Returns one of the L</CONSTANTS> listed below.
511  
512 =item getFloatValue
513  
514 Returns a number if the value is numeric.
515  
516 =back
517  
518 The rest have still to be implemented.
519  
520 =head2 Rect Interface
521  
522 The four methods C<top>, C<right>, C<bottom> and C<left> each return
523 another
524 value object representing the individual value.
525  
526 =head2 RGBColor Interface
527  
528 The four methods C<red>, C<green>, C<blue> and C<alpha> each return another
529 value object representing the individual value.
530  
531 =head2 Constructor
532  
533 You probably don't need to call this, but here it is anyway:
534  
535 $val = new CSS::DOM::Value::Primitive:: %args;
536  
537 The hash-style arguments are as follows. Only C<type> and C<value> are
538 required.
539  
540 =over
541  
542 =item type
543  
544 One of the constants listed below under L</CONSTANTS>
545  
546 =item value
547  
548 The data stored inside the value object. The format expected depends on the
549 type. See below.
550  
551 =item css
552  
553 CSS code used for serialisation. This will make reading C<cssText> faster
554 at least until the value is modified.
555  
556 =item owner
557  
558 The style object that owns this value; if this is omitted, then the value
559 is read-only. The value object holds a weak reference to the owner.
560  
561 =item property
562  
563 The name of the CSS property to which this value belongs. C<cssText> uses
564 this to determine how to parse text passed to it. This does not
565 apply to the sub-values of colours, counters and rects, but it I<does>
566 apply to individual elements of a list value.
567  
568 =item index
569  
570 The index of this value within a list value (only applies to elements of a
571 list, of course).
572  
573 =item format
574  
575 This is used by sub-values of colours and rects. It determines
576 how assignment to C<cssText> is handled. This uses the same syntax as the
577 formats in L<CSS::DOM::PropertyParser|CSS::DOM::PropertyParser/format>.
578  
579 =back
580  
581 Here are the formats for the C<value> argument, which depend on the type:
582  
583 =over
584  
585 =item CSS_UNKNOWN
586  
587 A string of CSS code.
588  
589 =item CSS_NUMBER, CSS_PERCENTAGE
590  
591 A simple scalar containing a number.
592  
593 =item Standard Dimensions
594  
595 Also a simple scalar containing a number.
596  
597 This applies to C<CSS_EMS>, C<CSS_EXS>, C<CSS_PX>, C<CSS_CM>, C<CSS_MM>, C<CSS_IN>, C<CSS_PT>, C<CSS_PC>, C<CSS_DEG>, C<CSS_RAD>, C<CSS_GRAD>, C<CSS_MS>, C<CSS_S>, C<CSS_HZ> and C<CSS_KHZ>.
598  
599 =item CSS_DIMENSION
600  
601 An array ref: C<[$number, $unit_text]>
602  
603 =item CSS_STRING
604  
605 A simple scalar containing a string (not a CSS string literal; i.e., no
606 quotes or escapes).
607  
608 =item CSS_URI
609  
610 The URL (not a CSS literal)
611  
612 =item CSS_IDENT
613  
614 A string (no escapes)
615  
616 =item CSS_ATTR
617  
618 A string containing the name of the attribute.
619  
620 =item CSS_COUNTER
621  
622 An array ref: C<[$name, $separator, $style]>
623  
624 C<$separator> and C<$style> may each be C<undef>. If C<$separator> is
625 C<undef>, the object represents a C<counter(...)>. Otherwise it represents
626 C<counters(...)>.
627  
628 =item CSS_RECT
629  
630 An array ref: C<[$top, $right, $bottom, $left]>
631  
632 The four elements are either CSSValue objects or
633 array refs of arguments to be passed to the constructor. E.g.:
634  
635 [
636 [type => CSS_PX, value => 20],
637 [type => CSS_PERCENTAGE, value => 50],
638 [type => CSS_PERCENTAGE, value => 50],
639 [type => CSS_PX, value => 50],
640 ]
641  
642 When these array refs are converted to objects, the C<format>
643 argument is supplied automatically, so you do not need to include it here.
644  
645 =item CSS_RGBCOLOR
646  
647 A string beginning with '#', with no escapes (such as '#fff' or '#c0ffee'),
648 a colour name (like red) or an array ref with three to four elements:
649  
650 [$r, $g, $b]
651 [$r, $g, $b, $alpha]
652  
653 The elements are either CSSValue objects or array refs of
654 argument lists, as with C<CSS_RECT>.
655  
656 =back
657  
658 =head1 CONSTANTS
659  
660 The following constants can be imported with
661 C<use CSS::DOM::Value::Primitive ':all'>.
662 They represent the type of primitive value.
663  
664 =over
665  
666 =item CSS_UNKNOWN
667  
668 =item CSS_NUMBER
669  
670 =item CSS_PERCENTAGE
671  
672 =item CSS_EMS
673  
674 =item CSS_EXS
675  
676 =item CSS_PX
677  
678 =item CSS_CM
679  
680 =item CSS_MM
681  
682 =item CSS_IN
683  
684 =item CSS_PT
685  
686 =item CSS_PC
687  
688 =item CSS_DEG
689  
690 =item CSS_RAD
691  
692 =item CSS_GRAD
693  
694 =item CSS_MS
695  
696 =item CSS_S
697  
698 =item CSS_HZ
699  
700 =item CSS_KHZ
701  
702 =item CSS_DIMENSION
703  
704 =item CSS_STRING
705  
706 =item CSS_URI
707  
708 =item CSS_IDENT
709  
710 =item CSS_ATTR
711  
712 =item CSS_COUNTER
713  
714 =item CSS_RECT
715  
716 =item CSS_RGBCOLOR
717  
718 =back
719  
720 =head1 SEE ALSO
721  
722 L<CSS::DOM>
723  
724 L<CSS::DOM::Value>
725  
726 L<CSS::DOM::Value::List>
727  
728 L<CSS::DOM::Style>
« DOM/Parser.pm ↑ Summary DOM/PropertyParser.pm »