Case ce-0172-selection-invalid-after-replace · Scenario scenario-selection-restoration

Selection becomes invalid after replacing content

OS: Windows 11 Device: Desktop or Laptop Any Browser: Safari 17.0 Keyboard: US Status: draft
selection range replace safari

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

  1. Select some text
  2. Programmatically replace it with new content
  3. 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
Before
Hello World Test
Selected text (World)
After Replace (Bug)
Hello New Test
After programmatic replacement, selection range invalidated, cursor position unclear
vs
✅ Expected
Hello New Test
Expected: Selection range valid, cursor positioned after replaced content

Browser compatibility matrix

This matrix shows which browser and OS combinations have documented cases for this scenario. The current case is highlighted. Click on a cell to view other cases.

Current case
Confirmed
Draft
No case documented

All variants (detailed table)

Complete list of all cases for this scenario with full environment details.

Case OS Device Browser Keyboard Status
ce-0110-selection-lost-after-dom-update Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0130-selection-invalid-after-wrap Windows 11 Desktop or Laptop Any Firefox 120.0 US draft
ce-0138-selection-collapse-on-click macOS 14.0 Desktop or Laptop Any Safari 17.0 US draft
ce-0149-selection-range-invalid-after-insert Windows 11 Desktop or Laptop Any Firefox 120.0 US draft
ce-0172-selection-invalid-after-replace Windows 11 Desktop or Laptop Any Safari 17.0 US draft

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: Windows 11
Device: Desktop or Laptop Any
Browser: Safari 17.0
Keyboard: US
Your environment
Sample HTML:
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.

Comments & Discussion

Have questions, suggestions, or want to share your experience? Join the discussion below.