scrub_html_and_truncate/1 -> filter_html_and_truncate/1

They shouldn't share the same name when /1 was used for a different type of incoming data anyway
This commit is contained in:
Mark Felder 2021-06-11 15:58:55 -05:00
parent 07064f73bc
commit bb4ced0eb5
2 changed files with 15 additions and 18 deletions

View File

@ -7,7 +7,7 @@ defmodule Pleroma.Web.Metadata.Utils do
alias Pleroma.Formatter alias Pleroma.Formatter
alias Pleroma.HTML alias Pleroma.HTML
def scrub_html_and_truncate(%{data: %{"content" => content}} = _object) do def filter_html_and_truncate(%{data: %{"content" => content}} = _object) do
content content
# html content comes from DB already encoded, decode first and scrub after # html content comes from DB already encoded, decode first and scrub after
|> Emoji.Formatter.demojify() |> Emoji.Formatter.demojify()
@ -20,9 +20,10 @@ defmodule Pleroma.Web.Metadata.Utils do
def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
content content
|> Emoji.Formatter.demojify() |> Emoji.Formatter.demojify()
|> HTML.filter_tags(Pleroma.HTML.Scrubber.BreaksOnly)
|> HtmlEntities.decode() |> HtmlEntities.decode()
|> String.replace(~r/<br\s?\/?>/, "&#10;&#13;") |> String.replace(~r/<br\s?\/?>/, " ")
|> HTML.strip_tags()
|> HtmlEntities.decode()
|> Formatter.truncate(max_length) |> Formatter.truncate(max_length)
end end

View File

@ -7,8 +7,8 @@ defmodule Pleroma.Web.Metadata.UtilsTest do
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Web.Metadata.Utils alias Pleroma.Web.Metadata.Utils
describe "scrub_html_and_truncate" do describe "filter_html_and_truncate/1" do
test "it returns text without encode HTML (objects)" do test "it returns text without HTML" do
user = insert(:user) user = insert(:user)
note = note =
@ -20,10 +20,10 @@ defmodule Pleroma.Web.Metadata.UtilsTest do
} }
}) })
assert Utils.scrub_html_and_truncate(note) == "Pleroma's really cool!" assert Utils.filter_html_and_truncate(note) == "Pleroma's really cool!"
end end
test "it replaces <br> with compatible HTML entity (objects)" do test "it replaces <br> with compatible HTML entity (meta tags, push notifications)" do
user = insert(:user) user = insert(:user)
note = note =
@ -35,31 +35,27 @@ defmodule Pleroma.Web.Metadata.UtilsTest do
} }
}) })
assert Utils.scrub_html_and_truncate(note) == assert Utils.filter_html_and_truncate(note) ==
"First line&#10;&#13;Second line" "First line&#10;&#13;Second line"
end end
end
test "it returns text without encode HTML (binaries)" do describe "scrub_html_and_truncate/2" do
test "it returns text without encode HTML" do
assert Utils.scrub_html_and_truncate("Pleroma's really cool!") == "Pleroma's really cool!" assert Utils.scrub_html_and_truncate("Pleroma's really cool!") == "Pleroma's really cool!"
end end
test "it truncates to specified chars (binaries)" do test "it truncates to specified chars" do
assert Utils.scrub_html_and_truncate("Pleroma's really cool!", 10) == "Pleroma..." assert Utils.scrub_html_and_truncate("Pleroma's really cool!", 10) == "Pleroma..."
end end
# push notifications and link previews should be able to display newlines test "it strips emojis" do
test "it replaces <br> with compatible HTML entity (binaries)" do
assert Utils.scrub_html_and_truncate("First line<br>Second line") ==
"First line&#10;&#13;Second line"
end
test "it strips emojis (binaries)" do
assert Utils.scrub_html_and_truncate( assert Utils.scrub_html_and_truncate(
"Open the door get on the floor everybody walk the dinosaur :dinosaur:" "Open the door get on the floor everybody walk the dinosaur :dinosaur:"
) == "Open the door get on the floor everybody walk the dinosaur" ) == "Open the door get on the floor everybody walk the dinosaur"
end end
test "it strips HTML tags and other entities (binaries)" do test "it strips HTML tags and other entities" do
assert Utils.scrub_html_and_truncate("<title>my title</title> <p>and a paragraph&#33;</p>") == assert Utils.scrub_html_and_truncate("<title>my title</title> <p>and a paragraph&#33;</p>") ==
"my title and a paragraph!" "my title and a paragraph!"
end end