Commit a09ac6cc authored by Jason Simmons's avatar Jason Simmons

Merge pull request #1762 from jason-simmons/selection_delete_surround

Keep the selection within bounds in deleteSurroundingText
parents a6c473ea 76f83d1d
...@@ -37,10 +37,10 @@ class Input extends Scrollable { ...@@ -37,10 +37,10 @@ class Input extends Scrollable {
final StringValueChanged onChanged; final StringValueChanged onChanged;
final StringValueSubmitted onSubmitted; final StringValueSubmitted onSubmitted;
_InputState createState() => new _InputState(); InputState createState() => new InputState();
} }
class _InputState extends ScrollableState<Input> { class InputState extends ScrollableState<Input> {
String _value; String _value;
EditableString _editableValue; EditableString _editableValue;
KeyboardHandle _keyboardHandle = KeyboardHandle.unattached; KeyboardHandle _keyboardHandle = KeyboardHandle.unattached;
...@@ -48,6 +48,8 @@ class _InputState extends ScrollableState<Input> { ...@@ -48,6 +48,8 @@ class _InputState extends ScrollableState<Input> {
double _contentWidth = 0.0; double _contentWidth = 0.0;
double _containerWidth = 0.0; double _containerWidth = 0.0;
EditableString get editableValue => _editableValue;
void initState() { void initState() {
super.initState(); super.initState();
_value = config.initialValue; _value = config.initialValue;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:math' as math;
import 'package:mojo_services/keyboard/keyboard.mojom.dart'; import 'package:mojo_services/keyboard/keyboard.mojom.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
...@@ -112,8 +113,9 @@ class EditableString implements KeyboardClient { ...@@ -112,8 +113,9 @@ class EditableString implements KeyboardClient {
_delete(afterRange); _delete(afterRange);
_delete(beforeRange); _delete(beforeRange);
selection = new TextRange( selection = new TextRange(
start: selection.start - beforeLength, start: math.max(selection.start - beforeLength, 0),
end: selection.end - beforeLength); end: math.max(selection.end - beforeLength, 0)
);
onUpdated(); onUpdated();
} }
......
...@@ -101,4 +101,38 @@ void main() { ...@@ -101,4 +101,38 @@ void main() {
checkCursorToggle(); checkCursorToggle();
}); });
}); });
test('Selection remains valid', () {
testWidgets((WidgetTester tester) {
GlobalKey inputKey = new GlobalKey();
String inputValue;
Widget builder() {
return new Center(
child: new Input(
key: inputKey,
placeholder: 'Placeholder'
)
);
}
tester.pumpWidget(builder());
const String testValue = 'ABC';
mockKeyboard.client.commitText(testValue, testValue.length);
InputState input = tester.findStateOfType(InputState);
// Delete characters and verify that the selection follows the length
// of the text.
for (int i = 0; i < testValue.length; i++) {
mockKeyboard.client.deleteSurroundingText(1, 0);
expect(input.editableValue.selection.start, equals(testValue.length - i - 1));
}
// Delete a characters when the text is empty. The selection should
// remain at zero.
mockKeyboard.client.deleteSurroundingText(1, 0);
expect(input.editableValue.selection.start, equals(0));
});
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment