| 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
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # 'element' - match on tag name | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | /\G$sel_re/gc and | ||||||||||||||||||||
| 27 | 379 | ▶ | return do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 30 | 98 | }; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # '#id' - match on id attribute | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | /\G#$sel_re/gc and | ||||||||||||||||||||
| 35 | 281 | ▶ | return do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | 1 | }; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | # '[attr]' - match attribute being present: | ||||||||||||||||||||
| 130 | /\G\[$sel_re\]/gc and | ||||||||||||||||||||
| 131 | 180 | ▶ | return do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 149 | 176 | local *_; | |||||||||||||||||||
| 150 | 176 | for ($sel) { | |||||||||||||||||||
| 151 | 176 | my @sub; | |||||||||||||||||||
| 152 | 176 | ▶ | PARSE: { do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 158 | 202 | push @this_chain, $sel; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 177 | ▶ | if( @this_chain == 1 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 181 | 4 | ▶ | /\G\s*,\s*/gc or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
| |||||||||||||||||||||
| 205 | 3 | die "Error parsing dispatch specification: ${error}\n | |||||||||||||||||||
| 206 | ${_} | ||||||||||||||||||||
| 207 | ${hat} here\n"; | ||||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 14 | 1; | |||||||||||||||||||