[iOS] TextField should support typing accented words with external keyboard (#191062)
<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->
Fixes #59541 as suggested in @loic-sharma
[comment](https://github.com/flutter/flutter/issues/59541#issuecomment-4617810595)
### Before
https://github.com/user-attachments/assets/a423cfff-b963-4c05-8de9-00b159731d19
### After
https://github.com/user-attachments/assets/841b388a-2859-45cf-b20f-42d9d72a5bf0
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.
**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
---------
Co-authored-by: Chris Bracken <chris@bracken.jp>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
index d454ada..b3cad57 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
@@ -2319,12 +2319,16 @@
self.temporarilyDeletedComposedCharacter = nil;
}
+ // insertText finalizes composing text and should replace the whole marked
+ // range, not just the last reported selection within it. See
+ // https://github.com/flutter/flutter/issues/59541#issuecomment-4617810595.
+ UITextRange* replacedRange = self.markedTextRange ?: _selectedTextRange;
+
NSMutableArray<FlutterTextSelectionRect*>* copiedRects =
[[NSMutableArray alloc] initWithCapacity:[_selectionRects count]];
- NSAssert([_selectedTextRange.start isKindOfClass:[FlutterTextPosition class]],
- @"Expected a FlutterTextPosition for position (got %@).",
- [_selectedTextRange.start class]);
- NSUInteger insertPosition = ((FlutterTextPosition*)_selectedTextRange.start).index;
+ NSAssert([replacedRange.start isKindOfClass:[FlutterTextPosition class]],
+ @"Expected a FlutterTextPosition for position (got %@).", [replacedRange.start class]);
+ NSUInteger insertPosition = ((FlutterTextPosition*)replacedRange.start).index;
for (NSUInteger i = 0; i < [_selectionRects count]; i++) {
NSUInteger rectPosition = _selectionRects[i].position;
if (rectPosition == insertPosition) {
@@ -2349,7 +2353,7 @@
[self resetScribbleInteractionStatusIfEnding];
self.selectionRects = copiedRects;
_selectionAffinity = kTextAffinityDownstream;
- [self replaceRange:_selectedTextRange withText:text];
+ [self replaceRange:replacedRange withText:text];
}
- (UITextPlaceholder*)insertTextPlaceholderWithSize:(CGSize)size API_AVAILABLE(ios(13.0)) {
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm
index 07920eb..d1847d6 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm
@@ -593,6 +593,22 @@
XCTAssertEqual(substring.length, 0ul);
}
+- (void)testInsertTextReplacesEntireMarkedRangeForDeadKeySequence {
+ // Regression test for https://github.com/flutter/flutter/issues/59541#issuecomment-4617810595.
+ NSDictionary* config = self.mutableTemplateCopy;
+ [self setClientId:123 configuration:config];
+ NSArray<FlutterTextInputView*>* inputFields = self.installedInputViews;
+ FlutterTextInputView* inputView = inputFields[0];
+
+ [inputView setMarkedText:@"´" selectedRange:NSMakeRange(1, 0)];
+ [inputView insertText:@"Á"];
+
+ UITextRange* range = [inputView textRangeFromPosition:inputView.beginningOfDocument
+ toPosition:inputView.endOfDocument];
+ NSString* substring = [inputView textInRange:range];
+ XCTAssertEqualObjects(substring, @"Á");
+}
+
- (void)testTextInRangeAcceptsNSNotFoundLocationGracefully {
NSDictionary* config = self.mutableTemplateCopy;
[self setClientId:123 configuration:config];