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:
Dan Field
2020-02-07 12:20:30 -08:00
committed by GitHub
parent d8390d3f38
commit 21f6cef10b
4 changed files with 18 additions and 1 deletions

View File

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

View File

@@ -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);
}

View File

@@ -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];
}

View File

@@ -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];
}