Mojo/DOM/CSS.pm

filter: Statementstmt 100% 250 / 250 Branchbran 95.6% 218 / 228 Conditioncond 87.3% 104 / 119 MC/DCmcdc 80.0% 56 / 70 Subroutinesub 100% 28 / 28 Podpod 100% 3 / 3 totaltotal 94.4% 659 / 698 CC 169 SCAR 28.2
CC
169
Cov
96%
_pc
35.0
_compile
30.0
_step_back
17.2
CRAP 16.8
« Mojolicious/Renderer.pm ↑ Summary Mojo/IOLoop/Subprocess.pm »
1 package Mojo::DOM::CSS;
2 31 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 31 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use Mojo::Util qw(dumper trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶use constant DEBUG => $ENV{MOJO_DOM_CSS_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_DOM_CSS_DEBUG'} || 0
l!l
031
MC/DC0%0/1
$ENV{'MOJO_DOM_CSS_DEBUG'} || 0
$ENV{'MOJO_DOM_CSS_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_DOM_CSS_DEBUG'}
Aresult
11
00
8  
9 31 has 'tree';
10  
11 31 my $ESCAPE_RE = qr/\\[^0-9a-fA-F]|\\[0-9a-fA-F]{1,6}/;
12 31 my $ATTR_RE = qr/
13 \[
14 ((?:$ESCAPE_RE|[\w\-])+) # Key
15 (?:
16 (\W)?= # Operator
17 (?:"((?:\\"|[^"])*)"|'((?:\\'|[^'])*)'|([^\]]+?)) # Value
18 (?:\s+(?:(i|I)|s|S))? # Case-sensitivity
19 )?
20 \]
21 /x;
22  
23 51 ▶sub matches {
Subroutine matches: called CC=2 SCAR=6.9
Pod documented
24 51 my $tree = shift->tree;
25 51 ▶ return undef if $tree->[0] ne 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if $tree->[0] ne "tag" 2 49 51
26 49 return _match(_compile(@_), $tree, $tree, _root($tree));
27 }
28  
29 455 ▶sub select { _select(0, shift->tree, _compile(@_)) }
Subroutine select: called CC=1 SCAR=0.0
Pod documented
30 738 ▶sub select_one { _select(1, shift->tree, _compile(@_)) }
Subroutine select_one: called CC=1 SCAR=0.0
Pod documented
31  
32 45 ▶sub _absolutize { [map { _is_scoped($_) ? $_ : [[['pc', 'scope']], ' ', @$_] } @{shift()}] }
Subroutine _absolutize: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
_is_scoped($_) ? : 45 3 48
33  
34 1772 ▶sub _all_tags {
Subroutine _all_tags: called CC=4 SCAR=13.9
35 1772 my $tree = shift;
36  
37 1772 my @tags;
38 1772 ▶ my @queue = @$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree];
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] eq 'root' ? : 678 1094 1772
39 1772 ▶ while (my $current = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @queue) 136443 1772 138215
40 136443 ▶ next unless $current->[0] eq 'tag';
Branch: 1 branch
BranchTrueFalseTotal
unless $current->[0] eq "tag" 90470 45973 136443
41 45973 push @tags, $current;
42 45973 unshift @queue, @$current[4 .. $#$current];
43 }
44  
45 1772 return \@tags;
46 }
47  
48 1655 ▶sub _attr {
Subroutine _attr: called CC=6 SCAR=17.9
49 1655 my ($name, $value_re, $current) = @_;
50  
51 1655 my $attrs = $current->[2];
52 1655 ▶ return undef unless exists $attrs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{$name} 991 664 1655
53  
54 664 my $value = $attrs->{$name};
55 664 ▶ return undef if !defined $value && defined $value_re;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and defined $value_re 1 663 664
Condition100%3/3
not defined $value and defined $value_re
!ll&&!rl&&r
653101
MC/DC100%2/2
not defined $value and defined $value_re
not defined $value defined $value_re
Truth table100%3/3
not(A) and B
A= not defined $valueB= defined $value_re
ABresult
0X0
100
111
56 663 ▶ return 1 if !defined $value_re || $value =~ $value_re;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value_re or $value =~ /$value_re/u 417 246 663
Condition100%3/3
not defined $value_re or $value =~ /$value_re/u
l!l&&r!l&&!r
36381246
MC/DC100%2/2
not defined $value_re or $value =~ /$value_re/u
not defined $value_re $value =~ /$value_re/u
Truth table100%3/3
not(A) or B
A= not defined $value_reB= $value =~ /$value_re/u
ABresult
1X1
011
000
57  
58 246 return undef;
59 }
60  
61 1298 ▶sub _compile {
Subroutine _compile: called CC=30 SCAR=34.0
62 1298 my ($css, %ns) = (trim('' . shift), @_);
63  
64 1298 my $group = [[]];
65 1298 ▶ while (my $selectors = $group->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $selectors = $group->[-1]) 4486 0 4486
66 4486 ▶ push @$selectors, [] unless @$selectors && ref $selectors->[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless @$selectors and ref $selectors->[-1] 2068 2418 4486
Condition100%3/3
@$selectors and ref $selectors->[-1]
!ll&&!rl&&r
13157532418
MC/DC100%2/2
@$selectors and ref $selectors->[-1]
@$selectors ref $selectors->[-1]
Truth table100%3/3
A and B
A= @$selectorsB= ref $selectors->[-1]
ABresult
0X0
100
111
67 4486 my $last = $selectors->[-1];
68  
69 # Separator
70 4486 ▶ if ($css =~ /\G\s*,\s*/gc) { push @$group, [] }
Branch: 7 branches
BranchTrueFalseTotal
if ($css =~ /\G\s*,\s*/cgu) { } 17 4469 4486
elsif ($css =~ /\G\s*([>+~])\s*/cgu) { } 491 3978 4469
elsif ($css =~ /\G\s+/cgu) { } 262 3716 3978
elsif ($css =~ /\G([.#])((?:$ESCAPE_RE\s?|[^,.#:[\s>~+])+)/cgou) { } 170 3546 3716
elsif ($css =~ /\G$ATTR_RE/cgou) { } 250 3296 3546
elsif ($css =~ /\G:([\w\-]+)(?:\(((?:\([^)]+\)|[^)])+)\))?/cgsu) { } 299 2997 3296
elsif ($css =~ /\G((?:$ESCAPE_RE\s|\\[\s\S]|[^,.#:[\s>~+])+)/cgou) { } 1700 1297 2997
71  
72 # Combinator
73 elsif ($css =~ /\G\s*([>+~])\s*/gc) {
74 491 ▶ push @$last, ['pc', 'scope'] unless @$last;
Branch: 1 branch
BranchTrueFalseTotal
unless @$last 11 480 491
75 491 push @$selectors, $1;
76 }
77  
78 # Descendant combinator
79 elsif ($css =~ /\G\s+/gc) {
80 262 ▶ push @$last, ['pc', 'scope'] unless @$last;
Branch: 1 branch
BranchTrueFalseTotal
unless @$last 0 262 262
81 262 push @$selectors, ' ';
82 }
83  
84 # Class or ID
85 elsif ($css =~ /\G([.#])((?:$ESCAPE_RE\s?|[^,.#:[\s>~+])+)/gco) {
86 170 ▶ my ($name, $op) = $1 eq '.' ? ('class', '~') : ('id', '');
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '.' ? : 59 111 170
87 170 push @$last, ['attr', $name, _value($op, $2)];
88 }
89  
90 # Attributes
91 250 ▶ elsif ($css =~ /\G$ATTR_RE/gco) { push @$last, ['attr', _unescape($1), _value($2 // '', $3 // $4 // $5, $6)] }
Condition100%8/8
$2 // ""
l!l
108142
($3 // $4) // $5
l!l&&r!l&&!r
1139245
$3 // $4
l!l&&r!l&&!r
1115134
MC/DC100%1/1
$2 // ""
$2
MC/DC100%3/3
($3 // $4) // $5
$3 $4 $5
Truth table100%2/2
A
A= $2
Aresult
11
00
Truth table100%4/4
A // B // C
A= $3B= $4C= $5
ABCresult
1XX1
01X1
0011
0000
92  
93 # Pseudo-class
94 elsif ($css =~ /\G:([\w\-]+)(?:\(((?:\([^)]+\)|[^)])+)\))?/gcs) {
95 299 my ($name, $args) = (lc $1, $2);
96  
97 # ":text" (raw text)
98 299 ▶ $args = [$args =~ m!^/(.+)/$! ? qr/$1/ : qr/\Q$args\E/i] if $name eq 'text';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "text" 21 278 299
$args =~ m[^/(.+)/$]u ? : 6 15 21
99  
100 # ":is" and ":not" (contains more selectors)
101 299 ▶ $args = _compile($args, %ns) if $name eq 'has' || $name eq 'is' || $name eq 'not';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "has" or $name eq "is" or $name eq "not" 56 243 299
Condition100%6/6
$name eq "has" or $name eq "is" or $name eq "not"
l!l&&r!l&&!r
1145243
$name eq "has" or $name eq "is"
l!l&&r!l&&!r
65288
MC/DC100%3/3
$name eq "has" or $name eq "is" or $name eq "not"
$name eq "has" $name eq "is" $name eq "not"
Truth table100%4/4
A or B or C
A= $name eq "has"B= $name eq "is"C= $name eq "not"
ABCresult
1XX1
01X1
0011
0000
102  
103 # ":nth-*" (with An+B notation)
104 299 ▶ $args = _equation($args) if $name =~ /^nth-/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^nth-/u 117 182 299
105  
106 # ":first-*", ":last-*" (rewrite to ":nth-(last-)*")
107 299 ▶ ($name, $args) = ("nth-$+", [0, 1]) if $name =~ /^(?:first-(.+)|(last-.+))$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(?:first-(.+)|(last-.+))$/u 24 275 299
108  
109 299 push @$last, ['pc', $name, $args];
110 }
111  
112 # Tag
113 elsif ($css =~ /\G((?:$ESCAPE_RE\s|\\[\s\S]|[^,.#:[\s>~+])+)/gco) {
114 1700 ▶ my $alias = (my $name = $1) =~ s/^([^|]*)\|// && $1 ne '*' ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
(my $name = $1) =~ s/^([^|]*)\|//u && $1 ne '*' ? : 69 1631 1700
Condition100%3/3
(my $name = $1) =~ s/^([^|]*)\|//u and $1 ne "*"
!ll&&!rl&&r
1630169
MC/DC100%2/2
(my $name = $1) =~ s/^([^|]*)\|//u and $1 ne "*"
(my $name = $1) =~ s/^([^|]*)\|//u $1 ne "*"
Truth table100%3/3
A and B
A= (my $name = $1) =~ s/^([^|]*)\|//uB= $1 ne "*"
ABresult
0X0
100
111
115 1700 ▶ my $ns = length $alias ? $ns{$alias} // return [['invalid']] : $alias;
Branch: 1 branch
BranchTrueFalseTotal
length $alias ? : 65 1635 1700
Condition100%2/2
$ns{$alias} // (return [["invalid"]])
l!l
641
MC/DC100%1/1
$ns{$alias} // (return [["invalid"]])
$ns{$alias}
Truth table100%2/2
A
A= $ns{$alias}
Aresult
11
00
116 1699 ▶ push @$last, ['tag', $name eq '*' ? undef : _name($name), _unescape($ns)];
Branch: 1 branch
BranchTrueFalseTotal
$name eq '*' ? : 45 1654 1699
117 }
118  
119 1297 ▶ else { pos $css < length $css ? croak "Unknown CSS selector: $css" : last }
Branch: 1 branch
BranchTrueFalseTotal
pos $css < length $css ? : 2 1295 1297
120 }
121  
122 1295 warn qq{-- CSS Selector ($css)\n@{[dumper $group]}} if DEBUG;
123 1295 return $group;
124 }
125  
126 117 ▶sub _equation {
Subroutine _equation: called CC=9 SCAR=22.0
127 117 ▶ return [0, 0] unless my $equation = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $equation = shift() 2 115 117
128  
129 # "even"
130 115 ▶ return [2, 0] if $equation =~ /^\s*even\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $equation =~ /^\s*even\s*$/iu 9 106 115
131  
132 # "odd"
133 106 ▶ return [2, 1] if $equation =~ /^\s*odd\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $equation =~ /^\s*odd\s*$/iu 12 94 106
134  
135 # "4", "+4" or "-4"
136 94 ▶ return [0, $1] if $equation =~ /^\s*((?:\+|-)?\d+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $equation =~ /^\s*((?:\+|-)?\d+)\s*$/u 42 52 94
137  
138 # "n", "4n", "+4n", "-4n", "n+1", "4n-1", "+4n-1" (and other variations)
139 52 ▶ return [0, 0] unless $equation =~ /^\s*((?:\+|-)?(?:\d+)?)?n\s*((?:\+|-)\s*\d+)?\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $equation =~ /^\s*((?:\+|-)?(?:\d+)?)?n\s*((?:\+|-)\s*\d+)?\s*$/iu 1 51 52
140 51 ▶ return [$1 eq '-' ? -1 : !length $1 ? 1 : $1, join('', split(' ', $2 // 0))];
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '-' ? : 18 33 51
!length($1) ? : 6 27 33
Condition100%2/2
$2 // 0
l!l
429
MC/DC100%1/1
$2 // 0
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
141 }
142  
143 688 ▶sub _evaluate {
Subroutine _evaluate: called CC=2 SCAR=6.9
144 688 my ($group, $tree, $scope, $pool) = @_;
145  
146 688 my (@results, %seen);
147 688 push @results, grep { !$seen{$_}++ } _evaluate_one($_, $tree, $scope, $pool) for @$group;
148  
149 688 return \@results;
150 }
151  
152 699 ▶sub _evaluate_one {
Subroutine _evaluate_one: called CC=8 SCAR=20.8
153 699 my ($selector, $tree, $scope, $pool) = @_;
154  
155 # Match the leftmost compound, then propagate forward through combinators
156 699 my @parts = @$selector;
157 699 my $compound = shift @parts;
158 699 ▶ return () unless ref $compound;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $compound 1 698 699
159 698 my @candidates = grep { _selector($compound, $_, $tree, $scope) } @$pool;
160  
161 698 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 756 698 1454
162 756 my $combinator = shift @parts;
163 756 my $next = shift @parts;
164 756 ▶ return () unless ref $next;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $next 0 756 756
165  
166 756 my (%seen, @new);
167 756 for my $node (@candidates) {
168 1735 for my $cand (_step_forward($combinator, $node)) {
169 41010 ▶ next if $seen{$cand}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$cand}++ 37649 3361 41010
170 3361 ▶ push @new, $cand if _selector($next, $cand, $tree, $scope);
Branch: 1 branch
BranchTrueFalseTotal
if _selector($next, $cand, $tree, $scope) 1824 1537 3361
171 }
172 }
173 756 @candidates = @new;
174 }
175  
176 698 return @candidates;
177 }
178  
179 1338 ▶sub _is_scoped {
Subroutine _is_scoped: called CC=8 SCAR=20.8
180 1338 my $selector = shift;
181  
182 1338 ▶ for my $pc (grep { $_->[0] eq 'pc' } map { ref $_ ? @$_ : () } @$selector) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2142 806 2948
183  
184 # Selector with ":scope"
185 361 ▶ return 1 if $pc->[1] eq 'scope';
Branch: 1 branch
BranchTrueFalseTotal
if $pc->[1] eq "scope" 90 271 361
186  
187 # Argument of functional pseudo-class with ":scope"
188 271 ▶ return 1 if ($pc->[1] eq 'has' || $pc->[1] eq 'is' || $pc->[1] eq 'not') && grep { _is_scoped($_) } @{$pc->[2]};
Branch: 1 branch
BranchTrueFalseTotal
if $pc->[1] eq "has" || $pc->[1] eq "is" || $pc->[1] eq "not" and grep {_is_scoped($_);} @{$$pc[2];} 10 261 271
Condition100%9/9
$pc->[1] eq "has" || $pc->[1] eq "is" || $pc->[1] eq "not" and grep {_is_scoped($_);} @{$$pc[2];}
!ll&&!rl&&r
2085310
$pc->[1] eq "has" or $pc->[1] eq "is" or $pc->[1] eq "not"
l!l&&r!l&&!r
1548208
$pc->[1] eq "has" or $pc->[1] eq "is"
l!l&&r!l&&!r
96256
MC/DC100%4/4
$pc->[1] eq "has" || $pc->[1] eq "is" || $pc->[1] eq "not" and grep {_is_scoped($_);} @{$$pc[2];}
$pc->[1] eq "has" $pc->[1] eq "is" $pc->[1] eq "not" grep {_is_scoped($_);} @{$$pc[2];}
Truth table100%7/7
A or B or C and D
A= $pc->[1] eq "has"B= $pc->[1] eq "is"C= $pc->[1] eq "not"D= grep {_is_scoped($_);} @{$$pc[2];}
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
189 }
190  
191 1238 return undef;
192 }
193  
194 157 ▶sub _match {
Subroutine _match: called CC=3 SCAR=11.0
195 157 my ($group, $current, $tree, $scope) = @_;
196 157 ▶ _match_one($_, $current, $tree, $scope) and return 1 for @$group;
Branch: 1 branch
BranchTrueFalseTotal
_match_one($_, $current, $tree, $scope) and return 1 64 103 167
197 93 return undef;
198 }
199  
200 167 ▶sub _match_one {
Subroutine _match_one: called CC=10 SCAR=23.1
201 167 my ($selector, $current, $tree, $scope) = @_;
202  
203 # Match the rightmost compound, then propagate backward through combinators
204 167 my @parts = reverse @$selector;
205 167 my $compound = shift @parts;
206 167 ▶ return undef unless ref $compound && _selector($compound, $current, $tree, $scope);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $compound and _selector($compound, $current, $tree, $scope) 101 66 167
Condition66%2/3
ref $compound and _selector($compound, $current, $tree, $scope)
!ll&&!rl&&r
010166
MC/DC50%1/2
ref $compound and _selector($compound, $current, $tree, $scope)
ref $compound _selector($compound, $current, $tree, $scope)
Truth table66%2/3
A and B
A= ref $compoundB= _selector($compound, $current, $tree, $scope)
ABresult
0X0
100
111
207  
208 66 my @candidates = ($current);
209 66 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 6 64 70
210 6 my $combinator = shift @parts;
211 6 my $next = shift @parts;
212 6 ▶ return undef unless ref $next;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $next 0 6 6
213  
214 6 my (%seen, @new);
215 6 for my $node (@candidates) {
216 6 for my $cand (_step_back($combinator, $node, $scope)) {
217 9 ▶ next if $seen{$cand}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$cand}++ 0 9 9
218 9 ▶ push @new, $cand if _selector($next, $cand, $tree, $scope);
Branch: 1 branch
BranchTrueFalseTotal
if _selector($next, $cand, $tree, $scope) 4 5 9
219 }
220 }
221 6 ▶ return undef unless @new;
Branch: 1 branch
BranchTrueFalseTotal
unless @new 2 4 6
222 4 @candidates = @new;
223 }
224  
225 64 return 1;
226 }
227  
228 1654 ▶sub _name {qr/(?:^|:)\Q@{[_unescape(shift)]}\E$/}
Subroutine _name: called CC=1 SCAR=0.0
229  
230 77 ▶sub _namespace {
Subroutine _namespace: called CC=5 SCAR=16.1
231 77 my ($ns, $current) = @_;
232  
233 77 ▶ my $attr = $current->[1] =~ /^([^:]+):/ ? "xmlns:$1" : 'xmlns';
Branch: 1 branch
BranchTrueFalseTotal
$current->[1] =~ /^([^:]+):/u ? : 4 73 77
234 77 ▶ while ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) 119 0 119
235 119 ▶ last if $current->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $current->[0] eq "root" 4 115 119
236 115 ▶ return $current->[2]{$attr} eq $ns if exists $current->[2]{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $current->[2]{$attr} 73 42 115
237  
238 42 $current = $current->[3];
239 }
240  
241 # Failing to match yields true if searching for no namespace, false otherwise
242 4 return !length $ns;
243 }
244  
245 1836 ▶sub _pc {
Subroutine _pc: called CC=35 SCAR=35.6
246 1836 my ($class, $args, $current, $tree, $scope) = @_;
247  
248 # ":scope" (root can only be a :scope)
249 1836 ▶ return $current eq $scope if $class eq 'scope';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "scope" 494 1342 1836
250 1342 ▶ return undef if $current->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $current->[0] eq "root" 26 1316 1342
251  
252 # ":checked"
253 1316 ▶ return exists $current->[2]{checked} || exists $current->[2]{selected} if $class eq 'checked';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "checked" 195 1121 1316
Condition100%3/3
exists $current->[2]{'checked'} || exists $current->[2]{'selected'}
l!l&&r!l&&!r
2138136
MC/DC100%2/2
exists $current->[2]{'checked'} || exists $current->[2]{'selected'}
exists $current->[2]{'checked'} exists $current->[2]{'selected'}
Truth table100%3/3
A || B
A= exists $current->[2]{'checked'}B= exists $current->[2]{'selected'}
ABresult
1X1
011
000
254  
255 # ":not"
256 1121 ▶ return !_match($args, $current, $current, $scope) if $class eq 'not';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "not" 95 1026 1121
257  
258 # ":is"
259 1026 ▶ return !!_match($args, $current, $current, $scope) if $class eq 'is';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "is" 13 1013 1026
260  
261 # ":has"
262 1013 ▶ return !!_select(1, $current, $args) if $class eq 'has';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "has" 19 994 1013
263  
264 # ":empty"
265 994 ▶ return !grep { !($_->[0] eq 'comment' || $_->[0] eq 'pi') } @$current[4 .. $#$current] if $class eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "empty" 28 966 994
Condition100%3/3
$_->[0] eq 'comment' || $_->[0] eq 'pi'
l!l&&r!l&&!r
2254
MC/DC100%2/2
$_->[0] eq 'comment' || $_->[0] eq 'pi'
$_->[0] eq 'comment' $_->[0] eq 'pi'
Truth table100%3/3
A || B
A= $_->[0] eq 'comment'B= $_->[0] eq 'pi'
ABresult
1X1
011
000
266  
267 # ":root"
268 966 ▶ return $current->[3] && $current->[3][0] eq 'root' if $class eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "root" 52 914 966
Condition66%2/3
$current->[3] && $current->[3][0] eq 'root'
!ll&&!rl&&r
0448
MC/DC50%1/2
$current->[3] && $current->[3][0] eq 'root'
$current->[3] $current->[3][0] eq 'root'
Truth table66%2/3
A && B
A= $current->[3]B= $current->[3][0] eq 'root'
ABresult
0X0
100
111
269  
270 # ":text"
271 914 ▶ return grep { ($_->[0] eq 'text' || $_->[0] eq 'raw') && $_->[1] =~ $args->[0] } @$current[4 .. $#$current]
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "text" 78 836 914
Condition83%5/6
($_->[0] eq 'text' || $_->[0] eq 'raw') && $_->[1] =~ /$args->[0]/u
!ll&&!rl&&r
6612816
$_->[0] eq "text" or $_->[0] eq "raw"
l!l&&r!l&&!r
144066
MC/DC66%2/3
($_->[0] eq 'text' || $_->[0] eq 'raw') && $_->[1] =~ /$args->[0]/u
$_->[0] eq "text" $_->[0] eq "raw" $_->[1] =~ /$args->[0]/u
Truth table60%3/5
A or B && C
A= $_->[0] eq "text"B= $_->[0] eq "raw"C= $_->[1] =~ /$args->[0]/u
ABCresult
00X0
1X00
0100
1X11
0111
272 if $class eq 'text';
273  
274 # ":any-link", ":link" and ":visited"
275 836 ▶ if ($class eq 'any-link' || $class eq 'link' || $class eq 'visited') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "any-link" or $class eq "link" or $class eq "visited") 39 797 836
Condition100%6/6
$class eq "any-link" or $class eq "link" or $class eq "visited"
l!l&&r!l&&!r
2613797
$class eq "any-link" or $class eq "link"
l!l&&r!l&&!r
1313810
MC/DC100%3/3
$class eq "any-link" or $class eq "link" or $class eq "visited"
$class eq "any-link" $class eq "link" $class eq "visited"
Truth table100%4/4
A or B or C
A= $class eq "any-link"B= $class eq "link"C= $class eq "visited"
ABCresult
1XX1
01X1
0011
0000
276 39 ▶ return undef unless $current->[0] eq 'tag' && exists $current->[2]{href};
Branch: 1 branch
BranchTrueFalseTotal
unless $current->[0] eq "tag" and exists $current->[2]{"href"} 18 21 39
Condition66%2/3
$current->[0] eq "tag" and exists $current->[2]{"href"}
!ll&&!rl&&r
01821
MC/DC50%1/2
$current->[0] eq "tag" and exists $current->[2]{"href"}
$current->[0] eq "tag" exists $current->[2]{"href"}
Truth table66%2/3
A and B
A= $current->[0] eq "tag"B= exists $current->[2]{"href"}
ABresult
0X0
100
111
277 21 return !!grep { $current->[1] eq $_ } qw(a area link);
278 }
279  
280 # ":only-child" or ":only-of-type"
281 797 ▶ if ($class eq 'only-child' || $class eq 'only-of-type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "only-child" or $class eq "only-of-type") 26 771 797
Condition100%3/3
$class eq "only-child" or $class eq "only-of-type"
l!l&&r!l&&!r
206771
MC/DC100%2/2
$class eq "only-child" or $class eq "only-of-type"
$class eq "only-child" $class eq "only-of-type"
Truth table100%3/3
A or B
A= $class eq "only-child"B= $class eq "only-of-type"
ABresult
1X1
011
000
282 26 ▶ my $type = $class eq 'only-of-type' ? $current->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class eq 'only-of-type' ? : 6 20 26
283 26 ▶ $_ ne $current and return undef for @{_siblings($current, $type)};
Branch: 1 branch
BranchTrueFalseTotal
$_ ne $current and return undef 20 10 30
284 6 return 1;
285 }
286  
287 # ":nth-child", ":nth-last-child", ":nth-of-type" or ":nth-last-of-type"
288 771 ▶ if (ref $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args) 763 8 771
289 763 ▶ my $type = $class eq 'nth-of-type' || $class eq 'nth-last-of-type' ? $current->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class eq 'nth-of-type' || $class eq 'nth-last-of-type' ? : 68 695 763
Condition100%3/3
$class eq "nth-of-type" or $class eq "nth-last-of-type"
l!l&&r!l&&!r
2741695
MC/DC100%2/2
$class eq "nth-of-type" or $class eq "nth-last-of-type"
$class eq "nth-of-type" $class eq "nth-last-of-type"
Truth table100%3/3
A or B
A= $class eq "nth-of-type"B= $class eq "nth-last-of-type"
ABresult
1X1
011
000
290 763 my @siblings = @{_siblings($current, $type)};
291 763 my $index;
292 763 for my $i (0 .. $#siblings) {
293 3204 ▶ $index = $i, last if $siblings[$i] eq $current;
Branch: 1 branch
BranchTrueFalseTotal
if $siblings[$i] eq $current 763 2441 3204
294 }
295 763 ▶ $index = $#siblings - $index if $class eq 'nth-last-child' || $class eq 'nth-last-of-type';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "nth-last-child" or $class eq "nth-last-of-type" 233 530 763
Condition100%3/3
$class eq "nth-last-child" or $class eq "nth-last-of-type"
l!l&&r!l&&!r
19241530
MC/DC100%2/2
$class eq "nth-last-child" or $class eq "nth-last-of-type"
$class eq "nth-last-child" $class eq "nth-last-of-type"
Truth table100%3/3
A or B
A= $class eq "nth-last-child"B= $class eq "nth-last-of-type"
ABresult
1X1
011
000
296 763 $index++;
297  
298 763 my $delta = $index - $args->[1];
299 763 ▶ return 1 if $delta == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 0 117 646 763
300 646 ▶ return $args->[0] != 0 && ($delta < 0) == ($args->[0] < 0) && $delta % $args->[0] == 0;
Condition100%6/6
$args->[0] != 0 && $delta < 0 == $args->[0] < 0 && $delta % $args->[0] == 0
!ll&&!rl&&r
301156189
$args->[0] != 0 and $delta < 0 == $args->[0] < 0
!ll&&!rl&&r
189112345
MC/DC100%3/3
$args->[0] != 0 && $delta < 0 == $args->[0] < 0 && $delta % $args->[0] == 0
$args->[0] != 0 $delta < 0 == $args->[0] < 0 $delta % $args->[0] == 0
Truth table100%4/4
A and B && C
A= $args->[0] != 0B= $delta < 0 == $args->[0] < 0C= $delta % $args->[0] == 0
ABCresult
0XX0
10X0
1100
1111
301 }
302  
303 # Everything else
304 8 return undef;
305 }
306  
307 94 ▶sub _root {
Subroutine _root: called CC=2 SCAR=6.9
308 94 my $tree = shift;
309 94 ▶ $tree = $tree->[3] while $tree->[0] ne 'root';
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] ne "root" and do { $tree = $tree->[3] } 163 94 257
310 94 return $tree;
311 }
312  
313 1210 ▶sub _select {
Subroutine _select: called CC=13 SCAR=25.6
314 1210 my ($one, $scope, $group) = @_;
315  
316 # Scoped selectors require the whole tree to be searched
317 1210 my $tree = $scope;
318 1210 ▶ ($group, $tree) = (_absolutize($group), _root($scope)) if grep { _is_scoped($_) } @$group;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_is_scoped($_);} @$group 45 1165 1210
319  
320 # Without combinators the first match in document order can be returned right away
321 1210 ▶ if ($one && !grep { @$_ != 1 || !ref $_->[0] } @$group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one and not grep({@$_ != 1 or not ref $_->[0];} @$group)) 522 688 1210
Condition100%6/6
$one and not grep({@$_ != 1 or not ref $_->[0];} @$group)
!ll&&!rl&&r
454234522
@$_ != 1 || !ref($_->[0])
l!l&&r!l&&!r
2361524
MC/DC100%2/2
$one and not grep({@$_ != 1 or not ref $_->[0];} @$group)
$one not grep({@$_ != 1 or not ref $_->[0];} @$group)
MC/DC100%2/2
@$_ != 1 || !ref($_->[0])
@$_ != 1 !ref($_->[0])
Truth table100%3/3
A and not(B)
A= $oneB= not grep({@$_ != 1 or not ref $_->[0];} @$group)
ABresult
0X0
100
111
Truth table100%3/3
A || not(B)
A= @$_ != 1B= !ref($_->[0])
ABresult
1X1
011
000
322 522 ▶ my @queue = @$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree];
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] eq 'root' ? : 492 30 522
323 522 ▶ while (my $current = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @queue) 2851 53 2904
324 2851 ▶ next unless $current->[0] eq 'tag';
Branch: 1 branch
BranchTrueFalseTotal
unless $current->[0] eq "tag" 1496 1355 2851
325 1355 ▶ return $current if grep { _selector($_->[0], $current, $tree, $scope) } @$group;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_selector($_->[0], $current, $tree, $scope);} @$group 469 886 1355
326 886 unshift @queue, @$current[4 .. $#$current];
327 }
328 53 return undef;
329 }
330  
331 # Pool includes $tree so ":scope" can match it, but results exclude it
332 688 my $tags = _all_tags($tree);
333 688 my %match = map { $_ => 1 } @{_evaluate($group, $tree, $scope, [$tree, @$tags])};
334  
335 688 my @results;
336 688 for my $node (@$tags) {
337 5727 ▶ next unless $match{$node};
Branch: 1 branch
BranchTrueFalseTotal
unless $match{$node} 4462 1265 5727
338 1265 ▶ return $node if $one;
Branch: 1 branch
BranchTrueFalseTotal
if $one 208 1057 1265
339 1057 push @results, $node;
340 }
341  
342 480 ▶ return $one ? undef : \@results;
Branch: 1 branch
BranchTrueFalseTotal
$one ? : 26 454 480
343 }
344  
345 11900 ▶sub _selector {
Subroutine _selector: called CC=13 SCAR=25.6
346 11900 my ($selector, $current, $tree, $scope) = @_;
347  
348 # The root might be the scope
349 11900 my $is_tag = $current->[0] eq 'tag';
350 11900 for my $s (@$selector) {
351 12967 my $type = $s->[0];
352  
353 # Tag
354 12967 ▶ if ($is_tag && $type eq 'tag') {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_tag and $type eq 'tag') { } 8854 4113 12967
elsif ($is_tag and $type eq 'attr') { } 1655 2458 4113
elsif ($type eq 'pc') { } 1836 622 2458
Condition100%3/3
$is_tag and $type eq "tag"
!ll&&!rl&&r
68734268854
MC/DC100%2/2
$is_tag and $type eq "tag"
$is_tag $type eq "tag"
Truth table100%3/3
A and B
A= $is_tagB= $type eq "tag"
ABresult
0X0
100
111
355 8854 ▶ return undef if defined $s->[1] && $current->[1] !~ $s->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->[1] and not $current->[1] =~ /$s->[1]/u 5122 3732 8854
Condition100%3/3
defined $s->[1] and not $current->[1] =~ /$s->[1]/u
!ll&&!rl&&r
87828545122
MC/DC100%2/2
defined $s->[1] and not $current->[1] =~ /$s->[1]/u
defined $s->[1] not $current->[1] =~ /$s->[1]/u
Truth table100%3/3
A and not(B)
A= defined $s->[1]B= not $current->[1] =~ /$s->[1]/u
ABresult
0X0
100
111
356 3732 ▶ return undef if defined $s->[2] && !_namespace($s->[2], $current);
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->[2] and not _namespace($s->[2], $current) 16 3716 3732
Condition100%3/3
defined $s->[2] and not _namespace($s->[2], $current)
!ll&&!rl&&r
36556116
MC/DC100%2/2
defined $s->[2] and not _namespace($s->[2], $current)
defined $s->[2] not _namespace($s->[2], $current)
Truth table100%3/3
A and not(B)
A= defined $s->[2]B= not _namespace($s->[2], $current)
ABresult
0X0
100
111
357 }
358  
359 # Attribute
360 1655 ▶ elsif ($is_tag && $type eq 'attr') { return undef unless _attr(@$s[1, 2], $current) }
Branch: 1 branch
BranchTrueFalseTotal
unless _attr(@$s[1, 2], $current) 1238 417 1655
Condition100%3/3
$is_tag and $type eq "attr"
!ll&&!rl&&r
68717711655
MC/DC100%2/2
$is_tag and $type eq "attr"
$is_tag $type eq "attr"
Truth table100%3/3
A and B
A= $is_tagB= $type eq "attr"
ABresult
0X0
100
111
361  
362 # Pseudo-class
363 1836 ▶ elsif ($type eq 'pc') { return undef unless _pc(@$s[1, 2], $current, $tree, $scope) }
Branch: 1 branch
BranchTrueFalseTotal
unless _pc(@$s[1, 2], $current, $tree, $scope) 1295 541 1836
364  
365 # No match
366 622 else { return undef }
367 }
368  
369 3607 return 1;
370 }
371  
372 892 ▶sub _siblings {
Subroutine _siblings: called CC=3 SCAR=11.0
373 892 my ($current, $type) = @_;
374 892 my $parent = $current->[3];
375 892 ▶ my @siblings = grep { $_->[0] eq 'tag' } @$parent[($parent->[0] eq 'root' ? 1 : 4) .. $#$parent];
Branch: 1 branch
BranchTrueFalseTotal
$parent->[0] eq 'root' ? : 97 795 892
376 892 ▶ @siblings = grep { $type eq $_->[1] } @siblings if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 74 818 892
377 892 return \@siblings;
378 }
379  
380 6 ▶sub _step_back {
Subroutine _step_back: called CC=13 SCAR=28.5
381 6 my ($combinator, $node, $scope) = @_;
382  
383 # " " (ancestors) and ">" (parent only)
384 6 ▶ if ($combinator eq ' ' || $combinator eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($combinator eq " " or $combinator eq ">") 2 4 6
Condition66%2/3
$combinator eq " " or $combinator eq ">"
l!l&&r!l&&!r
024
MC/DC50%1/2
$combinator eq " " or $combinator eq ">"
$combinator eq " " $combinator eq ">"
Truth table66%2/3
A or B
A= $combinator eq " "B= $combinator eq ">"
ABresult
1X1
011
000
385 2 my @ancestors;
386 2 ▶ while ($node ne $scope && $node->[0] ne 'root' && ($node = $node->[3])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node ne $scope and $node->[0] ne "root" and $node = $node->[3]) 2 0 2
Condition33%2/6
$node ne $scope and $node->[0] ne "root" and $node = $node->[3]
!ll&&!rl&&r
002
$node ne $scope and $node->[0] ne "root"
!ll&&!rl&&r
002
MC/DC0%0/3
$node ne $scope and $node->[0] ne "root" and $node = $node->[3]
$node ne $scope $node->[0] ne "root" $node = $node->[3]
Truth table25%1/4
A and B and C
A= $node ne $scopeB= $node->[0] ne "root"C= $node = $node->[3]
ABCresult
0XX0
10X0
1100
1111
387 2 push @ancestors, $node;
388 2 ▶ last if $combinator eq '>' || $node eq $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $combinator eq ">" or $node eq $scope 2 0 2
Condition33%1/3
$combinator eq ">" or $node eq $scope
l!l&&r!l&&!r
200
MC/DC0%0/2
$combinator eq ">" or $node eq $scope
$combinator eq ">" $node eq $scope
Truth table33%1/3
A or B
A= $combinator eq ">"B= $node eq $scope
ABresult
1X1
011
000
389 }
390 2 return @ancestors;
391 }
392  
393 # "~" (preceding siblings) and "+" (immediately preceding)
394 4 ▶ return () if $node->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] eq "root" 0 4 4
395 4 my @prev;
396 4 for my $sib (@{_siblings($node)}) {
397 14 ▶ last if $sib eq $node;
Branch: 1 branch
BranchTrueFalseTotal
if $sib eq $node 4 10 14
398 10 push @prev, $sib;
399 }
400 4 ▶ return $combinator eq '+' ? ($prev[-1] || ()) : @prev;
Branch: 1 branch
BranchTrueFalseTotal
$combinator eq '+' ? : 2 2 4
Condition33%1/3
$prev[-1] or ()
l!l&&r!l&&!r
200
MC/DC0%0/2
$prev[-1] or ()
$prev[-1] ()
Truth table33%1/3
A or B
A= $prev[-1]B= ()
ABresult
1X1
011
000
401 }
402  
403 1735 ▶sub _step_forward {
Subroutine _step_forward: called CC=10 SCAR=23.1
404 1735 my ($combinator, $node) = @_;
405  
406 # " " (descendants)
407 1735 ▶ return @{_all_tags($node)} if $combinator eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $combinator eq " " 1084 651 1735
408  
409 # ">" (children only)
410 651 ▶ return grep { $_->[0] eq 'tag' } @$node[($node->[0] eq 'root' ? 1 : 4) .. $#$node] if $combinator eq '>';
Branch: 2 branches
BranchTrueFalseTotal
if $combinator eq ">" 552 99 651
$node->[0] eq 'root' ? : 3 549 552
411  
412 # "~" (following siblings) and "+" (immediately following)
413 99 ▶ return () if $node->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] eq "root" 0 99 99
414 99 my (@next, $found);
415 99 for my $sib (@{_siblings($node)}) {
416 405 ▶ push @next, $sib if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 175 230 405
417 405 ▶ $found = 1 if $sib eq $node;
Branch: 1 branch
BranchTrueFalseTotal
if $sib eq $node 99 306 405
418 }
419 99 ▶ return $combinator eq '+' ? ($next[0] || ()) : @next;
Branch: 1 branch
BranchTrueFalseTotal
$combinator eq '+' ? : 47 52 99
Condition66%2/3
$next[0] or ()
l!l&&r!l&&!r
4403
MC/DC50%1/2
$next[0] or ()
$next[0] ()
Truth table66%2/3
A or B
A= $next[0]B= ()
ABresult
1X1
011
000
420 }
421  
422 3979 ▶sub _unescape {
Subroutine _unescape: called CC=2 SCAR=6.9
423 3979 ▶ return undef unless defined(my $value = shift);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $value = shift()) 1631 2348 3979
424  
425 # Remove escaped newlines
426 2348 $value =~ s/\\\n//g;
427  
428 # Unescape Unicode characters
429 2348 $value =~ s/\\([0-9a-fA-F]{1,6})\s?/pack 'U', hex $1/ge;
430  
431 # Remove backslash
432 2348 $value =~ s/\\//g;
433  
434 2348 return $value;
435 }
436  
437 420 ▶sub _value {
Subroutine _value: called CC=8 SCAR=20.8
438 420 my ($op, $value, $insensitive) = @_;
439 420 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 44 376 420
440 376 ▶ $value = ($insensitive ? '(?i)' : '') . quotemeta _unescape($value);
Branch: 1 branch
BranchTrueFalseTotal
$insensitive ? : 10 366 376
441  
442 # "~=" (word)
443 376 ▶ return qr/(?:^|\s+)$value(?:\s+|$)/ if $op eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "~" 82 294 376
444  
445 # "|=" (hyphen-separated)
446 294 ▶ return qr/^$value(?:-|$)/ if $op eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "|" 10 284 294
447  
448 # "*=" (contains)
449 284 ▶ return qr/$value/ if $op eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "*" 12 272 284
450  
451 # "^=" (begins with)
452 272 ▶ return qr/^$value/ if $op eq '^';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "^" 32 240 272
453  
454 # "$=" (ends with)
455 240 ▶ return qr/$value$/ if $op eq '$';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "\$" 31 209 240
456  
457 # Everything else
458 209 return qr/^$value$/;
459 }
460  
461 31 1;
462  
463 =encoding utf8
464  
465 =head1 NAME
466  
467 Mojo::DOM::CSS - CSS selector engine
468  
469 =head1 SYNOPSIS
470  
471 use Mojo::DOM::CSS;
472  
473 # Select elements from DOM tree
474 my $css = Mojo::DOM::CSS->new(tree => $tree);
475 my $elements = $css->select('h1, h2, h3');
476  
477 =head1 DESCRIPTION
478  
479 L<Mojo::DOM::CSS> is the CSS selector engine used by L<Mojo::DOM>, based on the L<HTML Living
480 Standard|https://html.spec.whatwg.org> and L<Selectors Level 3|https://www.w3.org/TR/css3-selectors/>.
481  
482 =head1 SELECTORS
483  
484 All CSS selectors that make sense for a standalone parser are supported.
485  
486 =head2 *
487  
488 Any element.
489  
490 my $all = $css->select('*');
491  
492 =head2 E
493  
494 An element of type C<E>.
495  
496 my $title = $css->select('title');
497  
498 =head2 E[foo]
499  
500 An C<E> element with a C<foo> attribute.
501  
502 my $links = $css->select('a[href]');
503  
504 =head2 E[foo="bar"]
505  
506 An C<E> element whose C<foo> attribute value is exactly equal to C<bar>.
507  
508 my $case_sensitive = $css->select('input[type="hidden"]');
509 my $case_sensitive = $css->select('input[type=hidden]');
510  
511 =head2 E[foo="bar" i]
512  
513 An C<E> element whose C<foo> attribute value is exactly equal to any (ASCII-range) case-permutation of C<bar>. Note
514 that this selector is B<EXPERIMENTAL> and might change without warning!
515  
516 my $case_insensitive = $css->select('input[type="hidden" i]');
517 my $case_insensitive = $css->select('input[type=hidden i]');
518 my $case_insensitive = $css->select('input[class~="foo" i]');
519  
520 This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
521  
522 =head2 E[foo="bar" s]
523  
524 An C<E> element whose C<foo> attribute value is exactly and case-sensitively equal to C<bar>. Note that this selector
525 is B<EXPERIMENTAL> and might change without warning!
526  
527 my $case_sensitive = $css->select('input[type="hidden" s]');
528  
529 This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
530  
531 =head2 E[foo~="bar"]
532  
533 An C<E> element whose C<foo> attribute value is a list of whitespace-separated values, one of which is exactly equal to
534 C<bar>.
535  
536 my $foo = $css->select('input[class~="foo"]');
537 my $foo = $css->select('input[class~=foo]');
538  
539 =head2 E[foo^="bar"]
540  
541 An C<E> element whose C<foo> attribute value begins exactly with the string C<bar>.
542  
543 my $begins_with = $css->select('input[name^="f"]');
544 my $begins_with = $css->select('input[name^=f]');
545  
546 =head2 E[foo$="bar"]
547  
548 An C<E> element whose C<foo> attribute value ends exactly with the string C<bar>.
549  
550 my $ends_with = $css->select('input[name$="o"]');
551 my $ends_with = $css->select('input[name$=o]');
552  
553 =head2 E[foo*="bar"]
554  
555 An C<E> element whose C<foo> attribute value contains the substring C<bar>.
556  
557 my $contains = $css->select('input[name*="fo"]');
558 my $contains = $css->select('input[name*=fo]');
559  
560 =head2 E[foo|="en"]
561  
562 An C<E> element whose C<foo> attribute has a hyphen-separated list of values beginning (from the left) with C<en>.
563  
564 my $english = $css->select('link[hreflang|=en]');
565  
566 =head2 E:root
567  
568 An C<E> element, root of the document.
569  
570 my $root = $css->select(':root');
571  
572 =head2 E:nth-child(n)
573  
574 An C<E> element, the C<n-th> child of its parent.
575  
576 my $third = $css->select('div:nth-child(3)');
577 my $odd = $css->select('div:nth-child(odd)');
578 my $even = $css->select('div:nth-child(even)');
579 my $top3 = $css->select('div:nth-child(-n+3)');
580  
581 =head2 E:nth-last-child(n)
582  
583 An C<E> element, the C<n-th> child of its parent, counting from the last one.
584  
585 my $third = $css->select('div:nth-last-child(3)');
586 my $odd = $css->select('div:nth-last-child(odd)');
587 my $even = $css->select('div:nth-last-child(even)');
588 my $bottom3 = $css->select('div:nth-last-child(-n+3)');
589  
590 =head2 E:nth-of-type(n)
591  
592 An C<E> element, the C<n-th> sibling of its type.
593  
594 my $third = $css->select('div:nth-of-type(3)');
595 my $odd = $css->select('div:nth-of-type(odd)');
596 my $even = $css->select('div:nth-of-type(even)');
597 my $top3 = $css->select('div:nth-of-type(-n+3)');
598  
599 =head2 E:nth-last-of-type(n)
600  
601 An C<E> element, the C<n-th> sibling of its type, counting from the last one.
602  
603 my $third = $css->select('div:nth-last-of-type(3)');
604 my $odd = $css->select('div:nth-last-of-type(odd)');
605 my $even = $css->select('div:nth-last-of-type(even)');
606 my $bottom3 = $css->select('div:nth-last-of-type(-n+3)');
607  
608 =head2 E:first-child
609  
610 An C<E> element, first child of its parent.
611  
612 my $first = $css->select('div p:first-child');
613  
614 =head2 E:last-child
615  
616 An C<E> element, last child of its parent.
617  
618 my $last = $css->select('div p:last-child');
619  
620 =head2 E:first-of-type
621  
622 An C<E> element, first sibling of its type.
623  
624 my $first = $css->select('div p:first-of-type');
625  
626 =head2 E:last-of-type
627  
628 An C<E> element, last sibling of its type.
629  
630 my $last = $css->select('div p:last-of-type');
631  
632 =head2 E:only-child
633  
634 An C<E> element, only child of its parent.
635  
636 my $lonely = $css->select('div p:only-child');
637  
638 =head2 E:only-of-type
639  
640 An C<E> element, only sibling of its type.
641  
642 my $lonely = $css->select('div p:only-of-type');
643  
644 =head2 E:empty
645  
646 An C<E> element that has no children (including text nodes).
647  
648 my $empty = $css->select(':empty');
649  
650 =head2 E:any-link
651  
652 Alias for L</"E:link">. Note that this selector is B<EXPERIMENTAL> and might change without warning! This selector is
653 part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
654  
655 =head2 E:link
656  
657 An C<E> element being the source anchor of a hyperlink of which the target is not yet visited (C<:link>) or already
658 visited (C<:visited>). Note that L<Mojo::DOM::CSS> is not stateful, therefore C<:any-link>, C<:link> and C<:visited>
659 yield exactly the same results.
660  
661 my $links = $css->select(':any-link');
662 my $links = $css->select(':link');
663 my $links = $css->select(':visited');
664  
665 =head2 E:visited
666  
667 Alias for L</"E:link">.
668  
669 =head2 E:scope
670  
671 An C<E> element being a designated reference element. Note that this selector is B<EXPERIMENTAL> and might change
672 without warning!
673  
674 my $scoped = $css->select('a:not(:scope > a)');
675 my $scoped = $css->select('div :scope p');
676 my $scoped = $css->select('~ p');
677  
678 This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
679  
680 =head2 E:checked
681  
682 A user interface element C<E> which is checked (for instance a radio-button or checkbox).
683  
684 my $input = $css->select(':checked');
685  
686 =head2 E.warning
687  
688 An C<E> element whose class is "warning".
689  
690 my $warning = $css->select('div.warning');
691  
692 =head2 E#myid
693  
694 An C<E> element with C<ID> equal to "myid".
695  
696 my $foo = $css->select('div#foo');
697  
698 =head2 E:not(s1, s2)
699  
700 An C<E> element that does not match either compound selector C<s1> or compound selector C<s2>. Note that support for
701 compound selectors is B<EXPERIMENTAL> and might change without warning!
702  
703 my $others = $css->select('div p:not(:first-child, :last-child)');
704  
705 Support for compound selectors was added as part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is
706 still a work in progress.
707  
708 =head2 E:is(s1, s2)
709  
710 An C<E> element that matches compound selector C<s1> and/or compound selector C<s2>. Note that this selector is
711 B<EXPERIMENTAL> and might change without warning!
712  
713 my $headers = $css->select(':is(section, article, aside, nav) h1');
714  
715 This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
716  
717 =head2 E:has(rs1, rs2)
718  
719 An C<E> element, if either of the relative selectors C<rs1> or C<rs2>, when evaluated with C<E> as the :scope elements,
720 match an element. Note that this selector is B<EXPERIMENTAL> and might change without warning!
721  
722 my $link = $css->select('a:has(> img)');
723  
724 This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress.
725 Also be aware that this feature is currently marked C<at-risk>, so there is a high chance that it will get removed
726 completely.
727  
728 =head2 E:text(string_or_regex)
729  
730 An C<E> element containing text content that substring matches C<string_or_regex> case-insensitively or that regex
731 matches C<string_or_regex>. For regular expressions use the format C<:text(/.../)>. Note that this selector is
732 B<EXPERIMENTAL> and might change without warning!
733  
734 # Substring match
735 my $login = $css->select(':text(Log in)');
736  
737 # Regex match
738 my $login = $css->select(':text(/Log ?in/)');
739  
740 # Regex match (case-insensitive)
741 my $login = $css->select(':text(/(?i:Log ?in)/)');
742  
743 This is a custom selector for L<Mojo::DOM> and not part of any spec.
744  
745 =head2 A|E
746  
747 An C<E> element that belongs to the namespace alias C<A> from L<CSS Namespaces Module Level
748 3|https://www.w3.org/TR/css-namespaces-3/>. Key/value pairs passed to selector methods are used to declare namespace
749 aliases.
750  
751 my $elem = $css->select('lq|elem', lq => 'http://example.com/q-markup');
752  
753 Using an empty alias searches for an element that belongs to no namespace.
754  
755 my $div = $c->select('|div');
756  
757 =head2 E F
758  
759 An C<F> element descendant of an C<E> element.
760  
761 my $headlines = $css->select('div h1');
762  
763 =head2 E E<gt> F
764  
765 An C<F> element child of an C<E> element.
766  
767 my $headlines = $css->select('html > body > div > h1');
768  
769 =head2 E + F
770  
771 An C<F> element immediately preceded by an C<E> element.
772  
773 my $second = $css->select('h1 + h2');
774  
775 =head2 E ~ F
776  
777 An C<F> element preceded by an C<E> element.
778  
779 my $second = $css->select('h1 ~ h2');
780  
781 =head2 E, F, G
782  
783 Elements of type C<E>, C<F> and C<G>.
784  
785 my $headlines = $css->select('h1, h2, h3');
786  
787 =head2 E[foo=bar][bar=baz]
788  
789 An C<E> element whose attributes match all following attribute selectors.
790  
791 my $links = $css->select('a[foo^=b][foo$=ar]');
792  
793 =head1 ATTRIBUTES
794  
795 L<Mojo::DOM::CSS> implements the following attributes.
796  
797 =head2 tree
798  
799 my $tree = $css->tree;
800 $css = $css->tree(['root']);
801  
802 Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
803  
804 =head1 METHODS
805  
806 L<Mojo::DOM::CSS> inherits all methods from L<Mojo::Base> and implements the following new ones.
807  
808 =head2 matches
809  
810 my $bool = $css->matches('head > title');
811 my $bool = $css->matches('svg|line', svg => 'http://www.w3.org/2000/svg');
812  
813 Check if first node in L</"tree"> matches the CSS selector. Trailing key/value pairs can be used to declare xml
814 namespace aliases.
815  
816 =head2 select
817  
818 my $results = $css->select('head > title');
819 my $results = $css->select('svg|line', svg => 'http://www.w3.org/2000/svg');
820  
821 Run CSS selector against L</"tree">. Trailing key/value pairs can be used to declare xml namespace aliases.
822  
823 =head2 select_one
824  
825 my $result = $css->select_one('head > title');
826 my $result =
827 $css->select_one('svg|line', svg => 'http://www.w3.org/2000/svg');
828  
829 Run CSS selector against L</"tree"> and stop as soon as the first node matched. Trailing key/value pairs can be used to
830 declare xml namespace aliases.
831  
832 =head1 DEBUGGING
833  
834 You can set the C<MOJO_DOM_CSS_DEBUG> environment variable to get some advanced diagnostics information printed to
835 C<STDERR>.
836  
837 MOJO_DOM_CSS_DEBUG=1
838  
839 =head1 SEE ALSO
840  
841 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
842  
843 =cut
« Mojolicious/Renderer.pm ↑ Summary Mojo/IOLoop/Subprocess.pm »