Skip to content
Open
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: 2 additions & 1 deletion benchmark/xpath.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ contexts:
prelude: |
require 'rexml/document'
DEPTH = 100
DEPTH = 30
xml = '<a>' * DEPTH + '</a>' * DEPTH
doc = REXML::Document.new(xml)
benchmark:
"REXML::XPath.match(REXML::Document.new(xml), 'a//a')" : REXML::XPath.match(doc, "a//a")
"REXML::XPath.match(REXML::Document.new(xml), '//a//a')" : REXML::XPath.match(doc, "//a//a")
11 changes: 5 additions & 6 deletions lib/rexml/xpath_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,21 +462,20 @@ def step(path_stack, any_type: :element, order: :forward)
if nodesets.size == 1
ordered_nodeset = nodesets[0]
else
seen = {}.compare_by_identity
raw_nodes = []
nodesets.each do |nodeset|
nodeset.each do |node|
if node.respond_to?(:raw_node)
raw_nodes << node.raw_node
else
raw_nodes << node
end
raw_node = node.respond_to?(:raw_node) ? node.raw_node : node
next if seen.key?(raw_node)
seen[raw_node] = true
raw_nodes << raw_node
end
end
ordered_nodeset = sort(raw_nodes, order)
end
new_nodeset = []
ordered_nodeset.each do |node|
# TODO: Remove duplicated
new_nodeset << XPathNode.new(node, position: new_nodeset.size + 1)
end
new_nodeset
Expand Down
104 changes: 104 additions & 0 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1276,5 +1276,109 @@ def test_match_with_deprecated_usage
ensure
$VERBOSE = verbose
end

def test_descendant_no_duplicates
doc = <<-EOT
<a>
<a id='1'>
<a id='2'/>
</a>
<a id='3'/>
</a>
EOT

xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a//a")
assert_equal(["1", "2", "3"], result.map { |e| e.attributes["id"] })
end

def test_descendant_document_order
doc = <<-EOT
<a>
<a id='1'>
<a id='2'>
<a id='3'/>
</a>
</a>
</a>
EOT
xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a//a")
assert_equal(["1", "2", "3"], result.map { |e| e.attributes["id"] })
end

def test_descendant_ancestor_descendant_overlap
doc = <<-EOT
<root>
<a id='1'>
<a id='2'>
<b id='b1'/>
</a>
</a>
</root>
EOT

xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a//b")
assert_equal(["b1"], result.map { |e| e.attributes["id"] })
end

def test_descendant_sibling_branches
doc = <<-EOT
<root>
<a id='1'>
<b id='b1'/>
</a>
<a id='2'>
<b id='b2'/>
</a>
</root>
EOT
xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a//b")
assert_equal(["b1", "b2"], result.map { |e| e.attributes["id"] })
end

def test_descendant_document_order_with_shared_subtree
doc = <<-EOT
<root>
<a>
<b id='b1'/>
<a>
<b id='b2'/>
</a>
</a>
</root>
EOT

xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a//b")
assert_equal(["b1", "b2"], result.map { |e| e.attributes["id"] })
end

def test_descendant_axis_on_leaf_element
doc = Document.new("<root><b/></root>")
result = XPath.match(doc, "//b/descendant::node()")
assert_equal([], result)
end

def test_descendant_axis_position_predicate_per_context_node
doc = <<-EOT
<a id="a1">
<b id="b1">
<a id="a2">
<b id="b2">
<b id="b3">
</b>
</b>
</a>
</b>
</a>
EOT

xmldoc = Document.new(doc)
result = XPath.match(xmldoc, "//a/descendant::b[1]")
assert_equal(["b1", "b2"], result.map { |e| e.attributes["id"] })
end
end
end
Loading