From: Jakub Narebski Date: Sun, 9 Jan 2011 12:31:36 +0000 (+0100) Subject: Add support for [attr~="value"] selector (attribute contains word) X-Git-Tag: release_0.009004~9^2~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=5b63d2447b01acd333b2a0a9adb7aa793b72fcf6 Add support for [attr~="value"] selector (attribute contains word) http://www.w3.org/TR/CSS2/selector.html#attribute-selectors http://api.jquery.com/attribute-contains-word-selector/ This selector matches the test string against each word in the attribute value, where a "word" is defined as a string delimited by whitespace. The selector matches if the test string is exactly equal to any of the words. --- diff --git a/lib/HTML/Zoom/SelectorParser.pm b/lib/HTML/Zoom/SelectorParser.pm index 9bc7bc3..be08175 100644 --- a/lib/HTML/Zoom/SelectorParser.pm +++ b/lib/HTML/Zoom/SelectorParser.pm @@ -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,7 +103,7 @@ sub _raw_parse_simple_selector { } }; - # '[attr] - match attribute being present: + # '[attr]' - match attribute being present: /\G\[$sel_re\]/gc and return do { my $attribute = $1; diff --git a/t/selectors.t b/t/selectors.t index 09875ad..13a8154 100644 --- a/t/selectors.t +++ b/t/selectors.t @@ -89,6 +89,14 @@ is( HTML::Zoom->from_html('
'.$stub) '
grg
'.$stub, 'E[attr*="val"] works' ); +# el[attr~="foo"] +is( HTML::Zoom->from_html('
'.$stub) + ->select('div[frew~="bar"]') + ->replace_content('grg') + ->to_html, + '
grg
'.$stub, + 'E[attr~="val"] works' ); + # [attr=bar] ok( check_select( '[prop=moo]'), '[attr=bar]' );