File Coverage

blib/lib/CSS/DOM/Rule.pm
Criterion Covered Total %
statement 57 57 100.0
branch 14 14 100.0
condition 3 4 75.0
subroutine 18 18 100.0
pod 4 5 80.0
total 96 98 97.9


line stmt bran cond sub pod time code
1             package CSS::DOM::Rule;
2              
3             $VERSION = '0.15';
4              
5 22     22   1929 use warnings;
  22         90  
  22         717  
6 22     22   153 use strict;
  22         44  
  22         703  
7              
8 22     22   119 use Carp 'croak';
  22         100  
  22         1143  
9 22     22   782 use CSS::DOM::Constants;
  22         47  
  22         954  
10 22     22   817 use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
  22         51  
  22         1195  
11 22     22   128 use Exporter 5.57 'import';
  22         519  
  22         725  
12 22     22   123 use Scalar::Util 'weaken';
  22         128  
  22         2818  
13              
14             *EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
15             our %EXPORT_TAGS = (all => \our @EXPORT_OK);
16              
17 22         2191 use constant 1.03 our $_const = {
18             # Don’t let these conflict with subclasses!
19             prnt => 0,
20             shet => 1,
21             typs => 2, # token types These two are not used by subclassed,
22             tokn => 3, # tokens so there’s no chance of a conflict.
23 22     22   121 };
  22         392  
24 22     22   126 { no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
  22         50  
  22         15539  
25              
26             sub new {
27 304     304 0 2899 my $self = bless[],shift;
28 304   100     1182 my $parent = shift || return $self;
29 233 100       1777 if($parent->isa('CSS::DOM::Rule')) {
30 7         47 weaken($$self[shet] = $parent->parentStyleSheet);
31 7         27 weaken($$self[prnt] = $parent);
32             }
33             else {
34 226         1180 weaken($$self[shet] = $parent)
35             }
36 233         1338 $self;
37             }
38              
39 40     40 1 605 sub type { UNKNOWN_RULE }
40              
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.
47 36     36   57 my $self = shift;
48 36         245 require CSS::DOM::Parser;
49 36   50     111 my $new_rule = CSS::DOM::Parser'parse_statement(shift)
50             || die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
51              
52 36 100       139 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."
55             );
56 29         109 $new_rule;
57             };
58              
59             sub cssText {
60 43     43 1 92 my $self = shift;
61 43         59 my $old;
62 43 100       357 if(defined wantarray) {
63 40         71 $old = join '', @{$self->[tokn]},;
  40         175  
64 40 100       195 $old .= ';' unless $self->[typs] =~ /[;}]\z/;
65 40         86 $old .= "\n";
66             }
67 43 100       103 if (@_) {
68 4         10 my $new_rule = $self->_parse(shift);
69 3         12 @$self[typs,tokn] = @$new_rule[typs,tokn];
70             }
71 42         288 $old;
72             };
73              
74              
75 173 100   173 1 1342 sub parentStyleSheet { shift->[shet]||() }
76 15 100   15 1 7266 sub parentRule { shift->[prnt]||() }
77              
78 31     31   166 sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
79 31     31   256 sub _set_parentRule { weaken(shift->[prnt] = pop) }
80              
81 48     48   117 sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
  48         205  
82            
83             !()__END__()!
84              
85             =head1 NAME
86              
87             CSS::DOM::Rule - CSS rule class for CSS::DOM
88              
89             =head1 VERSION
90              
91             Version 0.15
92              
93             =head1 SYNOPSIS
94              
95             use CSS::DOM::Rule ':all'; # import constants
96              
97             use CSS::DOM;
98             $sheet = new CSS::DOM;
99             $sheet->insertRule('bla blah blah {}');
100             $rule = $sheet->cssRules->[0];
101            
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
106              
107             =head1 DESCRIPTION
108              
109             This module provides the CSS rule class for L<CSS::DOM>. It implements
110             the CSSRule and CSSUnknownRule DOM interfaces.
111              
112             =head1 METHODS
113              
114             =head2 Constructor
115              
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
118             you do not need to
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
121             it, here it
122             is:
123              
124             new CSS::DOM::Rule $parent; # unknown rule
125            
126             require CSS::DOM::Rule::Style
127             new CSS::DOM::Rule::Style $parent;
128             # etc.
129              
130             C<$parent> is the parent rule, if the rule is nested, or the parent style
131             sheet otherwise.
132              
133             =head2 Object Methods
134              
135             =over 4
136              
137             =item type
138              
139             Returns one of the constants below indicating the type of rule.
140              
141             =item cssText
142              
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.
147              
148             =item parentStyleSheet
149              
150             This returns the style sheet to which the rule belongs.
151              
152             =item parentRule
153              
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
156             a media rule.
157              
158             =back
159              
160             =head1 EXPORTS
161              
162             The following constants that indicate the type of rule will be exported on
163             request (individually or with the ':all' tag):
164              
165             UNKNOWN_RULE
166             STYLE_RULE
167             CHARSET_RULE
168             IMPORT_RULE
169             MEDIA_RULE
170             FONT_FACE_RULE
171             PAGE_RULE
172              
173             =head1 SEE ALSO
174              
175             L<CSS::DOM>
176              
177             L<CSS::DOM::Rule::Style>
178              
179             L<CSS::DOM::Rule::Media>
180              
181             L<CSS::DOM::Rule::Page>
182              
183             L<CSS::DOM::Rule::Import>