Fix XPath queries using default XML namespace

This fixes https://gitlab.com/yorickpeterse/oga/issues/195
This commit is contained in:
KitaitiMakoto 2019-12-02 17:40:11 +00:00 committed by Yorick Peterse
parent d492a775bf
commit 95da93949b
2 changed files with 29 additions and 2 deletions

View File

@ -1391,14 +1391,19 @@ module Oga
if ns and ns != STAR
if @namespaces
ns_uri = @namespaces[ns]
ns_match =
ns_match =
if ns_uri
input.namespace.and(input.namespace.uri.eq(string(ns_uri)))
else
self.false
end
else
ns_match = input.namespace_name.eq(string(ns))
ns_match =
if ns == XML::Element::XMLNS_PREFIX
input
else
input.namespace_name.eq(string(ns))
end
end
condition = condition ? condition.and(ns_match) : ns_match

View File

@ -68,4 +68,26 @@ describe Oga::XPath::Compiler do
end
end
end
describe 'querying elements with a default namespace' do
before do
@document = parse('<a xmlns="n" xmlns:ns1="x">Foo<b></b><b></b><ns1:c></ns1:c></a>')
@a1 = @document.children[0]
@b1 = @a1.children[1]
@b2 = @a1.children[2]
end
describe '/xmlns:a' do
it 'returns a NodeSet' do
expect(evaluate_xpath(@document)).to eq(node_set(@a1))
end
end
describe '//xmlns:b' do
it 'returns a NodeSet' do
expect(evaluate_xpath(@document)).to eq(node_set(@b1, @b2))
end
end
end
end