Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
case UITextGranularityLine:
// The default UITextInputStringTokenizer does not handle line granularity
// correctly. We need to implement our own line tokenizer.
result = [self lineEnclosingPosition:position];
result = [self lineEnclosingPosition:position inDirection:direction];
break;
case UITextGranularityCharacter:
case UITextGranularityWord:
Expand All @@ -618,7 +618,19 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
return result;
}

- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position {
- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position
inDirection:(UITextDirection)direction {
// TODO(hellohuanlin): remove iOS 17 check. The same logic should apply to older iOS version.
if (@available(iOS 17.0, *)) {
// If querying the line from end of document in forward direction, report not found.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should link to and quote the docs I mentioned in my comment so that it's clear why.

FlutterTextPosition* flutterPosition = (FlutterTextPosition*)position;
if (flutterPosition.index > _textInputView.text.length ||
(flutterPosition.index == _textInputView.text.length &&
direction == UITextStorageDirectionForward)) {
return nil;
}
}

// Gets the first line break position after the input position.
NSString* textAfter = [_textInputView
textInRange:[_textInputView textRangeFromPosition:position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,54 @@ - (void)testFlutterTokenizerCanParseLines {
XCTAssertEqual(range.range.length, 20u);
}

- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInBackwardDirectionShouldNotReturnNil {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionBackward];
XCTAssertEqual(range.range.location, 11u);
XCTAssertEqual(range.range.length, 6u);
}

- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInForwardDirectionShouldReturnNilOnIOS17 {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionForward];
if (@available(iOS 17.0, *)) {
XCTAssertNil(range);
} else {
XCTAssertEqual(range.range.location, 11u);
XCTAssertEqual(range.range.length, 6u);
}
}

- (void)testFlutterTokenizerLineEnclosingOutOfRangePositionShouldReturnNilOnIOS17 {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextPosition* position = [FlutterTextPosition positionWithIndex:100];
FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:position
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionForward];
if (@available(iOS 17.0, *)) {
XCTAssertNil(range);
} else {
XCTAssertEqual(range.range.location, 0u);
XCTAssertEqual(range.range.length, 0u);
}
}

- (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine];
Expand Down