Allow deletion from overflowed textinput

This commit is contained in:
teapotd 2020-01-10 19:02:41 +01:00
parent c6192dc286
commit c7a31990a2
2 changed files with 22 additions and 6 deletions

View file

@ -450,12 +450,6 @@ impl<T: ClipboardProvider> TextInput<T> {
let allowed_to_insert_count = if let Some(max_length) = self.max_length {
let len_after_selection_replaced =
self.utf16_len().saturating_sub(self.selection_utf16_len());
if len_after_selection_replaced >= max_length {
// If, after deleting the selection, the len is still greater than the max
// length, then don't delete/insert anything
return;
}
max_length.saturating_sub(len_after_selection_replaced)
} else {
UTF16CodeUnits(usize::MAX)

View file

@ -161,6 +161,28 @@ fn test_single_line_textinput_with_max_length_doesnt_allow_appending_characters_
assert_eq!(textinput.get_content(), "atooe");
}
#[test]
fn test_single_line_textinput_with_max_length_allows_deletion_when_replacing_a_selection() {
let mut textinput = TextInput::new(
Lines::Single,
DOMString::from("abcde"),
DummyClipboardContext::new(""),
Some(UTF16CodeUnits(1)),
None,
SelectionDirection::None,
);
textinput.adjust_horizontal(UTF8Bytes::one(), Direction::Forward, Selection::NotSelected);
textinput.adjust_horizontal(UTF8Bytes(2), Direction::Forward, Selection::Selected);
// Selection is now "abcde"
// --
textinput.replace_selection(DOMString::from("only deletion should be applied"));
assert_eq!(textinput.get_content(), "ade");
}
#[test]
fn test_single_line_textinput_with_max_length_multibyte() {
let mut textinput = TextInput::new(