editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

DOMException Crash in Caret Split via Premature deleteContents()

Open codeCraft-Ritik opened this issue 2 months ago โ€ข 1 comments

3. DOMException Crash in Caret Split via Premature deleteContents()

๐Ÿ“ Affected Locations

  • src/components/modules/caret.ts (Lines 197โ€“233)

๐Ÿ” Deep Technical Diagnosis

When pressing Enter to split a block, Caret.extractFragmentFromCaretPosition() is called to split content from the caret to the end of the block.

// src/components/modules/caret.ts: Lines 200-231
if (selection.rangeCount) {
  const selectRange = selection.getRangeAt(0);
  const currentBlockInput = this.Editor.BlockManager.currentBlock.currentInput;

  selectRange.deleteContents(); // <-- BUG: Mutates DOM before boundary inspection!

  if (currentBlockInput) {
    if ($.isNativeInput(currentBlockInput)) {
      // ...
    } else {
      const range = selectRange.cloneRange();

      range.selectNodeContents(currentBlockInput);
      range.setStart(selectRange.endContainer, selectRange.endOffset); // <-- CRASH!

      return range.extractContents();
    }
  }
}

Why It Crashes:

  1. selectRange.deleteContents() deletes the selected nodes from the document tree.
  2. In browsers (especially Chrome/WebKit), deleting nodes collapses text nodes and can completely remove the selectRange.endContainer node from the DOM, or reduce its character length below selectRange.endOffset.
  3. When range.setStart(selectRange.endContainer, selectRange.endOffset) runs immediately after:
    • If endContainer is no longer inside currentBlockInput, the browser throws:
      DOMException: Failed to execute 'setStart' on 'Range': The node provided is not a child of this node.
    • If endOffset exceeds the remaining text length, the browser throws:
      DOMException: Failed to execute 'setStart' on 'Range': The offset is larger than the node's length.
  4. This completely halts the block-splitting operation and leaves the editor in a corrupt, uneditable state.

๐Ÿงช Reproduction Steps

  1. Insert a paragraph with formatting: Hello <b>world and extra</b> text.
  2. Select text spanning across the tag boundary: e.g., select from world to extra.
  3. Press Enter.
  4. Observed: Console throws uncaught DOMException: Failed to execute 'setStart' on 'Range'. Block does not split.

๐Ÿ’ก Proposed Solution & Patch

Clone the boundary points or collapse the range cleanly before extracting contents:

src/components/modules/caret.ts

@@ -201,8 +201,6 @@ export default class Caret extends Module {
       const selectRange = selection.getRangeAt(0);
       const currentBlockInput = this.Editor.BlockManager.currentBlock.currentInput;

-      selectRange.deleteContents();
-
       if (currentBlockInput) {
         if ($.isNativeInput(currentBlockInput)) {
           /**
@@ -213,6 +211,7 @@ export default class Caret extends Module {
           const input = currentBlockInput as HTMLInputElement | HTMLTextAreaElement;
           const newFragment = document.createDocumentFragment();

+          selectRange.deleteContents();
           const inputRemainingText = input.value.substring(0, input.selectionStart);
           const fragmentText = input.value.substring(input.selectionStart);

@@ -222,8 +221,11 @@ export default class Caret extends Module {
           return newFragment;
         } else {
-          const range = selectRange.cloneRange();
+          selectRange.deleteContents();
+          const range = document.createRange();

           range.selectNodeContents(currentBlockInput);
-          range.setStart(selectRange.endContainer, selectRange.endOffset);
+          const safeSelection = Selection.get();
+          const anchorRange = safeSelection.rangeCount ? safeSelection.getRangeAt(0) : selectRange;
+          range.setStart(anchorRange.startContainer, anchorRange.startOffset);

           return range.extractContents();
         }

codeCraft-Ritik avatar Sep 20 '26 11:09 codeCraft-Ritik

On baseline b847d010c723ebce7ef0e0c879feda00baa529b5, local checks on Windows with Chrome 154.0.8037.99 headless did not reproduce the reported DOMException. Cases included the reported selection, selections across nodes, and formatted/nested text, using native Enter interaction through Chrome CDP.

After deleteContents(), the observed live Range boundaries updated with the DOM; block splitting, caret placement, and saved content matched expectations. Saved HTML was compared semantically, with empty formatting tags and boundary whitespace normalized; this does not imply byte-for-byte HTML equality.

The 10 CDP cases and 12 Cypress tests passed, but both used the same Chrome version. Firefox, Edge, WebKit, custom tools, multi-block selections, and IME were not tested.

Could you provide the exact browser/version, a runnable minimal example, and the actual exception stack? These results do not rule out a problem in another environment.

AI assistance: OpenAI Codex assisted with the local investigation and this draft.

Sion612 avatar Oct 10 '26 16:10 Sion612