Merge branch 'fix/formatter-ignore-html-chars' into 'develop'

[#441] Fix characters converted to HTML being picked up by hashtag parser

See merge request pleroma/pleroma!575
This commit is contained in:
lambda 2018-12-18 20:23:21 +00:00
commit b7f6350107
2 changed files with 15 additions and 3 deletions

View File

@ -4,12 +4,12 @@ defmodule Pleroma.Formatter do
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.Emoji alias Pleroma.Emoji
@tag_regex ~r/\#\w+/u @tag_regex ~r/((?<=[^&])|\A)(\#)(\w+)/u
@markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/ @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
def parse_tags(text, data \\ %{}) do def parse_tags(text, data \\ %{}) do
Regex.scan(@tag_regex, text) Regex.scan(@tag_regex, text)
|> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end) |> Enum.map(fn ["#" <> tag = full_tag | _] -> {full_tag, String.downcase(tag)} end)
|> (fn map -> |> (fn map ->
if data["sensitive"] in [true, "True", "true", "1"], if data["sensitive"] in [true, "True", "true", "1"],
do: [{"#nsfw", "nsfw"}] ++ map, do: [{"#nsfw", "nsfw"}] ++ map,
@ -155,7 +155,7 @@ def add_hashtag_links({subs, text}, tags) do
uuid_text = uuid_text =
tags tags
|> Enum.reduce(text, fn {match, _short, uuid}, text -> |> Enum.reduce(text, fn {match, _short, uuid}, text ->
String.replace(text, match, uuid) String.replace(text, ~r/((?<=[^&])|(\A))#{match}/, uuid)
end) end)
subs = subs =

View File

@ -22,6 +22,18 @@ test "turns hashtags into links" do
assert expected_text == assert expected_text ==
Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize() Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
end end
test "does not turn html characters to tags" do
text = "Fact #3: pleroma does what mastodon't"
expected_text =
"Fact <a data-tag='3' href='http://localhost:4001/tag/3' rel='tag'>#3</a>: pleroma does what mastodon't"
tags = Formatter.parse_tags(text)
assert expected_text ==
Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
end
end end
describe ".add_links" do describe ".add_links" do