From 468e70126f7efda02e111277e3c4431f2a3b471e 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 | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 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..8bde64bb 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -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(""" +
+
+ +
+ +
+ """) + ) + 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 @@ -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