Skip to content

[lexical-selection][lexical-y] Bug Fix: Keep nested inline elements and OverflowNode in the block $wrapNodes wraps - #9618

Merged
zurfyx merged 4 commits into
mainfrom
zurfyx/wrapnodes-inline-content
Oct 31, 2026
Merged

zurfyx merged 4 commits into
mainfrom
zurfyx/wrapnodes-inline-content

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Description

$wrapNodes (deprecated in favour of $setBlocksType) moves the content of
each selected text's block into a new element. It took that block to be the
text's parent, or its grandparent when the parent is inline. So:

  • with the selection inside an inline element nested in another one (a link
    inside a comment mark, overlapping marks, any two levels), the outer inline
    element was taken for the block: the paragraph was split around it and the
    outer element (the mark) was dropped;
  • the character limit's OverflowNode, which is inline content although its
    isInline() is false, was taken for a block the same way;
  • with a range that starts there and ends later in the paragraph or in the
    next one, the insertion point was inside content that then moved into the
    new element, so the new element was inserted inside itself and the call
    never returned.

The block of a selected node is now its nearest ancestor that is an element
and not inline content, as the node structure normalization counts it, and
each node's block is decided before anything moves (otherwise a node deep in
a block whose content already moved finds the new element as its block). An
empty inline element in the selection is no longer taken for an empty block.
The paragraph becomes the new element with all of its inline content, as for
text directly in the paragraph.

Closes #9612

Changeset

.changeset/wrap-nodes-inline-content.md (@lexical/selection patch, [fix] with a Verify: paragraph).

Test plan

New packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts:
every nesting of up to three inline wrappers (link, mark, overflow) around
the inner text (39 shapes), the selection as a caret before the wrappers, a
caret inside the inner text, the whole paragraph, a range from the inner text
to the end, and a range over the paragraph and the next one; at the root and
inside a shadow root; with and without NodeStructureExtension; with and
without a wrapping element (1,170 cells). The 24 test.fails cells naming
#9612 in StructuralCommands.test.ts become plain tests.

Before

On main (4421e5b), the new test per selection group (each bounded at 60 s):

[caret before the wrappers] Tests  234 passed | 936 skipped (1170)
[caret inside the inner text] Tests  222 failed | 12 passed | 936 skipped (1170)
[the whole paragraph] Tests  216 failed | 18 passed | 936 skipped (1170)
[from the inner text to the end] no result: killed after 60 s (the run never returns)
[this paragraph and the next] no result: killed after 60 s (the run never returns)

For example mark > link, caret inside the inner text, in the root:

Expected: "quote("abc ",mark(link("inner"))," end"),paragraph("next")"
Received: "paragraph("abc ",quote(link("inner"))," end"),paragraph("next")"

StructuralCommands.test.ts with the #9612 markers removed:

Tests  24 failed | 3308 passed | 128 skipped (3460)
× $wrapNodes in a quote, caret inside it, on a paragraph with link/in-mark in root [unbound, NodeStructureExtension]

After

$ vitest --run --project unit packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts
 Test Files  2 passed (2)
      Tests  4502 passed | 128 skipped (4630)

Unit tests of lexical, lexical-selection, lexical-extension, lexical-list,
lexical-rich-text, lexical-y, lexical-mark, lexical-link, lexical-overflow,
lexical-table and lexical-utils: 23,260 passed, 166 expected fail, 0 failed.
pnpm run ci-check passes.

Follow-up commit (eec843f)

Two problems in the first commit, found in review, fixed in eec843f (its message has the Before/After output):

  • the walk to a selected node's block went past an inline shadow root, so with the caret in an empty inline shadow root inside a paragraph the quote replaced the paragraph outside it (main puts it in the scope). The walk now stops at the block container that holds the node; 40 new cells cover inline and block shadow roots (in the text, in a mark, at the root, as a named slot value), empty, holding a paragraph or text, three selections, with and without NodeStructureExtension. It also fixes a throw on main for text held directly by a block shadow root that is a named slot value;
  • each selected node's block was found by a walk of its own: about n²/2 parent lookups for content nested n inline elements deep (main about 8n). The walks now share what they found: about 2n (52/84/148/276 at depth 16/32/64/128). A new test fails on the quadratic version.

Follow-up commit (5be87e9)

Two more, found in review, fixed in 5be87e9 (Before/After in its message):

  • a block container whose content mixes inline content and blocks (a shadow root holding AA, paragraph(mark("BB")), CC) came out as AA CC BB after eec843f (main throws): inline content directly in a block container is now wrapped by runs, each block from its own content, so the order and every text node stay; 216 new cells;
  • a range from a list item's text to an element point on its list threw on main and on this branch: the selection is cleared while content moves and set again afterwards; 48 new cells.

Follow-up commit (56f91f5)

From review, fixed in 56f91f5 (Before/After in its message): a range that starts at the end of a block moved that block after the wrapped ones (main too); the selection now stays active as on main, with emptied nodes removed only once the new elements are in the document (no throw, nothing out of range). One placement differs from main: with the whole root selected by element points 0 to 3 and a wrapping element supplied, main leaves a collapsed root point at 0, before the new wrapper (typing gives Z aa bb cc), and this branch a collapsed point after it (typing gives aa bb cc Z); caret placement only, the wrapped text is kept either way; finding each node's child in a block container walks each ancestor once. 40 new cells and a second parent-lookup count.

Other gates

  • Collaboration soak, 400 sessions each of the structural-command lanes (mix and list profiles with structural commands, which call $wrapNodes inside containers) against main with the same seeds: the same failure classes and counts on both trees, nothing only on this branch.
  • StructFuzz (structural commands in bound editors vs an unbound control, 10,000 seeds): passes on both.

Verify

  • $wrapNodes now wraps a paragraph with all of its inline content (nested inline elements, OverflowNode), keeps a selection inside the shadow root that holds it, wraps inline content directly in a block container by runs (each block from its own content, a block decorator on its own), no longer throws for a range from a list item to an element point on its list, keeps blocks before a range in place, and no longer hangs on the ranges above.
  • Caret placement differs from main in one family: with element points (for example the whole root selected by root element points 0 to 3, a wrapping element supplied), main leaves a collapsed point before the new wrapper (typing Z gives Z aa bb cc) and this branch a collapsed point after it (aa bb cc Z); a review found 31 element-point cases with such a placement difference, all keeping every text node and its order. Caret only, no content changes.
  • Pre-existing and not fixed here: a range across a horizontal rule in a block element that is not a container never returns (Bug: [lexical-selection] $wrapNodes never returns for a range across a horizontal rule inside a block element that is not a container #9638), on main too.

…nd OverflowNode in the block $wrapNodes wraps

## Description

`$wrapNodes` (deprecated in favour of `$setBlocksType`) moves the content of
each selected text's block into a new element. It took that block to be the
text's parent, or its grandparent when the parent is inline. So:

- with the selection inside an inline element nested in another one (a link
  inside a comment mark, overlapping marks, any two levels), the outer inline
  element was taken for the block: the paragraph was split around it and the
  outer element (the mark) was dropped;
- the character limit's `OverflowNode`, which is inline content although its
  `isInline()` is false, was taken for a block the same way;
- with a range that starts there and ends later in the paragraph or in the
  next one, the insertion point was inside content that then moved into the
  new element, so the new element was inserted inside itself and the call
  never returned.

The block of a selected node is now its nearest ancestor that is an element
and not inline content, as the node structure normalization counts it, and
each node's block is decided before anything moves (otherwise a node deep in
a block whose content already moved finds the new element as its block). An
empty inline element in the selection is no longer taken for an empty block.
The paragraph becomes the new element with all of its inline content, as for
text directly in the paragraph.

Closes #9612

## Test plan

New `packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts`:
every nesting of up to three inline wrappers (link, mark, overflow) around
the inner text (39 shapes), the selection as a caret before the wrappers, a
caret inside the inner text, the whole paragraph, a range from the inner text
to the end, and a range over the paragraph and the next one; at the root and
inside a shadow root; with and without `NodeStructureExtension`; with and
without a wrapping element (1,170 cells). The 24 `test.fails` cells naming
#9612 in `StructuralCommands.test.ts` become plain tests.

### Before

On main (4421e5b), the new test per selection group (each bounded at 60 s):

```
[caret before the wrappers] Tests  234 passed | 936 skipped (1170)
[caret inside the inner text] Tests  222 failed | 12 passed | 936 skipped (1170)
[the whole paragraph] Tests  216 failed | 18 passed | 936 skipped (1170)
[from the inner text to the end] no result: killed after 60 s (the run never returns)
[this paragraph and the next] no result: killed after 60 s (the run never returns)
```

For example `mark > link, caret inside the inner text, in the root`:

```
Expected: "quote("abc ",mark(link("inner"))," end"),paragraph("next")"
Received: "paragraph("abc ",quote(link("inner"))," end"),paragraph("next")"
```

`StructuralCommands.test.ts` with the #9612 markers removed:

```
Tests  24 failed | 3308 passed | 128 skipped (3460)
× $wrapNodes in a quote, caret inside it, on a paragraph with link/in-mark in root [unbound, NodeStructureExtension]
```

### After

```
$ vitest --run --project unit packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts
 Test Files  2 passed (2)
      Tests  4502 passed | 128 skipped (4630)
```

Unit tests of lexical, lexical-selection, lexical-extension, lexical-list,
lexical-rich-text, lexical-y, lexical-mark, lexical-link, lexical-overflow,
lexical-table and lexical-utils: 23,260 passed, 166 expected fail, 0 failed.
`pnpm run ci-check` passes.
@vercel

vercel Bot commented Oct 30, 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 30, 2026 11:45pm UTC
lexical-playground Ready Ready Preview Oct 30, 2026 11:45pm UTC

Request Review

@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 Oct 30, 2026
@meta-code-review

meta-code-review Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 31, 2026, 12:03 AM UTC

@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9618

@lexical/yjs

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

commit: 56f91f5

…hat holds the selection, and walk each ancestor once

## Description

Follow-up to the previous commit. Its walk from a selected node up to the
block whose content `$wrapNodes` moves treated an inline shadow root as
inline content and went past it: with the caret in an empty inline shadow
root inside a paragraph (`paragraph("outside before", scope(), "outside
after")`), `$wrapNodes` replaced the paragraph outside the scope with the
quote and left the scope empty. main keeps the paragraph and puts the quote
in the scope.

