Skip to content

Truly independent web browser

LibWeb: Don’t fire select for edits and value changes in text controls

sideshowbarker8ba35e66e498
diff --git a/Libraries/LibWeb/Editing/EditCommand.cpp b/Libraries/LibWeb/Editing/EditCommand.cpp
index 8a6c900edeb..6dda223527d 100644
--- a/Libraries/LibWeb/Editing/EditCommand.cpp
+++ b/Libraries/LibWeb/Editing/EditCommand.cpp
@@ -156,7 +156,8 @@ bool ReplaceDataCommand::replay(Utf16String const& from, Utf16String const& to)
return false;
if (control) {
- MUST(control->set_range_text(to, m_offset, m_offset + length, Bindings::SelectionMode::Preserve));
+ // NB: Replaying an edit moves the cursor because the value changed, as the edit did, so it fires no select event.
+ MUST(control->set_range_text(to, m_offset, m_offset + length, Bindings::SelectionMode::Preserve, HTML::SelectionSource::ValueChange));
// NB: History edits also create a pending change, while its input event is dispatched once by EditingHistory.
if (auto* textarea = dynamic_cast<HTML::HTMLTextAreaElement*>(control))
textarea->record_user_edit(current_data);
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index a64eaacccf0..ee7a83d5450 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -1082,7 +1082,7 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text_bindi
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
-WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16View replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode selection_mode)
+WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16View replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode selection_mode, SelectionSource selection_source)
{
// 2. Set this element's dirty value flag to true.
set_dirty_value_flag(true);
@@ -1128,7 +1128,22 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16
builder.append(the_relevant_value.substring_view(start));
the_relevant_value = builder.to_string();
+
+ // NB: A cursor reveal that an earlier execCommand() edit left pending is for the cursor that edit placed, so reveal
+ // it while that cursor is still within the value, as setting the selection range does.
+ if (m_cursor_reveal_after_edit == CursorRevealAfterEdit::Immediately)
+ reveal_cursor_if_pending();
+
+ // NB: Setting the relevant value clamps the selection to the new length, and the selection the last step sets
+ // would then seem unchanged, firing no select or selectionchange event. That step starts from the selection
+ // as it was, so keep it.
+ auto selection_start_before = m_selection_start;
+ auto selection_end_before = m_selection_end;
+ auto selection_direction_before = m_selection_direction;
TRY(set_relevant_value(the_relevant_value));
+ m_selection_start = selection_start_before;
+ m_selection_end = selection_end_before;
+ m_selection_direction = selection_direction_before;
// 11. Let new length be the length of the value of the first argument.
auto new_length = replacement.length_in_code_units();
@@ -1188,7 +1203,14 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16
}
// 14. Set the selection range with selection start and selection end.
- set_the_selection_range(selection_start, selection_end);
+ set_the_selection_range(selection_start, selection_end, SelectionDirection::None, selection_source);
+
+ // NB: A script's replacement ends typing coalescence in the editing history even when the selection ends up where
+ // it was, so the next typed text doesn't join the undo step of the text typed before it.
+ if (selection_source == SelectionSource::DOM) {
+ if (auto history = text_control_to_html_element().document().editing_history_if_exists())
+ history->selection_changed();
+ }
return {};
}
@@ -1264,9 +1286,10 @@ void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::
if (was_modified) {
auto& html_element = text_control_to_html_element();
- // AD-HOC: We don't fire the event if the user moves the cursor without selecting any text.
- // This is not in the spec but matches how other browsers behave.
- if (source == SelectionSource::DOM || m_selection_start != m_selection_end) {
+ // AD-HOC: We don't fire the event if the user moves the cursor without selecting any text, or if the cursor
+ // moves because the value changed (an edit, or the value setter). This is not in the spec but matches
+ // how other browsers behave.
+ if (source == SelectionSource::DOM || (source == SelectionSource::UI && m_selection_start != m_selection_end)) {
html_element.queue_an_element_task(Task::Source::UserInteraction, [&html_element] {
auto select_event = DOM::Event::create(
EventNames::select,
@@ -1320,7 +1343,7 @@ void FormAssociatedTextControlElement::handle_insert(Utf16FlyString const& input
{
Editing::EditingHistory::ProxyMutationScope proxy_scope { html_element };
- MUST(set_range_text(data_for_insertion, selection_start, selection_end, Bindings::SelectionMode::End));
+ MUST(set_range_text(data_for_insertion, selection_start, selection_end, Bindings::SelectionMode::End, SelectionSource::ValueChange));
}
if (auto step = history->undo_step_being_recorded(); step) {
@@ -1420,7 +1443,7 @@ void FormAssociatedTextControlElement::handle_delete(Utf16FlyString const& input
{
Editing::EditingHistory::ProxyMutationScope proxy_scope { html_element };
- MUST(set_range_text({}, selection_start, selection_end, Bindings::SelectionMode::End));
+ MUST(set_range_text({}, selection_start, selection_end, Bindings::SelectionMode::End, SelectionSource::ValueChange));
}
if (auto step = history->undo_step_being_recorded(); step && !removed_data.is_empty()) {
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Libraries/LibWeb/HTML/FormAssociatedElement.h
index d96a45c75b8..a25382e26b0 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.h
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.h
@@ -227,6 +227,8 @@ private:
enum class SelectionSource {
UI,
DOM,
+ // The selection moved because the value changed, as when an edit or the value setter moves the cursor.
+ ValueChange,
};
class WEB_API FormAssociatedTextControlElement
@@ -263,7 +265,7 @@ public:
WebIDL::ExceptionOr<void> set_range_text_binding(Utf16View replacement);
WebIDL::ExceptionOr<void> set_range_text_binding(Utf16View replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
WebIDL::ExceptionOr<void> set_range_text(Utf16String const& replacement);
- WebIDL::ExceptionOr<void> set_range_text(Utf16View replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
+ WebIDL::ExceptionOr<void> set_range_text(Utf16View replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve, SelectionSource = SelectionSource::DOM);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None, SelectionSource source = SelectionSource::DOM);
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index b6c7c3eb5ed..8cc41559258 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -806,7 +806,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)
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());
+ set_the_selection_range(m_value.length_in_code_units(), m_value.length_in_code_units(), SelectionDirection::None, SelectionSource::ValueChange);
update_shadow_tree();
} else if (!m_date_time_fields.is_empty()) {
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
index ff6b154eccc..b19c72d504d 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
@@ -198,31 +198,38 @@ void HTMLTextAreaElement::set_value_binding(Utf16View value)
void HTMLTextAreaElement::set_value(Utf16View value)
{
// 1. Let oldAPIValue be this element's API value.
- auto old_api_value = api_value();
-
// 2. Set this element's raw value to the new value.
- set_raw_value(Utf16String::from_utf16(value));
-
// 3. Set this element's dirty value flag to true.
- m_dirty_value = true;
+ if (!replace_value(value))
+ return;
// 4. If the new API value is different from oldAPIValue, then 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 (api_value() != old_api_value) {
- // AD-HOC: Changing the value may change which validity pseudo-classes match.
- CSS::Invalidation::invalidate_style_after_validity_change(*this);
+ // NB: A textarea without a text node still has a selection, so this also applies to it.
+ auto end_of_text_control = m_text_node ? m_text_node->length() : api_value().length_in_code_units();
+ set_the_selection_range(end_of_text_control, end_of_text_control, SelectionDirection::None, SelectionSource::ValueChange);
+}
- if (m_text_node) {
- MUST(m_text_node->replace_data(0, m_text_node->length_in_utf16_code_units(), m_raw_value));
- update_placeholder_visibility();
- }
+// The value setter's steps but the last: set the raw value and the dirty value flag, and update the text node. Returns
+// whether the API value changed.
+bool HTMLTextAreaElement::replace_value(Utf16View value)
+{
+ auto old_api_value = api_value();
+ set_raw_value(Utf16String::from_utf16(value));
+ m_dirty_value = true;
+ if (api_value() == old_api_value)
+ return false;
- // NB: A textarea without a text node still has a selection, so this also applies to it.
- auto end_of_text_control = m_text_node ? m_text_node->length() : api_value().length_in_code_units();
- set_the_selection_range(end_of_text_control, end_of_text_control);
+ // AD-HOC: Changing the value may change which validity pseudo-classes match.
+ CSS::Invalidation::invalidate_style_after_validity_change(*this);
- CSS::Invalidation::invalidate_style_after_placeholder_shown_change(*this);
+ if (m_text_node) {
+ MUST(m_text_node->replace_data(0, m_text_node->length_in_utf16_code_units(), m_raw_value));
+ update_placeholder_visibility();
}
+
+ CSS::Invalidation::invalidate_style_after_placeholder_shown_change(*this);
+ return true;
}
void HTMLTextAreaElement::set_raw_value(Utf16String value)
@@ -245,7 +252,9 @@ Utf16String HTMLTextAreaElement::api_value() const
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_relevant_value(Utf16View value)
{
- set_value(value);
+ // NB: Whoever sets the relevant value (setRangeText(), an edit) sets the selection afterward, so this doesn't move
+ // the cursor to the end, as the value setter does.
+ replace_value(value);
return {};
}
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
index dfd0ba0f1a9..801c405f327 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
@@ -160,6 +160,7 @@ private:
virtual CSS::ElementBoxKind box_kind() const override;
void set_raw_value(Utf16String);
+ bool replace_value(Utf16View);
// ^DOM::Element
virtual i32 default_tab_index_value() const override;
diff --git a/Tests/LibWeb/Text/expected/DOM/FormAssociatedElement-selection.txt b/Tests/LibWeb/Text/expected/DOM/FormAssociatedElement-selection.txt
index e1991cb4eff..85d6a681077 100644
--- a/Tests/LibWeb/Text/expected/DOM/FormAssociatedElement-selection.txt
+++ b/Tests/LibWeb/Text/expected/DOM/FormAssociatedElement-selection.txt
@@ -15,5 +15,4 @@ select event fired: 6 6
text-input selectionStart: 6 selectionEnd: 6 selectionDirection: forward
select event fired: 6 6
text-input selectionStart: 6 selectionEnd: 6 selectionDirection: backward
-select event fired: 6 6
textarea selectionStart: 0 selectionEnd: 9 selectionDirection: none
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setRangeText.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setRangeText.txt
new file mode 100644
index 00000000000..63668575ad3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setRangeText.txt
@@ -0,0 +1 @@
+scrolled to the edit caret: true
diff --git a/Tests/LibWeb/Text/expected/HTML/text-control-select-event-for-edits.txt b/Tests/LibWeb/Text/expected/HTML/text-control-select-event-for-edits.txt
new file mode 100644
index 00000000000..ecd31168831
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/text-control-select-event-for-edits.txt
@@ -0,0 +1,30 @@
+textarea: setRangeText() into the empty control: select events=1
+textarea: 100 insertions: select events=0
+textarea: 10 deletions: select events=0
+textarea: length after the edits: 91
+textarea: undoing the 10 deletions and the 100 insertions: select events=1
+textarea: length after undoing: 1
+textarea: 10 insertions in the middle: select events=0
+textarea: value after the insertions in the middle: start mmmmmmmmmmend
+textarea: undoing typing after setRangeText(): aYbcd
+textarea: value setter moving the cursor to the end: select events=0
+textarea: setSelectionRange() moving the cursor: select events=1
+textarea: select(): select events=1
+textarea: setRangeText(): select events=1
+textarea: setRangeText() shrinking the value under the selection: select events=1
+textarea: selection after that: 0-3
+input: setRangeText() into the empty control: select events=1
+input: 100 insertions: select events=0
+input: 10 deletions: select events=0
+input: length after the edits: 91
+input: undoing the 10 deletions and the 100 insertions: select events=1
+input: length after undoing: 1
+input: 10 insertions in the middle: select events=0
+input: value after the insertions in the middle: start mmmmmmmmmmend
+input: undoing typing after setRangeText(): aYbcd
+input: value setter moving the cursor to the end: select events=0
+input: setSelectionRange() moving the cursor: select events=1
+input: select(): select events=1
+input: setRangeText(): select events=1
+input: setRangeText() shrinking the value under the selection: select events=1
+input: selection after that: 0-3
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setRangeText.html b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setRangeText.html
new file mode 100644
index 00000000000..3c86bd77590
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setRangeText.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="textarea" style="height: 40px"></textarea>
+<script>
+ promiseTest(async () => {
+ textarea.focus();
+ for (let i = 0; i < 20; ++i)
+ document.execCommand("insertText", false, `\nline ${i}`);
+ const length = textarea.value.length;
+ textarea.setRangeText("", length - 1, length, "preserve");
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ println(`scrolled to the edit caret: ${textarea.scrollTop > 0}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/text-control-select-event-for-edits.html b/Tests/LibWeb/Text/input/HTML/text-control-select-event-for-edits.html
new file mode 100644
index 00000000000..9e2c151cc1a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/text-control-select-event-for-edits.html
@@ -0,0 +1,84 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="textarea"></textarea>
+<input id="input">
+<input id="barrier" value="x" style="display: none">
+<script>
+ // Editing a text control or setting its value moves the cursor, but fires no select event. The selection APIs still
+ // fire one whenever they change the selection.
+ asyncTest(async done => {
+ const barrier = document.getElementById("barrier");
+ let barrierEnd = 1;
+ // A select event queued on the same task source after the ones before it, so they've all fired once it fires.
+ async function flushSelectEvents() {
+ await new Promise(resolve => {
+ barrier.addEventListener("select", resolve, { once: true });
+ barrier.setSelectionRange(0, barrierEnd);
+ barrierEnd = 1 - barrierEnd;
+ });
+ }
+
+ for (const id of ["textarea", "input"]) {
+ const element = document.getElementById(id);
+ let count = 0;
+ element.addEventListener("select", () => ++count);
+
+ async function report(what, action) {
+ await flushSelectEvents();
+ count = 0;
+ action();
+ await flushSelectEvents();
+ println(`${id}: ${what}: select events=${count}`);
+ }
+
+ await report("setRangeText() into the empty control", () => element.setRangeText("a", 0, 0, "end"));
+
+ element.focus();
+ await report("100 insertions", () => {
+ for (let i = 0; i < 100; ++i)
+ document.execCommand("insertText", false, "a");
+ });
+ await report("10 deletions", () => {
+ for (let i = 0; i < 10; ++i)
+ document.execCommand("delete");
+ });
+ println(`${id}: length after the edits: ${element.value.length}`);
+
+ await report("undoing the 10 deletions and the 100 insertions", () => {
+ document.execCommand("undo");
+ document.execCommand("undo");
+ });
+ println(`${id}: length after undoing: ${element.value.length}`);
+
+ element.value = "start end";
+ element.setSelectionRange(6, 6);
+ await report("10 insertions in the middle", () => {
+ for (let i = 0; i < 10; ++i)
+ document.execCommand("insertText", false, "m");
+ });
+ println(`${id}: value after the insertions in the middle: ${element.value}`);
+
+ // Typing after a script's replacement starts a new undo step, even with the cursor where it was.
+ element.value = "abcd";
+ element.setSelectionRange(1, 1);
+ document.execCommand("insertText", false, "X");
+ element.setRangeText("Y", 1, 2, "end");
+ document.execCommand("insertText", false, "Z");
+ document.execCommand("undo");
+ println(`${id}: undoing typing after setRangeText(): ${element.value}`);
+
+ element.setSelectionRange(1, 1);
+ await report("value setter moving the cursor to the end", () => element.value = "a longer value than the cursor offset");
+
+ await report("setSelectionRange() moving the cursor", () => element.setSelectionRange(2, 2));
+ await report("select()", () => element.select());
+ await report("setRangeText()", () => element.setRangeText("X", 0, 1, "end"));
+
+ element.value = "abcd";
+ element.setSelectionRange(0, 4);
+ await report("setRangeText() shrinking the value under the selection", () => element.setRangeText("", 3, 4, "preserve"));
+ println(`${id}: selection after that: ${element.selectionStart}-${element.selectionEnd}`);
+ }
+ done();
+ });
+</script>