3395940d3a89ddd9daec4429ca8c05f61ed13918
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / SelectorParser.pm
1 package HTML::Zoom::SelectorParser;
2
3 use strict;
4 use warnings FATAL => 'all';
5 use base qw(HTML::Zoom::SubObject);
6 use Carp qw(confess);
7
8 my $sel_char = '-\w_';
9 my $sel_re = qr/([$sel_char]+)/;
10
11 sub new { bless({}, shift) }
12
13 sub _raw_parse_simple_selector {
14   for ($_[1]) { # same pos() as outside
15
16     # '*' - match anything
17
18     /\G\*/gc and
19       return sub { 1 };
20
21      # 'el[attr~="foo"]
22
23     /\G$sel_re\[$sel_re~="$sel_re"\]/gc and
24       return do {
25         my $name = $1;
26         my $attr = $2;
27         my $val = $3;
28         sub {
29           if (
30             $_[0]->{name} && $_[0]->{name} eq $name and
31             $_[0]->{attrs}{$attr}
32           ) {
33             my %vals = map { $_ => 1 } split /\s+/, $_[0]->{attrs}{$attr};
34             return $vals{$val}
35           }
36           return undef
37         }
38       };
39
40      # 'el[attr^="foo"]
41
42     /\G$sel_re\[$sel_re\^="$sel_re"\]/gc and
43       return do {
44         my $name = $1;
45         my $attr = $2;
46         my $val = $3;
47         sub {
48            $_[0]->{name} && $_[0]->{name} eq $name and
49            $_[0]->{attrs}{$attr} && $_[0]->{attrs}{$attr} =~ /^\Q$val\E/
50         }
51       };
52
53      # 'el[attr="foo"]
54
55     /\G$sel_re\[$sel_re="$sel_re"\]/gc and
56       return do {
57         my $name = $1;
58         my $attr = $2;
59         my $val = $3;
60         sub {
61            $_[0]->{name} && $_[0]->{name} eq $name and
62            $_[0]->{attrs}{$attr} && $_[0]->{attrs}{$attr} eq $val
63         }
64       };
65
66      # 'el[attr]
67
68     /\G$sel_re\[$sel_re\]/gc and
69       return do {
70         my $name = $1;
71         my $attr = $2;
72         sub {
73            $_[0]->{name} && $_[0]->{name} eq $name && $_[0]->{attrs}{$attr}
74         }
75       };
76
77     # 'element' - match on tag name
78
79     /\G$sel_re/gc and
80       return do {
81         my $name = $1;
82         sub { $_[0]->{name} && $_[0]->{name} eq $name }
83       };
84
85     # '#id' - match on id attribute
86
87     /\G#$sel_re/gc and
88       return do {
89         my $id = $1;
90         sub { $_[0]->{attrs}{id} && $_[0]->{attrs}{id} eq $id }
91       };
92
93     # '.class1.class2' - match on intersection of classes
94
95     /\G((?:\.$sel_re)+)/gc and
96       return do {
97         my $cls = $1; $cls =~ s/^\.//;
98         my @cl = split(/\./, $cls);
99         sub {
100           $_[0]->{attrs}{class}
101           && !grep $_[0]->{attrs}{class} !~ /(^|\s+)$_($|\s+)/, @cl
102         }
103       };
104
105     # 'el.class1' - element + class
106
107     /\G$sel_re\.$sel_re/gc and
108       return do {
109         my $cls = $1;
110         my $name = $2;
111         sub {
112            $_[0]->{name} && $_[0]->{name} eq $name and
113            $_[0]->{attrs}{class} && $_[0]->{attrs}{class} eq $cls
114         }
115       };
116
117     # 'el#id' - element + id
118
119     /\G$sel_re#$sel_re/gc and
120       return do {
121         my $id = $1;
122         my $name = $2;
123         sub {
124            $_[0]->{name} && $_[0]->{name} eq $name and
125            $_[0]->{attrs}{id} && $_[0]->{attrs}{id} eq $id
126         }
127       };
128
129     confess "Couldn't parse $_ as starting with simple selector";
130   }
131 }
132
133 sub parse_selector {
134   my $self = $_[0];
135   my $sel = $_[1]; # my pos() only please
136   die "No selector provided" unless $sel;
137   local *_;
138   for ($sel) {
139     my @sub;
140     PARSE: { do {
141       push(@sub, $self->_raw_parse_simple_selector($_));
142       last PARSE if (pos == length);
143       /\G\s*,\s*/gc or confess "Selectors not comma separated";
144     } until (pos == length) };
145     return $sub[0] if (@sub == 1);
146     return sub {
147       foreach my $inner (@sub) {
148         if (my $r = $inner->(@_)) { return $r }
149       }
150     };
151   }
152 }
153
154
155 1;