Add support for [attr~="value"] selector (attribute contains word)
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / SelectorParser.pm
CommitLineData
456a815d 1package HTML::Zoom::SelectorParser;
2
3use strict;
4use warnings FATAL => 'all';
6d0f20a6 5use base qw(HTML::Zoom::SubObject);
456a815d 6use Carp qw(confess);
7
8my $sel_char = '-\w_';
9my $sel_re = qr/([$sel_char]+)/;
682fa876 10my $match_value_re = qr/"?$sel_re"?/;
456a815d 11
96e44ffd 12
682fa876 13sub new { bless({}, shift) }
96e44ffd 14
456a815d 15sub _raw_parse_simple_selector {
16 for ($_[1]) { # same pos() as outside
17
18 # '*' - match anything
19
20 /\G\*/gc and
21 return sub { 1 };
22
23 # 'element' - match on tag name
24
25 /\G$sel_re/gc and
26 return do {
27 my $name = $1;
28 sub { $_[0]->{name} && $_[0]->{name} eq $name }
29 };
30
31 # '#id' - match on id attribute
32
33 /\G#$sel_re/gc and
34 return do {
35 my $id = $1;
36 sub { $_[0]->{attrs}{id} && $_[0]->{attrs}{id} eq $id }
37 };
38
39 # '.class1.class2' - match on intersection of classes
40
41 /\G((?:\.$sel_re)+)/gc and
42 return do {
43 my $cls = $1; $cls =~ s/^\.//;
44 my @cl = split(/\./, $cls);
45 sub {
46 $_[0]->{attrs}{class}
6d0f20a6 47 && !grep $_[0]->{attrs}{class} !~ /(^|\s+)$_($|\s+)/, @cl
456a815d 48 }
49 };
50
682fa876 51 # '[attr^=foo]' - match attribute with ^ anchored regex
52 /\G\[$sel_re\^=$match_value_re\]/gc and
6818876e 53 return do {
682fa876 54 my $attribute = $1;
55 my $value = $2;
6818876e 56 sub {
57 $_[0]->{attrs}{$attribute}
58 && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E/;
59 }
682fa876 60 };
61
62 # '[attr$=foo]' - match attribute with $ anchored regex
63 /\G\[$sel_re\$=$match_value_re\]/gc and
6818876e 64 return do {
682fa876 65 my $attribute = $1;
66 my $value = $2;
6818876e 67 sub {
68 $_[0]->{attrs}{$attribute}
69 && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/;
70 }
682fa876 71 };
72
73 # '[attr*=foo] - match attribute with regex:
74 /\G\[$sel_re\*=$match_value_re\]/gc and
6818876e 75 return do {
682fa876 76 my $attribute = $1;
77 my $value = $2;
6818876e 78 sub {
79 $_[0]->{attrs}{$attribute}
80 && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E/;
81 }
682fa876 82 };
83
5b63d244 84 # '[attr~=bar]' - match attribute contains word
85 /\G\[$sel_re~=$match_value_re\]/gc and
86 return do {
87 my $attribute = $1;
88 my $value = $2;
89 sub {
90 $_[0]->{attrs}{$attribute}
91 && grep { $_ eq $value } split(' ', $_[0]->{attrs}{$attribute});
92 }
93 };
94
682fa876 95 # '[attr=bar]' - match attributes
96 /\G\[$sel_re=$match_value_re\]/gc and
97 return do {
98 my $attribute = $1;
99 my $value = $2;
6818876e 100 sub {
682fa876 101 $_[0]->{attrs}{$attribute}
102 && $_[0]->{attrs}{$attribute} eq $value;
103 }
104 };
105
5b63d244 106 # '[attr]' - match attribute being present:
682fa876 107 /\G\[$sel_re\]/gc and
108 return do {
109 my $attribute = $1;
6818876e 110 sub {
111 exists $_[0]->{attrs}{$attribute};
112 }
bd4e2ca0 113 };
114
115 # none of the above matched, try catching some obvious errors:
116
117 # indicate unmatched square bracket:
118 /\G\[[^\]]*/gc and $_[0]->_blam('Unmatched [');
456a815d 119 }
120}
121
122sub parse_selector {
123 my $self = $_[0];
124 my $sel = $_[1]; # my pos() only please
125 die "No selector provided" unless $sel;
126 local *_;
127 for ($sel) {
128 my @sub;
129 PARSE: { do {
682fa876 130
131 my @this_chain;
132
133 # slurp selectors until we find something else:
134 while( my $sel = $self->_raw_parse_simple_selector($_) ){
135 push @this_chain, $sel;
136 }
137
138 if( @this_chain == 1 )
139 {
140 push @sub, @this_chain;
141 }
142 else{
143 # make a compound match closure of everything
144 # in this chain of selectors:
145 push @sub, sub{
146 my $r;
147 for my $inner ( @this_chain ){
148 if( ! ($r = $inner->( @_ )) ){
149 return $r;
150 }
151 }
152 return $r;
153 }
154 }
155
156 # now we're at the end or a delimiter:
157 last PARSE if( pos == length );
158 /\G\s*,\s*/gc or do {
159 /\G(.*)/;
160 $self->_blam( "Selectors not comma separated." );
161 }
162
163 } until (pos == length) };
456a815d 164 return $sub[0] if (@sub == 1);
165 return sub {
166 foreach my $inner (@sub) {
167 if (my $r = $inner->(@_)) { return $r }
168 }
169 };
170 }
7871f2ff 171}
172
456a815d 173
682fa876 174sub _blam {
175 my ($self, $error) = @_;
176 my $hat = (' ' x (pos||0)).'^';
177 die "Error parsing dispatch specification: ${error}\n
178${_}
179${hat} here\n";
180}
181
456a815d 1821;