philss / floki

Floki is a simple HTML parser that enables search for nodes using CSS selectors.
https://hex.pm/packages/floki
MIT License
2.07k stars 156 forks source link

Floki.attr can mix the order of the nodes #166

Closed francois2metz closed 6 years ago

francois2metz commented 6 years ago

I have some html and I need to modify some content. I tried to use Floki.attr, but on some specific html structure, the resulting tree have nodes with another order.

I have a testcase to illustrate the issue:

 test "changing attribute dontt change the order of nodes" do
    html = ~s(<p>a<em>b</em>c<a href="z">d</a></p><p>e</p><p><a href="f"><strong>g</strong></a>.<em>h</em>i</p><p><strong>j</strong>k<a href="m">n</a>o</p><p><em>p</em>q<em>r</em>s<a href="t">u</a></p>)
    result =
      html
      |> Floki.attr("a", "href", fn href ->
        href
      end)
      |> Floki.raw_html()
    assert result == html
  end
 1) test changing attribute don't change the order of nodes (FlokiTest)
     test/floki_test.exs:1068
     Assertion with == failed
     code:  assert result == html
     left:  "<p><em>p</em>q<em>r</em>s<a href=\"t\">u</a></p><p>e</p><p>a<em>b</em>c<a href=\"z\">d</a></p><p><a href=\"f\"><strong>g</strong></a>.<em>h</em>i</p><p><strong>j</strong>k<a href=\"m\">n</a>o</p>"
     right: "<p>a<em>b</em>c<a href=\"z\">d</a></p><p>e</p><p><a href=\"f\"><strong>g</strong></a>.<em>h</em>i</p><p><strong>j</strong>k<a href=\"m\">n</a>o</p><p><em>p</em>q<em>r</em>s<a href=\"t\">u</a></p>"
     stacktrace:
       test/floki_test.exs:1076: (test)

I tried to fix the issue, but I struggle on the tree -> tuple conversion. Do you have any idea ?

philss commented 6 years ago

Hi @francois2metz. This fix was released under version 0.20.0. Thank you!