1
package CSS::DOM::Rule;
9
use CSS::DOM::Constants;
10
use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
11
use Exporter 5.57 'import';
12
use Scalar::Util 'weaken';
14
*EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
15
our %EXPORT_TAGS = (all => \our @EXPORT_OK);
17
use constant 1.03 our $_const = {
18
# Don’t let these conflict with subclasses!
21
typs => 2, # token types These two are not used by subclassed,
22
tokn => 3, # tokens so there’s no chance of a conflict.
24
{ no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
27
my $self = bless[],shift;
28
my $parent = shift || return $self;
29
if($parent->isa('CSS::DOM::Rule')) {
30
weaken($$self[shet] = $parent->parentStyleSheet);
31
weaken($$self[prnt] = $parent);
34
weaken($$self[shet] = $parent)
39
sub type { UNKNOWN_RULE }
41
# This is used by cssText, both this class’s and subclasses’:
42
sub _parse { # This method parses the code passed to it and checks to see
43
# whether the retval is the same class as $self, throwing
44
# errors as appropriate. It returns the new rule resulting
45
# from the parse. Each subclass is responsible for extorting
46
# the rule data from the new rule.
48
require CSS::DOM::Parser;
49
my $new_rule = CSS::DOM::Parser'parse_statement(shift)
50
|| die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
52
ref $new_rule eq ref $self or die CSS::DOM::Exception->new(
53
INVALID_MODIFICATION_ERR,
54
"The rule cannot be converted to a different type."
62
if(defined wantarray) {
63
$old = join '', @{$self->[tokn]},;
64
$old .= ';' unless $self->[typs] =~ /[;}]\z/;
68
my $new_rule = $self->_parse(shift);
69
@$self[typs,tokn] = @$new_rule[typs,tokn];
75
sub parentStyleSheet { shift->[shet]||() }
76
sub parentRule { shift->[prnt]||() }
78
sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
79
sub _set_parentRule { weaken(shift->[prnt] = pop) }
81
sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
87
CSS::DOM::Rule - CSS rule class for CSS::DOM
95
use CSS::DOM::Rule ':all'; # import constants
98
$sheet = new CSS::DOM;
99
$sheet->insertRule('bla blah blah {}');
100
$rule = $sheet->cssRules->[0];
102
$rule->type; # STYLE_RULE
103
$rule->cssText; # 'bla blah blah {}' or similar
104
$rule->cssText('p { margin: 0 }'); # replace it
105
$rule->parentStyleSheet; # $sheet
109
This module provides the CSS rule class for L<CSS::DOM>. It implements
110
the CSSRule and CSSUnknownRule DOM interfaces.
116
Only call the constructor on this class to create an 'unknown' rule. You have to
117
call the constructor on a particular subclass to get another type. Normally
119
call this directly anyway. (See L<CSS::DOM>'s
120
C<parse> and C<insertRule> methods.) But just in case you do want to call
124
new CSS::DOM::Rule $parent; # unknown rule
126
require CSS::DOM::Rule::Style
127
new CSS::DOM::Rule::Style $parent;
130
C<$parent> is the parent rule, if the rule is nested, or the parent style
133
=head2 Object Methods
139
Returns one of the constants below indicating the type of rule.
143
Returns this rule's CSS code. If you pass an argument, it will be parsed as
144
the new CSS code for this rule (replacing the existing data), and the old
145
value will be returned. This method will die if the replacement CSS code
146
creates a different type of rule.
148
=item parentStyleSheet
150
This returns the style sheet to which the rule belongs.
154
This returns the rule's parent rule, if there is one, or an empty list
155
otherwise. There is only a parent rule if this one is nested, e.g., inside
162
The following constants that indicate the type of rule will be exported on
163
request (individually or with the ':all' tag):
177
L<CSS::DOM::Rule::Style>
179
L<CSS::DOM::Rule::Media>
181
L<CSS::DOM::Rule::Page>
183
L<CSS::DOM::Rule::Import>