Skip to content

Truly independent web browser

LibWeb: Do not start a text selection from a button press

Andreas Kling0f35e40b58f9
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 2996cdd91b2..040cd0e0f5d 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -3514,6 +3514,15 @@ void EventHandler::run_mousedown_default_actions(Layout::BegunRead const& read,
if (!caret_position.has_value())
return;
+ // NB: Pressing a button activates it, so a drag that starts on one does not select its label or the page around
+ // it.
+ if (auto hit = document.hit_test(read, visual_viewport_position); hit.has_value()) {
+ for (GC::Ptr<DOM::Node const> node = hit->dom_node(); node; node = node->parent_or_shadow_host_node()) {
+ if (auto const* element = as_if<HTML::HTMLElement>(*node); element && element->uses_button_layout())
+ return;
+ }
+ }
+
// https://drafts.csswg.org/css-ui/#valdef-user-select-none
// Attempting to start a selection in an element where user-select is none, such as by clicking in it or starting a
// drag in it, must not cause a pre-existing selection to become unselected or to be affected in any way.
diff --git a/Tests/LibWeb/Text/expected/button-drag-does-not-select.txt b/Tests/LibWeb/Text/expected/button-drag-does-not-select.txt
new file mode 100644
index 00000000000..d9c2788d355
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/button-drag-does-not-select.txt
@@ -0,0 +1,7 @@
+button, drag inside: ""
+button, drag out to the right: ""
+button, drag out to the left: ""
+submit, drag inside: ""
+submit, drag out to the right: ""
+submit, drag out to the left: ""
+text, drag into the button: " text afte"
diff --git a/Tests/LibWeb/Text/input/button-drag-does-not-select.html b/Tests/LibWeb/Text/input/button-drag-does-not-select.html
new file mode 100644
index 00000000000..efd68de0e0c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/button-drag-does-not-select.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+</style>
+<p>Some text above the buttons.</p>
+<button id="button">Button element</button> text after the button
+<p><input id="submit" type="submit" value="Submit input"> text after the input</p>
+<script>
+ test(() => {
+ const drag = (fromX, toX, y) => {
+ internals.mouseDown(fromX, y);
+ for (let x = fromX; fromX < toX ? x <= toX : x >= toX; x += fromX < toX ? 5 : -5)
+ internals.mouseMove(x, y, 0, 1);
+ internals.mouseUp(toX, y);
+ const selection = getSelection().toString();
+ getSelection().removeAllRanges();
+ return selection;
+ };
+
+ for (const target of [button, submit]) {
+ const rect = target.getBoundingClientRect();
+ const y = rect.top + rect.height / 2;
+ println(`${target.id}, drag inside: "${drag(rect.left + 3, rect.right - 3, y)}"`);
+ println(`${target.id}, drag out to the right: "${drag(rect.left + 3, rect.right + 100, y)}"`);
+ println(`${target.id}, drag out to the left: "${drag(rect.right - 3, 0, y)}"`);
+ }
+
+ // A drag that starts on text still selects, even when it ends on a button.
+ const rect = button.getBoundingClientRect();
+ println(`text, drag into the button: "${drag(rect.right + 80, rect.left + 10, rect.top + rect.height / 2)}"`);
+ });
+</script>