From 95da93949bf613612981f5cd7decc0d2c2a60e15 Mon Sep 17 00:00:00 2001 From: KitaitiMakoto Date: Mon, 2 Dec 2019 17:40:11 +0000 Subject: [PATCH] Fix XPath queries using default XML namespace This fixes https://gitlab.com/yorickpeterse/oga/issues/195 --- lib/oga/xpath/compiler.rb | 9 +++++++-- spec/oga/xpath/compiler/paths_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index d407fdf..01531da 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -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 diff --git a/spec/oga/xpath/compiler/paths_spec.rb b/spec/oga/xpath/compiler/paths_spec.rb index 2f00413..961fd19 100644 --- a/spec/oga/xpath/compiler/paths_spec.rb +++ b/spec/oga/xpath/compiler/paths_spec.rb @@ -68,4 +68,26 @@ describe Oga::XPath::Compiler do end end end + + describe 'querying elements with a default namespace' do + before do + @document = parse('Foo') + + @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