Truly independent web browser
LibWeb: Keep a text control’s selection within its new value
sideshowbarker844749b48601
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cppindex 621816079b6..30691141834 100644--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp@@ -849,7 +849,8 @@ void FormAssociatedTextControlElement::relevant_value_was_changed()auto relevant_value_length = the_relevant_value.length_in_code_units();// 1. If the element has a selection:- if (m_selection_start < m_selection_end) {+ // NB: A selection the user extended backward has its start past its end.+ if (m_selection_start != m_selection_end) {// 1. If the start of the selection is now past the end of the relevant value, set it to// the end of the relevant value.if (m_selection_start > relevant_value_length)@@ -870,8 +871,11 @@ void FormAssociatedTextControlElement::relevant_value_was_changed()// 2. Otherwise, the element must have a text entry cursor position position. If it is now past// the end of the relevant value, set it to the end of the relevant value.- if (m_selection_start > relevant_value_length)+ // NB: With no selection, m_selection_start and m_selection_end both hold the text entry cursor position.+ if (m_selection_start > relevant_value_length) {m_selection_start = relevant_value_length;+ m_selection_end = relevant_value_length;+ }}// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectdiff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cppindex 9c9800d8b24..c0e9b0b2a52 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp@@ -782,8 +782,6 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)// and the element has a text entry cursor position, move the text entry cursor position to the end of the// text control, unselecting any selected text and resetting the selection direction to "none".if (m_value != old_value) {- relevant_value_was_changed();-// AD-HOC: Changing the value may change which validity pseudo-classes match.CSS::Invalidation::invalidate_style_after_validity_change(*this);@@ -792,6 +790,9 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)update_placeholder_visibility();}+ // NB: The relevant value is read from the text node — so, this has to come after the text node is updated.+ relevant_value_was_changed();+if (selection_or_range_applies())set_the_selection_range(m_value.length_in_code_units(), m_value.length_in_code_units());@@ -1662,12 +1663,13 @@ void HTMLInputElement::form_associated_element_attribute_changed(Utf16FlyStringelsem_value = {};- if (m_value != old_value)- relevant_value_was_changed();-if (m_text_node)m_text_node->set_data(m_value);+ // The relevant value is read from the text node — so, this has to come after the text node is updated.+ if (m_value != old_value)+ relevant_value_was_changed();+update_shadow_tree();}} else if (name == HTML::AttributeNames::placeholder) {@@ -2149,9 +2151,6 @@ void HTMLInputElement::reset_algorithm()// and then invoke the value sanitization algorithm, if the type attribute's current state defines one.m_value = value_sanitization_algorithm(m_value);- if (m_value != old_value)- relevant_value_was_changed();-// AD-HOC: Resetting may change the value and the user validity, affecting which validity pseudo-classes match.CSS::Invalidation::invalidate_style_after_validity_change(*this);@@ -2160,6 +2159,10 @@ void HTMLInputElement::reset_algorithm()update_placeholder_visibility();}+ // NB: The relevant value is read from the text node — so, this has to come after the text node is updated.+ if (m_value != old_value)+ relevant_value_was_changed();+update_shadow_tree();}@@ -2188,14 +2191,15 @@ void HTMLInputElement::clear_algorithm()// changes caused by the user (and thus, e.g. do cause input events to fire).user_interaction_did_change_input_value();- if (m_value != old_value)- relevant_value_was_changed();-if (m_text_node) {m_text_node->set_data(m_value);update_placeholder_visibility();}+ // NB: The relevant value is read from the text node — so, this has to come after the text node is updated.+ if (m_value != old_value)+ relevant_value_was_changed();+update_shadow_tree();}diff --git a/Tests/LibWeb/Text/expected/HTML/text-control-selection-after-relevant-value-change.txt b/Tests/LibWeb/Text/expected/HTML/text-control-selection-after-relevant-value-change.txtnew file mode 100644index 00000000000..70556cef694--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/text-control-selection-after-relevant-value-change.txt@@ -0,0 +1,8 @@+input, caret at the end, then reset: "" 0 0+input, selection, then reset: "" 0 0+input, selection the user extended backward, then reset: "" 0 0+textarea, caret at the end, then reset: "" 0 0+textarea, selection, then reset: "" 0 0+textarea, selection the user extended backward, then reset: "" 0 0+textarea, selection, then reset to a shorter default value: "ab" 1 2+input, caret at the end, then value attribute emptied: "" 0 0diff --git a/Tests/LibWeb/Text/expected/input-method-state-after-form-reset.txt b/Tests/LibWeb/Text/expected/input-method-state-after-form-reset.txtnew file mode 100644index 00000000000..9d70893b888--- /dev/null+++ b/Tests/LibWeb/Text/expected/input-method-state-after-form-reset.txt@@ -0,0 +1 @@+"" 0 0diff --git a/Tests/LibWeb/Text/input/HTML/text-control-selection-after-relevant-value-change.html b/Tests/LibWeb/Text/input/HTML/text-control-selection-after-relevant-value-change.htmlnew file mode 100644index 00000000000..80a4101beda--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/text-control-selection-after-relevant-value-change.html@@ -0,0 +1,49 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<form id="form">+ <input id="input">+ <textarea id="textarea"></textarea>+ <textarea id="textarea-with-default">ab</textarea>+</form>+<input id="input-with-value-attribute" value="hello">+<script>+ // Whenever a text control's relevant value changes, a selection offset that's now past the value's end moves back+ // to the end — whether the control holds a selection, a selection the user extended backward, or just a caret.+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value+ test(() => {+ const form = document.getElementById("form");+ const report = (description, control) =>+ println(`${description}: "${control.value}" ${control.selectionStart} ${control.selectionEnd}`);++ for (const id of ["input", "textarea"]) {+ const control = document.getElementById(id);++ control.focus();+ control.value = "hello";+ form.reset();+ report(`${id}, caret at the end, then reset`, control);++ control.value = "hello";+ control.setSelectionRange(1, 4);+ form.reset();+ report(`${id}, selection, then reset`, control);++ control.value = "hello";+ internals.sendKey(control, "Left", internals.MOD_SHIFT, 2);+ form.reset();+ report(`${id}, selection the user extended backward, then reset`, control);+ }++ const textareaWithDefault = document.getElementById("textarea-with-default");+ textareaWithDefault.value = "hello";+ textareaWithDefault.setSelectionRange(1, 4);+ form.reset();+ report("textarea, selection, then reset to a shorter default value", textareaWithDefault);++ const inputWithValueAttribute = document.getElementById("input-with-value-attribute");+ inputWithValueAttribute.focus();+ inputWithValueAttribute.setSelectionRange(5, 5);+ inputWithValueAttribute.setAttribute("value", "");+ report("input, caret at the end, then value attribute emptied", inputWithValueAttribute);+ });+</script>diff --git a/Tests/LibWeb/Text/input/input-method-state-after-form-reset.html b/Tests/LibWeb/Text/input/input-method-state-after-form-reset.htmlnew file mode 100644index 00000000000..dece9d56f05--- /dev/null+++ b/Tests/LibWeb/Text/input/input-method-state-after-form-reset.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<script src="include.js"></script>+<form id="form"><input id="input"></form>+<script>+ // Once a key event has been handled, WebContent pushes the focused text control's surrounding text to the UI+ // process, for the platform input method — slicing the control's value at its selection offsets. So, those offsets+ // have to be within the value by then, even when the keydown listener emptied the value by resetting the form.+ asyncTest(done => {+ const form = document.getElementById("form");+ const input = document.getElementById("input");++ input.focus();+ input.value = "hello";+ input.addEventListener("keydown", event => {+ event.preventDefault();+ form.reset();+ });+ input.addEventListener("keyup", () => {+ println(`"${input.value}" ${input.selectionStart} ${input.selectionEnd}`);+ done();+ });+ internals.sendTextThroughUIProcess("a");+ });+</script>