From 14fd960fbb56fc9e8baedc2d8bab878df56671d4 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Fri, 27 Nov 2026 15:04:02 +0100 Subject: [PATCH] fix: text replacement and autocorrect attribtues restamping --- ios/EnrichedTextInputView.mm | 48 +++++++++++++++---- .../InputAttributesManager.h | 1 + .../InputAttributesManager.mm | 12 +++++ 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/ios/EnrichedTextInputView.mm b/ios/EnrichedTextInputView.mm index ca28eff12..b38d56f54 100644 --- a/ios/EnrichedTextInputView.mm +++ b/ios/EnrichedTextInputView.mm @@ -63,6 +63,7 @@ @implementation EnrichedTextInputView { NSArray *_contextMenuItems; NSString *_submitBehavior; NSDictionary *_capturedAttributesBeforeChange; + NSRange _capturedReplacementRange; NSString *_recentlyEmittedAlignment; CustomStyleData *_recentlyEmittedCustomStyle; } @@ -2080,15 +2081,32 @@ - (void)handleKeyPressInRange:(NSString *)text range:(NSRange)range { - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { - // Capture the attributes at range.location that are being replaced - // (autocorrect / predictive) so didProcessEditing: can re-stamp them onto the - // replacement. Skip pure deletions (text.length == 0) — there is no incoming - // text to receive attributes, and capturing here would cause the deleted - // character's CustomStyleData to be re-stamped onto the widened editedRange. - if (range.length > 0 && text.length > 0) { - _capturedAttributesBeforeChange = - [textView.textStorage attributesAtIndex:range.location - effectiveRange:NULL]; + // Autocorrect, spellcheck and predictive text replace a word at the caret and + // drop its styling, so capture it for didProcessEditing: to re-apply. + // Typing over a selection is handled by the typing attributes instead. + if (range.length > 0 && text.length > 0 && + textView.selectedRange.length == 0) { + NSDictionary *attrs = [textView.textStorage attributesAtIndex:range.location + effectiveRange:NULL]; + NSMutableDictionary *captured = [[NSMutableDictionary alloc] init]; + for (StyleBase *style in stylesDict.allValues) { + if ([style isParagraph]) { + id value = attrs[[style getKey]]; + if (value != nil) { + captured[[style getKey]] = value; + } + continue; + } + // Skips styles that never extend to new text (mentions, links, images). + AttributeEntry *entry = + [style getEntryIfPresent:NSMakeRange(range.location, 1)]; + if (entry != nullptr) { + captured[entry.key] = entry.value; + } + } + _capturedAttributesBeforeChange = captured; + // Only the replacement text; didProcessEditing:'s editedRange can be wider. + _capturedReplacementRange = NSMakeRange(range.location, text.length); } // Check if the user pressed "Enter" @@ -2174,6 +2192,14 @@ - (bool)textView:(UITextView *)textView // native TextKit issue. if (textView.markedTextRange == nil && text.length > 0) { [attributesManager repeatRecentTypingAttributesManagement]; + + // Replacing a selection - we need to manually extend typing attributes that + // were there before, as by default the attributes manager only takes a + // preceding index into account. Must run after the repeat above, which + // resets the typing attributes. + if (range.length > 0 && textView.selectedRange.length > 0) { + [attributesManager extendTypingAttributesFromReplacedRange:range]; + } } return YES; @@ -2318,12 +2344,14 @@ - (void)textStorage:(NSTextStorage *)textStorage if (_capturedAttributesBeforeChange != nil) { // Skip while an IME composition is in progress; restamp on commit. if (textView.markedTextRange == nil) { + NSRange restampRange = NSIntersectionRange( + _capturedReplacementRange, NSMakeRange(0, textStorage.length)); NSSet *customKeys = [attributesManager customAttributesKeys]; for (NSString *key in _capturedAttributesBeforeChange) { if ([customKeys containsObject:key]) { [textStorage addAttribute:key value:_capturedAttributesBeforeChange[key] - range:editedRange]; + range:restampRange]; } } } diff --git a/ios/inputAttributesManager/InputAttributesManager.h b/ios/inputAttributesManager/InputAttributesManager.h index b4ba67a7b..f8381756a 100644 --- a/ios/inputAttributesManager/InputAttributesManager.h +++ b/ios/inputAttributesManager/InputAttributesManager.h @@ -14,6 +14,7 @@ - (void)clearRemovedTypingAttributes; - (void)manageTypingAttributesWithOnlySelection:(BOOL)onlySelectionChanged; - (void)repeatRecentTypingAttributesManagement; +- (void)extendTypingAttributesFromReplacedRange:(NSRange)range; - (void)handleDirtyRangesStyling; - (NSSet *)customAttributesKeys; @end diff --git a/ios/inputAttributesManager/InputAttributesManager.mm b/ios/inputAttributesManager/InputAttributesManager.mm index bcf793214..d2d4ec5ce 100644 --- a/ios/inputAttributesManager/InputAttributesManager.mm +++ b/ios/inputAttributesManager/InputAttributesManager.mm @@ -242,4 +242,16 @@ - (void)repeatRecentTypingAttributesManagement { [self manageTypingAttributesWithOnlySelection:_recentOnlySelectionStatus]; } +- (void)extendTypingAttributesFromReplacedRange:(NSRange)range { + if (NSMaxRange(range) > _input->textView.textStorage.string.length) { + return; + } + // Same filtering as extending from the preceding character: styles that + // shouldn't be extended (mentions, links, images) are skipped. + NSMutableDictionary *newAttrs = + [_input->textView.typingAttributes mutableCopy]; + [self restoreInlineStylesPresentInRange:range intoAttrs:newAttrs]; + _input->textView.typingAttributes = newAttrs; +} + @end