X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FHTML%2FZoom%2FSelectorParser.pm;h=48545ffd16c4b19c49d3d471bf9e5a41abd83696;hb=f7dc3b611fcb491ad708b7f71347d36bad71fb1c;hp=ed1d586ed9dbdbc30c9ab167bef4d9036c13f1e2;hpb=6d0f20a655ed2cb8581f7ab27433febba6c44cbd;p=catagits%2FHTML-Zoom.git diff --git a/lib/HTML/Zoom/SelectorParser.pm b/lib/HTML/Zoom/SelectorParser.pm index ed1d586..48545ff 100644 --- a/lib/HTML/Zoom/SelectorParser.pm +++ b/lib/HTML/Zoom/SelectorParser.pm @@ -7,6 +7,8 @@ use Carp qw(confess); my $sel_char = '-\w_'; my $sel_re = qr/([$sel_char]+)/; +my $match_value_re = qr/"?$sel_re"?/; + sub new { bless({}, shift) } @@ -46,7 +48,96 @@ sub _raw_parse_simple_selector { } }; - confess "Couldn't parse $_ as starting with simple selector"; + # '[attr^=foo]' - match attribute with ^ anchored regex + /\G\[$sel_re\^=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E/; + } + }; + + # '[attr$=foo]' - match attribute with $ anchored regex + /\G\[$sel_re\$=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/; + } + }; + + # '[attr*=foo] - match attribute with regex: + /\G\[$sel_re\*=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E/; + } + }; + + # '[attr~=bar]' - match attribute contains word + /\G\[$sel_re~=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} =~ qr/\b\Q$value\E\b/; + } + }; + + # '[attr!=bar]' - match attribute contains prefix (for language matches) + /\G\[$sel_re\|=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E(?:-|$)/; + } + }; + + # '[attr=bar]' - match attributes + /\G\[$sel_re=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + $_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} eq $value; + } + }; + + # '[attr!=bar]' - attributes doesn't match + /\G\[$sel_re!=$match_value_re\]/gc and + return do { + my $attribute = $1; + my $value = $2; + sub { + ! ($_[0]->{attrs}{$attribute} + && $_[0]->{attrs}{$attribute} eq $value); + } + }; + + # '[attr]' - match attribute being present: + /\G\[$sel_re\]/gc and + return do { + my $attribute = $1; + sub { + exists $_[0]->{attrs}{$attribute}; + } + }; + + # none of the above matched, try catching some obvious errors: + + # indicate unmatched square bracket: + /\G\[[^\]]*/gc and $_[0]->_blam('Unmatched ['); } } @@ -58,10 +149,40 @@ sub parse_selector { for ($sel) { my @sub; PARSE: { do { - push(@sub, $self->_raw_parse_simple_selector($_)); - last PARSE if (pos == length); - /\G\s*,\s*/gc or confess "Selectors not comma separated"; - } until (pos == length) }; + + my @this_chain; + + # slurp selectors until we find something else: + while( my $sel = $self->_raw_parse_simple_selector($_) ){ + push @this_chain, $sel; + } + + if( @this_chain == 1 ) + { + push @sub, @this_chain; + } + else{ + # make a compound match closure of everything + # in this chain of selectors: + push @sub, sub{ + my $r; + for my $inner ( @this_chain ){ + if( ! ($r = $inner->( @_ )) ){ + return $r; + } + } + return $r; + } + } + + # now we're at the end or a delimiter: + last PARSE if( pos == length ); + /\G\s*,\s*/gc or do { + /\G(.*)/; + $self->_blam( "Selectors not comma separated." ); + } + + } until (pos == length) }; return $sub[0] if (@sub == 1); return sub { foreach my $inner (@sub) { @@ -69,7 +190,15 @@ sub parse_selector { } }; } -} - +} + + +sub _blam { + my ($self, $error) = @_; + my $hat = (' ' x (pos||0)).'^'; + die "Error parsing dispatch specification: ${error}\n +${_} +${hat} here\n"; +} 1;