diff --git a/servo/components/script/textinput.rs b/servo/components/script/textinput.rs index 871af9637c05..5beee247f96e 100644 --- a/servo/components/script/textinput.rs +++ b/servo/components/script/textinput.rs @@ -651,8 +651,17 @@ impl TextInput { start = end; } - self.selection_begin = Some(self.get_text_point_for_absolute_point(start)); - self.edit_point = self.get_text_point_for_absolute_point(end); + match self.selection_direction { + SelectionDirection::None | + SelectionDirection::Forward => { + self.selection_begin = Some(self.get_text_point_for_absolute_point(start)); + self.edit_point = self.get_text_point_for_absolute_point(end); + }, + SelectionDirection::Backward => { + self.selection_begin = Some(self.get_text_point_for_absolute_point(end)); + self.edit_point = self.get_text_point_for_absolute_point(start); + } + } self.assert_ok_selection(); } diff --git a/servo/tests/unit/script/textinput.rs b/servo/tests/unit/script/textinput.rs index d5e9c1268d60..01d8931ff672 100644 --- a/servo/tests/unit/script/textinput.rs +++ b/servo/tests/unit/script/textinput.rs @@ -458,3 +458,26 @@ fn test_textinput_cursor_position_correct_after_clearing_selection() { assert_eq!(textinput.edit_point.index, 0); assert_eq!(textinput.edit_point.line, 0); } + + +#[test] +fn test_textinput_set_selection_with_direction() { + let mut textinput = text_input(Lines::Single, "abcdef"); + textinput.selection_direction = SelectionDirection::Forward; + textinput.set_selection_range(2, 6); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 6); + + assert!(textinput.selection_begin.is_some()); + assert_eq!(textinput.selection_begin.unwrap().line, 0); + assert_eq!(textinput.selection_begin.unwrap().index, 2); + + textinput.selection_direction = SelectionDirection::Backward; + textinput.set_selection_range(2, 6); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 2); + + assert!(textinput.selection_begin.is_some()); + assert_eq!(textinput.selection_begin.unwrap().line, 0); + assert_eq!(textinput.selection_begin.unwrap().index, 6); +}