Skip to content

Truly independent web browser

LibWeb: Fire selectstart when a mouse press starts a selection

Andreas Klinga933373d7838
diff --git a/Libraries/LibWeb/DOM/EventHandler.idl b/Libraries/LibWeb/DOM/EventHandler.idl
index 2fc181caa9d..b1c0f9edc48 100644
--- a/Libraries/LibWeb/DOM/EventHandler.idl
+++ b/Libraries/LibWeb/DOM/EventHandler.idl
@@ -78,6 +78,7 @@ interface mixin GlobalEventHandlers {
attribute EventHandler onseeking;
attribute EventHandler onselect;
attribute EventHandler onselectionchange;
+ attribute EventHandler onselectstart;
attribute EventHandler onslotchange;
attribute EventHandler onstalled;
attribute EventHandler onsubmit;
diff --git a/Libraries/LibWeb/HTML/AttributeNames.h b/Libraries/LibWeb/HTML/AttributeNames.h
index 6011ddf3ae2..f5adfcae9d6 100644
--- a/Libraries/LibWeb/HTML/AttributeNames.h
+++ b/Libraries/LibWeb/HTML/AttributeNames.h
@@ -246,6 +246,7 @@ namespace AttributeNames {
__ENUMERATE_HTML_ATTRIBUTE(onseeking, "onseeking") \
__ENUMERATE_HTML_ATTRIBUTE(onselect, "onselect") \
__ENUMERATE_HTML_ATTRIBUTE(onselectionchange, "onselectionchange") \
+ __ENUMERATE_HTML_ATTRIBUTE(onselectstart, "onselectstart") \
__ENUMERATE_HTML_ATTRIBUTE(onslotchange, "onslotchange") \
__ENUMERATE_HTML_ATTRIBUTE(onstalled, "onstalled") \
__ENUMERATE_HTML_ATTRIBUTE(onstorage, "onstorage") \
diff --git a/Libraries/LibWeb/HTML/EventNames.h b/Libraries/LibWeb/HTML/EventNames.h
index b6f13ed2d9f..3cc4ef43bfe 100644
--- a/Libraries/LibWeb/HTML/EventNames.h
+++ b/Libraries/LibWeb/HTML/EventNames.h
@@ -138,6 +138,7 @@ namespace Web::HTML::EventNames {
__ENUMERATE_HTML_EVENT(select) \
__ENUMERATE_HTML_EVENT(selectedcandidatepairchange) \
__ENUMERATE_HTML_EVENT(selectionchange) \
+ __ENUMERATE_HTML_EVENT(selectstart) \
__ENUMERATE_HTML_EVENT(show) \
__ENUMERATE_HTML_EVENT(signalingstatechange) \
__ENUMERATE_HTML_EVENT(slotchange) \
diff --git a/Libraries/LibWeb/HTML/GlobalEventHandlers.h b/Libraries/LibWeb/HTML/GlobalEventHandlers.h
index 1fd6279fdba..57a511afa28 100644
--- a/Libraries/LibWeb/HTML/GlobalEventHandlers.h
+++ b/Libraries/LibWeb/HTML/GlobalEventHandlers.h
@@ -93,6 +93,7 @@
E(onseeking, HTML::EventNames::seeking) \
E(onselect, HTML::EventNames::select) \
E(onselectionchange, HTML::EventNames::selectionchange) \
+ E(onselectstart, HTML::EventNames::selectstart) \
E(onslotchange, HTML::EventNames::slotchange) \
E(onstalled, HTML::EventNames::stalled) \
E(onsubmit, HTML::EventNames::submit) \
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 1b0d037a8c5..dfccad16907 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -572,8 +572,16 @@ EventResult EventHandler::handle_mousemove(CSSPixelPoint visual_viewport_positio
return EventResult::Accepted;
#if defined(AK_OS_MACOS)
- if (should_start_selection_from_preserved_mousedown)
+ if (should_start_selection_from_preserved_mousedown) {
start_selection_from_preserved_mousedown(read, *document);
+
+ // NB: Starting a selection fires selectstart, which may have disturbed the world.
+ if (m_navigable->active_document() != document)
+ return EventResult::Accepted;
+ document->update_layout(DOM::UpdateLayoutReason::EventHandlerHandleMouseMove);
+ if (!has_committed_root_box())
+ return EventResult::Accepted;
+ }
#endif
}
}
@@ -3535,6 +3543,9 @@ void EventHandler::run_mousedown_default_actions(Layout::BegunRead const& read,
}
#endif
+ if (!fire_selectstart_event(read, document, visual_viewport_position, caret_position, user_select))
+ return;
+
auto selection_started = [&] {
if (click_count == 3)
return initiate_paragraph_selection(document, *caret_position, user_select);
@@ -3555,6 +3566,49 @@ void EventHandler::run_mousedown_default_actions(Layout::BegunRead const& read,
}
}
+// https://w3c.github.io/selection-api/#selectstart-event
+bool EventHandler::dispatch_selectstart_event(DOM::Document& document, DOM::Node& target)
+{
+ // When the user agent is about to associate a new range newRange to the selection in response to a user initiated
+ // action, the user agent must fire an event named selectstart, which bubbles and is cancelable, at the node
+ // associated with the boundary point of newRange's start prior to changing the selection if the selection was
+ // previously empty or the previously associated range was collapsed.
+ // INTEROP: Blink and WebKit fire it for any press that starts a selection, whatever the selection was before, which
+ // is what lets a page keep the user from selecting its text at all.
+ auto event = DOM::Event::create(HTML::EventNames::selectstart, HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(document)));
+ event->set_bubbles(true);
+ event->set_cancelable(true);
+ event->set_is_trusted(true);
+
+ // If the event is canceled, the user agent must not change the selection.
+ if (!target.dispatch_event(event))
+ return false;
+
+ // NB: Dispatching an event may have disturbed the world.
+ return m_navigable->active_document().ptr() == &document;
+}
+
+bool EventHandler::fire_selectstart_event(Layout::BegunRead const& read, DOM::Document& document, CSSPixelPoint visual_viewport_position, Optional<Painting::CaretPosition>& caret_position, CSS::UserSelect& user_select)
+{
+ auto target = caret_position->boundary_node();
+ if (!target || !dispatch_selectstart_event(document, *target))
+ return false;
+
+ // NB: A listener may have changed what the caret position points into, even if it left layout up to date.
+ document.update_layout(DOM::UpdateLayoutReason::EventHandlerHandleMouseDown);
+ if (!has_committed_root_box())
+ return false;
+ GC::Ptr<DOM::Node> hit_node;
+ if (auto hit = document.hit_test(read, visual_viewport_position); hit.has_value())
+ hit_node = hit->dom_node();
+ auto editing_host = hit_node ? associated_descendant_editing_host(read, *hit_node) : nullptr;
+ caret_position = caret_position_for_mouse_selection(read, document, visual_viewport_position, hit_node, editing_host);
+ if (!caret_position.has_value())
+ return false;
+ user_select = user_select_used_value_for_caret_position(read, *caret_position);
+ return user_select != CSS::UserSelect::None;
+}
+
Optional<Painting::CaretPosition> EventHandler::prepare_mouse_selection(DOM::Document& document, CSSPixelPoint visual_viewport_position)
{
Layout::ForcedReadScope read { document };
@@ -3589,6 +3643,11 @@ Optional<Painting::CaretPosition> EventHandler::prepare_mouse_selection(DOM::Doc
else if (auto focused_area = document.focused_area())
HTML::run_unfocusing_steps(focused_area);
+ return caret_position_for_mouse_selection(read, document, visual_viewport_position, editable_hit_node_before_focus, editing_host_before_focus);
+}
+
+Optional<Painting::CaretPosition> EventHandler::caret_position_for_mouse_selection(Layout::BegunRead const& read, DOM::Document& document, CSSPixelPoint visual_viewport_position, GC::Ptr<DOM::Node> editable_hit_node_before_focus, GC::Ptr<DOM::Node> editing_host_before_focus)
+{
// NB: Focusing may have invalidated layout.
document.update_layout(DOM::UpdateLayoutReason::EventHandlerHandleMouseDown);
if (!has_committed_root_box())
@@ -3716,6 +3775,9 @@ bool EventHandler::select_word_at_position(Layout::BegunRead const& read, DOM::D
if (user_select == CSS::UserSelect::None)
return false;
+ if (!fire_selectstart_event(read, document, visual_viewport_position, caret_position, user_select))
+ return false;
+
if (initiate_word_selection(read, document, *caret_position, user_select)) {
stop_updating_selection();
return true;
@@ -3738,6 +3800,9 @@ void EventHandler::start_selection_from_preserved_mousedown(Layout::BegunRead co
if (user_select == CSS::UserSelect::None)
return;
+ if (!fire_selectstart_event(read, document, *m_mousedown_visual_viewport_position, caret_position, user_select))
+ return;
+
if (!initiate_character_selection(document, *caret_position, user_select, false))
return;
@@ -4328,6 +4393,10 @@ bool EventHandler::select_context_menu_text(Layout::BegunRead const& read, DOM::
if (all_of(range->to_string(), Unicode::code_point_has_white_space_property) || !range_contains_viewport_position(*range, coordinates.viewport_position))
return false;
+ // NB: The range follows what a listener changes, unless the listener takes the text away.
+ if (!dispatch_selectstart_event(document, *hit_node) || range->start_container().ptr() != hit_node || range->end_container().ptr() != hit_node)
+ return false;
+
if (auto* target = document.active_input_events_target(hit_node)) {
target->set_selection_anchor(*hit_node, range->start_offset());
target->set_selection_focus(*hit_node, range->end_offset());
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index eaf1b138763..7fc5a43891c 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -259,6 +259,9 @@ private:
bool maybe_request_paste_for_middle_click(Layout::BegunRead const& read, DOM::Document&, CSSPixelPoint visual_viewport_position);
Optional<Painting::CaretPosition> prepare_mouse_selection(DOM::Document&, CSSPixelPoint visual_viewport_position);
+ Optional<Painting::CaretPosition> caret_position_for_mouse_selection(Layout::BegunRead const& read, DOM::Document&, CSSPixelPoint visual_viewport_position, GC::Ptr<DOM::Node> editable_hit_node_before_focus, GC::Ptr<DOM::Node> editing_host_before_focus);
+ bool dispatch_selectstart_event(DOM::Document&, DOM::Node& target);
+ bool fire_selectstart_event(Layout::BegunRead const& read, DOM::Document&, CSSPixelPoint visual_viewport_position, Optional<Painting::CaretPosition>&, CSS::UserSelect&);
bool initiate_character_selection(DOM::Document&, Painting::CaretPosition const&, CSS::UserSelect, bool shift_held);
bool initiate_word_selection(Layout::BegunRead const& read, DOM::Document&, Painting::CaretPosition const&, CSS::UserSelect);
bool initiate_paragraph_selection(DOM::Document&, Painting::CaretPosition const&, CSS::UserSelect);
diff --git a/Tests/LibWeb/Crash/Editing/selectstart-listener-shortens-the-text.html b/Tests/LibWeb/Crash/Editing/selectstart-listener-shortens-the-text.html
new file mode 100644
index 00000000000..8045962f1a4
--- /dev/null
+++ b/Tests/LibWeb/Crash/Editing/selectstart-listener-shortens-the-text.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<div id="editable" contenteditable style="width: max-content">some long text to press late in</div>
+<script>
+ editable.addEventListener("selectstart", () => {
+ editable.firstChild.data = "x";
+ editable.offsetHeight;
+ });
+ const rect = editable.getBoundingClientRect();
+ internals.mouseDown(rect.right - 5, rect.top + 5);
+ internals.mouseUp(rect.right - 5, rect.top + 5);
+</script>
diff --git a/Tests/LibWeb/Text/expected/selectstart-event-on-mouse-selection.txt b/Tests/LibWeb/Text/expected/selectstart-event-on-mouse-selection.txt
index 32c7906e8bf..cda0d32d1f7 100644
--- a/Tests/LibWeb/Text/expected/selectstart-event-on-mouse-selection.txt
+++ b/Tests/LibWeb/Text/expected/selectstart-event-on-mouse-selection.txt
@@ -1,9 +1,19 @@
-document.onselectstart: undefined
-window.onselectstart: undefined
-div.onselectstart: undefined
+document.onselectstart: null
+window.onselectstart: null
+div.onselectstart: null
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
free: selected "one two t"
-blocked: selected "six seven"
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
+blocked: selected ""
unselectable: selected ""
-handler: selected "fourteen "
+onselectstart attribute at #text
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
+handler: selected ""
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
click count 2: selected "one"
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
click count 3: selected "one two three four five"
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
+right press on free: selected "one"
+selectstart at #text, bubbles true, cancelable true, trusted true, selection ""
+right press on blocked: selected ""
diff --git a/Tests/LibWeb/Text/input/selectstart-event-on-mouse-selection.html b/Tests/LibWeb/Text/input/selectstart-event-on-mouse-selection.html
index d91853cf8b9..321a5a03c11 100644
--- a/Tests/LibWeb/Text/input/selectstart-event-on-mouse-selection.html
+++ b/Tests/LibWeb/Text/input/selectstart-event-on-mouse-selection.html
@@ -51,5 +51,14 @@
internals.mouseUp(rect.left + 1, rect.top + 15);
println(`click count ${clickCount}: selected ${describe()}`);
}
+
+ // A right press on a word selects it through the same event, and cancelling the event leaves it alone.
+ for (const id of ["free", "blocked"]) {
+ const { left, top } = document.getElementById(id).getBoundingClientRect();
+ getSelection().removeAllRanges();
+ internals.mouseDown(left + 1, top + 15, 1, 2);
+ internals.mouseUp(left + 1, top + 15, 2);
+ println(`right press on ${id}: selected ${describe()}`);
+ }
});
</script>