Case ce-0044 · Scenario scenario-selection-api-behavior

window.getSelection() returns null when contenteditable loses focus

OS: macOS Ubuntu 22.04 Device: Desktop or Laptop Any Browser: Safari 120.0 Keyboard: US Status: draft
selection api focus safari

Phenomenon

When a contenteditable region loses focus, window.getSelection() may return null in Safari, even if there was a valid selection before the focus loss. This makes it difficult to preserve or work with selections.

Reproduction example

  1. Create a contenteditable div.
  2. Select some text within it.
  3. Click outside the contenteditable to remove focus.
  4. Call window.getSelection().
  5. Observe the return value.

Observed behavior

  • In Safari on macOS, window.getSelection() returns null after focus loss.
  • The selection information is lost.
  • There is no way to retrieve the previous selection state.

Expected behavior

  • window.getSelection() should return a valid Selection object even after focus loss.
  • Or, there should be a way to preserve selection state before focus loss.
  • Selection information should be accessible when needed.

Playground for this case

Use the reported environment as a reference and record what happens in your environment while interacting with the editable area.

Reported environment
OS: macOS Ubuntu 22.04
Device: Desktop or Laptop Any
Browser: Safari 120.0
Keyboard: US
Your environment

Use this editable area to reproduce the described case.

Event log
Use this log together with the case description when filing or updating an issue.
0 events
Interact with the editable area to see events here.