Phenomenon
When programmatically replacing selected content in Safari, the selection range becomes invalid. The cursor position is lost and cannot be restored, making it impossible to continue editing.
Reproduction example
- Select some text
- Programmatically replace it with new content
- Check selection state
Observed behavior
- Selection range becomes invalid
- Cursor position is lost
- Cannot continue editing at correct position
- Selection cannot be restored
Expected behavior
- Selection should remain valid after replacement
- Cursor should be positioned after replaced content
- User should be able to continue editing
- Selection should be restored properly
Browser Comparison
- Chrome/Edge: Selection generally remains valid
- Firefox: Selection may become invalid
- Safari: Selection restoration most unreliable (this case)
Notes and possible direction for workarounds
- Save selection before replacement
- Restore selection after replacement
- Use Range API to maintain valid selection
- Handle invalid selection gracefully