Coverage/Deparse.pm

filter: Statementstmt 41.6% 25 / 60 Branchbran 40.0% 24 / 60 Conditioncond 36.8% 7 / 19 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 40.2% 62 / 154 CC 38 SCAR 54.9
CC
38
Cov
40%
stringify_condition
345.1
stringify_selector
18.1
BEGIN
1.0
CRAP 242.6
↑ Summary Coverage.pm »
1 package CSS::Coverage::Deparse;
2 {
3 4 $CSS::Coverage::Deparse::VERSION = '0.04';
4 }
5 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use CSS::SAC::Selector qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use CSS::SAC::Condition qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # taken from https://metacpan.org/source/BJOERN/CSS-SAC-0.08/lib/CSS/SAC/Writer.pm
10 # with tweaks to:
11 # ignore namespaces
12 # avoid emitting spurious *
13  
14 312 ▶sub stringify_selector {
Subroutine stringify_selector: called CC=10 SCAR=29.0
Pod undocumented
15 312 my $dh = shift;
16 312 my $sel = shift;
17  
18 # child
19 312 ▶ if ($sel->is_type(CHILD_SELECTOR)) {
Branch: 8 branches
BranchTrueFalseTotal
if ($sel->is_type(4)) { } 0 312 312
elsif ($sel->is_type(7)) { } 60 252 312
elsif ($sel->is_type(8)) { } 0 252 252
elsif ($sel->is_type(15)) { } 0 252 252
elsif ($sel->is_type(6)) { } 93 159 252
elsif ($sel->is_type(10)) { } 0 159 159
elsif ($sel->is_type(9)) { } 159 0 159
elsif ($sel->is_type(12)) { } 0 0 0
20 0 return $dh->stringify_selector($sel->AncestorSelector)
21 . ' > ' .
22 $dh->stringify_selector($sel->SimpleSelector);
23 }
24  
25 # descendant
26 elsif ($sel->is_type(DESCENDANT_SELECTOR)) {
27 60 return $dh->stringify_selector($sel->AncestorSelector)
28 . ' ' .
29 $dh->stringify_selector($sel->SimpleSelector);
30 }
31  
32 # direct adjacent
33 elsif ($sel->is_type(DIRECT_ADJACENT_SELECTOR)) {
34 0 return $dh->stringify_selector($sel->Selector)
35 . ' + ' .
36 $dh->stringify_selector($sel->SiblingSelector);
37 }
38  
39 # indirect adjacent
40 elsif ($sel->is_type(INDIRECT_ADJACENT_SELECTOR)) {
41 0 return $dh->stringify_selector($sel->Selector)
42 . ' ~ ' .
43 $dh->stringify_selector($sel->SiblingSelector);
44 }
45  
46 # conditional
47 elsif ($sel->is_type(CONDITIONAL_SELECTOR)) {
48 93 return $dh->stringify_selector($sel->SimpleSelector)
49 .
50 $dh->stringify_condition($sel->Condition);
51 }
52  
53 # negative
54 elsif ($sel->is_type(NEGATIVE_SELECTOR)) {
55 0 return ':not(' . $dh->stringify_selector($sel->SimpleSelector) . ')';
56 }
57  
58 # element
59 elsif ($sel->is_type(ELEMENT_NODE_SELECTOR)) {
60 # my $string;
61 # if (defined $sel->NamespaceURI) {
62 # if (length $sel->NamespaceURI) {
63 # $string = $dh->[_nsmap_]->{$sel->NamespaceURI} . '|';
64 # } # else we don't put anything and it's in the default ns
65 # }
66 # else {
67 # $string = '*|';
68 # }
69 # $string .= (defined $sel->LocalName)?$sel->LocalName:'*';
70 #
71 # return $string;
72  
73 159 ▶ return $sel->LocalName || '';
Condition100%2/2
$sel->LocalName || ''
l!l
8673
MC/DC100%1/1
$sel->LocalName || ''
$sel->LocalName
Truth table100%2/2
A
A= $sel->LocalName
Aresult
11
00
74 }
75  
76 # pseudo element
77 elsif ($sel->is_type(PSEUDO_ELEMENT_SELECTOR)) {
78 0 return '::' . $sel->LocalName;
79 }
80  
81 # error ?
82 else {
83 0 warn "unknown selector type";
84 }
85 }
86  
87 99 ▶sub stringify_condition {
Subroutine stringify_condition: called CC=29 SCAR=58.4
Pod undocumented
88 99 my $dh = shift;
89 99 my $cond = shift;
90  
91 # and
92 99 ▶ if ($cond->is_type(AND_CONDITION)) {
Branch: 13 branches
BranchTrueFalseTotal
if ($cond->is_type(2)) { } 3 96 99
elsif ($cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10) or $cond->is_type(16) or $cond->is_type(17) or $cond->is_type(18)) { } 0 96 96
elsif ($cond->is_type(5)) { } 34 62 96
elsif ($cond->is_type(6)) { } 0 62 62
elsif ($cond->is_type(7)) { } 47 15 62
elsif ($cond->is_type(8)) { } 0 15 15
elsif ($cond->is_type(9)) { } 0 15 15
elsif ($cond->is_type(11)) { } 0 15 15
elsif ($cond->is_type(12)) { } 0 15 15
elsif ($cond->is_type(19)) { } 0 15 15
elsif ($cond->is_type(20)) { } 0 15 15
elsif ($cond->is_type(15)) { } 15 0 15
elsif ($cond->is_type(14)) { } 0 0 0
93 3 return $dh->stringify_condition($cond->FirstCondition)
94 .
95 $dh->stringify_condition($cond->SecondCondition);
96 }
97  
98 # attr
99 ▶ elsif (
Condition33%5/15
$cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10) or $cond->is_type(16) or $cond->is_type(17) or $cond->is_type(18)
l!l&&r!l&&!r
0096
$cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10) or $cond->is_type(16) or $cond->is_type(17)
l!l&&r!l&&!r
0096
$cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10) or $cond->is_type(16)
l!l&&r!l&&!r
0096
$cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10)
l!l&&r!l&&!r
0096
$cond->is_type(3) or $cond->is_type(4)
l!l&&r!l&&!r
0096
MC/DC0%0/6
$cond->is_type(3) or $cond->is_type(4) or $cond->is_type(10) or $cond->is_type(16) or $cond->is_type(17) or $cond->is_type(18)
$cond->is_type(3) $cond->is_type(4) $cond->is_type(10) $cond->is_type(16) $cond->is_type(17) $cond->is_type(18)
Truth table14%1/7
A or B or C or D or E or F
A= $cond->is_type(3)B= $cond->is_type(4)C= $cond->is_type(10)D= $cond->is_type(16)E= $cond->is_type(17)F= $cond->is_type(18)
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
100 $cond->is_type(ATTRIBUTE_CONDITION) or
101 $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) or
102 $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) or
103 $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) or
104 $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) or
105 $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION)
106 ) {
107 0 my $string = '[';
108  
109 # the name
110 #if (defined $cond->NamespaceURI) {
111 # if (length $cond->NamespaceURI) {
112 # $string .= $dh->[_nsmap_]->{$cond->NamespaceURI} . '|';
113 # }
114 #}
115 #else {
116 # $string .= '*|';
117 #}
118 #$string .= (defined $cond->LocalName)?$cond->LocalName:'*';
119  
120 0 ▶ $string .= $cond->LocalName || '';
Condition0%0/2
$cond->LocalName || ''
l!l
00
MC/DC0%0/1
$cond->LocalName || ''
$cond->LocalName
Truth table0%0/2
A
A= $cond->LocalName
Aresult
11
00
121  
122 # the value
123 0 ▶ if ($cond->Specified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond->Specified) 0 0 0
124 0 my $op = '=';
125 0 ▶ $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) and $op = '|=';
Branch: 1 branch
BranchTrueFalseTotal
$cond->is_type(4) and $op = "|=" 0 0 0
126 0 ▶ $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) and $op = '~=';
Branch: 1 branch
BranchTrueFalseTotal
$cond->is_type(10) and $op = "~=" 0 0 0
127 0 ▶ $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) and $op = '^=';
Branch: 1 branch
BranchTrueFalseTotal
$cond->is_type(16) and $op = "^=" 0 0 0
128 0 ▶ $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) and $op = '$=';
Branch: 1 branch
BranchTrueFalseTotal
$cond->is_type(17) and $op = "\$=" 0 0 0
129 0 ▶ $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION) and $op = '*=';
Branch: 1 branch
BranchTrueFalseTotal
$cond->is_type(18) and $op = "*=" 0 0 0
130  
131 # find the right op depending on the attr type
132  
133 0 $string .= "$op'" . $cond->Value . "'";
134 }
135  
136 0 $string .= ']';
137  
138 0 return $string;
139 }
140  
141 # class
142 elsif ($cond->is_type(CLASS_CONDITION)) {
143 34 return '.' . $cond->Value;
144 }
145  
146 # content
147 elsif ($cond->is_type(CONTENT_CONDITION)) {
148 0 return ":contains('" . $cond->Data . "')";
149 }
150  
151 # id
152 elsif ($cond->is_type(ID_CONDITION)) {
153 47 return '#' . $cond->Value;
154 }
155  
156 # lang
157 elsif ($cond->is_type(LANG_CONDITION)) {
158 0 return ":lang(" . $cond->Lang . ")";
159 }
160  
161 # negative
162 elsif ($cond->is_type(NEGATIVE_CONDITION)) {
163 0 return ":not(" . $dh->stringify_condition($cond->Condition) . ")";
164 }
165  
166 # only child
167 elsif ($cond->is_type(ONLY_CHILD_CONDITION)) {
168 0 return ':only-child';
169 }
170  
171 # only of type
172 elsif ($cond->is_type(ONLY_TYPE_CONDITION)) {
173 0 return ':only-of-type';
174 }
175  
176 # root
177 elsif ($cond->is_type(IS_ROOT_CONDITION)) {
178 0 return ':root';
179 }
180  
181 # empty
182 elsif ($cond->is_type(IS_EMPTY_CONDITION)) {
183 0 return ':empty';
184 }
185  
186 # pseudo-class
187 elsif ($cond->is_type(PSEUDO_CLASS_CONDITION)) {
188 15 return ':' . $cond->Value;
189 }
190  
191 # positional
192 elsif ($cond->is_type(POSITIONAL_CONDITION)) {
193 0 my $string;
194  
195 # the second part right
196 0 ▶ if ($cond->Type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond->Type) { } 0 0 0
197 0 $string = 'of-type';
198 }
199 else {
200 0 $string = 'child';
201 }
202  
203 # get the first part right
204 0 ▶ if ($cond->Position == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cond->Position == 1) { } 0 0 0
elsif ($cond->Position == -1) { } 0 0 0
205 0 return ':first-' . $string;
206 }
207 elsif ($cond->Position == -1) {
208 0 return ':last-' . $string;
209 }
210 else {
211 0 $string = ':nth-' . $string;
212 }
213  
214 # add the expression
215 0 $string .= '(' . $cond->Position . ')';
216  
217 0 return $string;
218 }
219 }
220  
221 4 1;
222  
223 __END__
↑ Summary Coverage.pm »