mirror of
https://github.com/servo/servo.git
synced 2025-08-26 23:58:20 +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
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue