From 5fbcf348f59c190806137653d1c1d06a7da39d80 Mon Sep 17 00:00:00 2001 From: ethereal Date: Mon, 7 Sep 2020 00:57:54 -0400 Subject: [PATCH 1/2] Support data-mx-bg-color attribute on tags in formatted messages. Signed-off-by: Kestrel Williams-King --- CHANGES.md | 1 + .../java/im/vector/app/features/html/FontTagHandler.kt | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 09e1775ae2..2b5c1d1ec5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,6 +41,7 @@ Build 🧱: Other changes: - Use File extension functions to make code more concise (#1996) - Create a script to import SAS strings (#1909) + - Support `data-mx-bg-color` attribute on `` tags. Changes in Element 1.0.5 (2020-08-21) =================================================== diff --git a/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt b/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt index 0c5eff61ae..0f39e25dc4 100644 --- a/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt +++ b/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt @@ -17,6 +17,7 @@ package im.vector.app.features.html import android.graphics.Color import android.text.style.ForegroundColorSpan +import android.text.style.BackgroundColorSpan import io.noties.markwon.MarkwonConfiguration import io.noties.markwon.RenderProps import io.noties.markwon.html.HtmlTag @@ -31,7 +32,13 @@ class FontTagHandler : SimpleTagHandler() { override fun getSpans(configuration: MarkwonConfiguration, renderProps: RenderProps, tag: HtmlTag): Any? { val colorString = tag.attributes()["color"]?.let { parseColor(it) } ?: Color.BLACK - return ForegroundColorSpan(colorString) + val bgColor = tag.attributes()["data-mx-bg-color"] + if(bgColor == null) { + return ForegroundColorSpan(colorString) + } + else { + return arrayOf(ForegroundColorSpan(colorString), BackgroundColorSpan(bgColor.let { parseColor(it) })) + } } private fun parseColor(colorName: String): Int { From 8291dfc188f2b3ea2562e2bad0a32c73f7644767 Mon Sep 17 00:00:00 2001 From: ethereal Date: Mon, 7 Sep 2020 14:52:30 -0400 Subject: [PATCH 2/2] Support data-mx-color attribute in tags as well. --- CHANGES.md | 2 +- .../app/features/html/FontTagHandler.kt | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2b5c1d1ec5..263e630cc1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,7 +41,7 @@ Build 🧱: Other changes: - Use File extension functions to make code more concise (#1996) - Create a script to import SAS strings (#1909) - - Support `data-mx-bg-color` attribute on `` tags. + - Support `data-mx-[bg-]color` attributes on `` tags. Changes in Element 1.0.5 (2020-08-21) =================================================== diff --git a/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt b/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt index 0f39e25dc4..72e337b48f 100644 --- a/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt +++ b/vector/src/main/java/im/vector/app/features/html/FontTagHandler.kt @@ -31,17 +31,21 @@ class FontTagHandler : SimpleTagHandler() { override fun supportedTags() = listOf("font") override fun getSpans(configuration: MarkwonConfiguration, renderProps: RenderProps, tag: HtmlTag): Any? { - val colorString = tag.attributes()["color"]?.let { parseColor(it) } ?: Color.BLACK - val bgColor = tag.attributes()["data-mx-bg-color"] - if(bgColor == null) { - return ForegroundColorSpan(colorString) - } - else { - return arrayOf(ForegroundColorSpan(colorString), BackgroundColorSpan(bgColor.let { parseColor(it) })) + val mxColorString = tag.attributes()["data-mx-color"] + val colorString = tag.attributes()["color"] + val mxBgColorString = tag.attributes()["data-mx-bg-color"] + + val foregroundColor = mxColorString?.let { parseColor(it, Color.BLACK) } ?: colorString?.let { parseColor(it, Color.BLACK) } ?: Color.BLACK + + if (mxBgColorString != null) { + val backgroundColor = parseColor(mxBgColorString, Color.TRANSPARENT) + return arrayOf(ForegroundColorSpan(foregroundColor), BackgroundColorSpan(backgroundColor)) + } else { + return ForegroundColorSpan(foregroundColor) } } - private fun parseColor(colorName: String): Int { + private fun parseColor(colorName: String, failResult: Int): Int { try { return Color.parseColor(colorName) } catch (e: Exception) { @@ -63,7 +67,7 @@ class FontTagHandler : SimpleTagHandler() { "blue" -> Color.BLUE "orange" -> Color.parseColor("#FFA500") "navy" -> Color.parseColor("#000080") - else -> Color.BLACK + else -> failResult } } }