From bb4ced0eb5cdf8744adea2da9c268734a6170060 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 11 Jun 2021 15:58:55 -0500 Subject: [PATCH] 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 --- lib/pleroma/web/metadata/utils.ex | 7 ++++--- test/pleroma/web/metadata/utils_test.exs | 26 +++++++++++--------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex index 649319db5..f5b79a82c 100644 --- a/lib/pleroma/web/metadata/utils.ex +++ b/lib/pleroma/web/metadata/utils.ex @@ -7,7 +7,7 @@ defmodule Pleroma.Web.Metadata.Utils do alias Pleroma.Formatter alias Pleroma.HTML - def scrub_html_and_truncate(%{data: %{"content" => content}} = _object) do + def filter_html_and_truncate(%{data: %{"content" => content}} = _object) do content # html content comes from DB already encoded, decode first and scrub after |> 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 content |> Emoji.Formatter.demojify() - |> HTML.filter_tags(Pleroma.HTML.Scrubber.BreaksOnly) |> HtmlEntities.decode() - |> String.replace(~r//, " ") + |> String.replace(~r//, " ") + |> HTML.strip_tags() + |> HtmlEntities.decode() |> Formatter.truncate(max_length) end diff --git a/test/pleroma/web/metadata/utils_test.exs b/test/pleroma/web/metadata/utils_test.exs index c5d241886..9d94bf9ca 100644 --- a/test/pleroma/web/metadata/utils_test.exs +++ b/test/pleroma/web/metadata/utils_test.exs @@ -7,8 +7,8 @@ defmodule Pleroma.Web.Metadata.UtilsTest do import Pleroma.Factory alias Pleroma.Web.Metadata.Utils - describe "scrub_html_and_truncate" do - test "it returns text without encode HTML (objects)" do + describe "filter_html_and_truncate/1" do + test "it returns text without HTML" do user = insert(:user) 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 - test "it replaces
with compatible HTML entity (objects)" do + test "it replaces
with compatible HTML entity (meta tags, push notifications)" do user = insert(:user) 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 Second line" 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!" 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..." end - # push notifications and link previews should be able to display newlines - test "it replaces
with compatible HTML entity (binaries)" do - assert Utils.scrub_html_and_truncate("First line
Second line") == - "First line Second line" - end - - test "it strips emojis (binaries)" do + test "it strips emojis" do 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" 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("my title

and a paragraph!

") == "my title and a paragraph!" end