Skip to content
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

Always return find elements in the correct order #540

Merged
merged 1 commit into from
Feb 17, 2024
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/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
Loading