Collapse range before mutations in deleteContents and extract#1452
Collapse range before mutations in deleteContents and extract#1452annevk merged 3 commits intowhatwg:mainfrom
Conversation
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 whatwg#1446.
annevk
left a comment
There was a problem hiding this comment.
Thanks for fixing this. Let's remove the elaborate notes and put a variant of them in the commit message.
|
@annevk Updated per your review — removed the explanatory notes from both algorithms. The rationale is in the commit message. Also updated the implementer interest checklist. |
|
@annevk Thanks for the review! It looks like the Participation check is pending because our organization (UseJunior) hasn't been verified yet. Happy to provide anything that's needed on our end to move that along. |
|
I would like @smaug---- to double check as well since he filed the original issue. I saw that you filed browser bugs but I think browsers already handle this correctly. You can execute script during removal by using a |
|
I ran the empirical test. @annevk you're right — all three browsers already implement the corrected behavior. Test structure The mutation callback removes Results — Chrome 133, Firefox 135, Safari 18
Track A: Track B: What this means for the open items
|
|
Thanks again @stevenobiajulu! |



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
deleteContents()and theextractalgorithm. The DOM's built-in live range maintenance mechanisms (live range pre-remove stepsandreplace datarange adjustments) keep the range valid through subsequent operations. Collapsing after mutations is unreliable because script can run during removal (via removing steps) and modify the DOM, invalidating the pre-computed values.Fixes #1446.
(See WHATWG Working Mode: Changes for more details.)