Skip to content

Commit 05ed61b

Browse files
committed
Drop accepting array as an element in XPath.match, first and each
XPath.match, first, each accepted array as an element. This behavior is not documented, and making hard to optimize and refactor. The second argument of XPathParser#parse and XPathParser#match is also changed from nodeset to node
1 parent 5d2606a commit 05ed61b

4 files changed

Lines changed: 19 additions & 23 deletions

File tree

lib/rexml/xpath.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def XPath::first(element, path=nil, namespaces=nil, variables={}, options={})
3535
parser.namespaces = namespaces
3636
parser.variables = variables
3737
path = "*" unless path
38-
element = [element] unless element.kind_of? Array
3938
parser.parse(path, element).flatten[0]
4039
end
4140

@@ -64,7 +63,6 @@ def XPath::each(element, path=nil, namespaces=nil, variables={}, options={}, &bl
6463
parser.namespaces = namespaces
6564
parser.variables = variables
6665
path = "*" unless path
67-
element = [element] unless element.kind_of? Array
6866
parser.parse(path, element).each( &block )
6967
end
7068

@@ -74,7 +72,6 @@ def XPath::match(element, path=nil, namespaces=nil, variables={}, options={})
7472
parser.namespaces = namespaces
7573
parser.variables = variables
7674
path = "*" unless path
77-
element = [element] unless element.kind_of? Array
7875
parser.parse(path,element)
7976
end
8077
end

lib/rexml/xpath_parser.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ def variables=( vars={} )
7676
@variables = vars
7777
end
7878

79-
def parse path, nodeset
79+
def parse path, node
8080
path_stack = @parser.parse( path )
81-
match( path_stack, nodeset )
81+
match( path_stack, node )
8282
end
8383

84-
def get_first path, nodeset
84+
def get_first path, node
8585
path_stack = @parser.parse( path )
86-
first( path_stack, nodeset )
86+
first( path_stack, node )
8787
end
8888

89-
def predicate path, nodeset
89+
def predicate path, node
9090
path_stack = @parser.parse( path )
91-
match( path_stack, nodeset )
91+
match( path_stack, node )
9292
end
9393

9494
def []=( variable_name, value )
@@ -136,11 +136,8 @@ def first( path_stack, node )
136136
end
137137

138138

139-
def match(path_stack, nodeset)
140-
nodeset = nodeset.collect.with_index do |node, i|
141-
position = i + 1
142-
XPathNode.new(node, position: position)
143-
end
139+
def match(path_stack, node)
140+
nodeset = [XPathNode.new(node, position: 1)]
144141
result = expr(path_stack, nodeset)
145142
case result
146143
when Array # nodeset

test/test_jaxen.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def process_test_case(name)
5656

5757
# processes a tests/document/context node
5858
def process_context(doc, context)
59-
test_context = XPath.match(doc, context.attributes["select"])
59+
matched = XPath.match(doc, context.attributes["select"])
60+
assert_equal(matched.size, 1)
61+
test_context = matched.first
6062
namespaces = context.namespaces
6163
namespaces.delete("var")
6264
namespaces = nil if namespaces.empty?
@@ -104,7 +106,8 @@ def process_nominal_test(context, variables, namespaces, test)
104106
end
105107

106108
XPath.each(test, "valueOf") do |value_of|
107-
process_value_of(matched, variables, namespaces, value_of)
109+
assert_equal(1, matched.size)
110+
process_value_of(matched.first, variables, namespaces, value_of)
108111
end
109112
end
110113

@@ -118,10 +121,8 @@ def process_exceptional_test(context, variables, namespaces, test)
118121

119122
def user_message(context, xpath, matched)
120123
message = ""
121-
context.each_with_index do |node, i|
122-
message << "Node#{i}:\n"
123-
message << "#{node}\n"
124-
end
124+
message << "Node:\n"
125+
message << "#{context}\n"
125126
message << "XPath: <#{xpath}>\n"
126127
message << "Matched <#{matched}>"
127128
message

test/xpath/test_base.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,10 @@ def test_preceding
411411

412412
s = "<a><b><c id='1'/></b><b><b><c id='2'/><c id='3'/></b><c id='4'/></b><c id='NOMATCH'><c id='5'/></c></a>"
413413
d = REXML::Document.new(s)
414-
c = REXML::XPath.match( d, "//c[@id = '5']")
415-
cs = REXML::XPath.match( c, "preceding::c" )
416-
assert_equal( 4, cs.length )
414+
c = REXML::XPath.match(d, "//c[@id = '5']")
415+
assert_equal(1, c.length)
416+
cs = REXML::XPath.match(c.first, "preceding::c")
417+
assert_equal(4, cs.length)
417418
end
418419

419420
def test_following

0 commit comments

Comments
 (0)