diff --git a/ios/input/EnrichedMarkdownTextInput.mm b/ios/input/EnrichedMarkdownTextInput.mm index ee0d06d1..798e1038 100644 --- a/ios/input/EnrichedMarkdownTextInput.mm +++ b/ios/input/EnrichedMarkdownTextInput.mm @@ -1091,8 +1091,10 @@ - (void)handleTextChanged [self applyFormatting]; + NSUInteger clampedEditLocation = MIN(editLocation, newLength); + NSUInteger clampedInsertedLength = MIN(insertedLength, newLength - clampedEditLocation); [_detectorPipeline processTextChange:ENRMGetPlainText(_textView) - modificationRange:NSMakeRange(editLocation, insertedLength)]; + modificationRange:NSMakeRange(clampedEditLocation, clampedInsertedLength)]; [self updatePlaceholderVisibility]; [self emitOnChangeText]; @@ -1184,14 +1186,16 @@ - (void)textViewDidEndEditing:(UITextView *)textView - (void)textViewDidChangeSelection:(UITextView *)textView { + NSRange newSelection = textView.selectedRange; + NSRange previousSelection = _lastSelectedRange; + _lastSelectedRange = newSelection; + if (_isApplyingFormatting || _isTextChanging) { return; } - NSRange newSelection = textView.selectedRange; BOOL selectionMoved = - newSelection.location != _lastSelectedRange.location || newSelection.length != _lastSelectedRange.length; - _lastSelectedRange = newSelection; + newSelection.location != previousSelection.location || newSelection.length != previousSelection.length; if (selectionMoved) { [_pendingStyles removeAllObjects]; @@ -1263,10 +1267,11 @@ - (void)textInputDidChange - (void)textInputDidChangeSelection { + _lastSelectedRange = _textView.selectedRange; + if (_isApplyingFormatting || _isTextChanging) { return; } - _lastSelectedRange = _textView.selectedRange; [_pendingStyles removeAllObjects]; [_pendingStyleRemovals removeAllObjects];