Repository navigation
[lexical] Bug Fix: A named slot's value moved out of its slot no longer makes the reconciler remove the DOM of the element it moved into - #9778
Conversation
…er makes the reconciler remove the DOM of the element it moved into ## Description When a named slot's value moved, in one update, into an element rendered before its old host (as a child or into a slot of that element; the element can be new, existing, or built anew because its `updateDOM` returns true) while its slot was removed, given another value, or its host was removed, the reconciler removed the wrong element. `$slotContainerForKey` finds a slot's container as the parent of the value's DOM in the previous DOM map; by the time the old host's slot is cleaned up, creating or reconciling the new place has already moved that DOM (the reconciler reuses a moved node's DOM), so its parent is the new place. The cleanup removed the element the value moved into, or that element's new slot container: the editor state was right, but that element's DOM was gone, its DOM-map entry pointed at a detached element, and the next update that inserted a node next to it threw `NotFoundError: The child can not be found in the parent.` Now, when `$createNode` moves the DOM of a node that was a slot value in the previous state, it first records the DOM's parent, which is the value's old slot container, for the rest of that reconciliation, and `$slotContainerForKey` returns the recorded container for that key. The old slot's cleanup removes the old container. Nothing changes when the new place is rendered after the old host (the cleanup already ran with the right container), for slot values that stay, or for editors without slots. Named slots are experimental. Closes #9758 ## Changeset `.changeset/slot-value-moved-out-container.md` (`lexical` patch, `[fix]`). ## Test plan - `packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts` (new, 56 tests): the new place before or after the old host; a new, an existing and a rebuilt element; moved in as a child, into a new slot, or replacing a slot's value; the old slot removed, given a new value, or its host removed. Each checks that nothing throws, that every node's element is in the editor's DOM, that the DOM equals a fresh render, that the moved value reports neither `destroyed` nor `created`, that a later update inserting next to the new place works, and, where the element is not one built anew, that no removed node stays in the DOM map. Two more tests: a decorator host that placed its slot container outside its own DOM (the container is removed and the element the value moved into keeps its DOM), and a decorator slot value moved into an earlier element (that element and the value keep their DOM). ### Before ``` $ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts AssertionError: expected [ 'paragraph#4', 'text#5', …(3) ] to deeply equal [] AssertionError: expected [ 'paragraph#75', 'text#76' ] to deeply equal [] Tests 26 failed | 30 passed (56) ``` ### After ``` $ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts Tests 56 passed (56) ``` Every package's unit tests pass with no change from main, also together with #9773. The rich-text end-to-end tests for tables, copy and paste, lists, selection, headings, markdown, mutations and the four slot specs, and the collab-y specs including slot convergence, give the same results as main in Chromium and Firefox. A reconciler differential (random edits checked after every update against a fresh render, the DOM map and mutation events) over 100,000 sessions without slots gives the same results as main, and over 40,000 sessions with named slots finds no failure this change adds; together with #9773 the sessions with named slots show none of this class. ## Review Independent review with gpt-6.1-sol over two rounds (the last clean); both accepted mutants it found are rejected by the tests here.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks, this change looks good. Recording the moved value's old slot container in $createNode and returning it from $slotContainerForKey makes the old slot's cleanup remove that container instead of the element the value moved into, and the new matrix in LexicalSlotValueMovedOut.test.ts tells the old behaviour from the new one by asserting the DOM against a fresh render.
Two optional, non-blocking suggestions:
- Pin the slot-swap path as well (
packages/lexical/src/LexicalReconciler.ts, the container-reuse branch of$reconcileSlotChildrenat lines 632-633). The same record also fixes swapping two values between slots in one update,{s0: V, s1: W}to{s0: W, s1: V}on one host, or between two hosts when the later host renders first; without this change one value's element ends up outside the editor DOM. A swap case inLexicalSlotValueMovedOut.test.tswould keep that from regressing. - Qualify the
NotFoundErrorsentence in the changeset (.changeset/slot-value-moved-out-container.md, lines 7 and 9). Without the fix, a later insert next to a plain element (updateDOMreturning false) does not throw; the element just stays out of the DOM.NotFoundErrorappears only when that element'supdateDOMreturns true. AGENTS.md asks theVerify:paragraph to say what to test, so either add 'for an element whoseupdateDOMreturns true' or keep only the 'matches a fresh render' check, which already tells old from new.
|
|
||
| When a slot's value moved, in one update, into an element rendered before its old host (as a child or into a slot of that element, which can be new, existing, or built anew because its `updateDOM` returns true) while its slot was removed, given another value, or its host was removed, the reconciler had already moved the value's DOM when it cleaned up the old slot, and it removed the value's new parent instead of the old slot container. The element it moved into lost its DOM (the editor state was right, the DOM no longer matched it), and the next update that inserted a node next to that element threw `NotFoundError`. The reconciler now keeps the old slot container of a slot value whose DOM it moves and removes that container. Named slots are experimental. | ||
|
|
||
| Verify: in a mounted editor, with an element `R` before an element `A` whose slot `s0` holds `B`, in one update remove the slot and append `B` to `R`; `R`'s DOM stays in the editor and matches a fresh render, and inserting a paragraph before `R` in a later update does not throw. |
There was a problem hiding this comment.
Optional: without the fix, inserting a paragraph before a plain R (updateDOM returning false) does not throw; R just stays out of the DOM. NotFoundError only appears when R's updateDOM returns true. Consider qualifying the throw check (and the matching sentence on line 7), or keeping only the 'matches a fresh render' check, which already tells old from new.
There was a problem hiding this comment.
Done in 606bd29: the changeset says the later NotFoundError happened only when that element's updateDOM returns true, and the Verify: paragraph checks the DOM against a fresh render.
| 'slot gets a new value', | ||
| ] as const; | ||
|
|
||
| describe('a slot value moved out of its slot in one update', () => { |
There was a problem hiding this comment.
Optional: the recorded container also fixes swapping two slot values in one update ({s0: V, s1: W} to {s0: W, s1: V} on one host, or between two hosts when the later host renders first), which goes through the container-reuse branch of $reconcileSlotChildren. A swap case here would pin that path.
There was a problem hiding this comment.
Done in 606bd29: three swap tests (two slots of one host; two hosts, both orders of operations). They fail on main and pass here.
… the old behaviour threw
## Description
Adds three tests to `LexicalSlotValueMovedOut.test.ts` for two slot values swapped in one update, between two slots of one host and between two hosts in both orders of operations: each value keeps its DOM inside the editor and the editor's DOM matches a fresh render. The recorded old slot container fixes these too. The changeset now says that the later `NotFoundError` happened only when the element the value moved into has an `updateDOM` that returns true, mentions swaps, and its `Verify:` paragraph checks the DOM against a fresh render. No source change.
## Test plan
### Before
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
AssertionError: expected false to be true // Object.is equality
Tests 29 failed | 30 passed (59)
```
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
Tests 59 passed (59)
```
There was a problem hiding this comment.
Thanks — this change looks good. Recording the moved value's old slot container in $createNode and returning it from $slotContainerForKey makes the old slot's cleanup remove that container instead of the element the value moved into, and the matrix in LexicalSlotValueMovedOut.test.ts tells the old behaviour from the new one by checking the DOM against a fresh render. The swap tests and the qualified NotFoundError sentence in the changeset address the earlier suggestions.
One optional, non-blocking suggestion:
- Three "new place first" cases run in the other order (
packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts, lines 181-187 and 207-209). For a new element with the host removed,tis inserted directly beforeaandais removed, so the root goes from[a, tail]to[t, tail].$reconcileNodeChildrendestroysabefore it createst(LexicalReconciler.tslines 2104-2117), so the old slot is cleaned up before the value moves, and these three cases pass without the fix. The path is still covered for existing and rebuilt elements. Starting that branch from[x, a, tail]and insertingtbeforexmakes the new element render before the old host; that shape removest's DOM without the fix and passes with it.
| if (target === 'new element') { | ||
| t = new Host().append($text('t')); | ||
| if (order === 'new place first') { | ||
| a.insertBefore(t); |
There was a problem hiding this comment.
Optional: for target === 'new element' with leaves === 'host removed', the root goes from [a, tail] to [t, tail], and $reconcileNodeChildren destroys a before it creates t, so these three cases run the old-host-first order and pass without the fix. Starting this branch from [x, a, tail] and inserting t before x would make the new element render before the old host; that shape removes t's DOM without the fix and passes with it.
…is removed in the same update
## Description
Adds a test to `LexicalSlotValueMovedOut.test.ts`: from `[x, A, tail]`, where `A`'s slot holds a value, one update inserts a new element before `x`, moves the value into it and removes `A`. The new element renders before the old host, so without the fix its DOM was removed. (In the matrix's "new element, host removed" cases the old host is destroyed before the new element is created, so those pass on main too.) No source change.
## Test plan
### Before
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
× a new element rendered before the old host that is removed in the same update keeps its DOM
Tests 30 failed | 30 passed (60)
```
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
Tests 60 passed (60)
```
|
The optional item from the latest review is done in 87ac333: a test from |
There was a problem hiding this comment.
Thanks — this change looks good. Recording the moved value's old slot container in $createNode and returning it from $slotContainerForKey makes the old slot's cleanup remove that container instead of the element the value moved into, and the new matrix tells the old behaviour from the new one by comparing the DOM with a fresh render. The standalone test for a new element rendered before a removed host covers the earlier suggestion.
A few optional, non-blocking suggestions:
- Test the unwrap shape (
packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts). Moving a slot value into its host's own parent right before the host ($removeSlot(a, 's0'); a.insertBefore(value)in one update) is the most damaging form of this bug: onmainthe cleanup removes the host's parent, which for a top-level host is the editor's root element, so the whole editor leaves the page. The fix handles it, but every matrix case moves the value into aHost, so one case of that shape would pin it. - Three matrix cases still run in the other order (lines 184 and 208). For 'new place first, into a new element …, host removed',
tis inserted directly beforeaandais removed, so the root goes from[a, tail]to[t, tail], the same shape as the 'old host first' cases, and these three pass onmain. Starting that branch from[x, a, tail], as the new standalone test does, would also cover moving the value into the new element's slot. Relatedly, for a new element the 'replaced slot value' case fills an empty slot, because thetthat heldoldin the first update is never attached (lines 154-163). - Changeset scope (
.changeset/slot-value-moved-out-container.md, line 7). CONTRIBUTING.md asks for the entry to be written for someone who uses Lexical; 'into an element rendered before its old host' does not read as covering the unwrap case, so a clause such as "or next to the host in its own parent, where the editor's root element was removed" would help someone who hit that symptom find it.
|
|
||
| const ORDERS = ['new place first', 'old host first'] as const; | ||
| const TARGETS = ['new element', 'existing element', 'rebuilt element'] as const; | ||
| const INTO = ['child', 'new slot', 'replaced slot value'] as const; |
There was a problem hiding this comment.
Optional: every destination here is a Host, so the unwrap shape ($removeSlot(a, 's0'); a.insertBefore(value) in one update) is not covered. On main its cleanup removes the host's parent, which for a top-level host is the editor's root element; this fix handles it, and one case of that shape would pin it.
| if (target === 'new element') { | ||
| t = new Host().append($text('t')); | ||
| if (order === 'new place first') { | ||
| a.insertBefore(t); |
There was a problem hiding this comment.
Optional: with leaves === 'host removed' this branch goes from [a, tail] to [t, tail], the same shape as the 'old host first' cases, because a is destroyed before t is created; these three cases pass on main. Starting from [x, a, tail] and inserting t before x, as the standalone test at the end of the file does, would make them test the new-place-first order, slot variants included.
|
|
||
| [fix] A named slot's value moved out of its slot no longer makes the reconciler remove the DOM of the element it moved into | ||
|
|
||
| When a slot's value moved, in one update, into an element rendered before its old host (as a child or into a slot of that element, which can be new, existing, or built anew because its `updateDOM` returns true) while its slot was removed, given another value, or its host was removed, the reconciler had already moved the value's DOM when it cleaned up the old slot, and it removed the value's new parent instead of the old slot container. The element it moved into lost its DOM (the editor state was right, the DOM no longer matched it), and when that element's `updateDOM` returns true, the next update that inserted a node next to it threw `NotFoundError`. Swapping two slot values in one update could also leave one value's DOM outside the editor. The reconciler now keeps the old slot container of a slot value whose DOM it moves and removes that container. Named slots are experimental. |
There was a problem hiding this comment.
Optional: 'into an element rendered before its old host' does not read as covering a value unwrapped next to its host in the host's own parent, where the removed 'new parent' was that parent (the editor's root element for a top-level host). A clause naming that case would help users who hit it find this entry.
…ost, and name that symptom in the changeset
## Description
Adds a test to `LexicalSlotValueMovedOut.test.ts`: a top-level host's slot value is moved right before the host in one update. Without the fix the old slot's cleanup removed the host's parent, here the editor's root element, so the whole editor left the page. The changeset now names this shape and its symptom. No source change.
## Test plan
### Before
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
Tests 31 failed | 30 passed (61)
```
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts
Tests 61 passed (61)
```
|
Optional items from the latest review: the unwrap shape is taken in 1f7ebbf (a test where a top-level host's slot value moves right before the host; on main the editor's root element left the page) and the changeset and description now name that symptom. The matrix's three "new element, host removed" cases stay as they are: the standalone test from |
There was a problem hiding this comment.
This looks good to me. Recording the old slot container in $createNode before the moved value's DOM is reinserted (packages/lexical/src/LexicalReconciler.ts:734-739), and preferring that record in $slotContainerForKey (:591-594), makes the old slot's cleanup remove the right element in every shape I tried: a value moved into an earlier element as a child or into one of its slots, the unwrap right before a top-level host, swaps and rotations across slots and hosts, a host moved in the same update, and restoring the states on either side of the move. The record lives for a single $reconcileRoot call, nothing public changes, and the changeset follows .changeset/README.md, with the Verify: paragraph AGENTS.md asks for on a fix that changes output.
Thanks for adding the unwrap test and naming that symptom in the changeset. Leaving the three "new element, host removed" matrix cases as they are is fine, since the standalone test covers that order.
Optional, non-blocking
packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts:156-163: when the target is a new element, thetthat getsoldins1is never attached, and the second update builds a fresht(:182). So the three "new element as a replaced slot value" cases fill an empty slot, which is what the "new slot" cases already test. Puttingoldinto the new element'ss1inside the second update would make them test a replacement.
| t.append($text('t')); | ||
| if (into === 'replaced slot value') { | ||
| const old = $text('replaced'); | ||
| $setSlot(t as never, 's1' as never, old); |
There was a problem hiding this comment.
Optional, non-blocking: when target === 'new element', this t is never attached ($getRoot().clear().append(a, tail) at line 163), and the second update builds a fresh t at line 182, so the three "new element as a replaced slot value" cases fill an empty s1, the same as "new slot". Setting old into the new element's s1 inside the second update would make them test a replacement.
There was a problem hiding this comment.
Right: for a new element the old value sits on a host that is never attached, so those three cases fill an empty slot, as the "new slot" cases do. A replacement in an existing or rebuilt element is covered by its "replaced slot value" cases. Left as is, to keep the head that was verified.
…er makes the reconciler remove the DOM of the element it moved into (meta#9778)
Description
When a named slot's value moved, in one update, into an element rendered before its old host (as a child or into a slot of that element; the element can be new, existing, or built anew because its
updateDOMreturns true), or next to the host in the host's own parent (then that parent lost its DOM; for a top-level host the editor's root element left the page), while its slot was removed, given another value, or its host was removed, the reconciler removed the wrong element.$slotContainerForKeyfinds a slot's container as the parent of the value's DOM in the previous DOM map; by the time the old host's slot is cleaned up, creating or reconciling the new place has already moved that DOM (the reconciler reuses a moved node's DOM), so its parent is the new place. The cleanup removed the element the value moved into, or that element's new slot container: the editor state was right, but that element's DOM was gone, its DOM-map entry pointed at a detached element, and, when that element'supdateDOMreturns true, the next update that inserted a node next to it threwNotFoundError: The child can not be found in the parent.Swapping two slot values in one update could also leave one value's DOM outside the editor.Now, when
$createNodemoves the DOM of a node that was a slot value in the previous state, it first records the DOM's parent, which is the value's old slot container, for the rest of that reconciliation, and$slotContainerForKeyreturns the recorded container for that key. The old slot's cleanup removes the old container. Nothing changes when the new place is rendered after the old host (the cleanup already ran with the right container), for slot values that stay, or for editors without slots. Named slots are experimental.Closes #9758
Changeset
.changeset/slot-value-moved-out-container.md(lexicalpatch,[fix]).Test plan
packages/lexical/src/__tests__/unit/LexicalSlotValueMovedOut.test.ts(new, 61 tests): the new place before or after the old host; a new, an existing and a rebuilt element; moved in as a child, into a new slot, or replacing a slot's value; the old slot removed, given a new value, or its host removed. Each checks that nothing throws, that every node's element is in the editor's DOM, that the DOM equals a fresh render, that the moved value reports neitherdestroyednorcreated, that a later update inserting next to the new place works, and, where the element is not one built anew, that no removed node stays in the DOM map. Two more tests: a decorator host that placed its slot container outside its own DOM (the container is removed and the element the value moved into keeps its DOM), a decorator slot value moved into an earlier element (that element and the value keep their DOM), two slot values swapped between two slots of one host or between two hosts (each keeps its DOM, the DOM matches a fresh render), a new element rendered before an old host that is removed in the same update (it keeps its DOM), and a slot value moved right before its top-level host (without the fix the editor's root element left the page).Before
After
Every package's unit tests pass with no change from main, also together with #9773. The rich-text end-to-end tests for tables, copy and paste, lists, selection, headings, markdown, mutations and the four slot specs, and the collab-y specs including slot convergence, give the same results as main in Chromium and Firefox. A reconciler differential (random edits checked after every update against a fresh render, the DOM map and mutation events) over 100,000 sessions without slots gives the same results as main, and over 40,000 sessions with named slots finds no failure this change adds; together with #9773 the sessions with named slots show none of this class.
Review
Independent review with gpt-6.1-sol over two rounds (the last clean); both accepted mutants it found are rejected by the tests here.