diff options
author | elomscansio <163124154+elomscansio@users.noreply.github.com> | 2025-03-26 19:37:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-26 18:37:48 +0000 |
commit | 30a2a89d160dd40da6344a554ad285bb8d3d5e14 (patch) | |
tree | 7dd1390189dc0cd77330927650df64f8b500dbcf /components | |
parent | 4f33f31c8617271696be8a582788c94b3fe2d847 (diff) | |
download | servo-30a2a89d160dd40da6344a554ad285bb8d3d5e14.tar.gz servo-30a2a89d160dd40da6344a554ad285bb8d3d5e14.zip |
Fix Backspace deleting entire previous line in `<textarea>` (#36112)
* test(textinput): Add test for backspace at beginning of line in textarea
Introduce a test to reproduce and verify the fix for backspacing at the
beginning of a line in a multiline textarea. This ensures that pressing
Backspace when the cursor is at the start of a line correctly removes the
newline without deleting the entire previous line’s content.
Related to: #27523
Signed-off-by: Emmanuel Elom <elomemmanuel007@gmail.com>
* fix(textinput): Preserve selection origin when adjusting vertical position
Fixes an issue where pressing Backspace at the beginning of a line in a
textarea incorrectly deleted the entire previous line's content. This happened
because `self.adjust_vertical(-1, select)` modified `selection_origin` and
`edit_point`, but `selection_origin` was not restored before performing the
horizontal adjustment. As a result, `self.selection_start()` and
`self.selection_end()` were inconsistent, leading to `replace_operation`
erasing the entire line.
Now, we temporarily store `selection_origin` before adjusting vertical
position and restore it afterward to ensure proper cursor and selection
behavior.
Fixes: #27523
Signed-off-by: Emmanuel Elom <elomemmanuel007@gmail.com>
---------
Signed-off-by: Emmanuel Elom <elomemmanuel007@gmail.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components')
-rw-r--r-- | components/script/textinput.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 64f4d75d8ef..3cee1451528 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -671,8 +671,14 @@ impl<T: ClipboardProvider> TextInput<T> { Direction::Backward => { let remaining = self.edit_point.index; if adjust > remaining && self.edit_point.line > 0 { + // Preserve the current selection origin because `adjust_vertical` + // modifies `selection_origin`. Since we are moving backward instead of + // highlighting vertically, we need to restore it after adjusting the line. + let selection_origin_temp = self.selection_origin; self.adjust_vertical(-1, select); self.edit_point.index = self.current_line_length(); + // Restore the original selection origin to maintain expected behavior. + self.selection_origin = selection_origin_temp; // one shift is consumed by the change of line, hence the -1 self.adjust_horizontal( adjust.saturating_sub(remaining + UTF8Bytes::one()), |