Modern Apps git

← Modern-Apps.git

library: tolerate malformed HTML entities and font sizes

Commit
02cec4e928aa5da6c8d86413e643ec448802f642
Author
Vayun Mathur <[email protected]>
When
2026-11-19 01:54 UTC (17 days ago)
Parents
365a57e
HtmlOdfConverter.decodeEntities fed numeric character
references straight into Character.toChars, which throws
IllegalArgumentException for code points above U+10FFFF, so
importing HTML containing e.g. &#1114112; crashed the
converter. Out-of-range references now stay as literal text.

applyInlineStyle also called toFloat() on a [0-9.]+ match,
which matches bare '.' or '1.2.3' in malformed style
attributes and threw NumberFormatException. It now uses
toFloatOrNull and skips the size.

Verified: ./gradlew :library:ui:testDebugUnitTest and
./check library/ui pass.

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/odf/HtmlOdfConverter.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/odf/HtmlOdfConverter.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/odf/HtmlOdfConverter.ktindex 0119b203b2..e537352ecd 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/odf/HtmlOdfConverter.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/odf/HtmlOdfConverter.kt@@ -195,7 +195,9 @@ object HtmlOdfConverter {             val style = attrs["style"] ?: ""             Regex("color\\s*:\\s*#?([0-9a-fA-F]{6})").find(style)?.let { s = s.copy(color = 0xFF000000L or it.groupValues[1].toLong(16)) }             Regex("font-size\\s*:\\s*([0-9.]+)\\s*(pt|px)").find(style)?.let { m ->-                val v = m.groupValues[1].toFloat(); s = s.copy(fontSize = if (m.groupValues[2] == "px") v * 0.75f else v)+                // [0-9.]+ also matches "." / "1.2.3" in malformed HTML; toFloat() would throw.+                val v = m.groupValues[1].toFloatOrNull() ?: return@let+                s = s.copy(fontSize = if (m.groupValues[2] == "px") v * 0.75f else v)             }             if (style.contains("font-weight") && (style.contains("bold") || Regex("font-weight\\s*:\\s*[6-9]00").containsMatchIn(style))) s = s.copy(bold = true)             if (Regex("font-style\\s*:\\s*italic").containsMatchIn(style)) s = s.copy(italic = true)@@ -251,8 +253,8 @@ object HtmlOdfConverter {         if (!s.contains('&')) return s         return s.replace("&nbsp;", " ").replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">")             .replace("&quot;", "\"").replace("&#39;", "'").replace("&apos;", "'")-            .replace(Regex("&#(\\d+);")) { m -> m.groupValues[1].toIntOrNull()?.let { String(Character.toChars(it)) } ?: m.value }-            .replace(Regex("&#x([0-9a-fA-F]+);")) { m -> m.groupValues[1].toIntOrNull(16)?.let { String(Character.toChars(it)) } ?: m.value }+            .replace(Regex("&#(\\d+);")) { m -> m.groupValues[1].toIntOrNull()?.let { cp -> runCatching { String(Character.toChars(cp)) }.getOrNull() } ?: m.value }+            .replace(Regex("&#x([0-9a-fA-F]+);")) { m -> m.groupValues[1].toIntOrNull(16)?.let { cp -> runCatching { String(Character.toChars(cp)) }.getOrNull() } ?: m.value }     }      // endregion