Add support for [attr!="value"] selector (attribute not equal)
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / SelectorParser.pm
index d3a505a..ef61d93 100644 (file)
@@ -81,6 +81,17 @@ sub _raw_parse_simple_selector {
         }
       };
 
+    # '[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
     /\G\[$sel_re=$match_value_re\]/gc and
       return do {
@@ -92,14 +103,30 @@ sub _raw_parse_simple_selector {
         }
       };
 
-    # '[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;
         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 [');
   }
 }