File Coverage

blib/lib/CSS/SAC/Condition.pm
Criterion Covered Total %
statement 25 26 96.1
branch 2 6 33.3
condition 1 2 50.0
subroutine 7 8 87.5
pod 3 3 100.0
total 38 45 84.4


line stmt bran cond sub pod time code
1            
2             ###
3             # CSS::SAC::Condition - base class for SAC conditions
4             # Robin Berjon
5             # 24/02/2001
6             ###
7            
8             package CSS::SAC::Condition;
9 2     2   11 use strict;
  2         2  
  2         77  
10 2     2   11 use vars qw($VERSION);
  2         5  
  2         124  
11             $VERSION = $CSS::SAC::VERSION || '0.03';
12            
13             #---------------------------------------------------------------------#
14             # build the fields for an array based object
15             #---------------------------------------------------------------------#
16 2         16 use Class::ArrayObjects define => {
17             fields => [qw(_type_)],
18 2     2   2058 };
  2         1310  
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             sub import {
54 4     4   34 my $class = shift;
55 4   50     16 my $tag = shift || '';
56            
57             # check that we have the right tag
58 4 50       14 return unless $tag eq ':constants';
59            
60             # define some useful vars
61 4         7 my $pkg = caller;
62 4         23 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     2   807 no strict 'refs';
  2         4  
  2         570  
88 4         11 for my $c (@constants) {
89 80         138 my $qname = "${pkg}::$c";
90 80         78 *$qname = \&{$c};
  80         12025  
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             sub new {
111 38 50   38 1 70 my $class = ref($_[0])?ref(shift):shift;
112 38         62 my $type = shift;
113 38         196 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             sub ConditionType {
135 0 0   0 1 0 (@_==2) ? $_[0]->[_type_] = $_[1] :
136             $_[0]->[_type_];
137             }
138             #---------------------------------------------------------------------#
139             *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             sub is_type {
147 38     38 1 365 return $_[0]->[_type_] == $_[1];
148             }
149             #---------------------------------------------------------------------#
150            
151            
152             # #
153             # #
154             ### Accessors #########################################################
155            
156            
157            
158             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
270            
271             This module is licensed under the same terms as Perl itself.
272            
273             =cut
274            
275