Skip to content

Commit 959f5dc

Browse files
Revert "Prevent WhitelistingTextInputFormatter to return a empty string if the current value does not satisfy the formatter (flutter#57264)" (flutter#59221)
1 parent 8f59fdd commit 959f5dc

File tree

2 files changed

+2
-69
lines changed

2 files changed

+2
-69
lines changed

packages/flutter/lib/src/services/text_formatter.dart

+2-18
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ class BlacklistingTextInputFormatter extends TextInputFormatter {
109109

110110
@override
111111
TextEditingValue formatEditUpdate(
112-
TextEditingValue oldValue,
112+
TextEditingValue oldValue, // unused.
113113
TextEditingValue newValue,
114114
) {
115115
return _selectionAwareTextManipulation(
116-
oldValue,
117116
newValue,
118117
(String substring) {
119118
return substring.replaceAll(blacklistedPattern, replacementString);
@@ -242,11 +241,10 @@ class WhitelistingTextInputFormatter extends TextInputFormatter {
242241

243242
@override
244243
TextEditingValue formatEditUpdate(
245-
TextEditingValue oldValue,
244+
TextEditingValue oldValue, // unused.
246245
TextEditingValue newValue,
247246
) {
248247
return _selectionAwareTextManipulation(
249-
oldValue,
250248
newValue,
251249
(String substring) {
252250
return whitelistedPattern
@@ -263,7 +261,6 @@ class WhitelistingTextInputFormatter extends TextInputFormatter {
263261
}
264262

265263
TextEditingValue _selectionAwareTextManipulation(
266-
TextEditingValue oldValue,
267264
TextEditingValue value,
268265
String substringManipulation(String substring),
269266
) {
@@ -273,19 +270,6 @@ TextEditingValue _selectionAwareTextManipulation(
273270
TextSelection manipulatedSelection;
274271
if (selectionStartIndex < 0 || selectionEndIndex < 0) {
275272
manipulatedText = substringManipulation(value.text);
276-
} else if (value.selection.isCollapsed) { // Non-selection text manipulation
277-
int cursorPosition = value.selection.baseOffset;
278-
manipulatedText = substringManipulation(value.text);
279-
// We only return the old valid value if the current value is not empty and
280-
// if manipulation fails.
281-
if (value.text.isNotEmpty && manipulatedText.isEmpty) {
282-
manipulatedText = oldValue.text;
283-
// We decrease cursorPosition by one because
284-
// user entered a invalid character.
285-
cursorPosition -= 1;
286-
}
287-
// Move the caret to its previous position.
288-
manipulatedSelection = TextSelection.collapsed(offset: cursorPosition);
289273
} else {
290274
final String beforeSelection = substringManipulation(
291275
value.text.substring(0, selectionStartIndex)

packages/flutter/test/services/text_formatter_test.dart

-51
Original file line numberDiff line numberDiff line change
@@ -86,55 +86,4 @@ void main() {
8686
});
8787
});
8888
});
89-
90-
group('WhitelistingTextInputFormatter', () {
91-
test('should return the old value if new value contains non-white-listed character', () {
92-
const TextEditingValue oldValue = TextEditingValue(text: '12345');
93-
const TextEditingValue newValue = TextEditingValue(text: '12345@');
94-
95-
final WhitelistingTextInputFormatter formatter = WhitelistingTextInputFormatter.digitsOnly;
96-
final TextEditingValue formatted = formatter.formatEditUpdate(oldValue, newValue);
97-
98-
// assert that we are passing digits only at the first time
99-
expect(oldValue.text, equals('12345'));
100-
// The new value is always the oldValue plus a non-digit character (user press @)
101-
expect(newValue.text, equals('12345@'));
102-
// we expect that the formatted value returns the oldValue only since the newValue does not
103-
// satisfy the formatter condition (which is, in this case digitsOnly)
104-
expect(formatted.text, equals('12345'));
105-
});
106-
107-
test('should move the cursor to the right position', () {
108-
TextEditingValue collapsedValue(String text, int offset) =>
109-
TextEditingValue(
110-
text: text,
111-
selection: TextSelection.collapsed(offset: offset),
112-
);
113-
114-
TextEditingValue oldValue = collapsedValue('123', 0);
115-
TextEditingValue newValue = collapsedValue('123456', 6);
116-
117-
final WhitelistingTextInputFormatter formatter =
118-
WhitelistingTextInputFormatter.digitsOnly;
119-
TextEditingValue formatted = formatter.formatEditUpdate(oldValue,
120-
newValue);
121-
122-
// assert that we are passing digits only at the first time
123-
expect(oldValue.text, equals('123'));
124-
// assert that we are passing digits only at the second time
125-
expect(newValue.text, equals('123456'));
126-
// assert that cursor is at the end of the text
127-
expect(formatted.selection.baseOffset, equals(6));
128-
129-
// move cursor at the middle of the text and then add the number 9.
130-
oldValue = newValue.copyWith(
131-
selection: const TextSelection.collapsed(offset: 4));
132-
newValue = oldValue.copyWith(text: '1239456');
133-
134-
formatted = formatter.formatEditUpdate(oldValue, newValue);
135-
136-
// cursor must be now at fourth position (right after the number 9)
137-
expect(formatted.selection.baseOffset, equals(4));
138-
});
139-
});
14089
}

0 commit comments

Comments
 (0)