XUL/Style.pm

filter: Statementstmt 93.2% 83 / 89 Branchbran 80.0% 32 / 40 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 5 totaltotal 82.1% 143 / 174 CC 26 SCAR 19.6
CC
26
Cov
86%
parse
14.5
set
9.0
get
8.1
CRAP 7.1
« XUL/Controler.pm ↑ Summary XUL/Node.pm »
1 package POE::XUL::Style;
2 # $Id$
3 # Copyright Philip Gwyn 2008-2010. All rights reserved.
4  
5 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶use overload '""' => sub { $_[0]->as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 11 ▶ '+' => sub { refaddr $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 76 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 21 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 21 our $VERSION = '0.0600';
20  
21 21 my %EQUIV = qw(
22 border-top border
23 border-left border
24 border-bottom border
25 border-right border
26 overflow-x overflow
27 overflow-y overflow
28 -moz-outline outline
29 );
30  
31 21 my %SUBSET = ( # property ... offsets
32 'margin-top' => [ 'margin', 0, 0, 0, 0 ],
33 'margin-right' => [ 'margin', 0, 1, 1, 1 ],
34 'margin-bottom' => [ 'margin', 0, 0, 2, 2 ],
35 'margin-left' => [ 'margin', 0, 1, 1, 3 ],
36 'padding-top' => [ 'padding', 0, 0, 0, 0 ],
37 'padding-right' => [ 'padding', 0, 1, 1, 1 ],
38 'padding-bottom' => [ 'padding', 0, 0, 2, 2 ],
39 'padding-left' => [ 'padding', 0, 1, 1, 3 ],
40 'border-width' => [ 'border', 0, 0, 0 ],
41 'border-style' => [ 'border', 1, 1, 1 ],
42 'border-color' => [ 'border', 2, 2, 2 ],
43 'border-top-width' => [ 'border-top', 0, 0, 0 ],
44 'border-top-style' => [ 'border-top', 1, 1, 1 ],
45 'border-top-color' => [ 'border-top', 2, 2, 2 ],
46 'border-right-width' => [ 'border-right', 0, 0, 0 ],
47 'border-right-style' => [ 'border-right', 1, 1, 1 ],
48 'border-right-color' => [ 'border-right', 2, 2, 2 ],
49 'border-bottom-width' => [ 'border-bottom', 0, 0, 0 ],
50 'border-bottom-style' => [ 'border-bottom', 1, 1, 1 ],
51 'border-bottom-color' => [ 'border-bottom', 2, 2, 2 ],
52 'border-left-width' => [ 'border-left', 0, 0, 0 ],
53 'border-left-style' => [ 'border-left', 1, 1, 1 ],
54 'border-left-color' => [ 'border-left', 2, 2, 2 ],
55 'outline-width' => [ 'outline', 0, 0, 0 ],
56 'outline-style' => [ 'outline', 1, 1, 1 ],
57 'outline-color' => [ 'outline', 2, 2, 2 ],
58 '-moz-outline-width' => [ '-moz-outline', 0, 0, 0 ],
59 '-moz-outline-style' => [ '-moz-outline', 1, 1, 1 ],
60 '-moz-outline-color' => [ '-moz-outline', 2, 2, 2 ],
61 'list-style-type' => [ 'list-style', 0, 0, 0 ],
62 'list-style-position' => [ 'list-style', 1, 1, 1 ],
63 'list-style-image' => [ 'list-style', 2, 2, 2 ],
64 # http://developer.mozilla.org/en/docs/CSS:-moz-border-radius says:
65 # "If fewer than 4 values are given, the list of values is repeated
66 # to fill the remaining values."
67 # I take this to mean:
68 # 1 -> tl=1 tr=1 br=1 bl=1
69 # 1 2 -> tl=1 tr=2 br=1 bl=2
70 # 1 2 3 -> tl=1 tr=2 br=3 bl=1
71 '-moz-border-radius-topleft' => [ '-moz-border-radius', 0, 0, 0, 0 ],
72 '-moz-border-radius-topright' => [ '-moz-border-radius', 0, 1, 1, 1 ],
73 '-moz-border-radius-bottomright' => [ '-moz-border-radius', 0, 0, 2, 2 ],
74 '-moz-border-radius-bottomleft' => [ '-moz-border-radius', 0, 1, 0, 3 ],
75 );
76  
77 ##############################################################
78 sub new
79 {
80 19 ▶ my( $package, $init ) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
81 19 my $self = bless { properties => {}, text => [] }, $package;
82 19 ▶ $self->parse( $init ) if $init;
Branch: 1 branch
BranchTrueFalseTotal
if $init 16 3 19
83 19 return $self;
84 }
85  
86 ##############################################################
87 sub as_string
88 {
89 19 ▶ my( $self ) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
90 19 return join '', @{ $self->{text} };
91 }
92  
93 ##############################################################
94 sub parse
95 {
96 17 ▶ my( $self, $string ) = @_;
Subroutine parse: called CC=8 SCAR=26.7
Pod undocumented
97 17 ▶ return unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 17 17
98 # TODO : add ; to last property text
99 17 ▶ while( $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string) 42 17 59
100 # line starts with a comment
101 42 ▶ if( $string =~ s,^(\s*/\*[^*]*\*+([^/*][^*]*\*+)*/\s*),,s ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ s[^(\s*/\*[^*]*\*+([^/*][^*]*\*+)*/\s*)][]s) { } 4 38 42
elsif ($string =~ s/^(\s+)//s) { } 0 38 38
elsif ($string =~ s/^((-?[_a-z][-_a-zA-Z]*)\s*:\s*(.*?)\s*(\Z|;\s*))//is) { } 38 0 38
102 4 push @{ $self->{text} }, $1;
103 }
104 # line start with whitespace
105 elsif( $string =~ s,^(\s+),,s ) {
106 0 my $ws = $1;
107 0 ▶ if( @{ $self->{text} } and $self->{text}[-1] =~ /\s+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'text'};} and $self->{'text'}[-1] =~ /\s+$/) { } 0 0 0
Condition0%0/3
@{$self->{"text"};} and $self->{"text"}[-1] =~ /\s+$/
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->{"text"};} and $self->{"text"}[-1] =~ /\s+$/
@{$self->{"text"};} $self->{"text"}[-1] =~ /\s+$/
Truth table0%0/3
A and B
A= @{$self->{"text"};}B= $self->{"text"}[-1] =~ /\s+$/
ABresult
0X0
100
111
108 0 $self->{text}[-1] .= $ws;
109 }
110 else {
111 0 push @{ $self->{text} }, $ws;
112 }
113 }
114 # property: value
115 # Note this fails for property: "some; value"; please DON'T DO THAT
116 elsif( $string =~ s,^((-?[_a-z][-_a-zA-Z]*)\s*:\s*(.*?)\s*(\Z|;\s*)),,is ) {
117 38 push @{ $self->{text} }, $1;
118 $self->{prop}{lc $2} = {
119 # name => lc( $2 ),
120 38 text=>\$self->{text}[-1],
121 value => $3
122 };
123 }
124 }
125 }
126  
127 ##############################################################
128 sub get
129 {
130 70 ▶ my( $self, $key ) = @_;
Subroutine get: called CC=8 SCAR=20.9
Pod undocumented
131 70 $key = lc $key;
132 70 my $rv;
133 70 ▶ if( $self->{prop}{ $key } ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'prop'}{$key}) { } 37 33 70
elsif ($EQUIV{$key} and $self->{'prop'}{$EQUIV{$key}}) { } 3 30 33
elsif ($SUBSET{$key}) { } 26 4 30
134 37 $rv = $self->{prop}{ $key }{value};
135 }
136 ▶ elsif( $EQUIV{$key} and $self->{prop}{ $EQUIV{$key} } ) {
Condition66%2/3
$EQUIV{$key} and $self->{"prop"}{$EQUIV{$key}}
!ll&&!rl&&r
3003
MC/DC50%1/2
$EQUIV{$key} and $self->{"prop"}{$EQUIV{$key}}
$EQUIV{$key} $self->{"prop"}{$EQUIV{$key}}
Truth table66%2/3
A and B
A= $EQUIV{$key}B= $self->{"prop"}{$EQUIV{$key}}
ABresult
0X0
100
111
137 3 $rv = $self->{prop}{ $EQUIV{$key} }{value};
138 }
139 elsif( $SUBSET{ $key } ) {
140 26 my $subset = $SUBSET{$key};
141 26 my $value = $self->get( $subset->[0] );
142 26 ▶ if( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 26 0 26
143 26 my @values = split ' ', $value, $#$subset;
144 26 my $n = 0+@values;
145 26 ▶ $rv = $values[ $subset->[$n] ] if $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 0 26 0 26
146 }
147 }
148 70 ▶ $rv = '' unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 4 66 70
149 70 return $rv;
150 }
151  
152  
153 ##############################################################
154 sub set
155 {
156 18 ▶ my( $self, $key, $value ) = @_;
Subroutine set: called CC=9 SCAR=22.0
Pod undocumented
157 18 $key = lc $key;
158 18 my $prop = $self->{prop}{ $key };
159 18 $value =~ s/;\s*$//;
160 18 ▶ unless( $prop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop) { } 9 9 18
161 # special case...
162 9 ▶ return if !$value and $key eq 'display';
Branch: 1 branch
BranchTrueFalseTotal
if not $value and $key eq "display" 0 9 9
Condition66%2/3
not $value and $key eq "display"
!ll&&!rl&&r
810
MC/DC0%0/2
not $value and $key eq "display"
not $value $key eq "display"
Truth table66%2/3
not(A) and B
A= not $valueB= $key eq "display"
ABresult
0X0
100
111
163  
164 $self->{text}[-1] .= ";"
165 9 ▶ if @{$self->{text}} and $self->{text}[-1] !~ m([;/]\s*$)s;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"text"};} and not $self->{"text"}[-1] =~ m[[;/]\s*$]s 3 6 9
Condition100%3/3
@{$self->{"text"};} and not $self->{"text"}[-1] =~ m[[;/]\s*$]s
!ll&&!rl&&r
153
MC/DC100%2/2
@{$self->{"text"};} and not $self->{"text"}[-1] =~ m[[;/]\s*$]s
@{$self->{"text"};} not $self->{"text"}[-1] =~ m[[;/]\s*$]s
Truth table100%3/3
A and not(B)
A= @{$self->{"text"};}B= not $self->{"text"}[-1] =~ m[[;/]\s*$]s
ABresult
0X0
100
111
166 9 push @{ $self->{text} }, "$key: $value;\n";
167 $self->{prop}{ $key } = { value => $value,
168 # name => $key,
169 9 text => \$self->{text}[-1]
170 };
171 }
172 else {
173 # special case...
174 9 ▶ if( !$value and $key eq 'display' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value and $key eq 'display') { } 5 4 9
Condition66%2/3
not $value and $key eq "display"
!ll&&!rl&&r
405
MC/DC50%1/2
not $value and $key eq "display"
not $value $key eq "display"
Truth table66%2/3
not(A) and B
A= not $valueB= $key eq "display"
ABresult
0X0
100
111
175 5 ${ $prop->{text} } = '';
176 5 delete $self->{prop}{ lc $key };
177 }
178 else {
179 4 ${ $prop->{text} } =~ s/\Q$prop->{value}/$value/;
180 4 $prop->{value} = $value;
181 }
182 }
183 18 ▶ $POE::XUL::Node::CM->after_style_change( $self, $key, $value )
Branch: 1 branch
BranchTrueFalseTotal
if $POE::XUL::Node::CM 8 10 18
184 if $POE::XUL::Node::CM;
185 18 return;
186 }
187  
188 ##############################################################
189 sub AUTOLOAD
190 {
191 80 ▶ my( $self, $value ) = @_;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
192 80 my $key = our $AUTOLOAD;
193 80 ▶ return if $key =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /DESTROY$/ 18 62 80
194 62 $key =~ s/^.*:://;
195  
196 62 $key =~ s/([A-Z])/-\L$1/g;
197 62 ▶ if( 1 == @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_) { } 44 18 62
198 44 return $self->get( $key );
199 }
200 else {
201 18 return $self->set( $key, $value );
202 }
203
204 }
205  
206 21 1;
207  
208 __END__
« XUL/Controler.pm ↑ Summary XUL/Node.pm »