Skip to content

Fix #844 regression - text selection blocked by removeAllRanges() in click handler #867

Description

@swashbuck

Subject of the issue

Follow-up to #844. The fix added window.getSelection()?.removeAllRanges() to _refocusCurrentActiveElement() to clear Firefox's persistent selection anchor after programmatic focus. However, _refocusCurrentActiveElement() is also called from _onClick() (js/a11y/browserFocus.js:125) on every click that doesn't land on a focusable element. The click event fires on mouseup, by which time the user's drag-selection has been committed - so the new call unconditionally wipes any text the user just selected.

Net effect: text selection is now broken in all browsers (not just Firefox) when _accessibility._isEnabled is true.

Your environment

Steps to reproduce

  1. In config.json, set _accessibility._isEnabled: true
  2. Open any page with body text
  3. Click-and-drag to select text

Expected behaviour

Selected text remains selected after mouseup.

Actual behaviour

Selection is cleared the moment mouseup fires, because the click handler invokes _refocusCurrentActiveElement() -> removeAllRanges().

Proposed fix

Only clear the selection when it's collapsed (i.e. a stray programmatic anchor with no actual range). Genuine user selections from a drag are non-collapsed and should be preserved.

_refocusCurrentActiveElement() {
  const element = this.a11y.currentActiveElement;
  if (!element) return;
  this.a11y.focus(element, { preventScroll: true });
  const selection = window.getSelection();
  if (selection?.isCollapsed) selection.removeAllRanges();
}

Drag-select -> non-collapsed range -> preserved.
Firefox programmatic anchor -> collapsed -> cleared (original #844 intent retained).


Posted via collaboration with Claude Code

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

Status
Needs Reviewing

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions