X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FHTML%2FZoom%2FSelectorParser.pm;h=ef61d93e349449a692fefebaa395c870cd7817f6;hb=24725e7b32b680b376c06b1f9d641ec11fc1d067;hp=4e4f11c140cc4aa913f26412abf7b6a21c8822d0;hpb=682fa876be5265211f777cf85fac6528d4dd4d41;p=catagits%2FHTML-Zoom.git diff --git a/lib/HTML/Zoom/SelectorParser.pm b/lib/HTML/Zoom/SelectorParser.pm index 4e4f11c..ef61d93 100644 --- a/lib/HTML/Zoom/SelectorParser.pm +++ b/lib/HTML/Zoom/SelectorParser.pm @@ -50,29 +50,46 @@ sub _raw_parse_simple_selector { # '[attr^=foo]' - match attribute with ^ anchored regex /\G\[$sel_re\^=$match_value_re\]/gc and - return do{ + return do { my $attribute = $1; my $value = $2; - $_[0]->{attrs}{$attribute} - && $_[0]->{attrs}{$attribute} =~ qr/^\Q$value\E/; + 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{ + return do { my $attribute = $1; my $value = $2; - $_[0]->{attrs}{$attribute} - && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/; + 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{ + return do { my $attribute = $1; my $value = $2; - $_[0]->{attrs}{$attribute} - && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/; + 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} + && grep { $_ eq $value } split(' ', $_[0]->{attrs}{$attribute}); + } }; # '[attr=bar]' - match attributes @@ -80,18 +97,36 @@ sub _raw_parse_simple_selector { return do { my $attribute = $1; my $value = $2; - sub{ + sub { $_[0]->{attrs}{$attribute} && $_[0]->{attrs}{$attribute} eq $value; } }; - # '[attr] - match attribute being present: + # '[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; - $_[0]->{attrs}{$attribute}; - } + 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 ['); } }