Skip to content

Commit

Permalink
fix(css-selector): default XML namespace is not applied to wildcard
Browse files Browse the repository at this point in the history
When performing a CSS selector query, an XML document's root namespace
declarations are injected into the XPathVisitor, and the default
namespace is applied to any non-namespaced element in the resulting
xpath query.

However, the wildcard selector "*" should not have this namespace
applied. This regressed in v1.17.0 with 179d74d.

Fixes #3411
  • Loading branch information
flavorjones committed Jan 19, 2025
1 parent b112e18 commit 8378749
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def visit_element_name(node)
else
node.value.join(":")
end
elsif @namespaces&.key?("xmlns") # apply the default namespace if it's declared
elsif node.value.first != "*" && @namespaces&.key?("xmlns")
# apply the default namespace (if one is present) to a non-wildcard selector
"xmlns:#{node.value.first}"
else
node.value.first
Expand Down
7 changes: 7 additions & 0 deletions test/css/test_xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ def visit_pseudo_class_aaron(node)
)
end

it "default namespace is not applied to wildcard selectors" do
assert_equal(
["//xmlns:a//*"],
Nokogiri::CSS.xpath_for("a *", ns: ns, cache: false),
)
end

it "intentionally-empty namespace omits the default xmlns" do
# An intentionally-empty namespace
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))
Expand Down

0 comments on commit 8378749

Please sign in to comment.