Skip to content

Commit

Permalink
Always return find elements in the correct order (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypconstante committed Feb 17, 2024
1 parent dc9544e commit 5e7ec16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/floki/finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Floki.Finder do
stack = Enum.map(selectors, fn s -> {s, node_ids} end)

traverse_html_tree(stack, tree, [])
|> Enum.reverse()
|> Enum.sort_by(& &1.node_id)
|> Enum.uniq()
end

Expand Down
22 changes: 16 additions & 6 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -917,15 +917,24 @@ defmodule FlokiTest do
# Floki.find/2 - Selector with descendant combinator

test "get elements descending the parent" do
doc =
document!(
html_body("""
<div id="first-div">
<div id="second-div">
<span id="first-span"></span>
</div>
<span id="second-span"></span>
</div>
""")
)

expected = [
{
"img",
[{"src", "http://twitter.com/logo.png"}, {"class", "js-twitter-logo"}],
[]
}
{"span", [{"id", "first-span"}], []},
{"span", [{"id", "second-span"}], []}
]

assert_find(document!(@html_with_img), "a img", expected)
assert_find(doc, "div span", expected)
end

# Floki.find/2 - Selector with child combinator
Expand Down Expand Up @@ -1051,6 +1060,7 @@ defmodule FlokiTest do
]

assert_find(document!(@html_with_img), ".js-twitter-logo, #logo", expected)
assert_find(document!(@html_with_img), "#logo, .js-twitter-logo", expected)
end

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

0 comments on commit 5e7ec16

Please sign in to comment.