Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS text selection crash by returning nil range #55909

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -1290,7 +1290,14 @@ - (NSString*)textInRange:(UITextRange*)range {
NSAssert([range isKindOfClass:[FlutterTextRange class]],
@"Expected a FlutterTextRange for range (got %@).", [range class]);
NSRange textRange = ((FlutterTextRange*)range).range;
NSAssert(textRange.location != NSNotFound, @"Expected a valid text range.");
if (textRange.location == NSNotFound) {
importRyan marked this conversation as resolved.
Show resolved Hide resolved
// Avoids [crashes](https://github.com/flutter/flutter/issues/138464) from an assertion
// against NSNotFound.
// TODO(hellohuanlin): This is a temp workaround, but we should look into why
// framework is providing NSNotFound to the engine.
// https://github.com/flutter/flutter/issues/160100
return nil;
}
// Sanitize the range to prevent going out of bounds.
NSUInteger location = MIN(textRange.location, self.text.length);
NSUInteger length = MIN(self.text.length - location, textRange.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,19 @@ - (void)testTextInRange {
XCTAssertEqual(substring.length, 0ul);
}

- (void)testTextInRangeAcceptsNSNotFoundLocationGracefully {
NSDictionary* config = self.mutableTemplateCopy;
[self setClientId:123 configuration:config];
NSArray<FlutterTextInputView*>* inputFields = self.installedInputViews;
FlutterTextInputView* inputView = inputFields[0];

[inputView insertText:@"text"];
UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(NSNotFound, 0)];

NSString* substring = [inputView textInRange:range];
XCTAssertNil(substring);
}

- (void)testStandardEditActions {
NSDictionary* config = self.mutableTemplateCopy;
[self setClientId:123 configuration:config];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ - (NSString*)textInRange:(UITextRange*)range {
NSAssert([range isKindOfClass:[FlutterTextRange class]],
@"Expected a FlutterTextRange for range (got %@).", [range class]);
NSRange textRange = ((FlutterTextRange*)range).range;
NSAssert(textRange.location != NSNotFound, @"Expected a valid text range.");
if (textRange.location == NSNotFound) {
importRyan marked this conversation as resolved.
Show resolved Hide resolved
// Avoids [crashes](https://github.com/flutter/flutter/issues/138464) from an assertion
// against NSNotFound.
// TODO(hellohuanlin): This is a temp workaround, but we should look into why
// framework is providing NSNotFound to the engine.
// https://github.com/flutter/flutter/issues/160100
return nil;
}
return [self.text substringWithRange:textRange];
}

Expand Down
Loading