Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 89351fe

Browse files
authored
[web] Fix text selection from right to left (#24214)
1 parent 99eba1b commit 89351fe

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ class AutofillInfo {
373373
/// The current text and selection state of a text field.
374374
@visibleForTesting
375375
class EditingState {
376-
EditingState({this.text, this.baseOffset = 0, this.extentOffset = 0});
376+
EditingState({this.text, int? baseOffset, int? extentOffset}) :
377+
// Don't allow negative numbers. Pick the smallest selection index for base.
378+
baseOffset = math.max(0, math.min(baseOffset ?? 0, extentOffset ?? 0)),
379+
// Don't allow negative numbers. Pick the greatest selection index for extent.
380+
extentOffset = math.max(0, math.max(baseOffset ?? 0, extentOffset ?? 0));
377381

378382
/// Creates an [EditingState] instance using values from an editing state Map
379383
/// coming from Flutter.
@@ -401,9 +405,10 @@ class EditingState {
401405
final String? text = flutterEditingState['text'];
402406

403407
return EditingState(
404-
text: text,
405-
baseOffset: math.max(0, selectionBase),
406-
extentOffset: math.max(0, selectionExtent));
408+
text: text,
409+
baseOffset: selectionBase,
410+
extentOffset: selectionExtent,
411+
);
407412
}
408413

409414
/// Creates an [EditingState] instance using values from the editing element

lib/web_ui/test/text_editing_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,24 @@ void testMain() {
20952095
);
20962096
});
20972097

2098+
test('Fix flipped base and extent offsets', () {
2099+
expect(
2100+
EditingState(baseOffset: 10, extentOffset: 4),
2101+
EditingState(baseOffset: 4, extentOffset: 10),
2102+
);
2103+
2104+
expect(
2105+
EditingState.fromFrameworkMessage(<String, dynamic>{
2106+
'selectionBase': 10,
2107+
'selectionExtent': 4,
2108+
}),
2109+
EditingState.fromFrameworkMessage(<String, dynamic>{
2110+
'selectionBase': 4,
2111+
'selectionExtent': 10,
2112+
}),
2113+
);
2114+
});
2115+
20982116
test('Configure input element from the editing state', () {
20992117
final InputElement input = document.getElementsByTagName('input')[0];
21002118
_editingState =

0 commit comments

Comments
 (0)