Skip to content

Commit d96c527

Browse files
committed
Make sure we're checking only within text limit
1 parent 29673ed commit d96c527

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,13 @@ class PlaceholderManager(
157157
if (aztecText.length() == 0) {
158158
return null
159159
}
160+
val limitLength = aztecText.length() - 1
160161
val selectionStart = aztecText.selectionStart
161-
val selectionStartMinusOne = (selectionStart - 1).coerceAtLeast(0)
162-
val selectionStartMinusTwo = (selectionStart - 2).coerceAtLeast(0)
162+
val selectionStartMinusOne = (selectionStart - 1).coerceIn(0, limitLength)
163+
val selectionStartMinusTwo = (selectionStart - 2).coerceIn(0, limitLength)
163164
val selectionEnd = aztecText.selectionEnd
164-
val selectionEndPlusOne = (selectionStart + 1).coerceAtMost(aztecText.length())
165-
val selectionEndPlusTwo = (selectionStart + 2).coerceAtMost(aztecText.length())
165+
val selectionEndPlusOne = (selectionStart + 1).coerceIn(0, limitLength)
166+
val selectionEndPlusTwo = (selectionStart + 2).coerceIn(0, limitLength)
166167
val editableText = aztecText.editableText
167168
var placeAtStart = false
168169
val (from, to) = if (editableText[selectionStartMinusOne] == Constants.IMG_CHAR) {
@@ -171,10 +172,10 @@ class PlaceholderManager(
171172
selectionStartMinusTwo to selectionStart
172173
} else if (editableText[selectionEndPlusOne] == Constants.IMG_CHAR) {
173174
placeAtStart = true
174-
selectionEndPlusOne to (selectionEndPlusOne + 1).coerceAtMost(aztecText.length())
175+
selectionEndPlusOne to (selectionEndPlusOne + 1).coerceIn(0, limitLength)
175176
} else if (editableText[selectionEndPlusOne] == '\n' && editableText[selectionEndPlusTwo] == Constants.IMG_CHAR) {
176177
placeAtStart = true
177-
selectionEndPlusTwo to (selectionEndPlusTwo + 1).coerceAtMost(aztecText.length())
178+
selectionEndPlusTwo to (selectionEndPlusTwo + 1).coerceIn(0, limitLength)
178179
} else {
179180
selectionStart to selectionEnd
180181
}

0 commit comments

Comments
 (0)