Subject: | [PATCH] Support CSS3 "~" combinator |
Support for the "~" general-sibling combinator, added in CSS3, would be
helpful in some circumstances.
The attached patch provides implementation and tests for "~".
Subject: | html_sel_xpath_tilde.diff |
From 66f78d72b8270490e9f8642ac8a895abbbf25d57 Mon Sep 17 00:00:00 2001
From: Aaron Crane <arc@cpan.org>
Date: Fri, 8 Oct 2010 13:50:12 +0100
Subject: Support the ~ (general sibling) combinator
---
lib/HTML/Selector/XPath.pm | 4 +++-
t/01_xpath.t | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 1 deletions(-)
diff --git a/lib/HTML/Selector/XPath.pm b/lib/HTML/Selector/XPath.pm
index 958e5cf..bf99b89 100644
--- a/lib/HTML/Selector/XPath.pm
+++ b/lib/HTML/Selector/XPath.pm
@@ -24,7 +24,7 @@ my $reg = {
attrN => qr/^:not\((.*?)\)/i,
pseudo => qr/^:([()a-z0-9_-]+)/i,
# adjacency/direct descendance
- combinator => qr/^(\s*[>+\s])/i,
+ combinator => qr/^(\s*[>+~\s])/i,
# rule separator
comma => qr/^\s*,/i,
};
@@ -130,6 +130,8 @@ sub to_xpath {
my $match = $1;
if ($match =~ />/) {
push @parts, "/";
+ } elsif ($match =~ /~/) {
+ push @parts, "/following-sibling::";
} elsif ($match =~ /\+/) {
push @parts, "/following-sibling::";
@next_parts = ('*[1]/self::');
diff --git a/t/01_xpath.t b/t/01_xpath.t
index e36c69e..7c7be96 100644
--- a/t/01_xpath.t
+++ b/t/01_xpath.t
@@ -80,6 +80,48 @@ E + .bar
===
--- selector
+E ~ F
+--- xpath
+//E/following-sibling::F
+
+===
+--- selector
+E ~ #bar
+--- xpath
+//E/following-sibling::*[@id='bar']
+
+===
+--- selector
+E ~ .bar
+--- xpath
+//E/following-sibling::*[contains(concat(' ', @class, ' '), ' bar ')]
+
+===
+--- selector
+E ~ *
+--- xpath
+//E/following-sibling::*
+
+===
+--- selector
+.foo ~ E
+--- xpath
+//*[contains(concat(' ', @class, ' '), ' foo ')]/following-sibling::E
+
+===
+--- selector
+.foo ~ *
+--- xpath
+//*[contains(concat(' ', @class, ' '), ' foo ')]/following-sibling::*
+
+===
+--- selector
+.foo ~ .bar
+--- xpath
+//*[contains(concat(' ', @class, ' '), ' foo ')]/following-sibling::*[contains(concat(' ', @class, ' '), ' bar ')]
+
+===
+--- selector
E[foo]
--- xpath
//E[@foo]
--
1.7.3