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:
Jon Leighton 2017-12-09 20:17:49 +01:00
parent 0148e9705b
commit 02883a6f54
8 changed files with 221 additions and 179 deletions

View file

@ -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());
}

View file

@ -553039,7 +553039,7 @@
"testharness"
],
"html/semantics/forms/textfieldselection/selection-start-end.html": [
"3fd1c942f7ac3ed3097bbd1ec89db15fb0805476",
"0fd9c420f831943f0d992076a7828eac066b6596",
"testharness"
],
"html/semantics/forms/textfieldselection/selection-value-interactions.html": [

View file

@ -1,56 +1,20 @@
[selection-after-content-change.html]
type: testharness
[input out of document: selection must not change when setting the same value]
expected: FAIL
[input out of document: selection must change when setting a different value]
expected: FAIL
[input out of document: selection must not change when setting a value that becomes the same after the value sanitization algorithm]
expected: FAIL
[input in document: selection must not change when setting the same value]
expected: FAIL
[input in document: selection must change when setting a different value]
expected: FAIL
[input in document: selection must not change when setting a value that becomes the same after the value sanitization algorithm]
expected: FAIL
[input in document, with focus: selection must not change when setting the same value]
expected: FAIL
[input in document, with focus: selection must change when setting a different value]
expected: FAIL
[input in document, with focus: selection must not change when setting a value that becomes the same after the value sanitization algorithm]
expected: FAIL
[textarea out of document: selection must not change when setting the same value]
expected: FAIL
[textarea out of document: selection must change when setting a different value]
expected: FAIL
[textarea out of document: selection must not change when setting the same normalized value]
expected: FAIL
[textarea in document: selection must not change when setting the same value]
expected: FAIL
[textarea in document: selection must change when setting a different value]
expected: FAIL
[textarea in document: selection must not change when setting the same normalized value]
expected: FAIL
[textarea in document, with focus: selection must not change when setting the same value]
expected: FAIL
[textarea in document, with focus: selection must change when setting a different value]
expected: FAIL
[textarea in document, with focus: selection must not change when setting the same normalized value]
expected: FAIL

View file

@ -143,4 +143,30 @@
el.remove();
}
}, "selectionEnd edge-case values");
test(() => {
for (let el of createTestElements(testValue)) {
const start = 1;
const end = testValue.length - 1;
el.setSelectionRange(start, end);
assert_equals(el.selectionStart, start, `selectionStart on ${el.id}`);
assert_equals(el.selectionEnd, end, `selectionEnd on ${el.id}`);
el.selectionDirection = "backward";
assert_equals(el.selectionStart, start,
`selectionStart on ${el.id} after setting selectionDirection to "backward"`);
assert_equals(el.selectionEnd, end,
`selectionEnd on ${el.id} after setting selectionDirection to "backward"`);
el.selectionDirection = "forward";
assert_equals(el.selectionStart, start,
`selectionStart on ${el.id} after setting selectionDirection to "forward"`);
assert_equals(el.selectionEnd, end,
`selectionEnd on ${el.id} after setting selectionDirection to "forward"`);
}
}, "selectionStart and selectionEnd should remain the same when selectionDirection is changed");
</script>