[iOS] handle text plugin negative range (flutter/engine#19068)
This commit is contained in:
@@ -549,7 +549,17 @@ static NSString* uniqueIdFromDictionary(NSDictionary* dictionary) {
|
||||
toPosition:(UITextPosition*)toPosition {
|
||||
NSUInteger fromIndex = ((FlutterTextPosition*)fromPosition).index;
|
||||
NSUInteger toIndex = ((FlutterTextPosition*)toPosition).index;
|
||||
return [FlutterTextRange rangeWithNSRange:NSMakeRange(fromIndex, toIndex - fromIndex)];
|
||||
if (toIndex >= fromIndex) {
|
||||
return [FlutterTextRange rangeWithNSRange:NSMakeRange(fromIndex, toIndex - fromIndex)];
|
||||
} else {
|
||||
// toIndex may be less than fromIndex, because
|
||||
// UITextInputStringTokenizer does not handle CJK characters
|
||||
// well in some cases. See:
|
||||
// https://github.com/flutter/flutter/issues/58750#issuecomment-644469521
|
||||
// Swap fromPosition and toPosition to match the behavior of native
|
||||
// UITextViews.
|
||||
return [FlutterTextRange rangeWithNSRange:NSMakeRange(toIndex, fromIndex - toIndex)];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSUInteger)decrementOffsetPosition:(NSUInteger)position {
|
||||
|
||||
@@ -235,4 +235,17 @@ FLUTTER_ASSERT_ARC
|
||||
// Clean up mocks
|
||||
[engine stopMocking];
|
||||
}
|
||||
|
||||
- (void)testTextRangeFromPositionMatchesUITextViewBehavior {
|
||||
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithFrame:CGRectZero];
|
||||
FlutterTextPosition* fromPosition = [[FlutterTextPosition alloc] initWithIndex:2];
|
||||
FlutterTextPosition* toPosition = [[FlutterTextPosition alloc] initWithIndex:0];
|
||||
|
||||
FlutterTextRange* flutterRange = (FlutterTextRange*)[inputView textRangeFromPosition:fromPosition
|
||||
toPosition:toPosition];
|
||||
NSRange range = flutterRange.range;
|
||||
|
||||
XCTAssertEqual(range.location, 0);
|
||||
XCTAssertEqual(range.length, 2);
|
||||
}
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user