ed1d586ed9dbdbc30c9ab167bef4d9036c13f1e2
[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     # 'element' - match on tag name
22
23     /\G$sel_re/gc and
24       return do {
25         my $name = $1;
26         sub { $_[0]->{name} && $_[0]->{name} eq $name }
27       };
28
29     # '#id' - match on id attribute
30
31     /\G#$sel_re/gc and
32       return do {
33         my $id = $1;
34         sub { $_[0]->{attrs}{id} && $_[0]->{attrs}{id} eq $id }
35       };
36
37     # '.class1.class2' - match on intersection of classes
38
39     /\G((?:\.$sel_re)+)/gc and
40       return do {
41         my $cls = $1; $cls =~ s/^\.//;
42         my @cl = split(/\./, $cls);
43         sub {
44           $_[0]->{attrs}{class}
45           && !grep $_[0]->{attrs}{class} !~ /(^|\s+)$_($|\s+)/, @cl
46         }
47       };
48
49     confess "Couldn't parse $_ as starting with simple selector";
50   }
51 }
52
53 sub parse_selector {
54   my $self = $_[0];
55   my $sel = $_[1]; # my pos() only please
56   die "No selector provided" unless $sel;
57   local *_;
58   for ($sel) {
59     my @sub;
60     PARSE: { do {
61       push(@sub, $self->_raw_parse_simple_selector($_));
62       last PARSE if (pos == length);
63       /\G\s*,\s*/gc or confess "Selectors not comma separated";
64     } until (pos == length) };
65     return $sub[0] if (@sub == 1);
66     return sub {
67       foreach my $inner (@sub) {
68         if (my $r = $inner->(@_)) { return $r }
69       }
70     };
71   }
72
73   
74
75 1;