Skip to content

Commit

Permalink
Support direct combinators in CSS :has()
Browse files Browse the repository at this point in the history
Adds support for CSS queries like "a:has(> b)", "a:has(~ b)", and
"a:has(+ b)".

Closes sparklemotion#688.
  • Loading branch information
jonathanhefner authored and flavorjones committed Apr 22, 2019
1 parent 3b0e5b1 commit 630643f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/nokogiri/css/xpath_visitor.rb
Expand Up @@ -51,7 +51,8 @@ def visit_function node
when /^comment\(/
"comment()"
when /^has\(/
".//#{node.value[1].accept(self)}"
is_direct = node.value[1].value[0].nil? # e.g. "has(> a)", "has(~ a)", "has(+ a)"
".#{"//" if !is_direct}#{node.value[1].accept(self)}"
else
args = ['.'] + node.value[1..-1]
"#{node.value.first}#{args.join(', ')})"
Expand Down
3 changes: 3 additions & 0 deletions test/css/test_parser.rb
Expand Up @@ -65,6 +65,9 @@ def test_to_a
def test_has
assert_xpath "//a[.//b]", @parser.parse("a:has(b)")
assert_xpath "//a[.//b/c]", @parser.parse("a:has(b > c)")
assert_xpath "//a[./b]", @parser.parse("a:has(> b)")
assert_xpath "//a[./following-sibling::b]", @parser.parse("a:has(~ b)")
assert_xpath "//a[./following-sibling::*[1]/self::b]", @parser.parse("a:has(+ b)")
end

def test_dashmatch
Expand Down

0 comments on commit 630643f

Please sign in to comment.