Fix error when removing attributes from tree that contains comments

This commit is contained in:
Shadowfacts 2021-09-22 21:08:52 -04:00
parent d58dcf07fa
commit 75404b197d
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 7 additions and 1 deletions

View File

@ -29,6 +29,7 @@ defmodule Readability.Helper do
"""
@spec remove_attrs(html_tree, String.t() | [String.t()] | Regex.t()) :: html_tree
def remove_attrs(content, _) when is_binary(content), do: content
def remove_attrs({:comment, _} = comment, _), do: comment
def remove_attrs([], _), do: []
def remove_attrs([h | t], t_attrs) do

View File

@ -57,7 +57,6 @@ defmodule Readability.HelperTest do
assert result == expected
end
test "inner text length", %{html_tree: html_tree} do
result = html_tree |> Helper.text_length()
assert result == 5
@ -93,4 +92,10 @@ defmodule Readability.HelperTest do
assert result_with_scheme =~ foo_url
assert result_with_scheme =~ bar_url_https
end
test "remove attrs with comments" do
tree = Floki.parse("<div class=\"foo\">hello <span><!-- world --></span></div>")
expected = Floki.parse("<div>hello <span><!-- world --></span></div>")
assert expected == Helper.remove_attrs(tree, ~w[class])
end
end