Skip to content

Deprecate accepting array as an element in XPath.match, first and each #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/rexml/xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def XPath::first(element, path=nil, namespaces=nil, variables={}, options={})
parser.namespaces = namespaces
parser.variables = variables
path = "*" unless path
element = [element] unless element.kind_of? Array
parser.parse(path, element).flatten[0]
end

Expand Down Expand Up @@ -64,7 +63,6 @@ def XPath::each(element, path=nil, namespaces=nil, variables={}, options={}, &bl
parser.namespaces = namespaces
parser.variables = variables
path = "*" unless path
element = [element] unless element.kind_of? Array
parser.parse(path, element).each( &block )
end

Expand All @@ -74,7 +72,6 @@ def XPath::match(element, path=nil, namespaces=nil, variables={}, options={})
parser.namespaces = namespaces
parser.variables = variables
path = "*" unless path
element = [element] unless element.kind_of? Array
parser.parse(path,element)
end
end
Expand Down
22 changes: 12 additions & 10 deletions lib/rexml/xpath_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ def variables=( vars={} )
@variables = vars
end

def parse path, nodeset
def parse path, node
path_stack = @parser.parse( path )
match( path_stack, nodeset )
match( path_stack, node )
end

def get_first path, nodeset
def get_first path, node
path_stack = @parser.parse( path )
first( path_stack, nodeset )
first( path_stack, node )
end

def predicate path, nodeset
def predicate path, node
path_stack = @parser.parse( path )
match( path_stack, nodeset )
match( path_stack, node )
end

def []=( variable_name, value )
Expand Down Expand Up @@ -136,11 +136,13 @@ def first( path_stack, node )
end


def match(path_stack, nodeset)
nodeset = nodeset.collect.with_index do |node, i|
position = i + 1
XPathNode.new(node, position: position)
def match(path_stack, node)
if node.is_a?(Array)
Kernel.warn("REXML::XPath.each, REXML::XPath.first, REXML::XPath.match dropped support for nodeset...", uplevel: 1)
return [] if node.empty?
node = node.first
end
nodeset = [XPathNode.new(node, position: 1)]
result = expr(path_stack, nodeset)
case result
when Array # nodeset
Expand Down
16 changes: 10 additions & 6 deletions test/test_jaxen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def process_test_case(name)

# processes a tests/document/context node
def process_context(doc, context)
test_context = XPath.match(doc, context.attributes["select"])
matched = XPath.match(doc, context.attributes["select"])
assert_equal(1, matched.size)
test_context = matched.first
namespaces = context.namespaces
namespaces.delete("var")
namespaces = nil if namespaces.empty?
Expand Down Expand Up @@ -101,10 +103,14 @@ def process_nominal_test(context, variables, namespaces, test)
assert_equal(Integer(expected, 10),
matched.size,
user_message(context, xpath, matched))
else
assert_operator(matched.size, :>, 0, user_message(context, xpath, matched))
end

XPath.each(test, "valueOf") do |value_of|
process_value_of(matched, variables, namespaces, value_of)
matched.each do |subcontext|
process_value_of(subcontext, variables, namespaces, value_of)
end
end
end

Expand All @@ -118,10 +124,8 @@ def process_exceptional_test(context, variables, namespaces, test)

def user_message(context, xpath, matched)
message = ""
context.each_with_index do |node, i|
message << "Node#{i}:\n"
message << "#{node}\n"
end
message << "Node:\n"
message << "#{context}\n"
message << "XPath: <#{xpath}>\n"
message << "Matched <#{matched}>"
message
Expand Down
17 changes: 14 additions & 3 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,10 @@ def test_preceding

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>"
d = REXML::Document.new(s)
c = REXML::XPath.match( d, "//c[@id = '5']")
cs = REXML::XPath.match( c, "preceding::c" )
assert_equal( 4, cs.length )
c = REXML::XPath.match(d, "//c[@id = '5']")
assert_equal(1, c.length)
cs = REXML::XPath.match(c.first, "preceding::c")
assert_equal(4, cs.length)
end

def test_following
Expand Down Expand Up @@ -1158,5 +1159,15 @@ def test_or_and
end
assert_equal(["/"], hrefs, "Bug #3842 [ruby-core:32447]")
end

def test_match_with_deprecated_usage
verbose, $VERBOSE = $VERBOSE, nil
doc = Document.new("<a><b/></a>")
assert_equal(['b'], XPath.match([doc, doc], '//b').map(&:name))
assert_equal(['b'], XPath.match([doc], '//b').map(&:name))
assert_equal([], XPath.match([], '//b').map(&:name))
ensure
$VERBOSE = verbose
end
end
end
Loading