Skip to content

Commit 04a589a

Browse files
authored
Fix a bug that XPath can't be used for no document element (#268)
Fixes #267 #249 improved performance by introducing cache. It requires document but we should not break backward compatibility for performance improvement. This restores the previous behavior but no document case doesn't have performance improvement introduced by #249.
1 parent 66232ea commit 04a589a

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

lib/rexml/xpath_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ def parse path, node
8484
node = node.first
8585
end
8686

87-
node.document.__send__(:enable_cache) do
87+
document = node.document
88+
if document
89+
document.__send__(:enable_cache) do
90+
match( path_stack, node )
91+
end
92+
else
8893
match( path_stack, node )
8994
end
9095
end

test/parser/test_xpath.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require "rexml/parsers/xpathparser"
55

66
module REXMLTests
7-
class TestXPathParser < Test::Unit::TestCase
7+
class TestParserXPathParser < Test::Unit::TestCase
88
sub_test_case("#abbreviate") do
99
def abbreviate(xpath)
1010
parser = REXML::Parsers::XPathParser.new

test/test_xpath_parser.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module REXMLTests
4+
class TestXPathParser < Test::Unit::TestCase
5+
def setup
6+
@root_element = make_service_element(["urn:type1", "urn:type2"], ["http://uri"])
7+
@element = @root_element.children[0]
8+
@parser = REXML::XPathParser.new
9+
end
10+
11+
def make_service_element(types, uris)
12+
root_element = REXML::Element.new
13+
element = root_element.add_element("Service")
14+
types.each do |type_text|
15+
element.add_element("Type").text = type_text
16+
end
17+
uris.each do |uri_text|
18+
element.add_element("URI").text = uri_text
19+
end
20+
root_element
21+
end
22+
23+
def test_found
24+
res = @parser.parse("/Service", @root_element)
25+
assert_equal([@element],
26+
res)
27+
end
28+
29+
def test_not_found
30+
res = @parser.parse("/nonexistent", @root_element)
31+
assert_equal([],
32+
res)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)