SAC/Condition.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 33.3% 2 / 6 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 80.7% 42 / 52 CC 6 SCAR 10.5
CC
6
Cov
82%
ConditionType
6.0
import
4.0
new
2.0
CRAP 2.9
« SAC/SelectorList.pm ↑ Summary SAC/Selector/Sibling.pm »
1
2 ###
3 # CSS::SAC::Condition - base class for SAC conditions
4 # Robin Berjon <robin@knowscape.com>
5 # 24/02/2001
6 ###
7
8 package CSS::SAC::Condition;
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶$VERSION = $CSS::SAC::VERSION || '0.03';
Condition50%1/2
$CSS::SAC::VERSION || '0.03'
l!l
02
MC/DC0%0/1
$CSS::SAC::VERSION || '0.03'
$CSS::SAC::VERSION
Truth table50%1/2
A
A= $CSS::SAC::VERSION
Aresult
11
00
12
13 #---------------------------------------------------------------------#
14 # build the fields for an array based object
15 #---------------------------------------------------------------------#
16 2 ▶use Class::ArrayObjects define => {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 fields => [qw(_type_)],
18 2 };
19 #---------------------------------------------------------------------#
20
21
22 ### Constants #########################################################
23 # #
24 # #
25
26 sub UNKNOWN_CONDITION () { 1 }
27 sub AND_CONDITION () { 2 }
28 sub ATTRIBUTE_CONDITION () { 3 }
29 sub BEGIN_HYPHEN_ATTRIBUTE_CONDITION () { 4 }
30 sub CLASS_CONDITION () { 5 }
31 sub CONTENT_CONDITION () { 6 }
32 sub ID_CONDITION () { 7 }
33 sub LANG_CONDITION () { 8 }
34 sub NEGATIVE_CONDITION () { 9 }
35 sub ONE_OF_ATTRIBUTE_CONDITION () { 10 }
36 sub ONLY_CHILD_CONDITION () { 11 }
37 sub ONLY_TYPE_CONDITION () { 12 }
38 sub OR_CONDITION () { 13 }
39 sub POSITIONAL_CONDITION () { 14 }
40 sub PSEUDO_CLASS_CONDITION () { 15 }
41
42 # new non-standard conditions for CSS3 selectors
43 sub STARTS_WITH_ATTRIBUTE_CONDITION () { 16 } # [attr^='string']
44 sub ENDS_WITH_ATTRIBUTE_CONDITION () { 17 } # [attr$='string']
45 sub CONTAINS_ATTRIBUTE_CONDITION () { 18 } # [attr*='string']
46 sub IS_ROOT_CONDITION () { 19 } # :root
47 sub IS_EMPTY_CONDITION () { 20 } # :empty
48
49 #---------------------------------------------------------------------#
50 # import()
51 # all import can do is export the constants
52 #---------------------------------------------------------------------#
53 4 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
54 4 my $class = shift;
55 4 ▶ my $tag = shift || '';
Condition50%1/2
shift() || ''
l!l
40
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56
57 # check that we have the right tag
58 4 ▶ return unless $tag eq ':constants';
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq ":constants" 0 4 4
59
60 # define some useful vars
61 4 my $pkg = caller;
62 4 my @constants = qw(
63 UNKNOWN_CONDITION
64 AND_CONDITION
65 ATTRIBUTE_CONDITION
66 BEGIN_HYPHEN_ATTRIBUTE_CONDITION
67 CLASS_CONDITION
68 CONTENT_CONDITION
69 ID_CONDITION
70 LANG_CONDITION
71 NEGATIVE_CONDITION
72 ONE_OF_ATTRIBUTE_CONDITION
73 ONLY_CHILD_CONDITION
74 ONLY_TYPE_CONDITION
75 OR_CONDITION
76 POSITIONAL_CONDITION
77 PSEUDO_CLASS_CONDITION
78
79 STARTS_WITH_ATTRIBUTE_CONDITION
80 ENDS_WITH_ATTRIBUTE_CONDITION
81 CONTAINS_ATTRIBUTE_CONDITION
82 IS_ROOT_CONDITION
83 IS_EMPTY_CONDITION
84 );
85
86 # now lets create the constants in the caller's package
87 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 4 for my $c (@constants) {
89 80 my $qname = "${pkg}::$c";
90 80 *$qname = \&{$c};
91 }
92 }
93 #---------------------------------------------------------------------#
94
95
96 # #
97 # #
98 ### Constants #########################################################
99
100
101 ### Constructor #######################################################
102 # #
103 # #
104
105
106 #---------------------------------------------------------------------#
107 # CSS::SAC::Condition->new($type)
108 # creates a new sac condition object
109 #---------------------------------------------------------------------#
110 38 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
111 38 ▶ my $class = ref($_[0])?ref(shift):shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 38 38
112 38 my $type = shift;
113 38 return bless [$type], $class;
114 }
115 #---------------------------------------------------------------------#
116
117
118 # #
119 # #
120 ### Constructor #######################################################
121
122
123
124 ### Accessors #########################################################
125 # #
126 # #
127
128
129 #---------------------------------------------------------------------#
130 # my $type = $cond->ConditionType()
131 # $cond->ConditionType($type)
132 # get/set the condition type
133 #---------------------------------------------------------------------#
134 0 ▶sub ConditionType {
Subroutine ConditionType: not called CC=2 SCAR=17.9
Pod documented
135 0 ▶ (@_==2) ? $_[0]->[_type_] = $_[1] :
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
136 $_[0]->[_type_];
137 }
138 #---------------------------------------------------------------------#
139 2 *CSS::SAC::Condition::getConditionType = \&ConditionType;
140
141
142 #---------------------------------------------------------------------#
143 # $cond->is_type($condition_constant)
144 # returns true is this condition is of type $condition_constant
145 #---------------------------------------------------------------------#
146 38 ▶sub is_type {
Subroutine is_type: called CC=1 SCAR=0.0
Pod documented
147 38 return $_[0]->[_type_] == $_[1];
148 }
149 #---------------------------------------------------------------------#
150
151
152 # #
153 # #
154 ### Accessors #########################################################
155
156
157
158 2 1;
159
160 =pod
161
162 =head1 NAME
163
164 CSS::SAC::Condition - base class for SAC conditions
165
166 =head1 SYNOPSIS
167
168 use CSS::SAC::Condition qw(:constants);
169 foo if $cond->is_type(CONDITION_TYPE_CONSTANT);
170
171 =head1 DESCRIPTION
172
173 SAC Conditions describe conditions that can be expressed in CSS such
174 as AttributeConditions or PositionalConditions. This class provides
175 everything that is needed to implement simple conditions (methods,
176 constants) as well as what is needed by subclasses defining more
177 complex conditions.
178
179 The constants are those defined in the SAC spec, with the leading SAC_
180 removed. What the constants map to is to be considered an opaque token
181 that can be tested for equality. If there is demand for it, I will add
182 a way to add new constants (for people wishing to define new condition
183 types).
184
185 I have also added the UNKNOWN_CONDITION constant. It shouldn't occur
186 in normal processing but it's always useful to have such fallback
187 values.
188
189 The Condition interface adds $cond->is_type($condition_type) to the
190 interface defined in the SAC spec. This allows for more flexible type
191 checking. For instance, if you create a subclass of ContentCondition
192 that extends it with the ContentRegexCondition interface you will
193 probably want software ignorant of your subclass's existence to still
194 be able to do something useful with it. That software should also be
195 able to treat ContentRegexConditions as if they were
196 ContentConditions.
197
198 If that software tests condition types the following way:
199
200 $rcond->ConditionType == CONTENT_CONDITION
201
202 then you've lost because the condition type of ContentRegexCondition
203 is REGEX_CONTENT_CONDITION. If, however, it tests it that way:
204
205 $rcond->is_type(CONTENT_CONDITION)
206
207 then you can simply implement is_type() so that it returns true for
208 it's own type and the type of it's superclass. I strongly recommend
209 using the latter scheme except in cases when you want to know the
210 exact type.
211
212 =head1 CONSTANTS
213
214 =over 4
215
216 =item * UNKNOWN_CONDITION
217
218 =item * AND_CONDITION
219
220 =item * ATTRIBUTE_CONDITION
221
222 =item * BEGIN_HYPHEN_ATTRIBUTE_CONDITION
223
224 =item * CLASS_CONDITION
225
226 =item * CONTENT_CONDITION
227
228 =item * ID_CONDITION
229
230 =item * LANG_CONDITION
231
232 =item * NEGATIVE_CONDITION
233
234 =item * ONE_OF_ATTRIBUTE_CONDITION
235
236 =item * ONLY_CHILD_CONDITION
237
238 =item * ONLY_TYPE_CONDITION
239
240 =item * OR_CONDITION
241
242 =item * POSITIONAL_CONDITION
243
244 =item * PSEUDO_CLASS_CONDITION
245
246 =back
247
248 =head1 METHODS
249
250 =over 4
251
252 =item * CSS::SAC::Condition->new($type) or $cond->new($type)
253
254 Creates a new condition. The $type must be one of the type constants.
255
256 =item * $cond->ConditionType()
257
258 Returns the constant corresponding to the type of this condition.
259
260 =item * $cond->is_type($condition_type)
261
262 Returns a boolean indicating whether this condition is of type
263 $condition_type (a condition constant).
264
265 =back
266
267 =head1 AUTHOR
268
269 Robin Berjon <robin@knowscape.com>
270
271 This module is licensed under the same terms as Perl itself.
272
273 =cut
274
275
« SAC/SelectorList.pm ↑ Summary SAC/Selector/Sibling.pm »