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
- Create a contenteditable div.
- Select some text within it.
- Click outside the contenteditable to remove focus.
- Call
window.getSelection(). - Observe the return value.
Observed behavior
- In Safari on macOS,
window.getSelection()returnsnullafter 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.