From 5d583f8478d0a4f626badc88d1523fa323f25632 Mon Sep 17 00:00:00 2001 From: Steven Obiajulu Date: Mon, 23 Feb 2026 22:31:19 -0500 Subject: [PATCH] Collapse range before mutations in deleteContents and extract Move the step that sets the range's start and end to (newNode, newOffset) from after DOM mutations to before them, in both the deleteContents() and extract() algorithms. The pre-computed collapse target can become invalid if script runs during the remove operation (via "removing steps" hooks) and modifies the DOM. By collapsing the range before mutations, the DOM's built-in live range maintenance mechanisms (live range pre-remove steps and replace data range adjustments) automatically keep the range valid through all subsequent operations. Fixes #1446. --- dom.bs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/dom.bs b/dom.bs index 5266d1bb..27b9f232 100644 --- a/dom.bs +++ b/dom.bs @@ -9283,6 +9283,17 @@ method steps are: point. +
  • +

    Set this's start and end to + (newNode, newOffset). + +

    This is done before performing mutations, because the DOM's built-in + live range maintenance mechanisms + (live range pre-remove steps and replace data range adjustments) + will automatically keep the range valid through the subsequent operations. Collapsing the + range after mutations would be unreliable, since script can run during + removal and modify the DOM. +

  • If originalStartNode is a {{CharacterData}} node, then replace data of originalStartNode with originalStartOffset, originalStartNode's lengthoriginalStartOffset, and @@ -9294,9 +9305,6 @@ method steps are:

  • If originalEndNode is a {{CharacterData}} node, then replace data of originalEndNode with 0, originalEndOffset, and the empty string. - -

  • Set start and end to - (newNode, newOffset). @@ -9414,8 +9422,16 @@ method steps are: point. - +

  • +

    Set range's start and end to + (newNode, newOffset). + +

    This is done before performing mutations, because the DOM's built-in + live range maintenance mechanisms + (live range pre-remove steps and replace data range adjustments) + will automatically keep the range valid through the subsequent operations. Collapsing the + range after mutations would be unreliable, since script can run during + removal and modify the DOM.

  • If firstPartiallyContainedChild is a {{CharacterData}} node: @@ -9498,9 +9514,6 @@ method steps are:

  • Append subfragment to clone. -

  • Set range's start and end to - (newNode, newOffset). -

  • Return fragment. @@ -11284,6 +11297,7 @@ Simon Pieters, Simon Wülker, Stef Busking, Steve Byrne, +Steven Obiajulu, Stig Halvorsen, Tab Atkins, Takashi Sakamoto,