Improve [attr~="value"] selector (attribute contains word)
Jakub Narebski [Mon, 24 Jan 2011 12:04:33 +0000 (13:04 +0100)]
Instead of first splitting attribute value into words, and then
checking that one of words is equal to given value

   grep { $_ eq $value } split ' ', $attr;

use regexp anchored to words boundary for word matching

   /\b\Q$value\E\b/

Strictly speaking it makes [attr=~"value"] selector not conformant to
CSS2+ specification, but it shouldn't matter in real life.  See
http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

  [att~=val]

     [..] If "val" contains white space, it will never represent anything
     (since the words are separated by spaces). If "val" is the empty
     string, it will never represent anything either.

Suggested-by: Matt S Trout <mst@shadowcat.co.uk>

lib/HTML/Zoom/SelectorParser.pm

index 929e9a7..48545ff 100644 (file)
@@ -88,7 +88,7 @@ sub _raw_parse_simple_selector {
         my $value = $2;
         sub {
           $_[0]->{attrs}{$attribute}
-          && grep { $_ eq $value } split(' ', $_[0]->{attrs}{$attribute});
+          && $_[0]->{attrs}{$attribute} =~ qr/\b\Q$value\E\b/;
         }
       };