Fix error when removing attributes from tree that contains comments
This commit is contained in:
parent
d58dcf07fa
commit
75404b197d
|
@ -29,6 +29,7 @@ defmodule Readability.Helper do
|
||||||
"""
|
"""
|
||||||
@spec remove_attrs(html_tree, String.t() | [String.t()] | Regex.t()) :: html_tree
|
@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(content, _) when is_binary(content), do: content
|
||||||
|
def remove_attrs({:comment, _} = comment, _), do: comment
|
||||||
def remove_attrs([], _), do: []
|
def remove_attrs([], _), do: []
|
||||||
|
|
||||||
def remove_attrs([h | t], t_attrs) do
|
def remove_attrs([h | t], t_attrs) do
|
||||||
|
|
|
@ -57,7 +57,6 @@ defmodule Readability.HelperTest do
|
||||||
assert result == expected
|
assert result == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
test "inner text length", %{html_tree: html_tree} do
|
test "inner text length", %{html_tree: html_tree} do
|
||||||
result = html_tree |> Helper.text_length()
|
result = html_tree |> Helper.text_length()
|
||||||
assert result == 5
|
assert result == 5
|
||||||
|
@ -93,4 +92,10 @@ defmodule Readability.HelperTest do
|
||||||
assert result_with_scheme =~ foo_url
|
assert result_with_scheme =~ foo_url
|
||||||
assert result_with_scheme =~ bar_url_https
|
assert result_with_scheme =~ bar_url_https
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue