Update CompositionAwareMixin to correctly compute composingBase in Web engine (flutter/engine#44139)

It fixes an issue in `CompositionAwareMixin` causing wrong deltas being computed and reported to input clients resulting in wrong state when compositing text in rich text editors like [Fleather](https://github.com/fleather-editor/fleather) and possibly others.

Reported deltas on Mac:
```console
TextEditingDeltaInsertion: start: 0, end: 0, data: k
TextEditingDeltaInsertion: start: 1, end: 0, data: y
TextEditingDeltaReplacement: start: 0, end: 2, data: きょ
TextEditingDeltaInsertion: start: 2, end: 0, data: う
TextEditingDeltaInsertion: start: 3, end: 0, data: h
TextEditingDeltaReplacement: start: 0, end: 4, data: きょうは
TextEditingDeltaReplacement: start: 0, end: 4, data: 今日は
```

Reported deltas on Web (Before):
```console
TextEditingDeltaInsertion: start: 0, end: 0, data: k
TextEditingDeltaInsertion: start: 1, end: 0, data: y
TextEditingDeltaReplacement: start: 0, end: 2, data: きょ
TextEditingDeltaInsertion: start: 2, end: 0, data: う
TextEditingDeltaInsertion: start: 3, end: 0, data: h
TextEditingDeltaReplacement: start: 0, end: 4, data: きょうは
TextEditingDeltaInsertion: start: 4, end: 0, data: 今日は
```

Reported deltas on Web (After):
```console
TextEditingDeltaInsertion: start: 0, end: 0, data: k
TextEditingDeltaInsertion: start: 1, end: 0, data: y
TextEditingDeltaReplacement: start: 0, end: 2, data: きょ
TextEditingDeltaInsertion: start: 2, end: 0, data: う
TextEditingDeltaInsertion: start: 3, end: 0, data: h
TextEditingDeltaReplacement: start: 0, end: 4, data: きょうは
TextEditingDeltaReplacement: start: 0, end: 4, data: 今日は
```

* Fixes https://github.com/flutter/flutter/issues/131335
* Fixes https://github.com/fleather-editor/fleather/issues/150

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Amir Panahandeh
2023-08-14 23:21:19 +03:30
committed by GitHub
parent 1df606ab9d
commit 4c8bde6166
3 changed files with 29 additions and 4 deletions

View File

@@ -23,4 +23,5 @@ Koutaro Mori <koutaro.mo@gmail.com>
TheOneWithTheBraid <the-one@with-the-braid.cf>
Twin Sun, LLC <google-contrib@twinsunsolutions.com>
Qixing Cao <qixing.cao.83@gmail.com>
LinXunFeng <linxunfeng@yeah.net>
LinXunFeng <linxunfeng@yeah.net>
Amir Panahandeh <amirpanahandeh@gmail.com>

View File

@@ -71,11 +71,11 @@ mixin CompositionAwareMixin {
}
EditingState determineCompositionState(EditingState editingState) {
if (editingState.baseOffset == null || composingText == null || editingState.text == null) {
if (editingState.extentOffset == null || composingText == null || editingState.text == null) {
return editingState;
}
final int composingBase = editingState.baseOffset! - composingText!.length;
final int composingBase = editingState.extentOffset! - composingText!.length;
if (composingBase < 0) {
return editingState;

View File

@@ -106,7 +106,7 @@ Future<void> testMain() async {
});
group('determine composition state', () {
test('should return new composition state if valid new composition', () {
test('should return new composition state - compositing middle of text', () {
const int baseOffset = 100;
const String composingText = 'composeMe';
@@ -128,6 +128,30 @@ Future<void> testMain() async {
composingBaseOffset: expectedComposingBase,
composingExtentOffset: expectedComposingBase + composingText.length));
});
test('should return new composition state - compositing from beginning of text', () {
const String composingText = '今日は';
final EditingState editingState = EditingState(
text: '今日は',
baseOffset: 0,
extentOffset: 3,
);
final _MockWithCompositionAwareMixin mockWithCompositionAwareMixin =
_MockWithCompositionAwareMixin();
mockWithCompositionAwareMixin.composingText = composingText;
const int expectedComposingBase = 0;
expect(
mockWithCompositionAwareMixin
.determineCompositionState(editingState),
editingState.copyWith(
composingBaseOffset: expectedComposingBase,
composingExtentOffset:
expectedComposingBase + composingText.length));
});
});
});