Skip to content

[lexical-yjs] Bug Fix: Collab v2 pairs a new row with a look-alike sibling and remaps only that row - #9374

Closed
zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v2-table-row-mapping
Closed

zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v2-table-row-mapping

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Description

Current behavior (collab v2 only; v1 is not affected):

$updateYFragment matches a parent's Yjs children against its Lexical
children from both ends. A pair is accepted when mappedIdentity holds or,
failing that, when $equalYTypePNode says the two subtrees are structurally
equal, in which case only that one shared type is remapped. mappedIdentity
compares objects, and Lexical clones a node whenever its __prev/__next
pointers change, so inserting a row makes its neighbours fall through to the
structural check. A freshly inserted empty row is structurally equal to an
existing empty row next to it, so the scan pairs the new row with the existing
row's shared type:

  • The Yjs insert lands at the wrong index. A peer's concurrent edit in the
    existing row then shows up in the new row.
  • Only the row is remapped, not its cells and paragraphs, which stay mapped to
    the other row's Lexical nodes. A later remote edit in that row (with no
    concurrency at all) is applied to a different row on the peer that inserted
    the rows, and with three clients the stale entries end up pointing at removed
    nodes: "Lexical node does not exist in active editor state".

The same applies to any run of look-alike siblings, e.g. empty paragraphs.

New behavior:

  • $canPairStructurally: a Yjs child and a Lexical child may only be paired
    structurally if neither of them is still mapped to a different sibling that
    is present on the other side. Pairings the existing mapping does not
    contradict are unchanged.
  • $remapEqualSubtree: after a structural match the descendants are remapped
    positionally as well. If the two child lists do not line up one to one
    (different lengths or node kinds), the descendants are left alone instead of
    being paired by position.

mappedIdentity is unchanged.

Test plan

New tests in packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts,
run against both bindings with a small headless N-client harness
(collabTestHarness.ts) that mirrors useYjsCollaboration(V2) and can take
clients offline to model concurrent edits:

  • A inserts two rows (synced), then B types in cell (1,1).
  • 3-client variant of the same.
  • A fuzz-shrunk 3-client sequence of row/column inserts and typing on a 2x2
    table.
  • A inserts an empty row above ‖ B types in the empty row below.
  • A inserts an empty paragraph between empty paragraphs ‖ B types in the next
    one.

Before

$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts
 ❯ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts (10 tests | 5 failed) 429ms
   ✓ table sync (v1) (5)
     ✓ A inserts rows, then B types in a cell (no concurrency) 75ms
     ✓ rows inserted by two clients, then a third types (no concurrency) 62ms
     ✓ fuzz-shrunk 3-client sequential row and column edits 70ms
     ✓ row insert above ‖ peer types in an empty row below 21ms
     ✓ paragraph inserted between empty paragraphs ‖ peer types in the next one 17ms
   ❯ table sync (v2) (5)
     × A inserts rows, then B types in a cell (no concurrency) 45ms
     × rows inserted by two clients, then a third types (no concurrency) 46ms
     × fuzz-shrunk 3-client sequential row and column edits 54ms
     × row insert above ‖ peer types in an empty row below 20ms
     × paragraph inserted between empty paragraphs ‖ peer types in the next one 17ms

 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > A inserts rows, then B types in a cell (no concurrency)
