Skip to content

[lexical][lexical-extension][lexical-selection] Bug Fix: Fill an empty declared container in $setBlocksType whichever end of the range is in it - #9616

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/block-containers-backward-range
Oct 30, 2026

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 30, 2026

Copy link
Copy Markdown
Member

Description

Follow-up to #9590, from the Meta code-review bot's last review there (approved, optional suggestions), which came after the merge:

  • $setBlocksType filled an empty declared container only when the selection's anchor was in it, so a backward range ending in the container left it empty. It now fills it when either point is in it, and moves those points into the new block, also on the active selection that a replacement earlier in the same call can make anew.
  • The $isBlockContainer JSDoc says that only an empty declared container gets the new block from $setBlocksType (an empty root or shadow root still does not, as before).

Changeset

.changeset/selection-set-blocks-type-backward-container.md ([fix], @lexical/selection).

Test plan

Before

$ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts -t "backward range"   # range-selection.ts from main
     × $setBlocksType keeps a backward range that ends in the empty container
Expected: "p("before") / dbox[indent1](h()) / h("after")"
Received: "p("before") / dbox[indent1]() / h("after")"
      Tests  1 failed | 399 skipped (400)

After

$ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts packages/lexical-selection packages/lexical-list
      Tests  921 passed (921)
$ pnpm run ci-check
Found 0 errors

…y declared container in $setBlocksType whichever end of the range is in it

## Description

Follow-up to #9590, from the Meta code-review bot's last review there (approved, optional suggestions), which came after the merge:

- `$setBlocksType` filled an empty declared container only when the selection's anchor was in it, so a backward range ending in the container left it empty. It now fills it when either point is in it, and moves those points into the new block, also on the active selection that a replacement earlier in the same call can make anew.
- The `$isBlockContainer` JSDoc says that only an empty declared container gets the new block from `$setBlocksType` (an empty root or shadow root still does not, as before).

## Changeset

`.changeset/selection-set-blocks-type-backward-container.md` (`[fix]`, `@lexical/selection`).

## Test plan

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts -t "backward range"   # range-selection.ts from main
     × $setBlocksType keeps a backward range that ends in the empty container
Expected: "p("before") / dbox[indent1](h()) / h("after")"
Received: "p("before") / dbox[indent1]() / h("after")"
      Tests  1 failed | 399 skipped (400)
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts packages/lexical-selection packages/lexical-list
      Tests  921 passed (921)
$ pnpm run ci-check
Found 0 errors
```
@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 10:00pm UTC
lexical-playground Ready Ready Preview Oct 30, 2026 10:00pm 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 30, 2026, 10:19 PM UTC

@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 486aca3

@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.

This looks good: $setBlocksType now fills an empty declared container from either end of the range, reading the active selection covers the clone that LexicalNode.replace installs when it converts the anchor's block first, and the new test fails without the fix. Everything below is optional and non-blocking.

1. Decide the fill on the passed selection (packages/lexical-selection/src/range-selection.ts:205-214)

Points of $getSelection() currently count toward whether the container is filled, not only toward moving points into the new block. So when a caller passes a selection that is not the active one (for example a range from the paragraph before an empty declared container to the paragraph after it) while the caret sits in that container, the container gets an empty heading and the caret moves into it. The same range passed as the active selection leaves the container empty, which matches the existing rule for a range that passes over an empty container and the changeset's "whichever end of the selection is in it". Deciding on the passed selection's points while still moving the active selection's keeps the clone handling and passes the existing BlockContainers and @lexical/selection suites:

const points: Point[] = [];
let inPassed = false;
for (const s of new Set([selection, $getSelection()])) {
  if ($isRangeSelection(s)) {
    for (const point of [s.anchor, s.focus]) {
      if (point.key === prevNode.getKey()) {
        points.push(point);
        inPassed = inPassed || s === selection;
      }
    }
  }
}
if (inPassed && prevNode.isEmpty()) {

2. Cover the forward case (packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts:1426)

A forward range whose focus ends at offset 0 of the empty container changes with this PR as well (main leaves the container empty; this branch fills it), which is the intended "either end" behavior. A sibling of the new test with the anchor in before and the focus in the container would pin it.

3. Comment wording (range-selection.ts:198-200)

"(either end, in either direction), the new block goes in it, as with $insertList and $wrapNodes" reads as if those two also fill from either end; both still fill only when the anchor is in the container. Limiting the comparison to what they share would keep the comment accurate.

4. Changeset coverage for lexical

The Changesets check warns that packages/lexical/src/LexicalStructure.ts changed without a changeset. The edit there is JSDoc, which .changeset/README.md classes as docs; adding 'lexical': patch to .changeset/selection-set-blocks-type-backward-container.md clears the warning (see "Changesets" in CONTRIBUTING.md).

5. Docs wording

packages/lexical-website/docs/serialization/serialization.md (around line 679) still says "with the selection in an empty container"; the same "empty declared container" narrowing you made in the $isBlockContainer JSDoc would keep the two aligned.

}
}
}
if (points.length > 0 && prevNode.isEmpty()) {

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: the fill is decided by points of the passed selection or the active one, so a caller that passes a non-active range which only passes over this empty container fills it whenever the caret sits in it, while the same range passed as the active selection leaves it empty. Deciding on the passed selection's points (inPassed = inPassed || s === selection) and still moving both keeps the clone handling; the review comment has a sketch.

Comment on lines +199 to +200
// (either end, in either direction), the new block goes in it, as
// with $insertList and $wrapNodes. The container stays, so its indent

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): $insertList and $wrapNodes still fill only when the anchor is in the container, so "either end, in either direction ... as with" overstates the parity; consider limiting the comparison to the new block going in the container.

).toEqual(['heading:0', 'text:2']);
});

test('$setBlocksType keeps a backward range that ends in the empty container', () => {

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: a forward sibling (anchor in before, focus at offset 0 of the empty box) also changes with this PR, since main leaves the box empty there, and would pin the either-end rule.

@@ -0,0 +1,7 @@
---
'@lexical/selection': patch

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: the Changesets check also asks for lexical because of the $isBlockContainer JSDoc edit (docs per .changeset/README.md); adding 'lexical': patch here clears the warning.

@zurfyx
zurfyx added this pull request to the merge queue Oct 30, 2026
Merged via the queue into main with commit 1853707 Oct 30, 2026
57 of 58 checks passed

This branch was successfully deployed

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

1 participant