mirror of
https://github.com/servo/servo.git
synced 2025-09-29 16:19:14 +01:00
Fix selection{Start,End} when selectionDirection is "backward"
Per the spec, selectionStart and selectionEnd should return the same values regardless of the selectionDirection. (That is, selectionStart is always less than or equal to selectionEnd; the direction then implies which of selectionStart or selectionEnd is the cursor position.) There was no explicit WPT test for this, so I added one. This bug was initially quite hard to wrap my head around, and I think part of the problem is the code in TextInput. Therefore, in the process of fixing it I have refactored the implementation of TextInput: * Rename selection_begin to selection_origin. This value doesn't necessarily correspond directly to the selectionStart DOM value - in the case of a backward selection, it corresponds to selectionEnd. I feel that "origin" doesn't imply a specific ordering as strongly as "begin" (or "start" for that matter) does. * In various other cases where "begin" is used as a synonym for "start", just use "start" for consistency. * Implement selection_start() and selection_end() methods (and their _offset() variants) which directly correspond to their DOM equivalents. * Rename other related methods to make them less wordy and more consistent / intention-revealing. * Add assertions to assert_ok_selection() to ensure that our assumptions about the ordering of selection_origin and edit_point are met. This then revealed a bug in adjust_selection_for_horizontal_change() where the value of selection_direction was not maintained correctly (causing a unit test failure when the new assertion failed).
This commit is contained in:
parent
0148e9705b
commit
02883a6f54
8 changed files with 221 additions and 179 deletions
|
@ -222,7 +222,7 @@ fn test_textinput_delete_char() {
|
|||
let mut textinput = text_input(Lines::Single, "abcdefg");
|
||||
textinput.adjust_horizontal(2, Selection::NotSelected);
|
||||
// Set an empty selection range.
|
||||
textinput.selection_begin = Some(textinput.edit_point);
|
||||
textinput.selection_origin = Some(textinput.edit_point);
|
||||
textinput.delete_char(Direction::Backward);
|
||||
assert_eq!(textinput.get_content(), "acdefg");
|
||||
}
|
||||
|
@ -252,15 +252,15 @@ fn test_textinput_get_sorted_selection() {
|
|||
let mut textinput = text_input(Lines::Single, "abcdefg");
|
||||
textinput.adjust_horizontal(2, Selection::NotSelected);
|
||||
textinput.adjust_horizontal(2, Selection::Selected);
|
||||
let (begin, end) = textinput.get_sorted_selection().unwrap();
|
||||
assert_eq!(begin.index, 2);
|
||||
let (start, end) = textinput.sorted_selection_bounds();
|
||||
assert_eq!(start.index, 2);
|
||||
assert_eq!(end.index, 4);
|
||||
|
||||
textinput.clear_selection();
|
||||
|
||||
textinput.adjust_horizontal(-2, Selection::Selected);
|
||||
let (begin, end) = textinput.get_sorted_selection().unwrap();
|
||||
assert_eq!(begin.index, 2);
|
||||
let (start, end) = textinput.sorted_selection_bounds();
|
||||
assert_eq!(start.index, 2);
|
||||
assert_eq!(end.index, 4);
|
||||
}
|
||||
|
||||
|
@ -588,18 +588,18 @@ fn test_textinput_set_selection_with_direction() {
|
|||
assert_eq!(textinput.edit_point.index, 6);
|
||||
assert_eq!(textinput.selection_direction, SelectionDirection::Forward);
|
||||
|
||||
assert!(textinput.selection_begin.is_some());
|
||||
assert_eq!(textinput.selection_begin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_begin.unwrap().index, 2);
|
||||
assert!(textinput.selection_origin.is_some());
|
||||
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_origin.unwrap().index, 2);
|
||||
|
||||
textinput.set_selection_range(2, 6, SelectionDirection::Backward);
|
||||
assert_eq!(textinput.edit_point.line, 0);
|
||||
assert_eq!(textinput.edit_point.index, 2);
|
||||
assert_eq!(textinput.selection_direction, SelectionDirection::Backward);
|
||||
|
||||
assert!(textinput.selection_begin.is_some());
|
||||
assert_eq!(textinput.selection_begin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_begin.unwrap().index, 6);
|
||||
assert!(textinput.selection_origin.is_some());
|
||||
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_origin.unwrap().index, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -611,3 +611,22 @@ fn test_textinput_unicode_handling() {
|
|||
textinput.set_edit_point_index(4);
|
||||
assert_eq!(textinput.edit_point.index, 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_selection_bounds() {
|
||||
let mut textinput = text_input(Lines::Single, "abcdef");
|
||||
|
||||
textinput.set_selection_range(2, 5, SelectionDirection::Forward);
|
||||
assert_eq!(TextPoint { line: 0, index: 2 }, textinput.selection_origin_or_edit_point());
|
||||
assert_eq!(TextPoint { line: 0, index: 2 }, textinput.selection_start());
|
||||
assert_eq!(TextPoint { line: 0, index: 5 }, textinput.selection_end());
|
||||
assert_eq!(2, textinput.selection_start_offset());
|
||||
assert_eq!(5, textinput.selection_end_offset());
|
||||
|
||||
textinput.set_selection_range(3, 6, SelectionDirection::Backward);
|
||||
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_origin_or_edit_point());
|
||||
assert_eq!(TextPoint { line: 0, index: 3 }, textinput.selection_start());
|
||||
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
|
||||
assert_eq!(3, textinput.selection_start_offset());
|
||||
assert_eq!(6, textinput.selection_end_offset());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue