From: Jakub Narebski Date: Sun, 9 Jan 2011 13:09:20 +0000 (+0100) Subject: Add support for [attr!="value"] selector (attribute not equal) X-Git-Tag: release_0.009004~9^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=24725e7b32b680b376c06b1f9d641ec11fc1d067 Add support for [attr!="value"] selector (attribute not equal) http://api.jquery.com/attribute-not-equal-selector/ Description: Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value. This selector is equivalent to :not([attr="value"]). --- diff --git a/lib/HTML/Zoom/SelectorParser.pm b/lib/HTML/Zoom/SelectorParser.pm index be08175..ef61d93 100644 --- a/lib/HTML/Zoom/SelectorParser.pm +++ b/lib/HTML/Zoom/SelectorParser.pm @@ -103,6 +103,17 @@ sub _raw_parse_simple_selector { } }; + # '[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 { diff --git a/t/selectors.t b/t/selectors.t index 6dd32bd..e6be2c2 100644 --- a/t/selectors.t +++ b/t/selectors.t @@ -55,7 +55,14 @@ is( HTML::Zoom->from_html('
'.$stub) ->to_html, '
grg
'.$stub, 'E[attr=val] works' ); - + +# el[attr!="foo"] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[class!="waargh"]') + ->replace_content('grg') + ->to_html, + '
grg
grg
'.$stub, + 'E[attr!="val"] works' ); # el[attr*="foo"] is( HTML::Zoom->from_html('
'.$stub)