8000 Fix duplicate responses in XPath following, following-sibling, preceding, preceding-sibling by naitoh · Pull Request #255 · ruby/rexml · GitHub
[go: up one dir, main page]

Skip to content

Fix duplicate responses in XPath following, following-sibling, preceding, preceding-sibling #255

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
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
2 changes: 1 addition & 1 deletion lib/rexml/xpath_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def match(path_stack, nodeset)
result = expr(path_stack, nodeset)
case result
when Array # nodeset
unnode(result)
unnode(result).uniq
else
[result]
end
Expand Down
97 changes: 94 additions & 3 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,103 @@ def test_preceding
assert_equal( 4, cs.length )
end

def test_preceding_sibling
d = REXML::Document.new("<a><b><c/><d/><x/></b><b><e/><x/></b></a>")
matches = REXML::XPath.match(d, "a/b/x/preceding-sibling::node()")
def test_preceding_multiple
source = <<-XML
<a>
<b/><c/><d/><d/><e/><f/>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/d/preceding::*")
assert_equal(["d", "c", "b"], matches.map(&:name))
end

def test_following_multiple
source = <<-XML
<a>
<b/><c/><d/><d/><e/><f/>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/d/following::*")
assert_equal(["d", "e", "f"], matches.map(&:name))
end

def test_following_sibling_across_multiple_nodes
source = <<-XML
<a>
<b>
<x/><c/><d/>
</b>
<b>
<x/><e/>
</b>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/b/x/following-sibling::*")
assert_equal(["c", "d", "e"], matches.map(&:name))
end

def test_following_sibling_within_single_node
source = <<-XML
<a>
<b>
<x/><c/><d/><x/><e/>
</b>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/b/x/following-sibling::*")
assert_equal(["c", "d", "x", "e"], matches.map(&:name))
end

def test_following_sibling_predicates
source = <<-XML
<div>
<div>
<a/><w/>
</div>
<a/><x/>
<a/><y/>
<a/><z/>
</div>
XML
doc = REXML::Document.new(source)
# Finds a node flowing <a/>
matches = REXML::XPath.match(doc, "//a/following-sibling::*[1]")
assert_equal(["w", "x", "y", "z"], matches.map(&:name))
end

def test_preceding_sibling_across_multiple_nodes
source = <<-XML
<a>
<b>
<c/><d/><x/>
</b>
<b>
<e/><x/>
</b>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/b/x/preceding-sibling::*")
assert_equal(["e", "d", "c"], matches.map(&:name))
end

def test_preceding_sibling_within_single_node
source = <<-XML
<a>
<b>
<c/><d/><x/><e/><x/>
</b>
</a>
XML
doc = REXML::Document.new(source)
matches = REXML::XPath.match(doc, "a/b/x/preceding-sibling::*")
assert_equal(["e", "x", "d", "c"], matches.map(&:name))
end

def test_following
d = Document.new "<a><b id='0'/><b/><b><c id='1'/><c id='2'/></b><b id='1'/></a>"
start = XPath.first( d, "/a/b[@id='0']" )
Expand Down
Loading
0