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 {
final StringValueChanged onChanged;
final StringValueSubmitted onSubmitted;
_InputState createState() => new _InputState();
InputState createState() => new InputState();
}
class _InputState extends ScrollableState<Input> {
class InputState extends ScrollableState<Input> {
String _value;
EditableString _editableValue;
KeyboardHandle _keyboardHandle = KeyboardHandle.unattached;
......@@ -48,6 +48,8 @@ class _InputState extends ScrollableState<Input> {
double _contentWidth = 0.0;
double _containerWidth = 0.0;
EditableString get editableValue => _editableValue;
void initState() {
super.initState();
_value = config.initialValue;
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:math' as math;
import 'package:mojo_services/keyboard/keyboard.mojom.dart';
import 'package:flutter/painting.dart';
......@@ -112,8 +113,9 @@ class EditableString implements KeyboardClient {
_delete(afterRange);
_delete(beforeRange);
selection = new TextRange(
start: selection.start - beforeLength,
end: selection.end - beforeLength);
start: math.max(selection.start - beforeLength, 0),
end: math.max(selection.end - beforeLength, 0)
);
onUpdated();
}
......
......@@ -101,4 +101,38 @@ void main() {
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