fix voice control delete line command does not delete line correctly (flutter/engine#24831)
This commit is contained in:
@@ -44,6 +44,10 @@
|
||||
|
||||
@end
|
||||
|
||||
/** A tokenizer used by `FlutterTextInputView` to customize string parsing. */
|
||||
@interface FlutterTokenizer : UITextInputStringTokenizer
|
||||
@end
|
||||
|
||||
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
|
||||
FLUTTER_DARWIN_EXPORT
|
||||
#endif
|
||||
|
||||
@@ -378,6 +378,73 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) {
|
||||
}
|
||||
@end
|
||||
|
||||
#pragma mark - FlutterTokenizer
|
||||
|
||||
@interface FlutterTokenizer ()
|
||||
|
||||
@property(nonatomic, assign) FlutterTextInputView* textInputView;
|
||||
|
||||
@end
|
||||
|
||||
@implementation FlutterTokenizer
|
||||
|
||||
- (instancetype)initWithTextInput:(UIResponder<UITextInput>*)textInput {
|
||||
NSAssert([textInput isKindOfClass:[FlutterTextInputView class]],
|
||||
@"The FlutterTokenizer can only be used in a FlutterTextInputView");
|
||||
self = [super initWithTextInput:textInput];
|
||||
if (self) {
|
||||
_textInputView = (FlutterTextInputView*)textInput;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
|
||||
withGranularity:(UITextGranularity)granularity
|
||||
inDirection:(UITextDirection)direction {
|
||||
UITextRange* result;
|
||||
switch (granularity) {
|
||||
case UITextGranularityLine:
|
||||
// The default UITextInputStringTokenizer does not handle line granularity
|
||||
// correctly. We need to implement our own line tokenizer.
|
||||
result = [self lineEnclosingPosition:position];
|
||||
break;
|
||||
case UITextGranularityCharacter:
|
||||
case UITextGranularityWord:
|
||||
case UITextGranularitySentence:
|
||||
case UITextGranularityParagraph:
|
||||
case UITextGranularityDocument:
|
||||
// The UITextInputStringTokenizer can handle all these cases correctly.
|
||||
result = [super rangeEnclosingPosition:position
|
||||
withGranularity:granularity
|
||||
inDirection:direction];
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position {
|
||||
// Gets the first line break position after the input position.
|
||||
NSString* textAfter = [_textInputView
|
||||
textInRange:[_textInputView textRangeFromPosition:position
|
||||
toPosition:[_textInputView endOfDocument]]];
|
||||
NSArray<NSString*>* linesAfter = [textAfter componentsSeparatedByString:@"\n"];
|
||||
NSInteger offSetToLineBreak = [linesAfter firstObject].length;
|
||||
UITextPosition* lineBreakAfter = [_textInputView positionFromPosition:position
|
||||
offset:offSetToLineBreak];
|
||||
// Gets the first line break position before the input position.
|
||||
NSString* textBefore = [_textInputView
|
||||
textInRange:[_textInputView textRangeFromPosition:[_textInputView beginningOfDocument]
|
||||
toPosition:position]];
|
||||
NSArray<NSString*>* linesBefore = [textBefore componentsSeparatedByString:@"\n"];
|
||||
NSInteger offSetFromLineBreak = [linesBefore lastObject].length;
|
||||
UITextPosition* lineBreakBefore = [_textInputView positionFromPosition:position
|
||||
offset:-offSetFromLineBreak];
|
||||
|
||||
return [_textInputView textRangeFromPosition:lineBreakBefore toPosition:lineBreakAfter];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// A FlutterTextInputView that masquerades as a UITextField, and forwards
|
||||
// selectors it can't respond to to a shared UITextField instance.
|
||||
//
|
||||
@@ -629,7 +696,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) {
|
||||
|
||||
- (id<UITextInputTokenizer>)tokenizer {
|
||||
if (_tokenizer == nil) {
|
||||
_tokenizer = [[UITextInputStringTokenizer alloc] initWithTextInput:self];
|
||||
_tokenizer = [[FlutterTokenizer alloc] initWithTextInput:self];
|
||||
}
|
||||
return _tokenizer;
|
||||
}
|
||||
|
||||
@@ -133,6 +133,16 @@ FLUTTER_ASSERT_ARC
|
||||
[FlutterTextInputView class]]];
|
||||
}
|
||||
|
||||
- (FlutterTextRange*)getLineRangeFromTokenizer:(id<UITextInputTokenizer>)tokenizer
|
||||
atIndex:(NSInteger)index {
|
||||
UITextRange* range =
|
||||
[tokenizer rangeEnclosingPosition:[FlutterTextPosition positionWithIndex:index]
|
||||
withGranularity:UITextGranularityLine
|
||||
inDirection:UITextLayoutDirectionRight];
|
||||
XCTAssertTrue([range isKindOfClass:[FlutterTextRange class]]);
|
||||
return (FlutterTextRange*)range;
|
||||
}
|
||||
|
||||
#pragma mark - Tests
|
||||
|
||||
- (void)testSecureInput {
|
||||
@@ -818,4 +828,41 @@ FLUTTER_ASSERT_ARC
|
||||
XCTAssertEqual(inputView.receivedNotificationTarget, backing);
|
||||
}
|
||||
|
||||
- (void)testFlutterTokenizerCanParseLines {
|
||||
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init];
|
||||
inputView.textInputDelegate = engine;
|
||||
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
|
||||
|
||||
// The tokenizer returns zero range When text is empty.
|
||||
FlutterTextRange* range = [self getLineRangeFromTokenizer:tokenizer atIndex:0];
|
||||
XCTAssertEqual(range.range.location, 0u);
|
||||
XCTAssertEqual(range.range.length, 0u);
|
||||
|
||||
[inputView insertText:@"how are you\nI am fine, Thank you"];
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:0];
|
||||
XCTAssertEqual(range.range.location, 0u);
|
||||
XCTAssertEqual(range.range.length, 11u);
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:2];
|
||||
XCTAssertEqual(range.range.location, 0u);
|
||||
XCTAssertEqual(range.range.length, 11u);
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:11];
|
||||
XCTAssertEqual(range.range.location, 0u);
|
||||
XCTAssertEqual(range.range.length, 11u);
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:12];
|
||||
XCTAssertEqual(range.range.location, 12u);
|
||||
XCTAssertEqual(range.range.length, 20u);
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:15];
|
||||
XCTAssertEqual(range.range.location, 12u);
|
||||
XCTAssertEqual(range.range.length, 20u);
|
||||
|
||||
range = [self getLineRangeFromTokenizer:tokenizer atIndex:32];
|
||||
XCTAssertEqual(range.range.location, 12u);
|
||||
XCTAssertEqual(range.range.length, 20u);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user