Add support for [attr~="value"] selector (attribute contains word)
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / SelectorParser.pm
index 4e4f11c..be08175 100644 (file)
@@ -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;
+        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;
-        $_[0]->{attrs}{$attribute}
-        && $_[0]->{attrs}{$attribute} =~ qr/\Q$value\E$/;
+        sub {
+          $_[0]->{attrs}{$attribute}
+          && grep { $_ eq $value } split(' ', $_[0]->{attrs}{$attribute});
+        }
       };
 
     # '[attr=bar]' - match attributes
@@ -80,18 +97,25 @@ 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]' - 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 [');
   }
 }