Skip to content

Commit 79cafcc

Browse files
authored
fix: android - pasting incompatible HTML (#135)
* fix: pasting incompatible text * chore: super safe
1 parent c80edf4 commit 79cafcc

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,12 @@ class EnrichedTextInputView : AppCompatEditText {
194194
val end = selection?.end ?: 0
195195

196196
if (htmlText != null) {
197-
val parsedText = parseText(htmlText) as Spannable
198-
val finalText = currentText.mergeSpannables(start, end, parsedText)
199-
setValue(finalText)
200-
return
197+
val parsedText = parseText(htmlText)
198+
if (parsedText is Spannable) {
199+
val finalText = currentText.mergeSpannables(start, end, parsedText)
200+
setValue(finalText)
201+
return
202+
}
201203
}
202204

203205
val finalText = currentText.mergeSpannables(start, end, item?.text.toString())
@@ -215,9 +217,14 @@ class EnrichedTextInputView : AppCompatEditText {
215217
val isHtml = text.startsWith("<html>") && text.endsWith("</html>")
216218
if (!isHtml) return text
217219

218-
val parsed = EnrichedParser.fromHtml(text.toString(), htmlStyle, null)
219-
val withoutLastNewLine = parsed.trimEnd('\n')
220-
return withoutLastNewLine
220+
try {
221+
val parsed = EnrichedParser.fromHtml(text.toString(), htmlStyle, null)
222+
val withoutLastNewLine = parsed.trimEnd('\n')
223+
return withoutLastNewLine
224+
} catch (e: Exception) {
225+
Log.e("EnrichedTextInputView", "Error parsing HTML: ${e.message}")
226+
return text
227+
}
221228
}
222229

223230
fun setValue(value: CharSequence?) {

0 commit comments

Comments
 (0)