Fix an issue that deleting an emoji may crash the app (flutter/engine#34508)
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include "unicode/uchar.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/platform/darwin/string_range_sanitization.h"
|
||||
|
||||
@@ -1896,6 +1898,22 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
|
||||
NSRange oldRange = ((FlutterTextRange*)oldSelectedRange).range;
|
||||
if (oldRange.location > 0) {
|
||||
NSRange newRange = NSMakeRange(oldRange.location - 1, 1);
|
||||
|
||||
// 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)) {
|
||||
newRange = NSMakeRange(charRange.location, oldRange.location - charRange.location);
|
||||
}
|
||||
|
||||
_selectedTextRange = [[FlutterTextRange rangeWithNSRange:newRange] copy];
|
||||
[oldSelectedRange release];
|
||||
}
|
||||
|
||||
@@ -406,6 +406,41 @@ FLUTTER_ASSERT_ARC
|
||||
XCTAssertEqualObjects(substring, @"bbbbaaaabbbbaaaa");
|
||||
}
|
||||
|
||||
- (void)testDeletingBackward {
|
||||
NSDictionary* config = self.mutableTemplateCopy;
|
||||
[self setClientId:123 configuration:config];
|
||||
NSArray<FlutterTextInputView*>* inputFields = self.installedInputViews;
|
||||
FlutterTextInputView* inputView = inputFields[0];
|
||||
|
||||
[inputView insertText:@"ឹ😀 text 🥰👨👩👧👦🇺🇳ดี "];
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
|
||||
// Thai vowel is removed.
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀 text 🥰👨👩👧👦🇺🇳ด");
|
||||
[inputView deleteBackward];
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀 text 🥰👨👩👧👦🇺🇳");
|
||||
[inputView deleteBackward];
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀 text 🥰👨👩👧👦");
|
||||
[inputView deleteBackward];
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀 text 🥰");
|
||||
[inputView deleteBackward];
|
||||
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀 text ");
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
[inputView deleteBackward];
|
||||
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ😀");
|
||||
[inputView deleteBackward];
|
||||
XCTAssertEqualObjects(inputView.text, @"ឹ");
|
||||
[inputView deleteBackward];
|
||||
XCTAssertEqualObjects(inputView.text, @"");
|
||||
}
|
||||
|
||||
- (void)testPastingNonTextDisallowed {
|
||||
NSDictionary* config = self.mutableTemplateCopy;
|
||||
[self setClientId:123 configuration:config];
|
||||
|
||||
@@ -78,6 +78,12 @@
|
||||
ReferencedContainer = "container:IosUnitTests.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
<CommandLineArguments>
|
||||
<CommandLineArgument
|
||||
argument = "--icu-data-file-path=Frameworks/Flutter.framework/icudtl.dat"
|
||||
isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
</CommandLineArguments>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
|
||||
Reference in New Issue
Block a user