Truly independent web browser
LibWeb: Fix backward text control selections set by script
Andreas Kling7abc8ff12860
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cppindex 722119b956e..b8e12a569d8 100644--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp@@ -930,7 +930,7 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_start_}// 2. Let end be the value of this element's selectionEnd attribute.- auto end = m_selection_end;+ auto end = selection_end();// 3. If end is less than the given value, set end to the given value.if (value.has_value() && end < value.value())@@ -984,7 +984,7 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_end_bi// 2. Set the selection range with the value of this element's selectionStart attribute, the// given value, and the value of this element's selectionDirection attribute.- set_the_selection_range(m_selection_start, value, selection_direction_state());+ set_the_selection_range(selection_start(), value, selection_direction_state());return {};}@@ -1030,7 +1030,9 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_direct// throw an "InvalidStateError" DOMException.TRY(validate_selection_direction_applies(*this));- set_the_selection_range(m_selection_start, m_selection_end, string_to_selection_direction(optional_utf16_view(direction)));+ // 2. Set the selection range with the value of this element's selectionStart attribute, the value of this+ // element's selectionEnd attribute, and the given value.+ set_the_selection_range(selection_start(), selection_end(), string_to_selection_direction(optional_utf16_view(direction)));return {};}@@ -1038,7 +1040,7 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_direct{TRY(validate_selection_direction_applies(*this));- set_the_selection_range(m_selection_start, m_selection_end, string_to_selection_direction(direction));+ set_the_selection_range(selection_start(), selection_end(), string_to_selection_direction(direction));return {};}@@ -1227,6 +1229,11 @@ void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::// before the character with offset end.new_selection_start = AK::min(new_selection_start, new_selection_end);+ // NB: m_selection_start and m_selection_end hold the anchor and the focus of the selection. The focus of a+ // backward selection is its start, so that the user extends it from there.+ if (direction == SelectionDirection::Backward)+ swap(new_selection_start, new_selection_end);+bool was_modified = m_selection_start != new_selection_start || m_selection_end != new_selection_end;m_selection_start = new_selection_start;m_selection_end = new_selection_end;diff --git a/Tests/LibWeb/Text/expected/HTML/selection-setters-backward-user-selection.txt b/Tests/LibWeb/Text/expected/HTML/selection-setters-backward-user-selection.txtnew file mode 100644index 00000000000..1fa3bf799d9--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/selection-setters-backward-user-selection.txt@@ -0,0 +1,24 @@+input 2-4 backward, selectionStart = 1: 1-4 backward+input 2-4 backward, selectionStart = 3: 3-4 backward+input 2-4 backward, selectionEnd = 5: 2-5 backward+input 2-4 backward, selectionEnd = 3: 2-3 backward+input 2-4 backward, selectionDirection = "forward": 2-4 forward+input 2-4 backward, selectionDirection = "backward": 2-4 backward+input 2-4 backward, selectionDirection = "none": 2-4 none+textarea 2-4 backward, selectionStart = 1: 1-4 backward+textarea 2-4 backward, selectionStart = 3: 3-4 backward+textarea 2-4 backward, selectionEnd = 5: 2-5 backward+textarea 2-4 backward, selectionEnd = 3: 2-3 backward+textarea 2-4 backward, selectionDirection = "forward": 2-4 forward+textarea 2-4 backward, selectionDirection = "backward": 2-4 backward+textarea 2-4 backward, selectionDirection = "none": 2-4 none+input setSelectionRange(2, 4, "backward") (2-4 backward), Shift+Left: 1-4 backward+input 2-4 forward, selectionDirection = "backward" (2-4 backward), Shift+Left: 1-4 backward+input 2-4 backward, selectionStart = 1 (1-4 backward), Shift+Left: 0-4 backward+input 2-4 backward, selectionEnd = 5 (2-5 backward), Shift+Left: 1-5 backward+input setSelectionRange(2, 4, "forward") (2-4 forward), Shift+Left: 2-3 forward+textarea setSelectionRange(2, 4, "backward") (2-4 backward), Shift+Left: 1-4 backward+textarea 2-4 forward, selectionDirection = "backward" (2-4 backward), Shift+Left: 1-4 backward+textarea 2-4 backward, selectionStart = 1 (1-4 backward), Shift+Left: 0-4 backward+textarea 2-4 backward, selectionEnd = 5 (2-5 backward), Shift+Left: 1-5 backward+textarea setSelectionRange(2, 4, "forward") (2-4 forward), Shift+Left: 2-3 forwarddiff --git a/Tests/LibWeb/Text/input/HTML/selection-setters-backward-user-selection.html b/Tests/LibWeb/Text/input/HTML/selection-setters-backward-user-selection.htmlnew file mode 100644index 00000000000..c0d25400efa--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/selection-setters-backward-user-selection.html@@ -0,0 +1,73 @@+<!doctype html>+<script src="../include.js"></script>+<input id="input" value="abcdef" />+<textarea id="textarea">abcdef</textarea>+<script>+ const modShift = 1 << 2;++ const cases = [+ ["selectionStart", 1],+ ["selectionStart", 3],+ ["selectionEnd", 5],+ ["selectionEnd", 3],+ ["selectionDirection", "forward"],+ ["selectionDirection", "backward"],+ ["selectionDirection", "none"],+ ];++ const scriptCases = [+ ['setSelectionRange(2, 4, "backward")', element => element.setSelectionRange(2, 4, "backward")],+ [+ '2-4 forward, selectionDirection = "backward"',+ element => {+ element.setSelectionRange(2, 4, "forward");+ element.selectionDirection = "backward";+ },+ ],+ [+ "2-4 backward, selectionStart = 1",+ element => {+ element.setSelectionRange(2, 4, "backward");+ element.selectionStart = 1;+ },+ ],+ [+ "2-4 backward, selectionEnd = 5",+ element => {+ element.setSelectionRange(2, 4, "backward");+ element.selectionEnd = 5;+ },+ ],+ ['setSelectionRange(2, 4, "forward")', element => element.setSelectionRange(2, 4, "forward")],+ ];++ function range(element) {+ return `${element.selectionStart}-${element.selectionEnd} ${element.selectionDirection}`;+ }++ test(() => {+ for (const id of ["input", "textarea"]) {+ const element = document.getElementById(id);+ element.focus();+ for (const [attribute, value] of cases) {+ element.setSelectionRange(4, 4);+ internals.sendKey(element, "Left", modShift);+ internals.sendKey(element, "Left", modShift);+ const before = range(element);+ element[attribute] = value;+ println(`${id} ${before}, ${attribute} = ${JSON.stringify(value)}: ${range(element)}`);+ }+ }++ for (const id of ["input", "textarea"]) {+ const element = document.getElementById(id);+ element.focus();+ for (const [description, setup] of scriptCases) {+ setup(element);+ const before = range(element);+ internals.sendKey(element, "Left", modShift);+ println(`${id} ${description} (${before}), Shift+Left: ${range(element)}`);+ }+ }+ });+</script>