DOM/Rule.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 14 / 14 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 18 / 18 Podpod 80.0% 4 / 5 totaltotal 97.2% 105 / 108 CC 10 SCAR 7.1
CC
10
Cov
99%
cssText
4.0
_parse
3.0
new
3.0
CRAP 2.0
« DOM/MediaList.pm ↑ Summary DOM/Rule/Page.pm »
1 package CSS::DOM::Rule;
2  
3 22 $VERSION = '0.16';
4  
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use CSS::DOM::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use CSS::DOM::Exception qw/ SYNTAX_ERR INVALID_MODIFICATION_ERR /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 *EXPORT_OK = $CSS::DOM::Constants::EXPORT_TAGS{rule};
15 22 our %EXPORT_TAGS = (all => \our @EXPORT_OK);
16  
17 22 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 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
24 22 ▶{ no strict; delete @{__PACKAGE__.'::'}{_const => keys %{our $_const}} }
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub new {
27 304 ▶ my $self = bless[],shift;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
28 304 ▶ my $parent = shift || return $self;
Condition100%2/2
shift() || (return $self)
l!l
23371
MC/DC100%1/1
shift() || (return $self)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
29 233 ▶ if($parent->isa('CSS::DOM::Rule')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa('CSS::DOM::Rule')) { } 7 226 233
30 7 weaken($$self[shet] = $parent->parentStyleSheet);
31 7 weaken($$self[prnt] = $parent);
32 }
33 else {
34 226 weaken($$self[shet] = $parent)
35 }
36 233 $self;
37 }
38  
39 40 ▶sub type { UNKNOWN_RULE }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
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 ▶ my $self = shift;
Subroutine _parse: called CC=3 SCAR=11.0
48 36 require CSS::DOM::Parser;
49 36 ▶ my $new_rule = CSS::DOM::Parser'parse_statement(shift)
Condition50%1/2
CSS::DOM::Parser::parse_statement(shift()) || die('CSS::DOM::Exception'->new(12, $@))
l!l
360
MC/DC0%0/1
CSS::DOM::Parser::parse_statement(shift()) || die('CSS::DOM::Exception'->new(12, $@))
CSS::DOM::Parser::parse_statement(shift())
Truth table50%1/2
A
A= CSS::DOM::Parser::parse_statement(shift())
Aresult
11
00
50 || die CSS::DOM::Exception->new(SYNTAX_ERR, $@);
51  
52 36 ▶ ref $new_rule eq ref $self or die CSS::DOM::Exception->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $new_rule eq ref $self or die "CSS::DOM::Exception"->new(13, "The rule cannot be converted to a different type.") 7 29 36
53 INVALID_MODIFICATION_ERR,
54 "The rule cannot be converted to a different type."
55 );
56 29 $new_rule;
57 };
58  
59 sub cssText {
60 43 ▶ my $self = shift;
Subroutine cssText: called CC=4 SCAR=13.9
Pod documented
61 43 my $old;
62 43 ▶ if(defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 40 3 43
63 40 $old = join '', @{$self->[tokn]},;
64 40 ▶ $old .= ';' unless $self->[typs] =~ /[;}]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[2] =~ /[;}]\z/ 6 34 40
65 40 $old .= "\n";
66 }
67 43 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 39 43
68 4 my $new_rule = $self->_parse(shift);
69 3 @$self[typs,tokn] = @$new_rule[typs,tokn];
70 }
71 42 $old;
72 };
73  
74  
75 173 ▶sub parentStyleSheet { shift->[shet]||() }
Subroutine parentStyleSheet: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[1] or () 42 131 173
76 15 ▶sub parentRule { shift->[prnt]||() }
Subroutine parentRule: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[0] or () 7 8 15
77  
78 31 ▶sub _set_parentStyleSheet { weaken(shift->[shet] = pop) }
Subroutine _set_parentStyleSheet: called CC=1 SCAR=0.0
79 31 ▶sub _set_parentRule { weaken(shift->[prnt] = pop) }
Subroutine _set_parentRule: called CC=1 SCAR=0.0
80  
81 48 ▶sub _set_tokens { @{+shift}[typs,tokn] = @_[1,2]; }
Subroutine _set_tokens: called CC=1 SCAR=0.0
82
83 22 !()__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<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>
« DOM/MediaList.pm ↑ Summary DOM/Rule/Page.pm »