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.16';
4              
5 22     22   2048 use warnings;
  22         70  
  22         644  
6 22     22   170 use strict;
  22         43  
  22         499  
7              
8 22     22   110 use Carp 'croak';
  22         75  
  22         1018  
9 22     22   666 use CSS::DOM::Constants;
  22         45  
  22         882  
10 22     22   671 use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
  22         67  
  22         1223  
11 22     22   118 use Exporter 5.57 'import';
  22         429  
  22         665  
12 22     22   114 use Scalar::Util 'weaken';
  22         116  
  22         2702  
13              
14             *EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
15             our %EXPORT_TAGS = (all => \our @EXPORT_OK);
16              
17 22         1911 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   115 };
  22         316  
24 22     22   126 { no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
  22         42  
  22         12119  
25              
26             sub new {
27 304     304 0 2531 my $self = bless[],shift;
28 304   100     1026 my $parent = shift || return $self;
29 233 100       1345 if($parent->isa('CSS::DOM::Rule')) {
30 7         54 weaken($$self[shet] = $parent->parentStyleSheet);
31 7         37 weaken($$self[prnt] = $parent);
32             }
33             else {
34 226         976 weaken($$self[shet] = $parent)
35             }
36 233         927 $self;
37             }
38              
39 40     40 1 652 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   62 my $self = shift;
48 36         198 require CSS::DOM::Parser;
49 36   50     121 my $new_rule = CSS::DOM::Parser'parse_statement(shift)
50             || die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
51              
52 36 100       162 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         123 $new_rule;
57             };
58              
59             sub cssText {
60 43     43 1 89 my $self = shift;
61 43         64 my $old;
62 43 100       129 if(defined wantarray) {
63 40         64 $old = join '', @{$self->[tokn]},;
  40         191  
64 40 100       169 $old .= ';' unless $self->[typs] =~ /[;}]\z/;
65 40         80 $old .= "\n";
66             }
67 43 100       121 if (@_) {
68 4         11 my $new_rule = $self->_parse(shift);
69 3         13 @$self[typs,tokn] = @$new_rule[typs,tokn];
70             }
71 42         240 $old;
72             };
73              
74              
75 173 100   173 1 1268 sub parentStyleSheet { shift->[shet]||() }
76 15 100   15 1 6558 sub parentRule { shift->[prnt]||() }
77              
78 31     31   169 sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
79 31     31   220 sub _set_parentRule { weaken(shift->[prnt] = pop) }
80              
81 48     48   97 sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
  48         181  
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.16
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. 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's
120             C and C 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
176              
177             L
178              
179             L
180              
181             L
182              
183             L