Skip to content

Commit 8378749

Browse files
committed
fix(css-selector): default XML namespace is not applied to wildcard
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
1 parent b112e18 commit 8378749

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/nokogiri/css/xpath_visitor.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ def visit_element_name(node)
283283
else
284284
node.value.join(":")
285285
end
286-
elsif @namespaces&.key?("xmlns") # apply the default namespace if it's declared
286+
elsif node.value.first != "*" && @namespaces&.key?("xmlns")
287+
# apply the default namespace (if one is present) to a non-wildcard selector
287288
"xmlns:#{node.value.first}"
288289
else
289290
node.value.first

test/css/test_xpath_visitor.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def visit_pseudo_class_aaron(node)
164164
)
165165
end
166166

167+
it "default namespace is not applied to wildcard selectors" do
168+
assert_equal(
169+
["//xmlns:a//*"],
170+
Nokogiri::CSS.xpath_for("a *", ns: ns, cache: false),
171+
)
172+
end
173+
167174
it "intentionally-empty namespace omits the default xmlns" do
168175
# An intentionally-empty namespace
169176
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))

0 commit comments

Comments
 (0)