Skip to content

Commit

Permalink
Apply a better try catch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cooltey committed Nov 18, 2023
1 parent 3fda90e commit 5a152ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/wikipedia/richtext/TextViewHtml.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.wikipedia.richtext

import android.widget.TextView
import org.wikipedia.util.StringUtil

fun TextView.setHtml(source: String?) {
this.text = CustomHtmlParser.fromHtml(source, this).trim()
this.text = StringUtil.fromHtml(source).trim()
}
7 changes: 6 additions & 1 deletion app/src/main/java/org/wikipedia/util/StringUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.text.style.StyleSpan
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.IntRange
import androidx.core.text.HtmlCompat
import androidx.core.text.buildSpannedString
import androidx.core.text.set
import okio.ByteString.Companion.encodeUtf8
Expand Down Expand Up @@ -109,7 +110,11 @@ object StringUtil {
}

fun fromHtml(source: String?): Spanned {
return CustomHtmlParser.fromHtml(source)
return try {
CustomHtmlParser.fromHtml(source)
} catch (e: Exception) {
HtmlCompat.fromHtml(source.orEmpty(), HtmlCompat.FROM_HTML_MODE_LEGACY)
}
}

fun highlightEditText(editText: EditText, parentText: String, highlightText: String) {
Expand Down

0 comments on commit 5a152ff

Please sign in to comment.