Skip to content

[lexical] Bug Fix: a parsed EditorState carries the _slotsUsed flag - #9042

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:slot-parse-slotsused
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:slot-parse-slotsused

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

$setSlot latches "this document uses slots" so the slot-gated paths know to
run. It marks the pending editor state:

function $setSlotsUsed() {
  const editor = $getEditor();
  editor._slotsUsed = true;
  if (editor._pendingEditorState) {
    editor._pendingEditorState._slotsUsed = true;
  }
}

Inside editor.update() that is correct, because activeEditorState === editor._pendingEditorState. But $setSlot also runs during
parseEditorState ($parseSerializedNodeImpl), and that function builds a
detached EditorState and assigns it to activeEditorState while leaving
editor._pendingEditorState untouched:

const editorState = createEmptyEditorState();
...
activeEditorState = editorState;   // _pendingEditorState is never touched

So a parsed state never receives the flag, and whatever unrelated state happens
to be pending may receive it instead.

EditorState._slotsUsed documents itself as "True if this EditorState or the
LexicalEditor that created it has ever used slots", and
LexicalEditor.setEditorState is the only way an editor learns that an
incoming state uses slots:

this._slotsUsed = this._slotsUsed || editorState._slotsUsed;

An editor that receives a parsed slot document therefore keeps
_slotsUsed === false and silently skips every gated path —
$clampRangeSelectionToSlotFrame (so a RangeSelection may straddle a slot
boundary), the slot-island re-render in setEditable, and the slot walks in
LexicalSelection, LexicalUtils and LexicalMutations. cloneEditorState
and EditorState.clone both propagate the wrong false onward.

This marks the active editor state, which is the one actually being built in
both cases. The existing test setEditorState latches _slotsUsed when the state contains slots covers the update() path, where the two are the same object;
the parse path was the gap.

The helper moves to LexicalUtils because LexicalSlot cannot import
LexicalUpdates directly — that edge forms a module-initialization cycle
(LexicalSlot -> LexicalUpdates -> LexicalSelection -> LexicalTabNode -> LexicalTextNode -> LexicalNode still undefined), which is the same reason this
file already avoids a runtime LexicalNode import. LexicalUtils already
imports getActiveEditorState and is already imported by LexicalSlot.

Test plan

New unit test packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts.
The third case has one editor parse and a different editor receive the state:
the parsing editor sets its own _slotsUsed as a side effect of $setSlot, so
only a receiving editor shows whether the flag travelled with the state. The
update() case and a slotless document are controls and pass before and after.

Before

$ npx vitest run packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts

     × a parsed state carries the flag 4ms
     × setEditorState latches the flag on an editor that did not parse it 1ms

AssertionError: expected false to be true // Object.is equality

      Tests  2 failed | 2 passed (4)

After

$ npx vitest run packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts

      Tests  4 passed (4)

$ npx vitest run packages/lexical/src/__tests__

 Test Files  58 passed (58)
      Tests  943 passed | 1 skipped (944)

@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@mayrang

mayrang commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Confirmed the bug on main (two of the four new tests fail) and all four pass on this branch. The core fix — keying off getActiveEditorState() instead of _pendingEditorState — is correct; parseEditorState sets activeEditorState to the state being built before the parse callback runs, so it works for both the update() path and the parse path. Two notes inline.

* pending would miss the parsed state entirely (and could stamp the flag onto
* an unrelated pending state).
*/
export function $markSlotsUsed(): void {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$markSlotsUsed was inserted between $removeFromParent's JSDoc (lines 488–500) and its function declaration (line 519). TSDoc binds only the immediately preceding block to the next declaration, so $removeFromParent ends up with no documentation. Move $markSlotsUsed (with its own JSDoc) to after $removeFromParent's closing brace.

);
}

function seedSlotDocument(editor: ReturnType<typeof buildEditor>) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IosEnterAcceptanceBoundary.test.ts and LexicalDOMCapturedSelection.test.ts (unit-test peers in this package) annotate helper parameters as LexicalEditorWithDispose — the concrete return type of buildEditorFromExtensions. Consider import type {LexicalEditorWithDispose} from '@lexical/extension' and changing the parameter type accordingly.

