CSel.pm

filter: Statementstmt 83.6% 215 / 257 Branchbran 75.8% 188 / 248 Conditioncond 40.4% 19 / 47 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 75.2% 448 / 595 CC 154 SCAR 59.9
CC
154
Cov
76%
_simpsel
492.1
csel
9.0
_sel
8.0
CRAP 398.8
↑ Summary CSel/Selection.pm »
1 package Data::CSel;
2  
3 4 ▶use 5.020000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Code::Includable::Tree::NodeMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use List::Util qw(first);
9 4 ▶use Scalar::Util qw(blessed refaddr looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 4 our $DATE = '2022-06-07'; # DATE
15 4 our $DIST = 'Data-CSel'; # DIST
16 4 our $VERSION = '0.128'; # VERSION
17  
18 4 our @EXPORT_OK = qw(
19 csel
20 csel_each
21 parse_csel
22 );
23  
24 4 our @CLASS_PREFIXES;
25  
26 4 our $_i1;
27  
28 0 ▶sub _fail { die __PACKAGE__.": $_[0] at offset ".pos()."\n" }
Subroutine _fail: not called CC=1 SCAR=6.9
29  
30 our $RE =
31 qr{
32 (?&SELECTORS) (?{ $_ = $^R->[1] })
33  
34 (?(DEFINE)
35 (?<SELECTORS>
36 (?{ [$^R, []] })
37 (?&SELECTOR) # [[$^R, []], $selector]
38 (?{ [$^R->[0][0], [$^R->[1]]] })
39 (?:
40 \s*,\s*
41 (?&SELECTOR)
42 (?{
43 push @{$^R->[0][1]}, $^R->[1];
44 $^R->[0];
45 })
46 )*
47 \s*
48 ) # SELECTORS
49  
50 (?<SELECTOR>
51 (?{ [$^R, []] })
52 (?&SIMPLE_SELECTOR) # [[$^R, []], $simple_selector]
53 (?{ [$^R->[0][0], [$^R->[1]]] })
54 (?:
55 (\s*>\s*|\s*\+\s*|\s*~\s*|\s+)
56 (?{
57 my $comb = $^N;
58 $comb =~ s/^\s+//; $comb =~ s/\s+$//;
59 $comb = " " if $comb eq '';
60 push @{$^R->[1]}, {combinator=>$comb};
61 $^R;
62 })
63  
64 (?&SIMPLE_SELECTOR)
65 (?{
66 push @{$^R->[0][1]}, $^R->[1];
67 $^R->[0];
68 })
69 )*
70 ) # SELECTOR
71  
72 (?<SIMPLE_SELECTOR>
73 (?:
74 (?:
75 # type selector + optional filters
76 ((?&TYPE_NAME))
77 (?{ [$^R, {type=>$^N}] })
78 (?:
79 (?&FILTER) # [[$^R, $simple_selector], $filter]
80 (?{
81 push @{ $^R->[0][1]{filters} }, $^R->[1];
82 $^R->[0];
83 })
84 (?:
85 \s*
86 (?&FILTER)
87 (?{
88 push @{ $^R->[0][1]{filters} }, $^R->[1];
89 $^R->[0];
90 })
91 )*
92 )?
93 )
94 |
95 (?:
96 # optional type selector + one or more filters
97 ((?&TYPE_NAME))?
98 (?{
99 # XXX sometimes $^N is ' '?
100 my $t = $^N // '*';
101 $t = '*' if $t eq ' ';
102 [$^R, {type=>$t}] })
103 (?&FILTER) # [[$^R, $simple_selector], $filter]
104 (?{
105 push @{ $^R->[0][1]{filters} }, $^R->[1];
106 $^R->[0];
107 })
108 (?:
109 \s*
110 (?&FILTER)
111 (?{
112 push @{ $^R->[0][1]{filters} }, $^R->[1];
113 $^R->[0];
114 })
115 )*
116 )
117 )
118 ) # SIMPLE_SELECTOR
119  
120 (?<TYPE_NAME>
121 [A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z0-9_]+)*|\*
122 )
123  
124 (?<FILTER>
125 (?{ [$^R, {}] })
126 (
127 (?&ATTR_SELECTOR) # [[$^R0, {}], [$attr, $op, $val]]
128 (?{
129 $^R->[0][1]{type} = 'attr_selector';
130 $^R->[0][1]{attr} = $^R->[1][0];
131 $^R->[0][1]{op} = $^R->[1][1] if defined $^R->[1][1];
132 $^R->[0][1]{value} = $^R->[1][2] if @{ $^R->[1] } > 2;
133 $^R->[0];
134 })
135 |
136 \.((?&TYPE_NAME))
137 (?{
138 $^R->[1]{type} = 'class_selector';
139 $^R->[1]{class} = $^N;
140 $^R;
141 })
142 |
143 \#(\w+)
144 (?{
145 $^R->[1]{type} = 'id_selector';
146 $^R->[1]{id} = $^N;
147 $^R;
148 })
149 |
150 (?&PSEUDOCLASS) # [[$^R, {}], [$pseudoclass, \@args]]
151 (?{
152 $^R->[0][1]{type} = 'pseudoclass';
153 $^R->[0][1]{pseudoclass} = $^R->[1][0];
154 $^R->[0][1]{args} = $^R->[1][1] if @{ $^R->[1] } > 1;
155 $^R->[0];
156 })
157 )
158 ) # FILTER
159  
160 (?<ATTR_SELECTOR>
161 \[\s*
162 (?{ [$^R, []] }) # [$^R, [$subjects, $op, $literal]]
163 (?&ATTR_SUBJECTS) # [[$^R, [{name=>$name, args=>$args}, ...]]
164 (?{
165 #use Data::Dmp; say "D:setting subjects: ", dmp $^R->[1];
166 push @{ $^R->[0][1] }, $^R->[1];
167 $^R->[0];
168 })
169  
170 (?:
171 (
172 \s*(?:=~|!~)\s* |
173 \s*(?:!=|<>|>=?|<=?|==?)\s* |
174 \s++(?:eq|ne|lt|gt|le|ge)\s++ |
175 \s+(?:isnt|is|has|hasnt|in|notin)\s+
176 )
177 (?{
178 my $op = $^N;
179 $op =~ s/^\s+//; $op =~ s/\s+$//;
180 $^R->[1][1] = $op;
181 $^R;
182 })
183  
184 (?:
185 (?&LITERAL) # [[$^R0, [$attr, $op]], $literal]
186 (?{
187 push @{ $^R->[0][1] }, $^R->[1];
188 $^R->[0];
189 })
190 |
191 (\w[^\s\]]*) # allow unquoted string
192 (?{
193 $^R->[1][2] = $^N;
194 $^R;
195 })
196 )
197 )?
198 \s*\]
199 ) # ATTR_SELECTOR
200  
201 (?<ATTR_NAME>
202 [A-Za-z_][A-Za-z0-9_]*
203 )
204  
205 (?<ATTR_SUBJECT>
206 (?{ [$^R, []] }) # [$^R, [name, \@args]]
207 ((?&ATTR_NAME))
208 (?{
209 #say "D:pushing attribute subject: $^N";
210 push @{ $^R->[1] }, $^N;
211 $^R;
212 })
213 (?:
214 # attribute arguments
215 \s*\(\s* (*PRUNE)
216 (?{
217 $^R->[1][1] = [];
218 $^R;
219 })
220 (?:
221 (?&LITERAL)
222 (?{
223 #use Data::Dmp; say "D:pushing argument: ", dmp $^R->[1];
224 push @{ $^R->[0][1][1] }, $^R->[1];
225 $^R->[0];
226 })
227 (?:
228 \s*,\s*
229 (?&LITERAL)
230 (?{
231 #use Data::Dmp; say "D:pushing argument: ", dmp $^R->[1];
232 push @{ $^R->[0][1][1] }, $^R->[1];
233 $^R->[0];
234 })
235 )*
236 )?
237 \s*\)\s*
238 )?
239 ) # ATTR_SUBJECT
240  
241 (?<ATTR_SUBJECTS>
242 (?{ $_i1 = 0; [$^R, []] })
243 (?&ATTR_SUBJECT) # [[$^R, [$name, \@args]]
244 (?{
245 $_i1++;
246 unless ($_i1 > 1) { # to prevent backtracking from executing tihs code block twice
247 #say "D:pushing subject(1)";
248 push @{ $^R->[0][1] }, {
249 name => $^R->[1][0],
250 (args => $^R->[1][1]) x !!defined($^R->[1][1]),
251 };
252 }
253 $^R->[0];
254 })
255 (?:
256 \s*\.\s*
257 (?{ $_i1 = 0; $^R })
258 (?&ATTR_SUBJECT) # [[$^R, $name, \@args]]
259 (?{
260 $_i1++;
261 unless ($_i1 > 1) { # to prevent backtracking from executing this code block twice
262 #say "D:pushing subject(2)";
263 push @{ $^R->[0][1] }, {
264 name => $^R->[1][0],
265 (args => $^R->[1][1]) x !!defined($^R->[1][1]),
266 };
267 }
268 $^R->[0];
269 })
270 )*
271 ) # ATTR_SUBJECTS
272  
273 (?<LITERAL>
274 (?&LITERAL_ARRAY)
275 |
276 (?&LITERAL_NUMBER)
277 |
278 (?&LITERAL_STRING_DQUOTE)
279 |
280 (?&LITERAL_STRING_SQUOTE)
281 |
282 (?&LITERAL_REGEX)
283 |
284 true (?{ [$^R, 1] })
285 |
286 false (?{ [$^R, 0] })
287 |
288 null (?{ [$^R, undef] })
289 ) # LITERAL
290  
291 (?<LITERAL_ARRAY>
292 \[\s*
293 (?{ [$^R, []] })
294 (?:
295 (?&LITERAL) # [[$^R, []], $val]
296 (?{ [$^R->[0][0], [$^R->[1]]] })
297 \s*
298 (?:
299 (?:
300 ,\s* (?&LITERAL)
301 (?{ push @{$^R->[0][1]}, $^R->[1]; $^R->[0] })
302 )*
303 |
304 (?: [^,\]]|\z ) (?{ _fail "Expected ',' or '\x5d'" })
305 )
306 )?
307 \s*
308 (?:
309 \]
310 |
311 (?:.|\z) (?{ _fail "Expected closing of array" })
312 )
313 ) # LITERAL_ARRAY
314  
315 (?<LITERAL_NUMBER>
316 (
317 -?
318 (?: 0 | [1-9]\d* )
319 (?: \. \d+ )?
320 (?: [eE] [-+]? \d+ )?
321 )
322 (?{ [$^R, 0+$^N] })
323 )
324  
325 (?<LITERAL_STRING_DQUOTE>
326 (
327 "
328 (?:
329 [^\\"]+
330 |
331 \\ [0-7]{1,3}
332 |
333 \\ x [0-9A-Fa-f]{1,2}
334 |
335 \\ ["\\'tnrfbae]
336 )*
337 "
338 )
339 (?{ [$^R, eval $^N] })
340 )
341  
342 (?<LITERAL_STRING_SQUOTE>
343 (
344 '
345 (?:
346 [^\\']+
347 |
348 \\ .
349 )*
350 '
351 )
352 (?{ [$^R, eval $^N] })
353 )
354  
355 (?<LITERAL_REGEX>
356 (
357 (?:
358 (?:
359 /
360 (?:
361 [^/\\]+
362 |
363 \\ .
364 )*
365 /
366 ) |
367 (?:
368 qr\(
369 (?:
370 [^\)\\]+
371 |
372 \\ .
373 )*
374 \)
375 )
376 )
377 [ims]*
378 )
379 (?{ my $code = substr($^N, 0, 2) eq "qr" ? $^N : "qr$^N"; my $re = eval $code; die if $@; [$^R, $re] })
380 )
381  
382 (?<PSEUDOCLASS_NAME>
383 [A-Za-z_][A-Za-z0-9_]*(?:-[A-Za-z0-9_]+)*
384 )
385  
386 (?<PSEUDOCLASS>
387 :
388 (?:
389 (?:
390 (has|not)
391 (?{ [$^R, [$^N]] })
392 \(\s*
393 (?:
394 (?&LITERAL)
395 (?{
396 push @{ $^R->[0][1][1] }, $^R->[1];
397 $^R->[0];
398 })
399 |
400 ((?&SELECTORS))
401 (?{
402 push @{ $^R->[0][1][1] }, $^N;
403 $^R->[0];
404 })
405 )
406 \s*\)
407 )
408 |
409 (?:
410 ((?&PSEUDOCLASS_NAME))
411 (?{ [$^R, [$^N]] })
412 (?:
413 \(\s*
414 (?&LITERAL)
415 (?{
416 push @{ $^R->[0][1][1] }, $^R->[1];
417 $^R->[0];
418 })
419 (?:
420 \s*,\s*
421 (?&LITERAL)
422 (?{
423 push @{ $^R->[0][1][1] }, $^R->[1];
424 $^R->[0];
425 })
426 )*
427 \s*\)
428 )?
429 )
430 )
431 ) # PSEUDOCLASS
432 ) # DEFINE
433 4 }x;
434  
435 107 ▶sub parse_csel {
Subroutine parse_csel: called CC=3 SCAR=11.0
Pod documented
436 107 state $re = qr{\A\s*$RE\s*\z};
437  
438 107 local $_ = shift;
439 107 local $^R;
440 107 ▶ eval { $_ =~ $re } and return $_;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_ =~ /$re/u } } and return $_ 104 3 107
441 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
442 3 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
443 }
444  
445 158 ▶sub _uniq_objects {
Subroutine _uniq_objects: called CC=3 SCAR=11.0
446 158 my @uniq;
447 my %mem;
448 158 for (@_) {
449 727 ▶ push @uniq, $_ unless $mem{refaddr($_)}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $mem{builtin::refaddr($_)}++ 685 42 727
450 }
451 158 @uniq;
452 }
453  
454 81 ▶sub _simpsel {
Subroutine _simpsel: called CC=135 SCAR=62.0
455 4 ▶ no warnings 'numeric', 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
456  
457 81 my ($opts, $simpsel, $is_recursive, @nodes) = @_;
458  
459 #use Data::Dmp; say "D: _simpsel(expr", dmp($simpsel), ", recursive=$is_recursive, nodes=[".join(",",map {$_->{id}} @nodes)."])";
460  
461 81 my @res;
462 81 ▶ if ($is_recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_recursive) { } 78 3 81
463 78 @res = (@nodes, map {Code::Includable::Tree::NodeMethods::descendants($_)} @nodes);
464 } else {
465 3 @res = @nodes;
466 }
467 #say "D: intermediate result (after walk): [".join(",",map {$_->{id}} @res)."]";
468  
469 81 ▶ unless ($simpsel->{type} eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($simpsel->{"type"} eq "*") 22 59 81
470 22 my @fres;
471  
472 my @types_to_match;
473 22 ▶ for (@{ $opts->{class_prefixes} // [] }, @CLASS_PREFIXES) {
Condition100%2/2
$opts->{"class_prefixes"} // []
l!l
220
MC/DC100%1/1
$opts->{"class_prefixes"} // []
$opts->{"class_prefixes"}
Truth table100%2/2
A
A= $opts->{"class_prefixes"}
Aresult
11
00
474 3 ▶ push @types_to_match, $_ . (/::$/ ? "" : "::") . $simpsel->{type};
Branch: 1 branch
BranchTrueFalseTotal
/::$/u ? : 0 3 3
475 }
476 22 push @types_to_match, $simpsel->{type};
477  
478 ELEM:
479 22 for my $o (@res) {
480 187 my $ref = ref($o);
481 187 for (@types_to_match) {
482 194 ▶ if ($ref eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq $_) 71 123 194
483 71 push @fres, $o;
484 71 next ELEM;
485 }
486 }
487 }
488 22 @res = @fres;
489 }
490  
491 81 @res = _uniq_objects(@res);
492 #say "D: intermediate result (after type): [".join(",",map {$_->{id}} @res)."]";
493  
494 81 ▶ for my $f (@{ $simpsel->{filters} // [] }) {
Condition100%2/2
$simpsel->{"filters"} // []
l!l
6318
MC/DC100%1/1
$simpsel->{"filters"} // []
$simpsel->{"filters"}
Truth table100%2/2
A
A= $simpsel->{"filters"}
Aresult
11
00
495 66 ▶ last unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 0 66 66
496  
497 66 my $type = $f->{type};
498  
499 66 ▶ if ($type eq 'attr_selector') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'attr_selector') { } 34 32 66
elsif ($type eq 'class_selector') { } 6 26 32
elsif ($type eq 'id_selector') { } 6 20 26
elsif ($type eq 'pseudoclass') { } 20 0 20
500  
501 34 my @attrs = @{ $f->{attr} };
502 34 my $op = $f->{op};
503 34 my $opv = $f->{value};
504  
505 34 my @newres;
506 ITEM:
507 34 for my $o0 (@res) {
508 168 my $o = $o0;
509 168 for my $i (0..$#attrs) {
510 175 my $attr_name = $attrs[$i]{name};
511 175 my $attr_args = $attrs[$i]{args};
512 175 ▶ next ITEM unless blessed($o) && $o->can($attr_name);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($o) and $o->can($attr_name) 13 162 175
Condition100%3/3
builtin::blessed($o) and $o->can($attr_name)
!ll&&!rl&&r
49162
MC/DC100%2/2
builtin::blessed($o) and $o->can($attr_name)
builtin::blessed($o) $o->can($attr_name)
Truth table100%3/3
A and B
A= builtin::blessed($o)B= $o->can($attr_name)
ABresult
0X0
100
111
513 162 ▶ if ($attr_args && $i == $#attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_args and $i == $#attrs) { } 0 162 162
Condition33%1/3
$attr_args and $i == $#attrs
!ll&&!rl&&r
16200
MC/DC0%0/2
$attr_args and $i == $#attrs
$attr_args $i == $#attrs
Truth table33%1/3
A and B
A= $attr_argsB= $i == $#attrs
ABresult
0X0
100
111
514 # T[meth()] means T[meth() is true]
515 0 ▶ unless ($op) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op) 0 0 0
516 0 $op = 'is';
517 0 $opv = 1;
518 }
519 0 $o = $o->$attr_name(@$attr_args);
520 } else {
521 162 $o = $o->$attr_name;
522 }
523 }
524 155 ▶ goto PASS unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 9 146 155
525 146 my $val = $o;
526  
527 146 ▶ if ($op eq '=' || $op eq '==') {
Branch: 20 branches
BranchTrueFalseTotal
if ($op eq '=' or $op eq '==') { } 15 131 146
elsif ($op eq 'eq') { } 8 123 131
elsif ($op eq '!=' or $op eq '<>') { } 15 108 123
elsif ($op eq 'ne') { } 5 103 108
elsif ($op eq '>') { } 10 93 103
elsif ($op eq 'gt') { } 5 88 93
elsif ($op eq '>=') { } 10 78 88
elsif ($op eq 'ge') { } 5 73 78
elsif ($op eq '<') { } 9 64 73
elsif ($op eq 'lt') { } 5 59 64
elsif ($op eq '<=') { } 9 50 59
elsif ($op eq 'le') { } 5 45 50
elsif ($op eq 'is') { } 15 30 45
elsif ($op eq 'isnt') { } 15 15 30
elsif ($op eq 'has') { } 0 15 15
elsif ($op eq 'hasnt') { } 0 15 15
elsif ($op eq 'in') { } 0 15 15
elsif ($op eq 'notin') { } 0 15 15
elsif ($op eq '=~') { } 10 5 15
elsif ($op eq '!~') { } 5 0 5
Condition100%3/3
$op eq "=" or $op eq "=="
l!l&&r!l&&!r
105131
MC/DC100%2/2
$op eq "=" or $op eq "=="
$op eq "=" $op eq "=="
Truth table100%3/3
A or B
A= $op eq "="B= $op eq "=="
ABresult
1X1
011
000
528 15 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 10 5 15
529 10 ▶ next ITEM unless $val == $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val == $opv 8 2 10
530 } else {
531 5 ▶ next ITEM unless $val eq $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq $opv 4 1 5
532 }
533 } elsif ($op eq 'eq') {
534 8 ▶ next ITEM unless $val eq $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq $opv 6 2 8
535 ▶ } elsif ($op eq '!=' || $op eq '<>') {
Condition100%3/3
$op eq "!=" or $op eq "<>"
l!l&&r!l&&!r
105108
MC/DC100%2/2
$op eq "!=" or $op eq "<>"
$op eq "!=" $op eq "<>"
Truth table100%3/3
A or B
A= $op eq "!="B= $op eq "<>"
ABresult
1X1
011
000
536 15 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 10 5 15
537 10 ▶ next ITEM unless $val != $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val != $opv 2 8 10
538 } else {
539 5 ▶ next ITEM unless $val ne $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val ne $opv 1 4 5
540 }
541 } elsif ($op eq 'ne') {
542 5 ▶ next ITEM unless $val ne $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val ne $opv 1 4 5
543 } elsif ($op eq '>') {
544 10 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 5 5 10
545 5 ▶ next ITEM unless $val > $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val > $opv 4 1 5
546 } else {
547 5 ▶ next ITEM unless $val gt $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val gt $opv 1 4 5
548 }
549 } elsif ($op eq 'gt') {
550 5 ▶ next ITEM unless $val gt $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val gt $opv 1 4 5
551 } elsif ($op eq '>=') {
552 10 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 5 5 10
553 5 ▶ next ITEM unless $val >= $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val >= $opv 4 1 5
554 } else {
555 5 ▶ next ITEM unless $val ge $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val ge $opv 1 4 5
556 }
557 } elsif ($op eq 'ge') {
558 5 ▶ next ITEM unless $val ge $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val ge $opv 1 4 5
559 } elsif ($op eq '<') {
560 9 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 4 5 9
561 4 ▶ next ITEM unless $val < $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val < $opv 1 3 4
562 } else {
563 5 ▶ next ITEM unless $val lt $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val lt $opv 4 1 5
564 }
565 } elsif ($op eq 'lt') {
566 5 ▶ next ITEM unless $val lt $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val lt $opv 4 1 5
567 } elsif ($op eq '<=') {
568 9 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 4 5 9
569 4 ▶ next ITEM unless $val <= $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val <= $opv 0 4 4
570 } else {
571 5 ▶ next ITEM unless $val le $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val le $opv 3 2 5
572 }
573 } elsif ($op eq 'le') {
574 5 ▶ next ITEM unless $val le $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val le $opv 3 2 5
575 } elsif ($op eq 'is') {
576 15 ▶ if (!defined($opv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opv) { } 5 10 15
elsif ($opv) { } 5 5 10
577 5 ▶ next ITEM unless !defined($val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 4 5
578 } elsif ($opv) {
579 5 ▶ next ITEM unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 4 1 5
580 } else {
581 5 ▶ next ITEM unless !$val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 1 4 5
582 }
583 } elsif ($op eq 'isnt') {
584 15 ▶ if (!defined($opv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opv) { } 5 10 15
elsif ($opv) { } 5 5 10
585 5 ▶ next ITEM unless defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 1 5
586 } elsif ($opv) {
587 5 ▶ next ITEM unless !$val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 1 4 5
588 } else {
589 5 ▶ next ITEM unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 4 1 5
590 }
591 } elsif ($op eq 'has') {
592 0 ▶ next ITEM unless defined $val && ref($val) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and ref $val eq "ARRAY" and defined $opv 0 0 0
Condition0%0/6
defined $val and ref $val eq "ARRAY" and defined $opv
!ll&&!rl&&r
000
defined $val and ref $val eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $val and ref $val eq "ARRAY" and defined $opv
defined $val ref $val eq "ARRAY" defined $opv
Truth table0%0/4
A and B and C
A= defined $valB= ref $val eq "ARRAY"C= defined $opv
ABCresult
0XX0
10X0
1100
1111
593 && defined $opv;
594 0 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 0 0 0
595 0 ▶ next ITEM unless grep { $_ == $opv } @$val;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $opv;} @$val 0 0 0
596 } else {
597 0 ▶ next ITEM unless grep { $_ eq $opv } @$val;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $opv;} @$val 0 0 0
598 }
599 } elsif ($op eq 'hasnt') {
600 0 ▶ next ITEM unless defined $val && ref($val) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and ref $val eq "ARRAY" and defined $opv 0 0 0
Condition0%0/6
defined $val and ref $val eq "ARRAY" and defined $opv
!ll&&!rl&&r
000
defined $val and ref $val eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $val and ref $val eq "ARRAY" and defined $opv
defined $val ref $val eq "ARRAY" defined $opv
Truth table0%0/4
A and B and C
A= defined $valB= ref $val eq "ARRAY"C= defined $opv
ABCresult
0XX0
10X0
1100
1111
601 && defined $opv;
602 0 ▶ if (looks_like_number($opv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($opv)) { } 0 0 0
603 0 ▶ next ITEM if grep { $_ == $opv } @$val;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $opv;} @$val 0 0 0
604 } else {
605 0 ▶ next ITEM if grep { $_ eq $opv } @$val;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $opv;} @$val 0 0 0
606 }
607 } elsif ($op eq 'in') {
608 0 ▶ next ITEM unless defined $val && defined $opv &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and defined $opv and ref $opv eq "ARRAY" 0 0 0
Condition0%0/6
defined $val and defined $opv and ref $opv eq "ARRAY"
!ll&&!rl&&r
000
defined $val and defined $opv
!ll&&!rl&&r
000
MC/DC0%0/3
defined $val and defined $opv and ref $opv eq "ARRAY"
defined $val defined $opv ref $opv eq "ARRAY"
Truth table0%0/4
A and B and C
A= defined $valB= defined $opvC= ref $opv eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
609 ref($opv) eq 'ARRAY';
610 0 ▶ if (looks_like_number($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($val)) { } 0 0 0
611 0 ▶ next ITEM unless grep { $_ == $val } @$opv;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $val;} @$opv 0 0 0
612 } else {
613 0 ▶ next ITEM unless grep { $_ eq $val } @$opv;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $val;} @$opv 0 0 0
614 }
615 } elsif ($op eq 'notin') {
616 0 ▶ next ITEM unless defined $val && defined $opv &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and defined $opv and ref $opv eq "ARRAY" 0 0 0
Condition0%0/6
defined $val and defined $opv and ref $opv eq "ARRAY"
!ll&&!rl&&r
000
defined $val and defined $opv
!ll&&!rl&&r
000
MC/DC0%0/3
defined $val and defined $opv and ref $opv eq "ARRAY"
defined $val defined $opv ref $opv eq "ARRAY"
Truth table0%0/4
A and B and C
A= defined $valB= defined $opvC= ref $opv eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
617 ref($opv) eq 'ARRAY';
618 0 ▶ if (looks_like_number($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($val)) { } 0 0 0
619 0 ▶ next ITEM if grep { $_ == $val } @$opv;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $val;} @$opv 0 0 0
620 } else {
621 0 ▶ next ITEM if grep { $_ eq $val } @$opv;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $val;} @$opv 0 0 0
622 }
623 } elsif ($op eq '=~') {
624 10 ▶ next ITEM unless $val =~ $opv;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /$opv/u 7 3 10
625 } elsif ($op eq '!~') {
626 5 ▶ next ITEM unless $val !~ $opv;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /$opv/u 3 2 5
627 } else {
628 0 die "BUG: Unsupported operator '$op' in attr_selector";
629 }
630  
631 81 PASS:
632 # pass all attribute filters, add to new result
633 #say "D: adding to result: ".$o->{id};
634 push @newres, $o0;
635 } # for each item
636 34 @res = @newres;
637  
638 } elsif ($type eq 'class_selector') {
639  
640 6 my $class = $f->{class};
641  
642 6 my @classes_to_match;
643 6 ▶ for (@{ $opts->{class_prefixes} // [] }, @CLASS_PREFIXES) {
Condition100%2/2
$opts->{"class_prefixes"} // []
l!l
15
MC/DC100%1/1
$opts->{"class_prefixes"} // []
$opts->{"class_prefixes"}
Truth table100%2/2
A
A= $opts->{"class_prefixes"}
Aresult
11
00
644 1 ▶ push @classes_to_match, $_ . (/::$/ ? "" : "::") . $class;
Branch: 1 branch
BranchTrueFalseTotal
/::$/u ? : 0 1 1
645 }
646 6 push @classes_to_match, $class;
647 6 my @filtered_res;
648 RES:
649 6 for my $res (@res) {
650 45 for (@classes_to_match) {
651 47 ▶ if ($res->isa($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->isa($_)) 15 32 47
652 15 push @filtered_res, $res;
653 15 next RES;
654 }
655 }
656 }
657 6 @res = @filtered_res;
658  
659 } elsif ($type eq 'id_selector') {
660  
661 6 ▶ my $method = $opts->{id_method} // 'id';
Condition50%1/2
$opts->{"id_method"} // "id"
l!l
06
MC/DC0%0/1
$opts->{"id_method"} // "id"
$opts->{"id_method"}
Truth table50%1/2
A
A= $opts->{"id_method"}
Aresult
11
00
662 6 my $id = $f->{id};
663  
664 6 ▶ @res = grep { $_->can($method) && $_->$method eq $id } @res;
Condition66%2/3
$_->can($method) && $_->$method eq $id
!ll&&!rl&&r
0384
MC/DC50%1/2
$_->can($method) && $_->$method eq $id
$_->can($method) $_->$method eq $id
Truth table66%2/3
A && B
A= $_->can($method)B= $_->$method eq $id
ABresult
0X0
100
111
665  
666 } elsif ($type eq 'pseudoclass') {
667  
668 20 my $pc = $f->{pseudoclass};
669  
670 20 ▶ if ($pc eq 'first') {
Branch: 20 branches
BranchTrueFalseTotal
if ($pc eq 'first') { } 1 19 20
elsif ($pc eq 'last') { } 1 18 19
elsif ($pc eq 'first-child') { } 3 15 18
elsif ($pc eq 'last-child') { } 1 14 15
elsif ($pc eq 'only-child') { } 1 13 14
elsif ($pc eq 'nth-child') { } 1 12 13
elsif ($pc eq 'nth-last-child') { } 1 11 12
elsif ($pc eq 'first-of-type') { } 1 10 11
elsif ($pc eq 'last-of-type') { } 1 9 10
elsif ($pc eq 'only-of-type') { } 1 8 9
elsif ($pc eq 'nth-of-type') { } 1 7 8
elsif ($pc eq 'nth-last-of-type') { } 1 6 7
elsif ($pc eq 'root') { } 2 4 6
elsif ($pc eq 'has-min-children') { } 0 4 4
elsif ($pc eq 'has-max-children') { } 0 4 4
elsif ($pc eq 'has-children-between') { } 0 4 4
elsif ($pc eq 'empty') { } 1 3 4
elsif ($pc eq 'has') { } 1 2 3
elsif ($pc eq 'not') { } 2 0 2
elsif ($pc eq 'parent') { } 0 0 0
671 1 @res = ($res[0]);
672 } elsif ($pc eq 'last') {
673 1 @res = ($res[-1]);
674 } elsif ($pc eq 'first-child') {
675 3 @res = grep { Code::Includable::Tree::NodeMethods::is_first_child($_) } @res;
676 } elsif ($pc eq 'last-child') {
677 1 @res = grep { Code::Includable::Tree::NodeMethods::is_last_child($_) } @res;
678 } elsif ($pc eq 'only-child') {
679 1 @res = grep { Code::Includable::Tree::NodeMethods::is_only_child($_) } @res;
680 } elsif ($pc eq 'nth-child') {
681 1 @res = grep { Code::Includable::Tree::NodeMethods::is_nth_child($_, $f->{args}[0]) } @res;
682 } elsif ($pc eq 'nth-last-child') {
683 1 @res = grep { Code::Includable::Tree::NodeMethods::is_nth_last_child($_, $f->{args}[0]) } @res;
684 } elsif ($pc eq 'first-of-type') {
685 1 @res = grep { Code::Includable::Tree::NodeMethods::is_first_child_of_type($_) } @res;
686 } elsif ($pc eq 'last-of-type') {
687 1 @res = grep { Code::Includable::Tree::NodeMethods::is_last_child_of_type($_) } @res;
688 } elsif ($pc eq 'only-of-type') {
689 1 @res = grep { Code::Includable::Tree::NodeMethods::is_only_child_of_type($_) } @res;
690 } elsif ($pc eq 'nth-of-type') {
691 1 @res = grep { Code::Includable::Tree::NodeMethods::is_nth_child_of_type($_, $f->{args}[0]) } @res;
692 } elsif ($pc eq 'nth-last-of-type') {
693 1 @res = grep { Code::Includable::Tree::NodeMethods::is_nth_last_child_of_type($_, $f->{args}[0]) } @res;
694 } elsif ($pc eq 'root') {
695 2 @res = grep { Code::Includable::Tree::NodeMethods::is_root($_) } @res;
696 } elsif ($pc eq 'has-min-children') {
697 0 @res = grep { Code::Includable::Tree::NodeMethods::has_min_children($_, $f->{args}[0]) } @res;
698 } elsif ($pc eq 'has-max-children') {
699 0 @res = grep { Code::Includable::Tree::NodeMethods::has_max_children($_, $f->{args}[0]) } @res;
700 } elsif ($pc eq 'has-children-between') {
701 0 @res = grep { Code::Includable::Tree::NodeMethods::has_children_between($_, $f->{args}[0], $f->{args}[1]) } @res;
702 } elsif ($pc eq 'empty') {
703 1 @res = grep { my @c = Code::Includable::Tree::NodeMethods::_children_as_list($_); !@c } @res;
704 } elsif ($pc eq 'has') {
705 @res = _uniq_objects(
706 2 map { reverse Code::Includable::Tree::NodeMethods::ancestors($_) }
707 1 csel($opts, $f->{args}[0], @res)
708 );
709 } elsif ($pc eq 'not') {
710 #say "D: res=(".join(",", map {$_->{id}} @res).")";
711 2 my @matches = csel($opts, $f->{args}[0], @res);
712 #say "D: all_matches=(".join(",", map {$_->{id}} @all_matches).")";
713 2 my %matches_refaddrs;
714 2 for (@matches) { $matches_refaddrs{refaddr($_)}++ }
715 2 @res = grep { !$matches_refaddrs{refaddr($_)} } @res;
716 } elsif ($pc eq 'parent') {
717 0 @res = _uniq_objects(map { Code::Includable::Tree::NodeMethods::retrieve_parent($_) } @res);
718 } else {
719 0 die "Unsupported pseudo-class '$pc'";
720 }
721  
722 }
723  
724 #say "D: intermediate result (after filter): [".join(",",map {$_->{id}} @res)."]";
725 } # for each filter
726 81 @res;
727 }
728  
729 77 ▶sub _sel {
Subroutine _sel: called CC=8 SCAR=20.8
730 77 my ($opts, $sel, @nodes) = @_;
731  
732 77 my @simpsels = @$sel;
733 77 my @res;
734  
735 77 my $i = 0;
736 77 ▶ while (@simpsels) {
Branch: 1 branch
BranchTrueFalseTotal
if (@simpsels) 81 77 158
737 81 ▶ if ($i++ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i++ == 0) { } 77 4 81
738 77 my $simpsel = shift @simpsels;
739 77 @res = _simpsel($opts, $simpsel, 1, @nodes);
740 } else {
741 4 my $combinator = shift @simpsels;
742 4 my $simpsel = shift @simpsels;
743 4 ▶ last unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 0 4 4
744 4 ▶ if ($combinator->{combinator} eq ' ') { # descendant
Branch: 4 branches
BranchTrueFalseTotal
if ($combinator->{'combinator'} eq ' ') { } 1 3 4
elsif ($combinator->{'combinator'} eq '>') { } 1 2 3
elsif ($combinator->{'combinator'} eq '~') { } 1 1 2
elsif ($combinator->{'combinator'} eq '+') { } 1 0 1
745 @res = _simpsel($opts, $simpsel, 1,
746 1 map { Code::Includable::Tree::NodeMethods::_children_as_list($_) } @res);
747 } elsif ($combinator->{combinator} eq '>') { # child
748 @res = _simpsel($opts, $simpsel, 0,
749 1 map { Code::Includable::Tree::NodeMethods::_children_as_list($_) } @res);
750 } elsif ($combinator->{combinator} eq '~') { # sibling
751 @res = _simpsel($opts, $simpsel, 0,
752 1 map { Code::Includable::Tree::NodeMethods::next_siblings($_) } @res);
753 } elsif ($combinator->{combinator} eq '+') { # adjacent sibling
754 @res = _simpsel($opts, $simpsel, 0,
755 5 grep {defined}
756 1 map { Code::Includable::Tree::NodeMethods::next_sibling($_) }
757 @res);
758 } else {
759 0 die "BUG: Unknown combinator '$combinator->{combinator}'";
760 }
761 }
762 }
763  
764 77 @res;
765 }
766  
767 76 ▶sub csel {
Subroutine csel: called CC=8 SCAR=22.0
Pod documented
768 76 my $opts;
769 76 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 75 1 76
770 75 $opts = shift;
771 } else {
772 1 $opts = {};
773 }
774 76 my $expr = shift;
775 76 my @nodes = @_;
776  
777 76 my $pexpr = parse_csel($expr);
778 76 ▶ $pexpr or die "Invalid CSel expression '$expr'";
Branch: 1 branch
BranchTrueFalseTotal
$pexpr or die "Invalid CSel expression '${expr}'" 76 0 76
779  
780 76 ▶ local $Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD = $opts->{get_parent_method} if $opts->{get_parent_method};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"get_parent_method"} 0 76 76
781 76 ▶ local $Code::Includable::Tree::NodeMethods::SET_PARENT_METHOD = $opts->{set_parent_method} if $opts->{set_parent_method};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"set_parent_method"} 0 76 76
782 76 ▶ local $Code::Includable::Tree::NodeMethods::GET_CHILDREN_METHOD = $opts->{get_children_method} if $opts->{get_children_method};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"get_children_method"} 0 76 76
783 76 ▶ local $Code::Includable::Tree::NodeMethods::SET_CHILDREN_METHOD = $opts->{set_children_method} if $opts->{set_children_method};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"set_children_method"} 0 76 76
784  
785 76 my @res = _uniq_objects(map { _sel($opts, $_, @nodes) } @$pexpr );
786  
787 76 ▶ if ($opts->{wrap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'wrap'}) { } 0 76 76
788 0 require Data::CSel::Selection;
789 0 return Data::CSel::Selection->new(\@res);
790 } else {
791 76 return @res;
792 }
793 }
794  
795 1 ▶sub csel_each(&;@) { ## no critic: Subroutines::ProhibitSubroutinePrototypes
Subroutine csel_each: called CC=2 SCAR=6.9
Pod documented
796 1 my $cb = shift;
797 1 for my $node (csel(@_)) {
798 3 local $_ = $node;
799 3 $cb->($_);
800 }
801 1 undef;
802 }
803  
804 4 1;
805 # ABSTRACT: Select tree node objects using CSS Selector-like syntax
806  
807 __END__
↑ Summary CSel/Selection.pm »