Merge pull request #2069 from etherealvisage/develop

Support data-mx-[bg-]color attribute on <font> tags in formatted messages.
This commit is contained in:
Benoit Marty 2020-09-08 10:09:25 +02:00 committed by GitHub
commit f65e96e7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -44,6 +44,7 @@ Build 🧱:
Other changes: Other changes:
- Use File extension functions to make code more concise (#1996) - Use File extension functions to make code more concise (#1996)
- Create a script to import SAS strings (#1909) - Create a script to import SAS strings (#1909)
- Support `data-mx-[bg-]color` attributes on `<font>` tags.
Changes in Element 1.0.5 (2020-08-21) Changes in Element 1.0.5 (2020-08-21)
=================================================== ===================================================

View File

@ -17,6 +17,7 @@ package im.vector.app.features.html
import android.graphics.Color import android.graphics.Color
import android.text.style.ForegroundColorSpan import android.text.style.ForegroundColorSpan
import android.text.style.BackgroundColorSpan
import io.noties.markwon.MarkwonConfiguration import io.noties.markwon.MarkwonConfiguration
import io.noties.markwon.RenderProps import io.noties.markwon.RenderProps
import io.noties.markwon.html.HtmlTag import io.noties.markwon.html.HtmlTag
@ -30,11 +31,21 @@ class FontTagHandler : SimpleTagHandler() {
override fun supportedTags() = listOf("font") override fun supportedTags() = listOf("font")
override fun getSpans(configuration: MarkwonConfiguration, renderProps: RenderProps, tag: HtmlTag): Any? { override fun getSpans(configuration: MarkwonConfiguration, renderProps: RenderProps, tag: HtmlTag): Any? {
val colorString = tag.attributes()["color"]?.let { parseColor(it) } ?: Color.BLACK val mxColorString = tag.attributes()["data-mx-color"]
return ForegroundColorSpan(colorString) 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 { try {
return Color.parseColor(colorName) return Color.parseColor(colorName)
} catch (e: Exception) { } catch (e: Exception) {
@ -56,7 +67,7 @@ class FontTagHandler : SimpleTagHandler() {
"blue" -> Color.BLUE "blue" -> Color.BLUE
"orange" -> Color.parseColor("#FFA500") "orange" -> Color.parseColor("#FFA500")
"navy" -> Color.parseColor("#000080") "navy" -> Color.parseColor("#000080")
else -> Color.BLACK else -> failResult
} }
} }
} }