-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Workaround iOS text input crash for emoji+Korean text #36295
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,19 @@ | |
|
||
#pragma mark - Static Functions | ||
|
||
// Determine if the character at `range` of `text` is an emoji. | ||
static BOOL IsEmoji(NSString* text, NSRange charRange) { | ||
UChar32 codePoint; | ||
BOOL gotCodePoint = [text getBytes:&codePoint | ||
maxLength:sizeof(codePoint) | ||
usedLength:NULL | ||
encoding:NSUTF32StringEncoding | ||
options:kNilOptions | ||
range:charRange | ||
remainingRange:NULL]; | ||
return gotCodePoint && u_hasBinaryProperty(codePoint, UCHAR_EMOJI); | ||
} | ||
|
||
// "TextInputType.none" is a made-up input type that's typically | ||
// used when there's an in-app virtual keyboard. If | ||
// "TextInputType.none" is specified, disable the system | ||
|
@@ -702,6 +715,10 @@ @interface FlutterTextInputView () | |
@property(nonatomic, assign) CGRect markedRect; | ||
@property(nonatomic) BOOL isVisibleToAutofill; | ||
@property(nonatomic, assign) BOOL accessibilityEnabled; | ||
// The composed character that is temporarily removed by the keyboard API. | ||
// This is cleared at the start of each keyboard interaction. (Enter a character, delete a character | ||
// etc) | ||
@property(nonatomic, copy) NSString* temporarilyDeletedComposedCharacter; | ||
|
||
- (void)setEditableTransform:(NSArray*)matrix; | ||
@end | ||
|
@@ -880,6 +897,8 @@ - (void)dealloc { | |
[_markedTextStyle release]; | ||
[_textContentType release]; | ||
[_textInteraction release]; | ||
[_temporarilyDeletedComposedCharacter release]; | ||
_temporarilyDeletedComposedCharacter = nil; | ||
[super dealloc]; | ||
} | ||
|
||
|
@@ -1224,6 +1243,10 @@ - (void)replaceRange:(UITextRange*)range withText:(NSString*)text { | |
} | ||
|
||
- (BOOL)shouldChangeTextInRange:(UITextRange*)range replacementText:(NSString*)text { | ||
// `temporarilyDeletedComposedCharacter` should only be used during a single text change session. | ||
// So it needs to be cleared at the start of each text editting session. | ||
self.temporarilyDeletedComposedCharacter = nil; | ||
|
||
if (self.returnKeyType == UIReturnKeyDefault && [text isEqualToString:@"\n"]) { | ||
[self.textInputDelegate flutterTextInputView:self | ||
performAction:FlutterTextInputActionNewline | ||
|
@@ -1848,6 +1871,15 @@ - (BOOL)hasText { | |
} | ||
|
||
- (void)insertText:(NSString*)text { | ||
if (self.temporarilyDeletedComposedCharacter.length > 0 && text.length == 1 && !text.UTF8String && | ||
[text characterAtIndex:0] == [self.temporarilyDeletedComposedCharacter characterAtIndex:0]) { | ||
// Workaround for https://github.com/flutter/flutter/issues/111494 | ||
// TODO(cyanglaz): revert this workaround if when flutter supports a minimum iOS version which | ||
// this bug is fixed by Apple. | ||
text = self.temporarilyDeletedComposedCharacter; | ||
self.temporarilyDeletedComposedCharacter = nil; | ||
} | ||
|
||
NSMutableArray<FlutterTextSelectionRect*>* copiedRects = | ||
[[NSMutableArray alloc] initWithCapacity:[_selectionRects count]]; | ||
NSAssert([_selectedTextRange.start isKindOfClass:[FlutterTextPosition class]], | ||
|
@@ -1918,15 +1950,7 @@ - (void)deleteBackward { | |
// We should check if the last character is a part of emoji. | ||
// If so, we must delete the entire emoji to prevent the text from being malformed. | ||
NSRange charRange = fml::RangeForCharacterAtIndex(self.text, oldRange.location - 1); | ||
UChar32 codePoint; | ||
BOOL gotCodePoint = [self.text getBytes:&codePoint | ||
maxLength:sizeof(codePoint) | ||
usedLength:NULL | ||
encoding:NSUTF32StringEncoding | ||
options:kNilOptions | ||
range:charRange | ||
remainingRange:NULL]; | ||
if (gotCodePoint && u_hasBinaryProperty(codePoint, UCHAR_EMOJI)) { | ||
if (IsEmoji(self.text, charRange)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: some emojis are in plane 0 and require only one code point to encode, e.g.: https://www.compart.com/en/unicode/U+2764 |
||
newRange = NSMakeRange(charRange.location, oldRange.location - charRange.location); | ||
} | ||
|
||
|
@@ -1936,6 +1960,15 @@ - (void)deleteBackward { | |
} | ||
|
||
if (!_selectedTextRange.isEmpty) { | ||
// Cache the last deleted emoji to use for an iOS bug where the next | ||
// insertion corrupts the emoji characters. | ||
// See: https://github.com/flutter/flutter/issues/111494#issuecomment-1248441346 | ||
if (IsEmoji(self.text, _selectedTextRange.range)) { | ||
NSString* deletedText = [self.text substringWithRange:_selectedTextRange.range]; | ||
NSRange deleteFirstCharacterRange = fml::RangeForCharacterAtIndex(deletedText, 0); | ||
self.temporarilyDeletedComposedCharacter = | ||
[deletedText substringWithRange:deleteFirstCharacterRange]; | ||
} | ||
[self replaceRange:_selectedTextRange withText:@""]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: also clear the stored string here since it's consumed?