diff --git a/CHANGES.md b/CHANGES.md index 7eecb0ee3a..101276278c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -44,6 +44,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` 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 0c5eff61ae..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 @@ -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 @@ -30,11 +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 - return ForegroundColorSpan(colorString) + 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) { @@ -56,7 +67,7 @@ class FontTagHandler : SimpleTagHandler() { "blue" -> Color.BLUE "orange" -> Color.parseColor("#FFA500") "navy" -> Color.parseColor("#000080") - else -> Color.BLACK + else -> failResult } } }