Skip to content

Commit a4896ba

Browse files
authored
[web] Drop stale composing range after text is replaced mid-composition (#194096)
Fixes #189056 Fixes #193847 Fixes #183078 Fixes #138136 ## Problem On web, pressing a dead key in a `TextField` whose input formatter rejects the composing character throws "Range end N is out of text of length M" and leaves editing in a broken state. When the framework writes the formatted text back to the DOM mid-composition, Chrome drops the composition without firing `compositionend`. The engine keeps its cached composing base and text, so the next `selectionchange` reports a composing range past the end of the text. ## Fix `DefaultTextEditingStrategy.setEditingState` now forgets the cached composition when the framework writes different text, which is exactly when Chrome ends the composition silently. Writing unchanged text keeps the composition, so normal IME input is unaffected. `disable()` also clears it, since a composition can no longer be observed ending once the listeners are removed; this stops a stale range carrying over to the next text field, which also happens on master. ## Demo Before: https://flutter-demo-76-before.web.app/ After: https://flutter-demo-76-after.web.app/ 1. Open the page in Chrome with the DevTools console open. 2. Click the text field and type `AAA`. 3. Press the circumflex dead key: Option+I on a Mac U.S. layout, or `^` on French AZERTY. 4. Press Shift+A. Before: the console shows "Range end 4 is out of text of length 3" and editing state breaks. After: the formatter rejects `ˆ` and `Â`, no assertion is thrown, and typing continues normally.
1 parent bd24e53 commit a4896ba

3 files changed

Lines changed: 151 additions & 5 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/composition_aware_mixin.dart‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,24 @@ mixin CompositionAwareMixin {
6262
domElement.removeEventListener(_kCompositionStart, _compositionStartListener);
6363
domElement.removeEventListener(_kCompositionUpdate, _compositionUpdateListener);
6464
domElement.removeEventListener(_kCompositionEnd, _compositionEndListener);
65+
// Composition can't be observed ending anymore.
66+
resetComposition();
6567
}
6668

67-
void _handleCompositionStart(DomEvent event) {
69+
void resetComposition() {
6870
composingText = null;
6971
composingBase = null;
7072
}
7173

74+
void _handleCompositionStart(DomEvent event) => resetComposition();
75+
7276
void _handleCompositionUpdate(DomEvent event) {
7377
if (event.isA<DomCompositionEvent>()) {
7478
composingText = (event as DomCompositionEvent).data;
7579
}
7680
}
7781

78-
void _handleCompositionEnd(DomEvent event) {
79-
composingText = null;
80-
composingBase = null;
81-
}
82+
void _handleCompositionEnd(DomEvent event) => resetComposition();
8283

8384
EditingState determineCompositionState(EditingState editingState) {
8485
if (composingText == null) {

‎engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,10 +1773,18 @@ abstract class DefaultTextEditingStrategy
17731773

17741774
@override
17751775
void setEditingState(EditingState? editingState) {
1776+
final String? previousText = lastEditingState?.text;
17761777
lastEditingState = editingState;
17771778
if (!isEnabled || !editingState!.isValid) {
17781779
return;
17791780
}
1781+
// Writing different text into the element ends any composition in
1782+
// progress, and Chrome does so without a `compositionend` event. This
1783+
// happens when an input formatter rejects a dead key character. Forget the
1784+
// composition so its stale range is not reported back to the framework.
1785+
if (editingState.text != previousText) {
1786+
resetComposition();
1787+
}
17801788
lastEditingState!.applyToDomElement(domElement);
17811789
}
17821790

‎engine/src/flutter/lib/web_ui/test/engine/composition_test.dart‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,143 @@ Future<void> testMain() async {
294294
),
295295
);
296296
});
297+
298+
// Regression test for https://github.com/flutter/flutter/issues/189056.
299+
test('should be cleared when the framework removes the composing text', () {
300+
_inputElement.value = 'AAA';
301+
_inputElement.setSelectionRange(3, 3);
302+
303+
// Dead key `^` starts a composition.
304+
_inputElement.dispatchEvent(
305+
createDomCompositionEvent(
306+
_MockWithCompositionAwareMixin._kCompositionUpdate,
307+
<Object?, Object?>{'data': '^'},
308+
),
309+
);
310+
_inputElement.value = 'AAA^';
311+
_inputElement.setSelectionRange(4, 4);
312+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
313+
314+
expect(editingStrategy.lastEditingState?.composingBaseOffset, 3);
315+
expect(editingStrategy.lastEditingState?.composingExtentOffset, 4);
316+
317+
// An input formatter rejects `^`. Chrome drops the composition without
318+
// firing compositionend.
319+
editingStrategy.setEditingState(EditingState(text: 'AAA', baseOffset: 3, extentOffset: 3));
320+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
321+
322+
expect(
323+
editingStrategy.lastEditingState,
324+
EditingState(text: 'AAA', baseOffset: 3, extentOffset: 3),
325+
);
326+
327+
// Typing afterwards must not mark the new character as composing.
328+
_inputElement.value = 'AAAB';
329+
_inputElement.setSelectionRange(4, 4);
330+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
331+
332+
expect(
333+
editingStrategy.lastEditingState,
334+
EditingState(text: 'AAAB', baseOffset: 4, extentOffset: 4),
335+
);
336+
});
337+
338+
test('should be cleared when the framework removes composing text mid-text', () {
339+
_inputElement.value = 'AB';
340+
_inputElement.setSelectionRange(1, 1);
341+
342+
_inputElement.dispatchEvent(
343+
createDomCompositionEvent(
344+
_MockWithCompositionAwareMixin._kCompositionUpdate,
345+
<Object?, Object?>{'data': '^'},
346+
),
347+
);
348+
_inputElement.value = 'A^B';
349+
_inputElement.setSelectionRange(2, 2);
350+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
351+
352+
expect(editingStrategy.lastEditingState?.composingBaseOffset, 1);
353+
expect(editingStrategy.lastEditingState?.composingExtentOffset, 2);
354+
355+
// The stale range 1..2 still fits the new text, so it must not be
356+
// reported as `B` composing.
357+
editingStrategy.setEditingState(EditingState(text: 'AB', baseOffset: 1, extentOffset: 1));
358+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
359+
360+
expect(
361+
editingStrategy.lastEditingState,
362+
EditingState(text: 'AB', baseOffset: 1, extentOffset: 1),
363+
);
364+
});
365+
366+
test('should be kept when the framework sets the same text', () {
367+
_inputElement.value = 'AAA';
368+
_inputElement.setSelectionRange(3, 3);
369+
370+
_inputElement.dispatchEvent(
371+
createDomCompositionEvent(
372+
_MockWithCompositionAwareMixin._kCompositionUpdate,
373+
<Object?, Object?>{'data': '^'},
374+
),
375+
);
376+
_inputElement.value = 'AAA^';
377+
_inputElement.setSelectionRange(4, 4);
378+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
379+
380+
// Writing unchanged text does not end the composition in the browser.
381+
editingStrategy.setEditingState(
382+
EditingState(
383+
text: 'AAA^',
384+
baseOffset: 4,
385+
extentOffset: 4,
386+
composingBaseOffset: 3,
387+
composingExtentOffset: 4,
388+
),
389+
);
390+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
391+
392+
expect(editingStrategy.lastEditingState?.composingBaseOffset, 3);
393+
expect(editingStrategy.lastEditingState?.composingExtentOffset, 4);
394+
});
395+
396+
test('should not carry over to the next text field', () {
397+
_inputElement.value = 'A';
398+
_inputElement.setSelectionRange(1, 1);
399+
400+
// The first field is mid-composition when focus moves away.
401+
_inputElement.dispatchEvent(
402+
createDomCompositionEvent(
403+
_MockWithCompositionAwareMixin._kCompositionUpdate,
404+
<Object?, Object?>{'data': '^'},
405+
),
406+
);
407+
_inputElement.value = 'A^';
408+
_inputElement.setSelectionRange(2, 2);
409+
_inputElement.dispatchEvent(createDomEvent('Event', 'input'));
410+
411+
expect(editingStrategy.lastEditingState?.composingBaseOffset, 1);
412+
expect(editingStrategy.lastEditingState?.composingExtentOffset, 2);
413+
414+
// The framework connects the second field: it sends its state while
415+
// the strategy is disabled, then shows the keyboard.
416+
editingStrategy.disable();
417+
editingStrategy.setEditingState(EditingState(text: 'B', baseOffset: 1, extentOffset: 1));
418+
editingStrategy.enable(
419+
InputConfiguration(viewId: kImplicitViewId),
420+
onChange: (_, _) {},
421+
onAction: (_) {},
422+
);
423+
424+
final input = editingStrategy.activeDomElement as DomHTMLInputElement;
425+
input.value = 'BC';
426+
input.setSelectionRange(2, 2);
427+
input.dispatchEvent(createDomEvent('Event', 'input'));
428+
429+
expect(
430+
editingStrategy.lastEditingState,
431+
EditingState(text: 'BC', baseOffset: 2, extentOffset: 2),
432+
);
433+
});
297434
});
298435

299436
group('Text Editing Delta Model', () {

0 commit comments

Comments
 (0)