feat(chat): support line breaks and pasted content. Closes #3108

This commit is contained in:
Gabe Kangas
2023-06-27 14:45:45 -07:00
parent bd6e263eb9
commit a354787a9e
7 changed files with 239 additions and 92 deletions

View File

@@ -149,7 +149,7 @@ func sanitize(raw string) string {
// Allow breaks
p.AllowElements("br")
p.AllowElementsContent("p")
p.AllowElements("p")
// Allow img tags from the the local emoji directory only
p.AllowAttrs("src").Matching(_sanitizeReSrcMatch).OnElements("img")

View File

@@ -16,11 +16,11 @@ Here is an iframe<iframe src="http://yahoo.com"></iframe>
[test link](http://owncast.online)
<img class="emoji" src="/img/emoji/bananadance.gif">`
expected := `Test one two three! I go to <a href="http://yahoo.com" rel="nofollow noreferrer noopener" target="_blank">http://yahoo.com</a> and search for <em>sports</em> and <strong>answers</strong>.
Here is an iframe
expected := `<p>Test one two three! I go to <a href="http://yahoo.com" rel="nofollow noreferrer noopener" target="_blank">http://yahoo.com</a> and search for <em>sports</em> and <strong>answers</strong>.
Here is an iframe</p>
blah blah blah
<a href="http://owncast.online" rel="nofollow noreferrer noopener" target="_blank">test link</a>
<img class="emoji" src="/img/emoji/bananadance.gif">`
<p><a href="http://owncast.online" rel="nofollow noreferrer noopener" target="_blank">test link</a>
<img class="emoji" src="/img/emoji/bananadance.gif"></p>`
result := events.RenderAndSanitize(messageContent)
if result != expected {
@@ -31,7 +31,7 @@ blah blah blah
// Test to make sure we block remote images in chat messages.
func TestBlockRemoteImages(t *testing.T) {
messageContent := `<img src="https://via.placeholder.com/img/emoji/350x150"> test ![](https://via.placeholder.com/img/emoji/350x150)`
expected := `test`
expected := `<p> test </p>`
result := events.RenderAndSanitize(messageContent)
if result != expected {
@@ -42,7 +42,7 @@ func TestBlockRemoteImages(t *testing.T) {
// Test to make sure emoji images are allowed in chat messages.
func TestAllowEmojiImages(t *testing.T) {
messageContent := `<img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test ![](/img/emoji/beerparrot.gif)`
expected := `<img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test <img src="/img/emoji/beerparrot.gif">`
expected := `<p><img alt=":beerparrot:" title=":beerparrot:" src="/img/emoji/beerparrot.gif"> test <img src="/img/emoji/beerparrot.gif"></p>`
result := events.RenderAndSanitize(messageContent)
if result != expected {