mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Merge pull request #7523 from hayaksi1/fix/1874-empty-formatted-body
Fall back to the message body when the formatted body has nothing to render
This commit is contained in:
+3
-2
@@ -19,7 +19,8 @@ import org.jsoup.safety.Safelist
|
||||
|
||||
/**
|
||||
* Converts the HTML string [FormattedBody.body] to a [Document] by parsing it.
|
||||
* If the message is not formatted or the format is not [MessageFormat.HTML] we return `null`.
|
||||
* If the message is not formatted, the format is not [MessageFormat.HTML], or nothing renderable is left once the
|
||||
* unsupported tags have been removed, we return `null` so that the plain text body is used instead.
|
||||
*
|
||||
* This will also make sure mentions are prefixed with `@`.
|
||||
*
|
||||
@@ -44,7 +45,7 @@ fun FormattedBody.toHtmlDocument(
|
||||
// Prepend `@` to mentions
|
||||
fixMentions(dom, permalinkParser)
|
||||
|
||||
dom
|
||||
dom.takeIf { it.text().isNotBlank() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
@@ -103,4 +103,28 @@ class ToHtmlDocumentTest : RobolectricTest() {
|
||||
})
|
||||
assertThat(document?.text()).isEqualTo("Hey Alice!")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toHtmlDocument - returns null if the body only contains unsupported tags`() {
|
||||
val body = FormattedBody(
|
||||
format = MessageFormat.HTML,
|
||||
body = "<img data-mx-emoticon src=\"mxc://matrix.org/anImage\" height=\"32\" width=\"32\" alt=\"\uD83D\uDE1C\" />"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument(permalinkParser = FakePermalinkParser())
|
||||
|
||||
assertThat(document).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toHtmlDocument - returns a Document if some text survives next to unsupported tags`() {
|
||||
val body = FormattedBody(
|
||||
format = MessageFormat.HTML,
|
||||
body = "Look at this <img src=\"mxc://matrix.org/anImage\" alt=\"cat\" />!"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument(permalinkParser = FakePermalinkParser())
|
||||
|
||||
assertThat(document?.text()).isEqualTo("Look at this !")
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -149,4 +149,16 @@ class ToPlainTextTest : RobolectricTest() {
|
||||
)
|
||||
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo("This is the message content.")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `TextMessageType toPlainText - returns the body if the formatted one only contains an image`() {
|
||||
val messageType = TextMessageType(
|
||||
body = "\uD83D\uDE1C",
|
||||
formatted = FormattedBody(
|
||||
format = MessageFormat.HTML,
|
||||
body = "<img data-mx-emoticon src=\"mxc://matrix.org/anImage\" height=\"32\" width=\"32\" alt=\"\uD83D\uDE1C\" />"
|
||||
)
|
||||
)
|
||||
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo("\uD83D\uDE1C")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user