From 5874b7a76f223d577a97f46b8d9ef9b04478d3bb Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 3 Jul 2025 21:28:49 +0200 Subject: [PATCH] LibWeb: Skip `update_associated_selection()` when there's no selection This change fixes at least two issues: - `update_associated_selection()` is responsible for selectionchange dispatch, and we were incorrectly dispatching this event on ranges that were not associated with a selection. - `Range::get_client_rects()` was using `update_associated_selection()` to refresh the selection state in the paintable tree for the current range, but since a range might not be associated with a selection, this could make the painted selection reflect the state of an arbitrary range instead of the actual selection range. Fixes a bug on Discord where any text typed into the message input would get selected. --- Libraries/LibWeb/DOM/Range.cpp | 16 ++++++++-- .../LibWeb/Painting/PaintableFragment.cpp | 18 +++++------ Libraries/LibWeb/Painting/PaintableFragment.h | 2 +- ...th-selection-should-not-affect-it-ref.html | 17 ++++++++++ ...d-with-selection-should-not-affect-it.html | 32 +++++++++++++++++++ 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 Tests/LibWeb/Ref/expected/range-not-assiciated-with-selection-should-not-affect-it-ref.html create mode 100644 Tests/LibWeb/Ref/input/range-not-assiciated-with-selection-should-not-affect-it.html diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cpp index 00be8803ec5..5cfdb380699 100644 --- a/Libraries/LibWeb/DOM/Range.cpp +++ b/Libraries/LibWeb/DOM/Range.cpp @@ -3,6 +3,7 @@ * Copyright (c) 2022, Luke Wilde * Copyright (c) 2022-2023, Andreas Kling * Copyright (c) 2024-2025, Jelle Raaijmakers + * Copyright (c) 2024-2025, Aliaksandr Kalenik * * SPDX-License-Identifier: BSD-2-Clause */ @@ -99,6 +100,9 @@ void Range::set_associated_selection(Badge, GC::Ptr