AssertionError: expected [ Array(5) ] to deeply equal [ [ '', '', '' ], …(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > rows inserted by two clients, then a third types (no concurrency)
AssertionError: expected [ [ '', '', '' ], …(4) ] to deeply equal [ [ '', '', '' ], …(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > fuzz-shrunk 3-client sequential row and column edits
AssertionError: expected [ Array(1) ] to deeply equal []
+   "Lexical node does not exist in active editor state (key 909, type paragraph). ..."
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > row insert above ‖ peer types in an empty row below
AssertionError: expected [ [ '', '', '' ], …(3) ] to deeply equal [ Array(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > paragraph inserted between empty paragraphs ‖ peer types in the next one
AssertionError: expected [ 'top', '', 'x', '', '' ] to deeply equal [ 'top', '', '', 'x', '' ]

 Test Files  1 failed (1)
      Tests  5 failed | 5 passed (10)

After

$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts (10 tests) 418ms
 Test Files  1 passed (1)
      Tests  10 passed (10)

$ npx vitest --project unit --no-watch packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx packages/lexical-table
 Test Files  38 passed (38)
      Tests  393 passed (393)

A sequential random table-edit fuzz (3 clients, 12 row/column insert/delete
and typing ops per seed, sync after each op; not included in this change)
went from 69 diverged / 58 with errors out of 150 seeds on v2 to 0 / 0. v1 is
0 / 0 before and after.

pnpm run test-unit: 393 files, 9547 tests passed (1 skipped).
pnpm run tsc, pnpm run flow (0 errors), ESLint and Prettier on the
changed files pass.

Not exercised: real browsers and a real network provider (all runs are
headless with in-memory update delivery). The bail-out in
$remapEqualSubtree for child lists that do not line up is not reachable
today, because $equalYTypePNode already requires equal child lists; it
guards against $equalYTypePNode later skipping children (e.g. node types
unknown to this client).

If #9379 (unknown node types) lands first, $remapEqualSubtree must compare
the visible children (visibleChildren(binding, ytype).children) instead of
ytype.toArray() when rebasing.

@vercel

vercel Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Oct 22, 2026 2:15pm UTC
lexical-playground Ready Ready Preview Oct 22, 2026 2:15pm UTC

Request Review

…bling and remaps only that row

## Description

Current behavior (collab v2 only; v1 is not affected):

`$updateYFragment` matches a parent's Yjs children against its Lexical
children from both ends. A pair is accepted when `mappedIdentity` holds or,
failing that, when `$equalYTypePNode` says the two subtrees are structurally
equal, in which case only that one shared type is remapped. `mappedIdentity`
compares objects, and Lexical clones a node whenever its `__prev`/`__next`
pointers change, so inserting a row makes its neighbours fall through to the
structural check. A freshly inserted empty row is structurally equal to an
existing empty row next to it, so the scan pairs the new row with the existing
row's shared type:

- The Yjs insert lands at the wrong index. A peer's concurrent edit in the
  existing row then shows up in the new row.
- Only the row is remapped, not its cells and paragraphs, which stay mapped to
  the other row's Lexical nodes. A later remote edit in that row (with no
  concurrency at all) is applied to a different row on the peer that inserted
  the rows, and with three clients the stale entries end up pointing at removed
  nodes: "Lexical node does not exist in active editor state".

The same applies to any run of look-alike siblings, e.g. empty paragraphs.

New behavior:

- `$canPairStructurally`: a Yjs child and a Lexical child may only be paired
  structurally if neither of them is still mapped to a different sibling that
  is present on the other side. Pairings the existing mapping does not
  contradict are unchanged.
- `$remapEqualSubtree`: after a structural match the descendants are remapped
  positionally as well. If the two child lists do not line up one to one
  (different lengths or node kinds), the descendants are left alone instead of
  being paired by position.

`mappedIdentity` is unchanged.

## Test plan

New tests in `packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts`,
run against both bindings with a small headless N-client harness
(`collabTestHarness.ts`) that mirrors `useYjsCollaboration(V2)` and can take
clients offline to model concurrent edits:

- A inserts two rows (synced), then B types in cell (1,1).
- 3-client variant of the same.
- A fuzz-shrunk 3-client sequence of row/column inserts and typing on a 2x2
  table.
- A inserts an empty row above ‖ B types in the empty row below.
- A inserts an empty paragraph between empty paragraphs ‖ B types in the next
  one.

### Before

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts
 ❯ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts (10 tests | 5 failed) 429ms
   ✓ table sync (v1) (5)
     ✓ A inserts rows, then B types in a cell (no concurrency) 75ms
     ✓ rows inserted by two clients, then a third types (no concurrency) 62ms
     ✓ fuzz-shrunk 3-client sequential row and column edits 70ms
     ✓ row insert above ‖ peer types in an empty row below 21ms
     ✓ paragraph inserted between empty paragraphs ‖ peer types in the next one 17ms
   ❯ table sync (v2) (5)
     × A inserts rows, then B types in a cell (no concurrency) 45ms
     × rows inserted by two clients, then a third types (no concurrency) 46ms
     × fuzz-shrunk 3-client sequential row and column edits 54ms
     × row insert above ‖ peer types in an empty row below 20ms
     × paragraph inserted between empty paragraphs ‖ peer types in the next one 17ms

 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > A inserts rows, then B types in a cell (no concurrency)
AssertionError: expected [ Array(5) ] to deeply equal [ [ '', '', '' ], …(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > rows inserted by two clients, then a third types (no concurrency)
AssertionError: expected [ [ '', '', '' ], …(4) ] to deeply equal [ [ '', '', '' ], …(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > fuzz-shrunk 3-client sequential row and column edits
AssertionError: expected [ Array(1) ] to deeply equal []
+   "Lexical node does not exist in active editor state (key 909, type paragraph). ..."
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > row insert above ‖ peer types in an empty row below
AssertionError: expected [ [ '', '', '' ], …(3) ] to deeply equal [ Array(4) ]
 FAIL  |unit| .../SyncV2Tables.test.ts > table sync (v2) > paragraph inserted between empty paragraphs ‖ peer types in the next one
AssertionError: expected [ 'top', '', 'x', '', '' ] to deeply equal [ 'top', '', '', 'x', '' ]

 Test Files  1 failed (1)
      Tests  5 failed | 5 passed (10)
```

### After

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV2Tables.test.ts (10 tests) 418ms
 Test Files  1 passed (1)
      Tests  10 passed (10)

$ npx vitest --project unit --no-watch packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx packages/lexical-table
 Test Files  38 passed (38)
      Tests  393 passed (393)
```

A sequential random table-edit fuzz (3 clients, 12 row/column insert/delete
and typing ops per seed, sync after each op; not included in this change)
went from 69 diverged / 58 with errors out of 150 seeds on v2 to 0 / 0. v1 is
0 / 0 before and after.

`pnpm run test-unit`: 393 files, 9547 tests passed (1 skipped).
`pnpm run tsc`, `pnpm run flow` (0 errors), ESLint and Prettier on the
changed files pass.

Not exercised: real browsers and a real network provider (all runs are
headless with in-memory update delivery). The bail-out in
`$remapEqualSubtree` for child lists that do not line up is not reachable
today, because `$equalYTypePNode` already requires equal child lists; it
guards against `$equalYTypePNode` later skipping children (e.g. node types
unknown to this client).

If #9379 (unknown node types) lands first, `$remapEqualSubtree` must compare
the visible children (`visibleChildren(binding, ytype).children`) instead of
`ytype.toArray()` when rebasing.
@zurfyx
zurfyx force-pushed the zurfyx/collab-v2-table-row-mapping branch from ebfd6a9 to 1ae97b3 Compare October 22, 2026 14:14
zurfyx added a commit that referenced this pull request Oct 22, 2026
…collaborator does not register

## Description

When collaborators run builds with different node classes (for example while a
new node type rolls out), a peer receives Yjs content whose type it has not
registered.

Current behavior:

- Collab v2 throws `$createOrUpdateNodeFromYElement: Node <type> is not
  registered` (or `$createTextNodesFromYText: ...` for a custom TextNode type)
  and never shows the content. The next local edit on that peer diffs its
  Lexical tree against Yjs, finds the unknown element missing, and deletes it
  from the shared document for everyone.
- Collab v1 throws `Node <type> is not registered`. The content stays in Yjs,
  but the throw aborts the whole remote update, so unrelated content in the
  same update (an edit to another paragraph, a new paragraph) is missed, and
  the peer's child offsets no longer match Yjs, so its later edits can land on
  the wrong node.

New behavior (both bindings): content of an unregistered node type is opaque.
It is kept in Yjs untouched, is not materialized into Lexical, and is never
deleted, rewritten, or reordered by the peer that cannot show it. Changes made
inside it by peers that do register the type keep syncing between them.

Each unregistered type is reported once per editor through the editor's
`onWarn` handler (the handler for conditions the editor has recovered from),
so applications can route it to telemetry in production. The report happens
while a remote change is being applied, which must not be aborted, so a
handler that throws (the default one does in development) is downgraded to a
`console.warn`: development builds still get a visible warning, and the
default production handler warns once per type.

- v2: an unregistered `XmlElement` is skipped when building the Lexical tree.
  The Lexical -> Yjs diff (`$updateYFragment`, `$equalYTypePNode`,
  `$computeChildEqualityFactor`) only compares the visible children; deletes go
  through the real indices so opaque siblings survive, and inserts go right
  after the preceding visible child (so the second half of a split stays next
  to its first half). Remote events inside opaque content are ignored. An
  opaque slot value is never deleted, and a stale local occupant of that slot
  name is dropped.
- v1: an unregistered shared type still gets a collab node, so the parent's
  child offsets stay in step with Yjs, but no Lexical node.
  `syncChildrenFromYjs` skips it, and `syncChildrenFromLexical` translates
  Lexical child indices around it (same insert rule as v2) and never deletes
  it. Remote events inside opaque content are ignored. Opaque slot values are
  handled as in v2.
- Unknown inline content among text (a custom TextNode type, or an unknown
  inline element between text nodes): skipping only that node would leave the
  text around it adjacent in Lexical, where normalization (v1) or the text-run
  diff (v2) joins it and writes the join back over the hidden node, moving or
  re-attributing text. Instead the whole containing element (e.g. the
  paragraph) is hidden on that peer while it holds such content, and shown
  again, with any edits made meanwhile, once the content is gone.

Whether an element of a registered type holds unknown content is cached per
element: it only depends on the element's own children, and only a remote
change can introduce an unknown type, so it is computed when the element is
first read from Yjs and refreshed when a remote change reaches that element.
Elements this editor created are never scanned. A local keystroke therefore
costs about the same as on `main`, with or without unknown content in the
document (benchmark below).

Known gaps:

- Remote cursors: element-offset positions (`createRelativePositionV2`,
  `$getNodeAndOffsetV2`, and the v1 equivalent) do not skip hidden siblings,
  so a remote caret next to hidden content can be drawn off by the number of
  hidden siblings before it. Content is not affected.
- Replacing a visible container through delete and recreate (for example a v2
  element whose type changes, or deleting the container) also drops the
  hidden content inside it, which the user on that client could not see.

No placeholder is rendered for hidden content yet; the peer that cannot show
it simply does not display it. That needs a registered placeholder node and is
left for a follow-up.

The collaboration FAQ gets a short section describing this behavior and the
`onWarn` report.

## Test plan

New `packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts`
wires two or three headless clients per binding (v1 and v2) the way
`useYjsCollaboration` does, each with its own node registry: the newer client
registers `CalloutNode` (block), `MentionNode` (custom TextNode) and an inline
element, the older one does not. The harness reports editor errors instead of
throwing (like an application would) and fails the test if any were reported.
Cases: the newer client inserts an unknown block and the older edits elsewhere;
one update carries an unknown block plus unrelated edits; edits inside the
block keep syncing; a late joiner; splits, removals and a full clear next to
the block; content added below a leading unknown block; concurrent offline
edits next to the block; nested unknown blocks; a paragraph with a custom
TextNode type or an unknown inline element; a paragraph that gains and then
loses an unknown inline node; an unknown slot value; one report per type
through a custom `onWarn` and through the default one.
Each case checks both Lexical trees and that both Yjs documents are identical
and still hold the unknown content.

### Before

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts
 FAIL  |unit| .../CollabUnknownNodeTypes.test.ts > collab v2: node types one client does not register > an unknown block survives an edit elsewhere by the older client
AssertionError: expected [ 'paragraph[text:alpha]', …(1) ] to deeply equal [ 'paragraph[text:alpha]', …(2) ]

- Expected
+ Received

  [
    "paragraph[text:alpha]",
-   "test-callout[paragraph[text:inside]]",
    "paragraph[text:omega!]",
  ]

 FAIL  |unit| .../CollabUnknownNodeTypes.test.ts > collab v1: node types one client does not register > the rest of an update that carries an unknown block is applied
 FAIL  |unit| .../CollabUnknownNodeTypes.test.ts > collab v2: node types one client does not register > the rest of an update that carries an unknown block is applied
AssertionError: expected [ 'paragraph[text:alpha]', …(1) ] to deeply equal [ 'paragraph[text:alpha beta]', …(2) ]

- Expected
+ Received

  [
-   "paragraph[text:alpha beta]",
+   "paragraph[text:alpha]",
    "paragraph[text:omega]",
-   "paragraph[text:tail]",
  ]

 FAIL  |unit| .../CollabUnknownNodeTypes.test.ts > collab v1: node types one client does not register > structural edits next to the unknown block do not move or delete it
AssertionError: expected [ 'paragraph[text:alpha]', …(2) ] to deeply equal [ 'paragraph[text:alpha]', …(2) ]

- Expected
+ Received

  [
    "paragraph[text:alpha]",
    "paragraph[text:a2]",
-   "test-callout[paragraph[text:inside]]",
+   "paragraph[text:omega]",
  ]

 FAIL  |unit| .../CollabUnknownNodeTypes.test.ts > collab v1: node types one client does not register > an unknown block survives an edit elsewhere by the older client
AssertionError: expected [ Array(1) ] to deeply equal []

- Expected
+ Received

- []
+ [
+   "Node test-callout is not registered",
+ ]

      Tests  28 failed (28)
```

### After

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts (28 tests)
 Test Files  1 passed (1)
      Tests  28 passed (28)

$ npx vitest --project unit --no-watch packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 Test Files  17 passed (17)
      Tests  198 passed (198)
```

`pnpm run test-unit`: 393 files, 9565 tests passed (1 skipped), including
`scripts/__tests__/unit/treeShakingSource.test.ts`.
`pnpm run tsc`, `pnpm run flow` (0 errors), ESLint and Prettier on the
changed files pass.

Keystroke benchmark (scratch test, not committed): two headless clients, a
document of 2,000 paragraphs, the client without the extra node classes
appends one character to paragraph 1,500, 300 timed keystrokes after 30
warm-up ones, `discrete` updates including the Yjs write. Mean ms per
keystroke:

| document | `main` v1 | this PR v1 | `main` v2 | this PR v2 |
|---|---|---|---|---|
| no unknown types | 6.4 | 7.0 | 5.0 | 4.9 |
| one unknown block | throws | 6.3 | throws | 4.8 |
| one paragraph with an unknown inline node | throws | 6.3 | throws | 4.8 |

Without the per-element cache, an earlier revision of this change measured
253 ms (block) and 368 ms (inline) per keystroke on v1, because every
keystroke rescanned the root's children.

Not exercised: real browsers and real network providers (headless editors with
in-memory update relay only), remote cursor rendering next to hidden content,
and the undo manager on the client that hides content.

If this lands after #9381 (which drops the `ContentFormat` import from
`SyncV2.ts`), restore that import when rebasing. If it lands after #9374,
switch `$remapEqualSubtree` to the visible children
(`visibleChildren(binding, ytype).children`).
@zurfyx
zurfyx marked this pull request as ready for review October 22, 2026 14:14
@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9374

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9374

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9374

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9374

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9374

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9374

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9374

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9374

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9374

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9374

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9374

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9374

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9374

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9374

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9374

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9374

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9374

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9374

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9374

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9374

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9374

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9374

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9374

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9374

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9374

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9374

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9374

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9374

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9374

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9374

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9374

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9374

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9374

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9374

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9374

commit: 1ae97b3

zurfyx added a commit that referenced this pull request Oct 22, 2026
… content of node types a collaborator does not register

## Description

When collaborators run builds with different node classes (for example while a
new node type rolls out), a peer receives Yjs content whose type it has not
registered.

Current behavior, which stays the default:

- Collab v2 throws `$createOrUpdateNodeFromYElement: Node <type> is not
  registered` (or `$createTextNodesFromYText: ...` for a custom TextNode type)
  and never shows the content. The next local edit on that peer diffs its
  Lexical tree against Yjs, finds the unknown element missing, and deletes it
  from the shared document for everyone.
- Collab v1 throws `Node <type> is not registered`. The content stays in Yjs,
  but the throw aborts the whole remote update, so unrelated content in the
  same update (an edit to another paragraph, a new paragraph) is missed, and
  the peer's child offsets no longer match Yjs, so its later edits can land on
  the wrong node.

Applications can depend on that error (for example to prompt "please reload"),
so this PR does not change it. It adds a binding option instead:

```ts
unregisteredNodeTypes?: 'throw' | 'keep'; // default 'throw'
```

on `CollaborationPlugin`, `CollaborationPluginV2__EXPERIMENTAL`,
`createYjsBinding`, `createBinding` (new optional last argument
`{unregisteredNodeTypes}`) and `createBindingV2__EXPERIMENTAL`, stored on the
binding as `BaseBinding.unregisteredNodeTypes` (optional, so object types that
mirror `BaseBinding` keep compiling). The type is exported as
`UnregisteredNodeTypes`; TS and Flow are updated.

- `'throw'` (default): exactly the behavior above, with the same error
  messages; nothing is reported to `onWarn`.
- `'keep'`: content of an unregistered node type is opaque. It is kept in Yjs
  untouched, is not materialized into Lexical, and is never deleted,
  rewritten, or reordered by the peer that cannot show it. Changes made inside
  it by peers that do register the type keep syncing between them. Each
  unregistered type is reported once per editor through the editor's `onWarn`
  handler (the handler for conditions the editor has recovered from), so
  applications can route it to telemetry in production. The report happens
  while a remote change is being applied, which must not be aborted, so a
  handler that throws (the default one does in development) is downgraded to
  a `console.warn`.

The names follow the code: the error says a type "is not registered", the
binding's helpers talk about unregistered types, and the content is "kept" in
the shared document. Existing binding options (`excludedProperties`,
`rootName`, `getXmlText`/`getXmlElement`) are nouns for what they configure,
and none of them uses either word.

How `'keep'` works:

- v2: an unregistered `XmlElement` is skipped when building the Lexical tree.
  The Lexical -> Yjs diff (`$updateYFragment`, `$equalYTypePNode`,
  `$computeChildEqualityFactor`) only compares the visible children; deletes go
  through the real indices so opaque siblings survive, and inserts go right
  after the preceding visible child (so the second half of a split stays next
  to its first half). Remote events inside opaque content are ignored. An
  opaque slot value is never deleted, and a stale local occupant of that slot
  name is dropped.
- v1: an unregistered shared type still gets a collab node, so the parent's
  child offsets stay in step with Yjs, but no Lexical node.
  `syncChildrenFromYjs` skips it, and `syncChildrenFromLexical` translates
  Lexical child indices around it (same insert rule as v2) and never deletes
  it. Remote events inside opaque content are ignored. Opaque slot values are
  handled as in v2.
- Unknown inline content among text (a custom TextNode type, or an unknown
  inline element between text nodes): skipping only that node would leave the
  text around it adjacent in Lexical, where normalization (v1) or the text-run
  diff (v2) joins it and writes the join back over the hidden node, moving or
  re-attributing text. Instead the whole containing element (e.g. the
  paragraph) is hidden on that peer while it holds such content, and shown
  again, with any edits made meanwhile, once the content is gone.

Whether an element of a registered type holds unknown content is cached per
element: it only depends on the element's own children, and only a remote
change can introduce an unknown type, so it is computed when the element is
first read from Yjs and refreshed when a remote change reaches that element.
Elements this editor created are never scanned. A local keystroke therefore
costs about the same as on `main`, with or without unknown content in the
document (benchmark below).

Known gaps:

- Remote cursors: element-offset positions (`createRelativePositionV2`,
  `$getNodeAndOffsetV2`, and the v1 equivalent) do not skip hidden siblings,
  so a remote caret next to hidden content can be drawn off by the number of
  hidden siblings before it. Content is not affected.
- Replacing a visible container through delete and recreate (for example a v2
  element whose type changes, or deleting the container) also drops the
  hidden content inside it, which the user on that client could not see.

No placeholder is rendered for hidden content yet; the peer that cannot show
it simply does not display it. That needs a registered placeholder node and is
left for a follow-up.

With `'throw'` none of this runs: no type is ever opaque
(`isOpaqueNodeType` returns false), so the hot paths take the
`hasUnregisteredNodeTypes` fast path, and an unregistered type reaches the
same `Node <type> is not registered` checks as on `main`.

The collaboration FAQ section "Collaborators with different node types"
documents both values.

## Test plan

- `lexical-yjs/.../CollabUnregisteredNodeTypesDefault.test.ts` (new): the
  default, v1 and v2. A client that registers more node types writes an
  unknown block, an unknown block plus unrelated edits in one update, a
  paragraph with a custom TextNode type, a paragraph with an unknown inline
  element, and an unknown slot value; the other client then edits. Checks the
  errors reported to `onError` (messages included), `onWarn`, both Lexical
  trees and both Yjs documents against values recorded on `main`. It runs
  unchanged on `main` (passes) and fails 10/10 against the previous revision
  of this PR.
- `lexical-yjs/.../CollabUnknownNodeTypes.test.ts`: the 28 existing cases
  now run with `unregisteredNodeTypes: 'keep'` (v1 through `createBinding`,
  v2 through `createBindingV2__EXPERIMENTAL`).
- `lexical-react/.../LexicalCollaborationPlugin.test.tsx`: both plugins, with
  the prop left out, `'throw'` and `'keep'`: a remote update with an unknown
  block reports `Node test-callout is not registered` (v1) /
  `$createOrUpdateNodeFromYElement: Node test-callout is not registered` (v2)
  to `onError`, or with `'keep'` shows the rest and warns once.

### Before

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts packages/lexical-yjs/src/__tests__/unit/CollabUnregisteredNodeTypesDefault.test.ts packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 FAIL  |unit| packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx > LexicalCollaborationPlugin > unregisteredNodeTypes > V1 plugin, unregisteredNodeTypes=keep
AssertionError: expected [ Array(1) ] to deeply equal []

- Expected
+ Received

- []
+ [
+   "Node test-callout is not registered",
+ ]

 FAIL  |unit| packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts > collab v2: node types one client does not register > an unknown block survives an edit elsewhere by the older client
AssertionError: expected [ 'paragraph[text:alpha]', …(1) ] to deeply equal [ 'paragraph[text:alpha]', …(2) ]

- Expected
+ Received

  [
    "paragraph[text:alpha]",
-   "test-callout[paragraph[text:inside]]",
    "paragraph[text:omega!]",
  ]

 Test Files  2 failed | 1 passed (3)
      Tests  30 failed | 25 passed (55)
```

The failures are the 28 `'keep'` cases and the two `'keep'` plugin cases; the
default cases (`CollabUnregisteredNodeTypesDefault.test.ts`, 10, and the
plugin's default and `'throw'` cases, 4) pass on `main`.

### After

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts packages/lexical-yjs/src/__tests__/unit/CollabUnregisteredNodeTypesDefault.test.ts packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/CollabUnregisteredNodeTypesDefault.test.ts (10 tests)
 ✓ |unit| packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx (17 tests)
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/CollabUnknownNodeTypes.test.ts (28 tests)
 Test Files  3 passed (3)
      Tests  55 passed (55)

$ npx vitest --project unit --no-watch packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 Test Files  18 passed (18)
      Tests  214 passed (214)
```

`pnpm run test-unit`: 394 files, 9581 tests passed (1 skipped), including
`scripts/__tests__/unit/treeShakingSource.test.ts`.
`pnpm run tsc`, `pnpm run tsc-test`, `pnpm run flow` (0 errors), ESLint and
Prettier on the changed files pass.
Playground e2e, Chromium, no retries: `Collaboration.spec.mjs`,
`SlotCollabConvergence.spec.mjs` and `Composition.spec.mjs` pass in
rich-text-with-collab (23) and rich-text-with-collab-v2 (23). The playground
uses the default, so this covers `'throw'`.

Keystroke benchmark from the previous revision (scratch test, not
committed). "This PR" is the `'keep'` behavior; with no unknown types,
`'throw'` runs the same code path. Two headless clients, a document of 2,000
paragraphs, the client without the extra node classes
appends one character to paragraph 1,500, 300 timed keystrokes after 30
warm-up ones, `discrete` updates including the Yjs write. Mean ms per
keystroke:

| document | `main` v1 | this PR v1 | `main` v2 | this PR v2 |
|---|---|---|---|---|
| no unknown types | 6.4 | 7.0 | 5.0 | 4.9 |
| one unknown block | throws | 6.3 | throws | 4.8 |
| one paragraph with an unknown inline node | throws | 6.3 | throws | 4.8 |

Without the per-element cache, an earlier revision of this change measured
253 ms (block) and 368 ms (inline) per keystroke on v1, because every
keystroke rescanned the root's children.

Not exercised: `'keep'` in a real browser or with a real network provider
(headless editors with in-memory update relay only), remote cursor rendering
next to hidden content, and the undo manager on the client that hides
content.

If this lands after #9381 (which drops the `ContentFormat` import from
`SyncV2.ts`), restore that import when rebasing. If it lands after #9374,
switch `$remapEqualSubtree` to the visible children
(`visibleChildren(binding, ytype).children`).
@zurfyx zurfyx closed this Oct 23, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 1ae97b3e Deployed Oct 22, 2026 by vercel[bot]
Preview – lexical-playground — 1ae97b3e Deployed Oct 22, 2026 by vercel[bot]
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