Skip to content

Unexpected order using find with comma separated selectors in v0.35.3 #539

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

Closed
danschultzer opened this issue Feb 16, 2024 · 2 comments · Fixed by #540
Closed

Unexpected order using find with comma separated selectors in v0.35.3 #539

danschultzer opened this issue Feb 16, 2024 · 2 comments · Fixed by #540
Labels

Comments

@danschultzer
Copy link

Upgrading from 0.35.2 to 0.35.3, find no longer conforms with the order of the document elements for comma separated selectors.

This is what 0.35.2 produces:

iex(1)> Floki.find("<div class=\"first\"></div><p class=\"second\"></p><div class=\"third\"></div>", "p,div")

[
  {"div", [{"class", "first"}], []},
  {"p", [{"class", "second"}], []},
  {"div", [{"class", "third"}], []}
]

This is what 0.35.3 produces:

iex(1)> Floki.find("<div class=\"first\"></div><p class=\"second\"></p><div class=\"third\"></div>", "p,div")

[
  {"p", [{"class", "second"}], []},
  {"div", [{"class", "first"}], []},
  {"div", [{"class", "third"}], []}
]
@philss
Copy link
Owner

philss commented Feb 16, 2024

Good call! I think the problem was introduced in #518 (d0be551 according to bisect).

@ypconstante would you mind to take a look? I wrote a small test case:

defmodule Floki.FindWithBug do
  use ExUnit.Case, async: true

  @tag only_parser: Floki.HTMLParser.Mochiweb
  test "correct order" do
    assert Floki.find(
             "<div class=\"first\"></div><p class=\"second\"></p><div class=\"third\"></div>",
             "p,div"
           ) ==
             [
               {"div", [{"class", "first"}], []},
               {"p", [{"class", "second"}], []},
               {"div", [{"class", "third"}], []}
             ]
  end
end

@ypconstante
Copy link
Contributor

Yeah, the stack change is causing this.
I did some checks and there are more cases in which we don't return the nodes in the right order, unrelated with this change

iex> Floki.find(
  Floki.parse_fragment!("""
  <div id="1">
    <div id="2">
      <p id="3"></p>
    </div>
    <p id="4"></p>
  </div>
  """),
  "div p"
)

[{"p", [{"id", "4"}], []}, {"p", [{"id", "3"}], []}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants