Skip to content

Commit 78b4419

Browse files
authored
fix(ios): prevent crash when deleting mixed selection or using IME input (#277)
Always update _lastSelectedRange in textViewDidChangeSelection and textInputDidChangeSelection before the isApplyingFormatting guard, ensuring _preEditSelectedRange is never stale when handleTextChanged computes edit parameters. Also clamp modificationRange passed to the detector pipeline so out-of-bounds values cannot reach ENRMWordsUtils. Made-with: Cursor
1 parent 98d17a4 commit 78b4419

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

ios/input/EnrichedMarkdownTextInput.mm

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,10 @@ - (void)handleTextChanged
10911091

10921092
[self applyFormatting];
10931093

1094+
NSUInteger clampedEditLocation = MIN(editLocation, newLength);
1095+
NSUInteger clampedInsertedLength = MIN(insertedLength, newLength - clampedEditLocation);
10941096
[_detectorPipeline processTextChange:ENRMGetPlainText(_textView)
1095-
modificationRange:NSMakeRange(editLocation, insertedLength)];
1097+
modificationRange:NSMakeRange(clampedEditLocation, clampedInsertedLength)];
10961098

10971099
[self updatePlaceholderVisibility];
10981100
[self emitOnChangeText];
@@ -1184,14 +1186,16 @@ - (void)textViewDidEndEditing:(UITextView *)textView
11841186

11851187
- (void)textViewDidChangeSelection:(UITextView *)textView
11861188
{
1189+
NSRange newSelection = textView.selectedRange;
1190+
NSRange previousSelection = _lastSelectedRange;
1191+
_lastSelectedRange = newSelection;
1192+
11871193
if (_isApplyingFormatting || _isTextChanging) {
11881194
return;
11891195
}
11901196

1191-
NSRange newSelection = textView.selectedRange;
11921197
BOOL selectionMoved =
1193-
newSelection.location != _lastSelectedRange.location || newSelection.length != _lastSelectedRange.length;
1194-
_lastSelectedRange = newSelection;
1198+
newSelection.location != previousSelection.location || newSelection.length != previousSelection.length;
11951199

11961200
if (selectionMoved) {
11971201
[_pendingStyles removeAllObjects];
@@ -1263,10 +1267,11 @@ - (void)textInputDidChange
12631267

12641268
- (void)textInputDidChangeSelection
12651269
{
1270+
_lastSelectedRange = _textView.selectedRange;
1271+
12661272
if (_isApplyingFormatting || _isTextChanging) {
12671273
return;
12681274
}
1269-
_lastSelectedRange = _textView.selectedRange;
12701275

12711276
[_pendingStyles removeAllObjects];
12721277
[_pendingStyleRemovals removeAllObjects];

0 commit comments

Comments
 (0)