From c7fc1dcafe7d521ccd04cff35332fdd6aa591a08 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 5 Jan 2019 01:10:54 -0500 Subject: [PATCH] CSS selector `:has()` matches against deep descendants Fixes #350. --- CHANGELOG.md | 5 +++++ lib/nokogiri/css/xpath_visitor.rb | 2 +- test/css/test_parser.rb | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57fa5218ec..ac4eea5001 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ * [MRI] Handle Xcode 10's new library path on macOS. [#1801, #1851] (Thanks, @mlj and @deepj!) +### Bug fixes + +* CSS selector `:has()` correctly matches against any descendant (previously matched against only direct children). [#350] (Thanks, @Phrogz!) + + ## 1.10.0 / 2019-01-04 ### Features diff --git a/lib/nokogiri/css/xpath_visitor.rb b/lib/nokogiri/css/xpath_visitor.rb index c79dffd5b6..28450fec15 100644 --- a/lib/nokogiri/css/xpath_visitor.rb +++ b/lib/nokogiri/css/xpath_visitor.rb @@ -51,7 +51,7 @@ def visit_function node when /^comment\(/ "comment()" when /^has\(/ - node.value[1].accept(self) + ".//#{node.value[1].accept(self)}" else args = ['.'] + node.value[1..-1] "#{node.value.first}#{args.join(', ')})" diff --git a/test/css/test_parser.rb b/test/css/test_parser.rb index 4ac39a1b64..0a1a55b12a 100644 --- a/test/css/test_parser.rb +++ b/test/css/test_parser.rb @@ -63,8 +63,8 @@ def test_to_a end 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[.//b/c]", @parser.parse("a:has(b > c)") end def test_dashmatch