From f2d09a614002cd1068fe4b8ea19ade573e57c58e Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Mon, 16 Feb 2026 17:12:38 +0100 Subject: [PATCH] :bug: Preserving selection when applying styles to selected text range --- .../src/editor/controllers/SelectionController.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frontend/text-editor/src/editor/controllers/SelectionController.js b/frontend/text-editor/src/editor/controllers/SelectionController.js index 88583a1116..683a887203 100644 --- a/frontend/text-editor/src/editor/controllers/SelectionController.js +++ b/frontend/text-editor/src/editor/controllers/SelectionController.js @@ -642,13 +642,15 @@ export class SelectionController extends EventTarget { } else { this.#anchorNode = anchorNode; this.#anchorOffset = anchorOffset; - if (anchorNode === focusNode) { - this.#focusNode = this.#anchorNode; - this.#focusOffset = this.#anchorOffset; + this.#focusNode = focusNode; + this.#focusOffset = focusOffset; + // setPosition() collapses the selection to a single caret. We must only use it + // when anchorOffset === focusOffset. When both points are in the same node but + // offsets differ (e.g. selecting "hola" in "hola adios"), we need setBaseAndExtent() + // to preserve the range so we don't incorrectly collapse ranges and lose the selection. + if (anchorNode === focusNode && anchorOffset === focusOffset) { this.#selection.setPosition(anchorNode, anchorOffset); } else { - this.#focusNode = focusNode; - this.#focusOffset = focusOffset; this.#selection.setBaseAndExtent( anchorNode, anchorOffset,