[iOS] handle text plugin negative range (flutter/engine#19068)

This commit is contained in:
LongCatIsLooong
2020-06-17 11:38:03 -07:00
committed by GitHub
parent f9f4426fba
commit e3f23590e6
2 changed files with 24 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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