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.17';
4              
5 22     22   1761 use warnings;
  22         31  
  22         555  
6 22     22   83 use strict;
  22         31  
  22         359  
7              
8 22     22   75 use Carp 'croak';
  22         33  
  22         832  
9 22     22   453 use CSS::DOM::Constants;
  22         55  
  22         676  
10 22     22   426 use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
  22         49  
  22         996  
11 22     22   112 use Exporter 5.57 'import';
  22         265  
  22         624  
12 22     22   98 use Scalar::Util 'weaken';
  22         29  
  22         2024  
13              
14             *EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
15             our %EXPORT_TAGS = (all => \our @EXPORT_OK);
16              
17 22         1481 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   120 };
  22         285  
24 22     22   116 { no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
  22         30  
  22         9984  
25              
26             sub new {
27 304     304 0 1802 my $self = bless[],shift;
28 304   100     612 my $parent = shift || return $self;
29 233 100       889 if($parent->isa('CSS::DOM::Rule')) {
30 7         28 weaken($$self[shet] = $parent->parentStyleSheet);
31 7         17 weaken($$self[prnt] = $parent);
32             }
33             else {
34 226         705 weaken($$self[shet] = $parent)
35             }
36 233         602 $self;
37             }
38              
39 40     40 1 458 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   39 my $self = shift;
48 36         132 require CSS::DOM::Parser;
49 36   50     81 my $new_rule = CSS::DOM::Parser'parse_statement(shift)
50             || die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
51              
52 36 100       90 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         76 $new_rule;
57             };
58              
59             sub cssText {
60 43     43 1 77 my $self = shift;
61 43         48 my $old;
62 43 100       87 if(defined wantarray) {
63 40         64 $old = join '', @{$self->[tokn]},;
  40         100  
64 40 100       146 $old .= ';' unless $self->[typs] =~ /[;}]\z/;
65 40         68 $old .= "\n";
66             }
67 43 100       79 if (@_) {
68 4         6 my $new_rule = $self->_parse(shift);
69 3         9 @$self[typs,tokn] = @$new_rule[typs,tokn];
70             }
71 42         144 $old;
72             };
73              
74              
75 173 100   173 1 729 sub parentStyleSheet { shift->[shet]||() }
76 15 100   15 1 5102 sub parentRule { shift->[prnt]||() }
77              
78 31     31   78 sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
79 31     31   118 sub _set_parentRule { weaken(shift->[prnt] = pop) }
80              
81 48     48   67 sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
  48         115  
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.17
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