Skip to content

Commit 7c88a05

Browse files
committed
Always return find elements in the correct order
1 parent dc9544e commit 7c88a05

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/floki/finder.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ defmodule Floki.Finder do
4747
stack = Enum.map(selectors, fn s -> {s, node_ids} end)
4848

4949
traverse_html_tree(stack, tree, [])
50-
|> Enum.reverse()
50+
|> Enum.sort_by(& &1.node_id)
5151
|> Enum.uniq()
5252
end
5353

test/floki_test.exs

+14-7
Original file line numberDiff line numberDiff line change
@@ -917,15 +917,21 @@ defmodule FlokiTest do
917917
# Floki.find/2 - Selector with descendant combinator
918918

919919
test "get elements descending the parent" do
920-
expected = [
921-
{
922-
"img",
923-
[{"src", "http://twitter.com/logo.png"}, {"class", "js-twitter-logo"}],
924-
[]
925-
}
920+
doc = document!(html_body("""
921+
<div id="first-div">
922+
<div id="second-div">
923+
<span id="first-span"></span>
924+
</div>
925+
<span id="second-span"></span>
926+
</div>
927+
"""))
928+
929+
expected = [
930+
{"span", [{"id", "first-span"}], []},
931+
{"span", [{"id", "second-span"}], []}
926932
]
927933

928-
assert_find(document!(@html_with_img), "a img", expected)
934+
assert_find(doc, "div span", expected)
929935
end
930936

931937
# Floki.find/2 - Selector with child combinator
@@ -1051,6 +1057,7 @@ defmodule FlokiTest do
10511057
]
10521058

10531059
assert_find(document!(@html_with_img), ".js-twitter-logo, #logo", expected)
1060+
assert_find(document!(@html_with_img), "#logo, .js-twitter-logo", expected)
10541061
end
10551062

10561063
test "get one element when search for multiple and just one exist" do

0 commit comments

Comments
 (0)