From d442276ffebcde6d5374802abd23ac826f063269 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 28 Apr 2023 11:12:02 -0400 Subject: [PATCH] feat: xpath generated from css uses custom function namespace --- lib/nokogiri/css/xpath_visitor.rb | 4 ++-- test/css/test_xpath_visitor.rb | 40 +++++++++++++++---------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/nokogiri/css/xpath_visitor.rb b/lib/nokogiri/css/xpath_visitor.rb index c8d45cf9c72..ba63f3c4122 100644 --- a/lib/nokogiri/css/xpath_visitor.rb +++ b/lib/nokogiri/css/xpath_visitor.rb @@ -133,7 +133,7 @@ def visit_function(node) args += node.value[1..-1].map do |n| n.is_a?(Nokogiri::CSS::Node) ? n.accept(self) : n end - "#{node.value.first}#{args.join(",")})" + "nokogiri:#{node.value.first}#{args.join(",")})" end end @@ -207,7 +207,7 @@ def visit_pseudo_class(node) when "parent" then "node()" when "root" then "not(parent::*)" else - node.value.first + "(.)" + "nokogiri:#{node.value.first}(.)" end end end diff --git a/test/css/test_xpath_visitor.rb b/test/css/test_xpath_visitor.rb index 31f82760a87..4dbd2ff3219 100644 --- a/test/css/test_xpath_visitor.rb +++ b/test/css/test_xpath_visitor.rb @@ -343,27 +343,27 @@ def assert_xpath(expecteds, asts) end it "miscellaneous pseudo-classes are converted into xpath function calls" do - assert_xpath("//a[aaron(.)]", parser.parse("a:aaron")) - assert_xpath("//a[aaron(.)]", parser.parse("a:aaron()")) - assert_xpath("//a[aaron(.,12)]", parser.parse("a:aaron(12)")) - assert_xpath("//a[aaron(.,12,1)]", parser.parse("a:aaron(12, 1)")) + assert_xpath("//a[nokogiri:aaron(.)]", parser.parse("a:aaron")) + assert_xpath("//a[nokogiri:aaron(.)]", parser.parse("a:aaron()")) + assert_xpath("//a[nokogiri:aaron(.,12)]", parser.parse("a:aaron(12)")) + assert_xpath("//a[nokogiri:aaron(.,12,1)]", parser.parse("a:aaron(12, 1)")) - assert_xpath("//a[link(.)]", parser.parse("a:link")) - assert_xpath("//a[visited(.)]", parser.parse("a:visited")) - assert_xpath("//a[hover(.)]", parser.parse("a:hover")) - assert_xpath("//a[active(.)]", parser.parse("a:active")) + assert_xpath("//a[nokogiri:link(.)]", parser.parse("a:link")) + assert_xpath("//a[nokogiri:visited(.)]", parser.parse("a:visited")) + assert_xpath("//a[nokogiri:hover(.)]", parser.parse("a:hover")) + assert_xpath("//a[nokogiri:active(.)]", parser.parse("a:active")) - assert_xpath("//a[foo(.,@href)]", parser.parse("a:foo(@href)")) - assert_xpath("//a[foo(.,@href,@id)]", parser.parse("a:foo(@href, @id)")) - assert_xpath("//a[foo(.,@a,b)]", parser.parse("a:foo(@a, b)")) - assert_xpath("//a[foo(.,a,@b)]", parser.parse("a:foo(a, @b)")) - assert_xpath("//a[foo(.,a,10)]", parser.parse("a:foo(a, 10)")) - assert_xpath("//a[foo(.,42)]", parser.parse("a:foo(42)")) - assert_xpath("//a[foo(.,'bar')]", parser.parse("a:foo('bar')")) + assert_xpath("//a[nokogiri:foo(.,@href)]", parser.parse("a:foo(@href)")) + assert_xpath("//a[nokogiri:foo(.,@href,@id)]", parser.parse("a:foo(@href, @id)")) + assert_xpath("//a[nokogiri:foo(.,@a,b)]", parser.parse("a:foo(@a, b)")) + assert_xpath("//a[nokogiri:foo(.,a,@b)]", parser.parse("a:foo(a, @b)")) + assert_xpath("//a[nokogiri:foo(.,a,10)]", parser.parse("a:foo(a, 10)")) + assert_xpath("//a[nokogiri:foo(.,42)]", parser.parse("a:foo(42)")) + assert_xpath("//a[nokogiri:foo(.,'bar')]", parser.parse("a:foo('bar')")) end it "bare pseudo-class matches any ident" do - assert_xpath("//*[link(.)]", parser.parse(":link")) + assert_xpath("//*[nokogiri:link(.)]", parser.parse(":link")) assert_xpath("//*[not(@id='foo')]", parser.parse(":not(#foo)")) assert_xpath("//*[count(preceding-sibling::*)=0]", parser.parse(":first-child")) end @@ -483,18 +483,18 @@ def visit_pseudo_class_aaron(node) it "handles pseudo-class with class selector" do assert_xpath( - "//a[active(.) and contains(concat(' ',normalize-space(@class),' '),' foo ')]", + "//a[nokogiri:active(.) and contains(concat(' ',normalize-space(@class),' '),' foo ')]", parser.parse("a:active.foo"), ) assert_xpath( - "//a[contains(concat(' ',normalize-space(@class),' '),' foo ') and active(.)]", + "//a[contains(concat(' ',normalize-space(@class),' '),' foo ') and nokogiri:active(.)]", parser.parse("a.foo:active"), ) end it "handles pseudo-class with an id selector" do - assert_xpath("//a[@id='foo' and active(.)]", parser.parse("a#foo:active")) - assert_xpath("//a[active(.) and @id='foo']", parser.parse("a:active#foo")) + assert_xpath("//a[@id='foo' and nokogiri:active(.)]", parser.parse("a#foo:active")) + assert_xpath("//a[nokogiri:active(.) and @id='foo']", parser.parse("a:active#foo")) end it "handles function with pseudo-class" do