The walk now stops at the block container (root, shadow root, inline or not,
or declared container) that holds the node and returns it, and content
directly in a block container is wrapped in place, before the container's
first child. That also fixes a throw on main ("Expected node ... to have a
parent") for text held directly by a block shadow root that is a paragraph's
named slot value, under `NodeStructureExtension`.

Also, the previous commit found each selected node's block with a walk of
its own, so a whole paragraph whose content is nested n inline elements deep
cost about n²/2 parent lookups (main: about 8n). The walks now share what
they found (each node walked through is mapped to its block), so each
ancestor is walked once: about 2n.

## Test plan

`WrapNodesInlineContent.test.ts` gains the shadow-root scopes: inline (in the
paragraph's text, inside a mark in it) or block (at the root, a paragraph's
named slot value; a slot value is never inline); empty, holding a paragraph,
or holding text directly; a caret in the empty scope, a caret in the inner
text, or the whole inner text; with and without `NodeStructureExtension` (40
cells). Each cell checks that the paragraph outside keeps its content and
that the scope holds the quote with the inner content.

A new test counts parent lookups for a whole-paragraph selection at nesting
depths 32, 64 and 128, and requires that doubling the depth at most a little
more than doubles them.

### Before

The previous commit (3a78622):

```
× inline, in the paragraph, empty, caret in the empty scope
× inline, in a mark in the paragraph, empty, caret in the empty scope
× inline, in the paragraph, empty, caret in the empty scope, NodeStructureExtension
× inline, in a mark in the paragraph, empty, caret in the empty scope, NodeStructureExtension
× block, the paragraph's slot value, holding text, caret in the inner text, NodeStructureExtension
× block, the paragraph's slot value, holding text, the whole inner text, NodeStructureExtension
Tests  6 failed | 34 passed | 1170 skipped (1210)
```

main (4421e5b) passes those and fails two others:

```
× block, the paragraph's slot value, holding text, caret in the inner text, NodeStructureExtension
× block, the paragraph's slot value, holding text, the whole inner text, NodeStructureExtension
Error: Expected node 300 to have a parent.
Tests  2 failed | 38 passed | 1170 skipped (1210)
```

Without the second part of the change (wrapping content directly in a block
container in place), the same two cells fail.

Parent lookups at depth 16/32/64/128 (19/35/67/131 selected nodes): the
previous commit 172/580/2164/8404, failing the new test; main
145/273/529/1041.

### After

```
$ vitest --run --project unit packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts -t 'shadow root inside that scope'
Tests  40 passed | 1170 skipped (1210)
```

Parent lookups at depth 16/32/64/128: 52/84/148/276.

```
$ vitest --run --project unit packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts
Tests  1211 passed (1211)
```

Unit tests of lexical, lexical-selection, lexical-extension, lexical-list,
lexical-rich-text, lexical-y, lexical-mark, lexical-link, lexical-overflow,
lexical-table and lexical-utils: 23,301 passed, 166 expected fail, 0 failed.
`pnpm run ci-check` passes.
…line runs and blocks in $wrapNodes, and never throw on a range ending on a list

## Description

Two more problems in `$wrapNodes`, found in review:

- With the content of a block container mixing inline content and blocks
  (a shadow root holding `AA`, `paragraph(mark("BB"))`, `CC`), the previous
  commit wrapped the container's whole content for its first direct text and
  then wrapped the paragraph a second time, after `CC`: the text came out as
  `AA CC BB`. main threw before writing anything. Inline content directly in
  a block container is now wrapped by runs: each run of inline siblings (up
  to the nearest block) becomes one element, a block leaf there (a
  decorator) is wrapped on its own, and each block is wrapped from its own
  content, so the container's content keeps its order. The runs are decided
  before anything moves, like the blocks.
- A range with one end in a list item's text and the other an element point
  on the list itself made `$wrapNodes` throw ("$comparePointCaretNext: ... do
  not have a common ancestor"), on main too: removing an emptied list item
  adjusts the selection, whose other end was by then inside a new element
  not in the document yet. The selection is now cleared while content moves
  and set again afterwards (to its old points where they are still in the
  document, else to the end of the last new element).

## Test plan

`WrapNodesInlineContent.test.ts` gains:

- block containers that mix inline content and blocks (a block shadow root
  at the root or as a paragraph's named slot value, an inline shadow root in
  a paragraph; six mixes of direct text and paragraphs, one with a mark,
  only text, only paragraphs; the selection over all of it, from the first
  text to the middle, or from the middle to the last text; forward and
  backward; with and without `NodeStructureExtension`; 216 cells): every
  text node keeps its identity and its place in document order;
- a range from a list item's text (each of three items) to an element point
  on its list (each offset 0 to 3), forward and backward, with and without
  `NodeStructureExtension` (48 cells): no throw, and every text stays, in
  order.

### Before

The previous commit (eec843f), on the first 144 container cells and the 48
list cells:

```
Tests  10 failed | 134 passed | 1259 skipped (1403)
Error: insert: node 1522 cannot become a child of 1522; the parent is reachable from the node through slot up-links, so the insertion would form a cycle that loops isAttached/GC.
Tests  48 failed | 1355 skipped (1403)
Error: $comparePointCaretNext: a (key 93) and b (key 94) do not have a common ancestor
```

main (4421e5b):

```
Tests  16 failed | 128 passed | 1259 skipped (1403)
Tests  48 failed | 1355 skipped (1403)
```

### After

```
$ vitest --run --project unit packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts
Tests  1475 passed (1475)
```

Unit tests of lexical, lexical-selection, lexical-extension, lexical-list,
lexical-rich-text, lexical-y, lexical-mark, lexical-link, lexical-overflow,
lexical-table and lexical-utils: 23,565 passed, 166 expected fail, 0 failed.
`pnpm run ci-check` passes.

@meta-code-review meta-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks good. $wrapNodesImpl in packages/lexical-selection/src/range-selection.ts now decides each selected node's block before anything moves and stops at the enclosing block container. The new matrix in WrapNodesInlineContent.test.ts fails on main for the shapes it names and passes here, and the @lexical/y structural-command cells no longer need test.fails. Everything below is optional and non-blocking.

  1. Changeset Verify: (.changeset/wrap-nodes-inline-content.md:7). CONTRIBUTING.md and .changeset/README.md ask a fix that changes observable behaviour to say what to test. Besides nested inline elements and OverflowNode, this PR also changes four things: a selection over a block decorator now wraps the decorator on its own instead of moving every later sibling into one block; a block container that mixes inline content and blocks keeps its content inside and in order; a range ending on a list's element point no longer throws; and with a wrapping element, a range whose block was removed now ends as a caret at the end of the last wrapped block. One more sentence would cover them.
  2. Assert the result in the mixed-container and list groups (WrapNodesInlineContent.test.ts:585-589 and :639-645). These groups check text order only, so a change that stopped wrapping would still pass. The file's $content helper could assert the structure, as the first matrix does.
  3. Pin the selection after the new restore branches (range-selection.ts:667-681). No test reads the selection after $wrapNodes. One cell with a wrapping element and a range starting in an empty paragraph, asserting where the caret lands, would cover the new fallback.
  4. Nit: clamp the restored selection (range-selection.ts:672). On the wrapping-element path, the pre-command selection comes back without the remapping that node removal used to do, so root.select(0, 3) can return as root@0 → root@3 on a root that now has one child. main already gives that result when the selection was committed, so this is polish, not a regression.


[fix] `$wrapNodes` keeps a paragraph's nested inline elements (a link inside a comment mark, overlapping marks) and the character limit's `OverflowNode` in the wrapped block, and no longer hangs on a range that starts inside one

Verify: `$wrapNodes` on a selection inside an inline element nested in another inline element, or inside an `OverflowNode`, now wraps the whole paragraph with all of its inline content, as it already did for text directly in the paragraph or in one inline element. Before, the paragraph was split around the inner element and the outer one (the mark, the overflow) was dropped, and a range starting there and ending later in the paragraph or in the next one never returned.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: CONTRIBUTING.md and .changeset/README.md ask a fix that changes observable behaviour to say what to test. This PR also changes four other things. A selection over a block decorator now wraps the decorator on its own instead of moving every later sibling into one block. A block container that mixes inline content and blocks keeps its content inside and in order. A range ending on a list's element point no longer throws. And with a wrapping element, a range whose block was removed now ends as a caret at the end of the last wrapped block. One more sentence here would cover them.

editor.read(() => {
const scope = $getNodeByKey(scopeKey) as ElementNode;
expect(scope.isAttached()).toBe(true);
expect($texts(scope)).toEqual(before);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this checks text identity and order only, so a change that stopped wrapping (or wrapped an unselected run) would still pass. Asserting the resulting structure with $content, as the first matrix does, would also pin which runs and blocks get wrapped. The list group at the end of the file has the same gap.

isPointAttached(active.anchor) &&
isPointAttached(active.focus)
) {
$setSelection(active);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit, optional: the selection restored here has not been remapped by the node removals, because the selection was null while content moved. For example, root.select(0, 3) with a wrapping element comes back as root@0 → root@3 on a root that now has one child. main already gives the same result when that selection was committed (the branch at the top clones it), so this is not a regression. Clamping an element point's offset to its node's children size before restoring would keep the selection well formed. A test that reads the selection after this branch and the selectEnd fallback below would pin the new behaviour.

…wrapNodes, remove emptied nodes after the new elements are in place, and walk each ancestor once

## Description

Follow-up to the previous commits, from review:

- A range that starts at the end of a block (an element point after the
  last child of a list or a paragraph) and ends in a later block moved
  that first block after the wrapped blocks ("aa bb" ended up after "cc",
  "dd"), on main too. The node list of such a range starts with the first
  block, which gives nothing to the new elements, and the elements were
  inserted relative to it. They are now inserted relative to the first node
  that is moved (or that an empty block stands for).
- The previous commit cleared the selection while content moved, to stop
  the throw for a range ending on a list (a list item's removal compared a
  point in a new element with one in the document). That changed where the
  selection ends up compared with main (with the whole root selected, a
  non-collapsed root selection past the root's last child was left, and
  typing then replaced the wrapped text). Instead, the selection stays as it
  was, and the nodes the moves empty are removed once the new elements are
  in the document, so no removal compares a point outside the document.
- Finding the child of a block container that holds each selected node
  walked every inline ancestor again for each node: quadratic for deeply
  nested inline content directly in a container (depth 16/32/64/128: 198,
  638, 2,286, 8,654 parent lookups). The walks now share what they found:
  110, 206, 398 at depth 32/64/128.

## Test plan

`WrapNodesInlineContent.test.ts` gains:

- a range from the end of a list or a paragraph to text in a later
  paragraph, an empty paragraph, an empty paragraph in a shadow root, or
  text in a shadow root, forward and backward, with and without
  `NodeStructureExtension` (32 cells): the first block stays (same key,
  same type, first), and every text node keeps its identity and order;
- the whole root selected with root element points 0 to 3 and a wrapping
  element, and from the first text to the root's end, forward and backward,
  with and without `NodeStructureExtension` (8 cells): the selection is in
  range, and collapsed as on main where main succeeds, and typing then keeps
  the wrapped text;
- the parent-lookup count for a whole selection of inline content nested
  32/64/128 deep directly in a paragraph's named-slot shadow root (with
  `NodeStructureExtension`), next to the existing count inside a paragraph.

### Before

The previous commit (5be87e9), on the new and earlier container, list,
start-at-end, selection and cost cells:

```
Tests  37 failed | 309 passed | 1170 skipped (1516)
AssertionError: expected 'range out of range' to be 'collapsed in range'
```

main (4421e5b) on the same cells (the start-at-end cells alone: 32 of 32
fail; the root cells throw "$comparePointCaretNext: ... do not have a
common ancestor" for the range from the first text, and leave the selection
before the wrapper for the root element points):

```
Tests  109 failed | 237 passed | 1170 skipped (1516)
```

### After

```
$ vitest --run --project unit packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts
Tests  1516 passed (1516)
```

Unit tests of lexical, lexical-selection, lexical-extension, lexical-list,
lexical-rich-text, lexical-y, lexical-mark, lexical-link, lexical-overflow,
lexical-table and lexical-utils: 23,605 passed, 166 expected fail, and one
heap-size test (`DisposeMemory.test.ts`, 12.3 MB against its 8 MB bound)
that failed only with 16 workers on a loaded host; it passes 3 of 3 runs on
its own, and this change does not touch `@lexical/y`. `pnpm run ci-check`
passes.

@meta-code-review meta-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for this fix — it looks good to me. The block lookup and the per-node units in $wrapNodesImpl fix the split paragraph, the dropped mark or OverflowNode, and the hang from #9612, and ordinary carets and ranges over paragraphs and list items behave exactly as before. The two suggestions below are optional and non-blocking.

Optional: assert the wrapped structure in three new test groups. The mixed-container group (packages/lexical-selection/src/__tests__/unit/WrapNodesInlineContent.test.ts:610-611), the list-point group (:662-668) and the start-at-end group (:749-752) check text order, attachment and the first block's key, but a $wrapNodes that wrapped nothing would also pass them. Asserting the resulting tree with the file's own $content helper, as the first matrix does at :236, would make them regression tests for the wrapping itself, not only for the throw and the order.

Optional: extend the changeset's Verify: paragraph. CONTRIBUTING.md (Changesets) and .changeset/README.md ask a fix that changes observable output to say what to recheck. .changeset/wrap-nodes-inline-content.md:7 covers nested inline elements, OverflowNode and the hang; the change also leaves a block in place when a range starts at its end (it used to move after the wrapped blocks, or split a list around them), gives a block decorator in the range a block of its own instead of taking every later sibling with it, keeps mixed inline and block content inside its container in order, and stops the throw for a range ending on a list or at the root's end. One more sentence would let people know what to recheck.

A small, untested extra: a list item between two nested lists no longer lets those lists merge across the wrapped item, so the content keeps its order. A cell for that, and one for a block decorator in the range, would pin both.


[fix] `$wrapNodes` keeps a paragraph's nested inline elements (a link inside a comment mark, overlapping marks) and the character limit's `OverflowNode` in the wrapped block, and no longer hangs on a range that starts inside one

Verify: `$wrapNodes` on a selection inside an inline element nested in another inline element, or inside an `OverflowNode`, now wraps the whole paragraph with all of its inline content, as it already did for text directly in the paragraph or in one inline element. Before, the paragraph was split around the inner element and the outer one (the mark, the overflow) was dropped, and a range starting there and ending later in the paragraph or in the next one never returned.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this Verify: paragraph covers nested inline elements, OverflowNode and the hang. The change also leaves a block in place when a range starts at its end, gives a block decorator in the range a block of its own, keeps mixed inline and block content inside its container in order, and stops the throw for a range ending on a list or at the root's end. CONTRIBUTING.md (Changesets) asks Verify: to say what to test when observable output changes, so one more sentence would cover these.

editor.read(() => {
const scope = $getNodeByKey(scopeKey) as ElementNode;
expect(scope.isAttached()).toBe(true);
expect($texts(scope)).toEqual(before);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this group (and the list-point and start-at-end groups at :662 and :749) checks text identity and order, which a $wrapNodes that wrapped nothing would also satisfy. Comparing $content(scope) with the expected quotes, as the first matrix does at :236, would pin the wrapping too.

@zurfyx
zurfyx added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit 551fe4a Oct 31, 2026
57 checks passed
zurfyx added a commit that referenced this pull request Oct 31, 2026
## Description

Merges main (2e2acb7, with #9625 and #9618). #9625's
`RemadeTextStaleLink.test.ts` marks its 20 cells where a stale format
reaches past the link as expected failures "(#9597, fixed by #9601)": they
pass with this pull request, so they are plain tests now. The DECISIONS
link list keeps both sides' references.

## Test plan

### Before

On main, the 20 cells are expected failures.

### After

```
$ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/RemadeTextStaleLink.test.ts
      Tests  36 passed (36)
```

This branch was successfully deployed

2 active deployments
Preview – lexical — 56f91f5d Deployed Oct 30, 2026 by vercel[bot]
Preview – lexical-playground — 56f91f5d Deployed Oct 30, 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.

Bug: [lexical-selection] $wrapNodes splits a paragraph and drops nodes when the caret is in an OverflowNode or in a link inside a mark

1 participant