Merge pull request #1762 from jason-simmons/selection_delete_surround

Keep the selection within bounds in deleteSurroundingText
This commit is contained in:
Jason Simmons
2015-10-26 09:58:06 -07:00
3 changed files with 42 additions and 4 deletions

View File

@@ -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;

View File

@@ -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();
}

View File

@@ -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));
});
});
}