Zoom/SelectorParser.pm

filter: Statementstmt 100% 107 / 107 Branchbran 95.1% 59 / 62 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 2 totaltotal 96.5% 194 / 201 SCAR 36.4
CC
38
Cov
98%
_raw_parse_simple_selector
15.0
parse_selector
9.0
__ANON__
3.0
CRAP 38.0
« Zoom/FilterBuilder.pm ↑ Summary Zoom/FilterStream.pm »
1 package HTML::Zoom::SelectorParser;
2  
3 14 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use base qw(HTML::Zoom::SubObject);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 my $sel_char = '-\w_';
8 14 my $sel_meta_char = q-!"#$%&'()*+,./:;<=>?@[\]^`{|}~-;
9 14 my $sel_item = qr/(?:(?:\\[\Q$sel_meta_char\E])|[$sel_char])/;
10 14 my $sel_re = qr/($sel_item+)/;
11 14 my $match_value_re = qr/"?($sel_item*)"?/;
12  
13  
14 15 ▶sub new { bless({}, shift) }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15  
16 sub _raw_parse_simple_selector {
17 380 ▶ for ($_[1]) { # same pos() as outside
Subroutine _raw_parse_simple_selector: called CC=15 SCAR=27.1
18  
19 # '*' - match anything
20  
21 /\G\*/gc and
22 380 ▶ return sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
/\G\*/cg and return sub { 1; } 1 379 380
23  
24 # 'element' - match on tag name
25  
26 /\G$sel_re/gc and
27 379 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G$sel_re/cg and return do { my $name = $_[0]->_unescape($1); sub { $_[0]{"name"} and $_[0]{"name"} eq $name; } } 98 281 379
28 98 my $name = $_[0]->_unescape($1);
29 286 ▶ sub { $_[0]->{name} && $_[0]->{name} eq $name }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"name"} and $_[0]{"name"} eq $name 286 0 286
30 98 };
31  
32 # '#id' - match on id attribute
33  
34 /\G#$sel_re/gc and
35 281 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G#$sel_re/cg and return do { my $id = $_[0]->_unescape($1); sub { $_[0]{"attrs"}{"id"} and $_[0]{"attrs"}{"id"} eq $id; } } 4 277 281
36 4 my $id = $_[0]->_unescape($1);
37 21 ▶ sub { $_[0]->{attrs}{id} && $_[0]->{attrs}{id} eq $id }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"attrs"}{"id"} and $_[0]{"attrs"}{"id"} eq $id 6 15 21
38 4 };
39  
40 # '.class1.class2' - match on intersection of classes
41  
42 /\G((?:\.$sel_re)+)/gc and
43 277 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G((?:\.$sel_re)+)/cg and return do { my $cls = $1; $cls =~ s/^\.//; my(@cl) = map($_[0]->_unescape($_), split(/(?<!\\)\./, $cls, 0)); sub { $_[0]{"attrs"}{"class"} and not grep((!($_[0]{"attrs"}{"class"} =~ /(^|\s+)\Q$_\E($|\s+)/)), @cl); } } 83 194 277
44 83 my $cls = $1; $cls =~ s/^\.//;
45 83 my @cl = map $_[0]->_unescape($_), split(/(?<!\\)\./, $cls);
46 sub {
47 $_[0]->{attrs}{class}
48 394 ▶ && !grep $_[0]->{attrs}{class} !~ /(^|\s+)\Q$_\E($|\s+)/, @cl
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"attrs"}{"class"} and not grep((!($_[0]{"attrs"}{"class"} =~ /(^|\s+)\Q$_\E($|\s+)/)), @cl) 260 134 394
49 }
50 83 };
51  
52 # '[attr^=foo]' - match attribute with ^ anchored regex
53 /\G\[$sel_re\^=$match_value_re\]/gc and
54 194 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re\^=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/^\Q$value\E/; } } 1 193 194
55 1 my $attribute = $_[0]->_unescape($1);
56 1 my $value = $_[0]->_unescape($2);
57 sub {
58 exists $_[0]->{attrs}{$attribute}
59 2 ▶ && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/^\Q$value\E/ 1 1 2
60 }
61 1 };
62  
63 # '[attr$=foo]' - match attribute with $ anchored regex
64 /\G\[$sel_re\$=$match_value_re\]/gc and
65 193 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re\$=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\Q$value\E$/; } } 1 192 193
66 1 my $attribute = $_[0]->_unescape($1);
67 1 my $value = $_[0]->_unescape($2);
68 sub {
69 exists $_[0]->{attrs}{$attribute}
70 2 ▶ && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\Q$value\E$/ 1 1 2
71 }
72 1 };
73  
74 # '[attr*=foo] - match attribute with regex:
75 /\G\[$sel_re\*=$match_value_re\]/gc and
76 192 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re\*=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\Q$value\E/; } } 2 190 192
77 2 my $attribute = $_[0]->_unescape($1);
78 2 my $value = $_[0]->_unescape($2);
79 sub {
80 exists $_[0]->{attrs}{$attribute}
81 4 ▶ && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\Q$value\E/ 2 2 4
82 }
83 2 };
84  
85 # '[attr~=bar]' - match attribute contains word
86 /\G\[$sel_re~=$match_value_re\]/gc and
87 190 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re~=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\b\Q$value\E\b/; } } 1 189 190
88 1 my $attribute = $_[0]->_unescape($1);
89 1 my $value = $_[0]->_unescape($2);
90 sub {
91 exists $_[0]->{attrs}{$attribute}
92 2 ▶ && $_[0]->{attrs}{$attribute} =~ qr/\b\Q$value\E\b/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/\b\Q$value\E\b/ 1 1 2
93 }
94 1 };
95  
96 # '[attr!=bar]' - match attribute contains prefix (for language matches)
97 /\G\[$sel_re\|=$match_value_re\]/gc and
98 189 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re\|=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/^\Q$value\E(?:-|$)/; } } 1 188 189
99 1 my $attribute = $_[0]->_unescape($1);
100 1 my $value = $_[0]->_unescape($2);
101 sub {
102 exists $_[0]->{attrs}{$attribute}
103 5 ▶ && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E(?:-|$)/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} =~ qr/^\Q$value\E(?:-|$)/ 4 1 5
104 }
105 1 };
106  
107 # '[attr=bar]' - match attributes
108 /\G\[$sel_re=$match_value_re\]/gc and
109 188 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} eq $value; } } 7 181 188
110 7 my $attribute = $_[0]->_unescape($1);
111 7 my $value = $_[0]->_unescape($2);
112 sub {
113 exists $_[0]->{attrs}{$attribute}
114 19 ▶ && $_[0]->{attrs}{$attribute} eq $value;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"attrs"}{$attribute} and $_[0]{"attrs"}{$attribute} eq $value 7 12 19
115 }
116 7 };
117  
118 # '[attr!=bar]' - attributes doesn't match
119 /\G\[$sel_re!=$match_value_re\]/gc and
120 181 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re!=$match_value_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); my $value = $_[0]->_unescape($2); sub { not exists $_[0]{"attrs"}{$attribute} && $_[0]{"attrs"}{$attribute} eq $value; } } 1 180 181
121 1 my $attribute = $_[0]->_unescape($1);
122 1 my $value = $_[0]->_unescape($2);
123 sub {
124 ! (exists $_[0]->{attrs}{$attribute}
125 3 ▶ && $_[0]->{attrs}{$attribute} eq $value);
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
exists $_[0]{'attrs'}{$attribute} && $_[0]{'attrs'}{$attribute} eq $value
!ll&&!rl&&r
111
MC/DC100%2/2
exists $_[0]{'attrs'}{$attribute} && $_[0]{'attrs'}{$attribute} eq $value
exists $_[0]{'attrs'}{$attribute} $_[0]{'attrs'}{$attribute} eq $value
Truth table100%3/3
A && B
A= exists $_[0]{'attrs'}{$attribute}B= $_[0]{'attrs'}{$attribute} eq $value
ABresult
0X0
100
111
126 }
127 1 };
128  
129 # '[attr]' - match attribute being present:
130 /\G\[$sel_re\]/gc and
131 180 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\[$sel_re\]/cg and return do { my $attribute = $_[0]->_unescape($1); sub { exists $_[0]{"attrs"}{$attribute}; } } 2 178 180
132 2 my $attribute = $_[0]->_unescape($1);
133 sub {
134 5 ▶ exists $_[0]->{attrs}{$attribute};
Subroutine __ANON__: called CC=1 SCAR=0.0
135 }
136 2 };
137  
138 # none of the above matched, try catching some obvious errors:
139  
140 # indicate unmatched square bracket:
141 178 ▶ /\G\[[^\]]*/gc and $_[0]->_blam('Unmatched [');
Branch: 1 branch
BranchTrueFalseTotal
/\G\[[^\]]*/cg and $_[0]->_blam("Unmatched [") 1 177 178
142 }
143 }
144  
145 sub parse_selector {
146 176 ▶ my $self = $_[0];
Subroutine parse_selector: called CC=9 SCAR=22.0
Pod undocumented
147 176 my $sel = $_[1]; # my pos() only please
148 176 ▶ die "No selector provided" unless $sel;
Branch: 1 branch
BranchTrueFalseTotal
unless $sel 0 176 176
149 176 local *_;
150 176 for ($sel) {
151 176 my @sub;
152 176 ▶ PARSE: { do {
Branch: 1 branch
BranchTrueFalseTotal
pos $_ == length $_ or do { do { my @this_chain; while (my $sel = $self->_raw_parse_simple_selector($_)) { do { push @this_chain, $sel }; } ; if (@this_chain == 1) { push @sub, @this_chain; } else { push @sub, sub { my $r; foreach my $inner (@this_chain) { unless ($r = &$inner(@_)) { return $r; } ; } ; return $r; } ; } ; last PARSE if pos $_ == length $_; /\G\s*,\s*/cg or do { /\G(.*)/; $self->_blam("Selectors not comma separated.") } } } 2 0 2
153  
154 178 my @this_chain;
155  
156 # slurp selectors until we find something else:
157 178 ▶ while( my $sel = $self->_raw_parse_simple_selector($_) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $sel = $self->_raw_parse_simple_selector($_)) 202 177 379
158 202 push @this_chain, $sel;
159 }
160  
161 177 ▶ if( @this_chain == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@this_chain == 1) { } 152 25 177
162 {
163 152 push @sub, @this_chain;
164 }
165 else{
166 # make a compound match closure of everything
167 # in this chain of selectors:
168 push @sub, sub{
169 66 ▶ my $r;
Subroutine __ANON__: called CC=3 SCAR=11.0
170 66 for my $inner ( @this_chain ){
171 121 ▶ if( ! ($r = $inner->( @_ )) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($r = &$inner(@_)) 39 82 121
172 39 return $r;
173 }
174 }
175 27 return $r;
176 }
177 25 }
178  
179 # now we're at the end or a delimiter:
180 177 ▶ last PARSE if( pos == length );
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 173 4 177
181 4 ▶ /\G\s*,\s*/gc or do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\s*,\s*/cg or do { /\G(.*)/; $self->_blam("Selectors not comma separated.") } 2 2 4
182 2 /\G(.*)/;
183 2 $self->_blam( "Selectors not comma separated." );
184 }
185  
186 } until (pos == length) };
187 173 ▶ return $sub[0] if (@sub == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @sub == 1 171 2 173
188 return sub {
189 17 ▶ foreach my $inner (@sub) {
Subroutine __ANON__: called CC=3 SCAR=11.0
190 32 ▶ if (my $r = $inner->(@_)) { return $r }
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$inner(@_)) 4 28 32
191 }
192 2 };
193 }
194 }
195  
196 sub _unescape {
197 215 ▶ my ($self, $escaped) = @_;
Subroutine _unescape: called CC=1 SCAR=0.0
198 215 (my $unescaped = $escaped) =~ s/\\([\Q$sel_meta_char\E])/$1/g;
199 215 return $unescaped;
200 }
201  
202 sub _blam {
203 3 ▶ my ($self, $error) = @_;
Subroutine _blam: called CC=2 SCAR=7.1
204 3 ▶ my $hat = (' ' x (pos||0)).'^';
Condition50%1/2
pos $_ || 0
l!l
30
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
205 3 die "Error parsing dispatch specification: ${error}\n
206 ${_}
207 ${hat} here\n";
208 }
209  
210 14 1;
« Zoom/FilterBuilder.pm ↑ Summary Zoom/FilterStream.pm »