## Description

`$setSlot` latches "this document uses slots" so the slot-gated paths know to
run. It marks the pending editor state:

```js
function $setSlotsUsed() {
  const editor = $getEditor();
  editor._slotsUsed = true;
  if (editor._pendingEditorState) {
    editor._pendingEditorState._slotsUsed = true;
  }
}
```

Inside `editor.update()` that is correct, because `activeEditorState ===
editor._pendingEditorState`. But `$setSlot` also runs during
`parseEditorState` (`$parseSerializedNodeImpl`), and that function builds a
*detached* `EditorState` and assigns it to `activeEditorState` while leaving
`editor._pendingEditorState` untouched:

```js
const editorState = createEmptyEditorState();
...
activeEditorState = editorState;   // _pendingEditorState is never touched
```

So a parsed state never receives the flag, and whatever unrelated state happens
to be pending may receive it instead.

`EditorState._slotsUsed` documents itself as "True if this EditorState or the
LexicalEditor that created it has ever used slots", and
`LexicalEditor.setEditorState` is the only way an editor learns that an
incoming state uses slots:

```js
this._slotsUsed = this._slotsUsed || editorState._slotsUsed;
```

An editor that receives a parsed slot document therefore keeps
`_slotsUsed === false` and silently skips every gated path —
`$clampRangeSelectionToSlotFrame` (so a RangeSelection may straddle a slot
boundary), the slot-island re-render in `setEditable`, and the slot walks in
`LexicalSelection`, `LexicalUtils` and `LexicalMutations`. `cloneEditorState`
and `EditorState.clone` both propagate the wrong `false` onward.

This marks the *active* editor state, which is the one actually being built in
both cases. The existing test `setEditorState latches _slotsUsed when the state
contains slots` covers the `update()` path, where the two are the same object;
the parse path was the gap.

The helper moves to `LexicalUtils` because `LexicalSlot` cannot import
`LexicalUpdates` directly — that edge forms a module-initialization cycle
(`LexicalSlot -> LexicalUpdates -> LexicalSelection -> LexicalTabNode ->
LexicalTextNode -> LexicalNode` still undefined), which is the same reason this
file already avoids a runtime `LexicalNode` import. `LexicalUtils` already
imports `getActiveEditorState` and is already imported by `LexicalSlot`.

## Test plan

New unit test `packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts`.
The third case has one editor parse and a *different* editor receive the state:
the parsing editor sets its own `_slotsUsed` as a side effect of `$setSlot`, so
only a receiving editor shows whether the flag travelled with the state. The
`update()` case and a slotless document are controls and pass before and after.

### Before

```
$ npx vitest run packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts

     × a parsed state carries the flag 4ms
     × setEditorState latches the flag on an editor that did not parse it 1ms

AssertionError: expected false to be true // Object.is equality

      Tests  2 failed | 2 passed (4)
```

### After

```
$ npx vitest run packages/lexical/src/__tests__/unit/SlotParseEditorState.test.ts

      Tests  4 passed (4)

$ npx vitest run packages/lexical/src/__tests__

 Test Files  58 passed (58)
      Tests  943 passed | 1 skipped (944)
```
@LeSingh1
LeSingh1 force-pushed the slot-parse-slotsused branch from faab6bb to 6017620 Compare August 9, 2026 20:42
@LeSingh1

LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Both addressed in 6017620 — thanks for the careful read.

The JSDoc one was a real defect I introduced: inserting $markSlotsUsed between $removeFromParent's doc block and its declaration left $removeFromParent undocumented, since TSDoc binds only the immediately preceding block. Moved $markSlotsUsed and its own doc above, so the original doc sits directly on its declaration again.

Also switched the helper to LexicalEditorWithDispose to match IosEnterAcceptanceBoundary.test.ts and LexicalDOMCapturedSelection.test.ts.

4/4 tests still pass.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9052 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants