Skip to content

Commit

Permalink
feat: xpath generated from css uses custom function namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Apr 28, 2023
1 parent d519c52 commit d442276
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions test/css/test_xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d442276

Please sign in to comment.