Skip to content

Inline decorations cause incorrect anchor/head behavior during keyboard selection (Shift + Arrow) in Safari #1560

@rdavletshin-a11y

Description

@rdavletshin-a11y

We implemented custom highlighting using Decoration.inline and discovered an issue with selecting text from the keyboard using Shift + Arrow combinations in the Safari browser.

In Safari, when using keyboard selection (Shift + Arrow), the selection area can only be enlarged and cannot be reduced back. Once the selection has started to expand, it continues to expand in both directions, regardless of the direction of the arrow keys, and does not return to a smaller range.

This is probably because the anchor and head values switch places depending on the direction of the hotkey used, which leads to incorrect selection logic when using Decoration.inline.

Steps to reproduce

  1. Select a word in the editor
  2. Press Shift + ArrowRight to expand the selection
  3. Press Shift + ArrowLeft to reduce the selection

Expected behavior

  • The selection expands to the right
  • After pressing Shift + ArrowLeft, the selection shrinks back toward the original range

Actual behavior

  • The selection expands to the right
  • After pressing Shift + ArrowLeft, the selection expands in the opposite direction instead of shrinking

Image

Example

To demonstrate the problem, a minimal editor based on basic schema was prepared. A plugin using Decoration.inline was added to the editor to implement custom highlighting. The example is available in CodeSandbox and reproduces the described behavior - https://codesandbox.io/p/sandbox/inline-decoration-selection-tvkfyq

Plugin code

import { Plugin, PluginKey } from "prosemirror-state";
import { Decoration, DecorationSet } from "prosemirror-view";

const selectionPlugin = () =>
  new Plugin({
    key: new PluginKey("customSelection"),
    props: {
      decorations(state) {
        return DecorationSet.create(state.doc, [
          Decoration.inline(state.selection.from, state.selection.to, {
            class: "customSelection",
          }),
        ]);
      },
    },
  });

Environment

prosemirror-view: 1.41.5
prosemirror-state: 1.4.4
Safari: 26.1 (21622.2.11.11.9)
macOS: 26.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions