From 7c88a053b39557381fc913bd92c3b88f771b60ab Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Sat, 17 Feb 2024 11:54:54 -0300 Subject: [PATCH] Always return find elements in the correct order --- lib/floki/finder.ex | 2 +- test/floki_test.exs | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/floki/finder.ex b/lib/floki/finder.ex index 4eabeab3..d5e5bbd8 100644 --- a/lib/floki/finder.ex +++ b/lib/floki/finder.ex @@ -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 diff --git a/test/floki_test.exs b/test/floki_test.exs index 94cfdbfe..f499ba76 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -917,15 +917,21 @@ defmodule FlokiTest do # Floki.find/2 - Selector with descendant combinator test "get elements descending the parent" do - expected = [ - { - "img", - [{"src", "http://twitter.com/logo.png"}, {"class", "js-twitter-logo"}], - [] - } + doc = document!(html_body(""" +
+
+ +
+ +
+ """)) + + expected = [ + {"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 @@ -1051,6 +1057,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