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 TextInput { 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()), diff --git a/tests/unit/script/textinput.rs b/tests/unit/script/textinput.rs index ad938475690..9df3ed2e6ce 100644 --- a/tests/unit/script/textinput.rs +++ b/tests/unit/script/textinput.rs @@ -856,3 +856,11 @@ fn test_select_all() { textinput.selection_end() ); } + +#[test] +fn test_backspace_in_textarea_at_beginning_of_line() { + let mut textinput = text_input(Lines::Multiple, "first line\n"); + textinput.handle_keydown_aux(Key::ArrowDown, Modifiers::empty(), false); + textinput.handle_keydown_aux(Key::Backspace, Modifiers::empty(), false); + assert_eq!(textinput.get_content(), DOMString::from("first line")); +}