Fix text range logic for a11y (flutter/engine#16496)
Make sure that a text range at the end of the string is still valid.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
namespace fml {
|
||||
|
||||
NSRange RangeForCharacterAtIndex(NSString* text, NSUInteger index) {
|
||||
if (text == nil || index >= text.length) {
|
||||
if (text == nil || index > text.length) {
|
||||
return NSMakeRange(NSNotFound, 0);
|
||||
}
|
||||
if (index < text.length)
|
||||
|
||||
@@ -28,3 +28,8 @@ TEST(StringRangeSanitizationTest, CanHandleUnicodeRange) {
|
||||
EXPECT_EQ(result.location, 0UL);
|
||||
EXPECT_EQ(result.length, 0UL);
|
||||
}
|
||||
|
||||
TEST(StringRangeSanitizationTest, HandlesEndOfRange) {
|
||||
EXPECT_EQ(fml::RangeForCharacterAtIndex(@"1234", 4).location, 4UL);
|
||||
EXPECT_EQ(fml::RangeForCharacterAtIndex(@"1234", 4).length, 0UL);
|
||||
}
|
||||
|
||||
@@ -312,7 +312,13 @@ static UIReturnKeyType ToUIReturnKeyType(NSString* inputType) {
|
||||
}
|
||||
|
||||
- (NSString*)textInRange:(UITextRange*)range {
|
||||
if (!range) {
|
||||
return nil;
|
||||
}
|
||||
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.");
|
||||
return [self.text substringWithRange:textRange];
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,13 @@
|
||||
}
|
||||
|
||||
- (NSString*)textInRange:(UITextRange*)range {
|
||||
if (!range) {
|
||||
return nil;
|
||||
}
|
||||
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.");
|
||||
return [self.text substringWithRange:textRange];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user