From 959f5dca83b14dde6a51e076308484964d62203b Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 15 Jun 2020 09:45:04 -0700 Subject: [PATCH] Revert "Prevent WhitelistingTextInputFormatter to return a empty string if the current value does not satisfy the formatter (#57264)" (#59221) --- .../lib/src/services/text_formatter.dart | 20 +------- .../test/services/text_formatter_test.dart | 51 ------------------- 2 files changed, 2 insertions(+), 69 deletions(-) diff --git a/packages/flutter/lib/src/services/text_formatter.dart b/packages/flutter/lib/src/services/text_formatter.dart index 787bf5df7b..766f9f6844 100644 --- a/packages/flutter/lib/src/services/text_formatter.dart +++ b/packages/flutter/lib/src/services/text_formatter.dart @@ -109,11 +109,10 @@ class BlacklistingTextInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( - TextEditingValue oldValue, + TextEditingValue oldValue, // unused. TextEditingValue newValue, ) { return _selectionAwareTextManipulation( - oldValue, newValue, (String substring) { return substring.replaceAll(blacklistedPattern, replacementString); @@ -242,11 +241,10 @@ class WhitelistingTextInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( - TextEditingValue oldValue, + TextEditingValue oldValue, // unused. TextEditingValue newValue, ) { return _selectionAwareTextManipulation( - oldValue, newValue, (String substring) { return whitelistedPattern @@ -263,7 +261,6 @@ class WhitelistingTextInputFormatter extends TextInputFormatter { } TextEditingValue _selectionAwareTextManipulation( - TextEditingValue oldValue, TextEditingValue value, String substringManipulation(String substring), ) { @@ -273,19 +270,6 @@ TextEditingValue _selectionAwareTextManipulation( TextSelection manipulatedSelection; if (selectionStartIndex < 0 || selectionEndIndex < 0) { manipulatedText = substringManipulation(value.text); - } else if (value.selection.isCollapsed) { // Non-selection text manipulation - int cursorPosition = value.selection.baseOffset; - manipulatedText = substringManipulation(value.text); - // We only return the old valid value if the current value is not empty and - // if manipulation fails. - if (value.text.isNotEmpty && manipulatedText.isEmpty) { - manipulatedText = oldValue.text; - // We decrease cursorPosition by one because - // user entered a invalid character. - cursorPosition -= 1; - } - // Move the caret to its previous position. - manipulatedSelection = TextSelection.collapsed(offset: cursorPosition); } else { final String beforeSelection = substringManipulation( value.text.substring(0, selectionStartIndex) diff --git a/packages/flutter/test/services/text_formatter_test.dart b/packages/flutter/test/services/text_formatter_test.dart index 8e0fe8fdc8..e90966fb7c 100644 --- a/packages/flutter/test/services/text_formatter_test.dart +++ b/packages/flutter/test/services/text_formatter_test.dart @@ -86,55 +86,4 @@ void main() { }); }); }); - - group('WhitelistingTextInputFormatter', () { - test('should return the old value if new value contains non-white-listed character', () { - const TextEditingValue oldValue = TextEditingValue(text: '12345'); - const TextEditingValue newValue = TextEditingValue(text: '12345@'); - - final WhitelistingTextInputFormatter formatter = WhitelistingTextInputFormatter.digitsOnly; - final TextEditingValue formatted = formatter.formatEditUpdate(oldValue, newValue); - - // assert that we are passing digits only at the first time - expect(oldValue.text, equals('12345')); - // The new value is always the oldValue plus a non-digit character (user press @) - expect(newValue.text, equals('12345@')); - // we expect that the formatted value returns the oldValue only since the newValue does not - // satisfy the formatter condition (which is, in this case digitsOnly) - expect(formatted.text, equals('12345')); - }); - - test('should move the cursor to the right position', () { - TextEditingValue collapsedValue(String text, int offset) => - TextEditingValue( - text: text, - selection: TextSelection.collapsed(offset: offset), - ); - - TextEditingValue oldValue = collapsedValue('123', 0); - TextEditingValue newValue = collapsedValue('123456', 6); - - final WhitelistingTextInputFormatter formatter = - WhitelistingTextInputFormatter.digitsOnly; - TextEditingValue formatted = formatter.formatEditUpdate(oldValue, - newValue); - - // assert that we are passing digits only at the first time - expect(oldValue.text, equals('123')); - // assert that we are passing digits only at the second time - expect(newValue.text, equals('123456')); - // assert that cursor is at the end of the text - expect(formatted.selection.baseOffset, equals(6)); - - // move cursor at the middle of the text and then add the number 9. - oldValue = newValue.copyWith( - selection: const TextSelection.collapsed(offset: 4)); - newValue = oldValue.copyWith(text: '1239456'); - - formatted = formatter.formatEditUpdate(oldValue, newValue); - - // cursor must be now at fourth position (right after the number 9) - expect(formatted.selection.baseOffset, equals(4)); - }); - }); }