From 75feb0104e8dc5d575392219546c74aa748a3fa7 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Tue, 10 Oct 2023 11:45:35 +0200 Subject: [PATCH] Make setSelection safe --- .../src/main/kotlin/org/wordpress/aztec/AztecText.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index ebc396d4f..b0890ce90 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -2433,6 +2433,18 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown this.uncaughtExceptionHandler = null } + override fun setSelection(index: Int) { + if (index in 0..this.length()) { + super.setSelection(index) + } else if (index < 0) { + AppLog.e(AppLog.T.EDITOR, "Attempted to set selection to incorrect value $index") + setSelection(0) + } else if (index > this.length()) { + AppLog.e(AppLog.T.EDITOR, "Attempted to set selection to incorrect value $index") + setSelection(this.length()) + } + } + override fun dispatchHoverEvent(event: MotionEvent): Boolean { return if (accessibilityDelegate.onHoverEvent(event)) true else super.dispatchHoverEvent(event) }