[iOS TextInputPlugin] adjust the markedTextRange when replacing text. (flutter/engine#46603)
Fixes https://github.com/flutter/flutter/issues/130880 , by making sure when the text is cleared the `markedTextRange` is cleared with it. `[UITextField replaceRange:withText:]` always sets the selection range to the end of the replacement text, and removes the current `markedTextRange`. This PR makes the input plugin do the same in the `replaceRange:withText:` implementation. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -128,7 +128,6 @@ FLUTTER_DARWIN_EXPORT
|
||||
|
||||
// UITextInput
|
||||
@property(nonatomic, readonly) NSMutableString* text;
|
||||
@property(nonatomic, readonly) NSMutableString* markedText;
|
||||
@property(readwrite, copy) UITextRange* selectedTextRange;
|
||||
@property(nonatomic, strong) UITextRange* markedTextRange;
|
||||
@property(nonatomic, copy) NSDictionary* markedTextStyle;
|
||||
|
||||
@@ -814,7 +814,6 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
|
||||
|
||||
// UITextInput
|
||||
_text = [[NSMutableString alloc] init];
|
||||
_markedText = [[NSMutableString alloc] init];
|
||||
_selectedTextRange = [[FlutterTextRange alloc] initWithNSRange:NSMakeRange(0, 0)];
|
||||
_markedRect = kInvalidFirstRect;
|
||||
_cachedFirstRect = kInvalidFirstRect;
|
||||
@@ -1249,25 +1248,17 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
|
||||
// Replace the text within the specified range with the given text,
|
||||
// without notifying the framework.
|
||||
- (void)replaceRangeLocal:(NSRange)range withText:(NSString*)text {
|
||||
NSRange selectedRange = _selectedTextRange.range;
|
||||
|
||||
// Adjust the text selection:
|
||||
// * reduce the length by the intersection length
|
||||
// * adjust the location by newLength - oldLength + intersectionLength
|
||||
NSRange intersectionRange = NSIntersectionRange(range, selectedRange);
|
||||
if (range.location <= selectedRange.location) {
|
||||
selectedRange.location += text.length - range.length;
|
||||
}
|
||||
if (intersectionRange.location != NSNotFound) {
|
||||
selectedRange.location += intersectionRange.length;
|
||||
selectedRange.length -= intersectionRange.length;
|
||||
}
|
||||
|
||||
[self.text replaceCharactersInRange:[self clampSelection:range forText:self.text]
|
||||
withString:text];
|
||||
[self setSelectedTextRangeLocal:[FlutterTextRange
|
||||
rangeWithNSRange:[self clampSelection:selectedRange
|
||||
forText:self.text]]];
|
||||
|
||||
// Adjust the selected range and the marked text range. There's no
|
||||
// documentation but UITextField always sets markedTextRange to nil,
|
||||
// and collapses the selection to the end of the new replacement text.
|
||||
const NSRange newSelectionRange =
|
||||
[self clampSelection:NSMakeRange(range.location + text.length, 0) forText:self.text];
|
||||
|
||||
[self setSelectedTextRangeLocal:[FlutterTextRange rangeWithNSRange:newSelectionRange]];
|
||||
self.markedTextRange = nil;
|
||||
}
|
||||
|
||||
- (void)replaceRange:(UITextRange*)range withText:(NSString*)text {
|
||||
@@ -1345,11 +1336,10 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
|
||||
return YES;
|
||||
}
|
||||
|
||||
// Either replaces the existing marked text or, if none is present, inserts it in
|
||||
// place of the current selection.
|
||||
- (void)setMarkedText:(NSString*)markedText selectedRange:(NSRange)markedSelectedRange {
|
||||
NSString* textBeforeChange = [self.text copy];
|
||||
NSRange selectedRange = _selectedTextRange.range;
|
||||
NSRange markedTextRange = ((FlutterTextRange*)self.markedTextRange).range;
|
||||
NSRange actualReplacedRange;
|
||||
|
||||
if (_scribbleInteractionStatus != FlutterScribbleInteractionStatusNone ||
|
||||
_scribbleFocusStatus != FlutterScribbleFocusStatusUnfocused) {
|
||||
@@ -1360,26 +1350,24 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
|
||||
markedText = @"";
|
||||
}
|
||||
|
||||
if (markedTextRange.length > 0) {
|
||||
// Replace text in the marked range with the new text.
|
||||
[self replaceRangeLocal:markedTextRange withText:markedText];
|
||||
actualReplacedRange = markedTextRange;
|
||||
markedTextRange.length = markedText.length;
|
||||
} else {
|
||||
// Replace text in the selected range with the new text.
|
||||
actualReplacedRange = selectedRange;
|
||||
[self replaceRangeLocal:selectedRange withText:markedText];
|
||||
markedTextRange = NSMakeRange(selectedRange.location, markedText.length);
|
||||
}
|
||||
const FlutterTextRange* currentMarkedTextRange = (FlutterTextRange*)self.markedTextRange;
|
||||
const NSRange& actualReplacedRange = currentMarkedTextRange && !currentMarkedTextRange.isEmpty
|
||||
? currentMarkedTextRange.range
|
||||
: _selectedTextRange.range;
|
||||
// No need to call replaceRangeLocal as this method always adjusts the
|
||||
// selected/marked text ranges anyways.
|
||||
[self.text replaceCharactersInRange:actualReplacedRange withString:markedText];
|
||||
|
||||
const NSRange newMarkedRange = NSMakeRange(actualReplacedRange.location, markedText.length);
|
||||
self.markedTextRange =
|
||||
markedTextRange.length > 0 ? [FlutterTextRange rangeWithNSRange:markedTextRange] : nil;
|
||||
newMarkedRange.length > 0 ? [FlutterTextRange rangeWithNSRange:newMarkedRange] : nil;
|
||||
|
||||
NSUInteger selectionLocation = markedSelectedRange.location + markedTextRange.location;
|
||||
selectedRange = NSMakeRange(selectionLocation, markedSelectedRange.length);
|
||||
[self setSelectedTextRangeLocal:[FlutterTextRange
|
||||
rangeWithNSRange:[self clampSelection:selectedRange
|
||||
forText:self.text]]];
|
||||
[self setSelectedTextRangeLocal:
|
||||
[FlutterTextRange
|
||||
rangeWithNSRange:[self clampSelection:NSMakeRange(markedSelectedRange.location +
|
||||
newMarkedRange.location,
|
||||
markedSelectedRange.length)
|
||||
forText:self.text]]];
|
||||
if (_enableDeltaModel) {
|
||||
NSRange nextReplaceRange = [self clampSelection:actualReplacedRange forText:textBeforeChange];
|
||||
[self updateEditingStateWithDelta:flutter::TextEditingDelta(
|
||||
|
||||
@@ -786,6 +786,25 @@ FLUTTER_ASSERT_ARC
|
||||
XCTAssertEqual(inputView.spellCheckingType, UITextSpellCheckingTypeNo);
|
||||
}
|
||||
|
||||
- (void)testReplaceTestLocalAdjustSelectionAndMarkedTextRange {
|
||||
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
|
||||
[inputView setMarkedText:@"test text" selectedRange:NSMakeRange(0, 5)];
|
||||
NSRange selectedTextRange = ((FlutterTextRange*)inputView.selectedTextRange).range;
|
||||
const NSRange markedTextRange = ((FlutterTextRange*)inputView.markedTextRange).range;
|
||||
XCTAssertEqual(selectedTextRange.location, 0ul);
|
||||
XCTAssertEqual(selectedTextRange.length, 5ul);
|
||||
XCTAssertEqual(markedTextRange.location, 0ul);
|
||||
XCTAssertEqual(markedTextRange.length, 9ul);
|
||||
|
||||
// Replaces space with space.
|
||||
[inputView replaceRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(4, 1)] withText:@" "];
|
||||
selectedTextRange = ((FlutterTextRange*)inputView.selectedTextRange).range;
|
||||
|
||||
XCTAssertEqual(selectedTextRange.location, 5ul);
|
||||
XCTAssertEqual(selectedTextRange.length, 0ul);
|
||||
XCTAssertEqual(inputView.markedTextRange, nil);
|
||||
}
|
||||
|
||||
- (void)testFlutterTextInputViewOnlyRespondsToInsertionPointColorBelowIOS17 {
|
||||
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
|
||||
BOOL respondsToInsertionPointColor =
|
||||
|
||||
Reference in New Issue
Block a user