Case ce-0110-selection-lost-after-dom-update · Scenario scenario-selection-restoration

Text selection is lost after programmatic DOM manipulation

OS: Windows 11 Device: Desktop or Laptop Any Browser: Chrome 120.0 Keyboard: US Status: draft
selection range cursor chrome

Phenomenon

After programmatically manipulating the DOM (e.g., inserting content, applying formatting), the text selection (cursor position) is lost or moved to an incorrect position. This makes it difficult to implement custom editing features.

Reproduction example

  1. Select some text in contenteditable
  2. Programmatically insert content or apply formatting
  3. Check cursor position

Observed behavior

  • Selection is lost (no cursor visible)
  • Or cursor moves to unexpected position
  • Selection range becomes invalid
  • User loses track of editing position

Expected behavior

  • Selection should be restored after DOM manipulation
  • Cursor should be in correct position
  • Selection should remain valid
  • User should be able to continue editing seamlessly

Browser Comparison

  • Chrome/Edge: Selection may be lost (this case)
  • Firefox: More likely to lose selection
  • Safari: Selection restoration most unreliable

Notes and possible direction for workarounds

  • Save selection before DOM manipulation
  • Restore selection after manipulation
  • Use Range API to save/restore selection
  • Handle invalid selection gracefully
  • Use requestAnimationFrame for timing
Before
Hello World
Selected text (World)
After DOM Update (Bug)
Hello World
After DOM manipulation, selection lost, cursor position unclear
vs
✅ Expected
Hello World
Expected: Selection restored, cursor at correct position

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: Chrome 120.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.