Skip to content

Truly independent web browser

LibWeb: Reveal the caret once after execCommand() edits in text controls

Andreas Kling7386e048c660
diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp
index c04b4e56f43..ad72be122b8 100644
--- a/Libraries/LibWeb/Editing/ExecCommand.cpp
+++ b/Libraries/LibWeb/Editing/ExecCommand.cpp
@@ -89,6 +89,13 @@ static Optional<bool> query_command_enabled_in_text_control(Document& document,
static bool exec_command_in_text_control(HTML::FormAssociatedTextControlElement& control, Utf16FlyString const& command, Utf16View value)
{
+ // NB: See the note at CursorRevealAfterEdit.
+ using CursorRevealAfterEdit = HTML::FormAssociatedTextControlElement::CursorRevealAfterEdit;
+ control.set_cursor_reveal_after_edit(CursorRevealAfterEdit::InNextRenderingUpdate);
+ ScopeGuard reveal_cursor_immediately_again = [&] {
+ control.set_cursor_reveal_after_edit(CursorRevealAfterEdit::Immediately);
+ };
+
if (command == Editing::CommandNames::delete_) {
control.handle_delete(UIEvents::InputTypes::deleteContentBackward);
} else if (command == Editing::CommandNames::forwardDelete) {
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 1cff885af24..453b3743ba8 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -27,6 +27,7 @@
#include <LibWeb/HTML/EventLoop/ClockPlan.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventLoop/FrameCompletion.h>
+#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/ListOfAvailableImages.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
@@ -906,6 +907,10 @@ void EventLoop::update_the_rendering()
navigable->process_main_thread_smooth_scrolls();
navigable->adopt_pending_async_scroll_offsets();
}
+ // AD-HOC: A text control that script edited reveals its cursor now, once for all of the edits. See the note
+ // at FormAssociatedTextControlElement::CursorRevealAfterEdit.
+ if (auto* text_control = as_if<FormAssociatedTextControlElement>(document->focused_area().ptr()))
+ text_control->reveal_cursor_if_pending();
document->run_the_scroll_steps();
}
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index 66de5b47d3d..fd8c84c9385 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -1195,6 +1195,12 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_range(
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-range
void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction, SelectionSource source)
{
+ // NB: A cursor reveal that an earlier execCommand() edit left pending is for the cursor that edit placed, so reveal
+ // it before the selection moves. Edits that execCommand() is making right now set the selection here too, and
+ // they keep their reveal pending.
+ if (m_cursor_reveal_after_edit == CursorRevealAfterEdit::Immediately)
+ reveal_cursor_if_pending();
+
// 1. If start is null, let start be 0.
start = start.value_or(0);
@@ -1315,7 +1321,7 @@ void FormAssociatedTextControlElement::handle_insert(Utf16FlyString const& input
did_edit_text_node(input_type, data_for_input_event);
html_element.document().spell_check_controller().did_user_edit(html_element);
- scroll_cursor_into_view();
+ reveal_cursor_after_edit();
}
GC::Ptr<DOM::StaticRange> FormAssociatedTextControlElement::deletion_target_range(Utf16FlyString const& input_type)
@@ -1404,7 +1410,7 @@ void FormAssociatedTextControlElement::handle_delete(Utf16FlyString const& input
did_edit_text_node(input_type, {});
html_element.document().spell_check_controller().did_user_edit(html_element);
- scroll_cursor_into_view();
+ reveal_cursor_after_edit();
}
Optional<Utf16String> FormAssociatedTextControlElement::selected_text_for_stringifier() const
@@ -1440,8 +1446,27 @@ void FormAssociatedTextControlElement::move_selection_end_to(size_t offset, Text
history->selection_changed();
}
+void FormAssociatedTextControlElement::reveal_cursor_after_edit()
+{
+ // NB: Only the focused text control reveals its cursor in the rendering update, so a control that lost focus during
+ // the edit reveals it right away.
+ if (m_cursor_reveal_after_edit == CursorRevealAfterEdit::InNextRenderingUpdate && text_control_to_html_element().is_focused()) {
+ m_has_pending_cursor_reveal = true;
+ return;
+ }
+ scroll_cursor_into_view();
+}
+
+void FormAssociatedTextControlElement::reveal_cursor_if_pending()
+{
+ if (m_has_pending_cursor_reveal)
+ scroll_cursor_into_view();
+}
+
void FormAssociatedTextControlElement::scroll_cursor_into_view()
{
+ m_has_pending_cursor_reveal = false;
+
auto& element = text_control_to_html_element();
Layout::ForcedReadScope read { element.document() };
element.document().update_layout(DOM::UpdateLayoutReason::ScrollCursorIntoView);
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Libraries/LibWeb/HTML/FormAssociatedElement.h
index 89f55f7ebc7..3e6a32926ad 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.h
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.h
@@ -310,6 +310,16 @@ public:
GC::Ptr<DOM::Position> cursor_position() const;
+ // NB: Edits that script makes with execCommand() reveal the cursor in the next rendering update instead of right
+ // away, so that a run of them lays out the text control once instead of after each edit. A pending reveal is
+ // made right away if the control loses focus or script moves its selection before that rendering update.
+ enum class CursorRevealAfterEdit : u8 {
+ Immediately,
+ InNextRenderingUpdate,
+ };
+ void set_cursor_reveal_after_edit(CursorRevealAfterEdit reveal) { m_cursor_reveal_after_edit = reveal; }
+ void reveal_cursor_if_pending();
+
protected:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
void relevant_value_was_changed();
@@ -323,6 +333,7 @@ private:
void move_selection_end_to(size_t offset, TextAffinity, CollapseSelection);
void move_cursor_by_page(bool forward, CollapseSelection);
void scroll_cursor_into_view();
+ void reveal_cursor_after_edit();
void selection_was_changed(SelectionSource);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-selection
@@ -335,6 +346,9 @@ private:
// https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
bool m_has_scheduled_selectionchange_event { false };
+
+ CursorRevealAfterEdit m_cursor_reveal_after_edit { CursorRevealAfterEdit::Immediately };
+ bool m_has_pending_cursor_reveal { false };
};
}
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 00454a897dc..d0255432ec8 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -1607,6 +1607,8 @@ void HTMLInputElement::did_receive_focus()
void HTMLInputElement::did_lose_focus()
{
+ reveal_cursor_if_pending();
+
if (m_text_node)
m_text_node->set_needs_repaint(InvalidateDisplayList::PaintCommands);
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
index 9f073fa0b6e..f2e49a73bd6 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
@@ -75,6 +75,8 @@ void HTMLTextAreaElement::did_receive_focus()
void HTMLTextAreaElement::did_lose_focus()
{
+ reveal_cursor_if_pending();
+
if (m_text_node)
m_text_node->set_needs_repaint(InvalidateDisplayList::PaintCommands);
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-blur-before-rendering.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-blur-before-rendering.txt
new file mode 100644
index 00000000000..da943dccb18
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-blur-before-rendering.txt
@@ -0,0 +1,2 @@
+scrolled to the caret after blur: true
+scrollTop after refocus: 0
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-reveals-caret-once.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-reveals-caret-once.txt
index 0703607aa3a..eb8f8dc1d38 100644
--- a/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-reveals-caret-once.txt
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-reveals-caret-once.txt
@@ -1,2 +1,2 @@
-layouts during edits: 50
+layouts during edits: 0
caret revealed after the next rendering update: true
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setSelectionRange.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setSelectionRange.txt
new file mode 100644
index 00000000000..63668575ad3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-in-textarea-then-setSelectionRange.txt
@@ -0,0 +1 @@
+scrolled to the edit caret: true
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-blur-before-rendering.html b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-blur-before-rendering.html
new file mode 100644
index 00000000000..713a18b2060
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-blur-before-rendering.html
@@ -0,0 +1,18 @@
+<!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}`);
+ textarea.blur();
+ println(`scrolled to the caret after blur: ${textarea.scrollTop > 0}`);
+
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ textarea.scrollTop = 0;
+ textarea.focus({ preventScroll: true });
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ println(`scrollTop after refocus: ${textarea.scrollTop}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setSelectionRange.html b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setSelectionRange.html
new file mode 100644
index 00000000000..26ca679c3fc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-in-textarea-then-setSelectionRange.html
@@ -0,0 +1,13 @@
+<!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}`);
+ textarea.setSelectionRange(0, 0);
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ println(`scrolled to the edit caret: ${textarea.scrollTop > 0}`);
+ });
+</script>