Skip to content

[lexical-html] Bug Fix: Keep the id of a list item that only wraps a nested list through an HTML round trip - #9648

Merged
zurfyx merged 5 commits into
mainfrom
zurfyx/block-id-html-wrapper-id
Oct 31, 2026
Merged

zurfyx merged 5 commits into
mainfrom
zurfyx/block-id-html-wrapper-id

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Description

Closes #9606.

With BlockIdHTMLExtension, every identified block's element carries its
id in data-block-id. A list item that only wraps a nested list (Lexical's
shape for a nested list) has no element of its own in exported HTML: its
export writes the nested list inside the previous item's element and removes
its own <li>, with the id on it. So the HTML had no id for it, and on import
it got a fresh id while every other block kept its own.

Export now puts that id on the first element the block's element held (the
nested list) in a separate attribute, data-block-wrapper-id
(BLOCK_WRAPPER_ID_ATTRIBUTE), when the block's export removes its own
element and hands its children to another element. Import records the id for
the node that element produces, and the block that ends up holding that node
alone (the item the list importer wraps a nested list in) takes it, through
the same uniqueness rules as an imported data-block-id. The root and shadow
roots never take one. The resolver only runs when the HTML carries the
attribute, looks for each waiting id from its own element up to its
grandparent only (where the list importer makes the wrapper item; past
that, the block gets a new id), so each id is looked at three times at
most (linear), and keeps its waiting ids per import session (an import a rule
runs inside another keeps the outer one's). data-block-id keeps its meaning (the block this element is) for
every other reader. Handles mode writes no wrapper id.

A block whose node exports no element at all (the playground's page, of
which only the content is written) still keeps no id through HTML; the Block
IDs page now says so. Recorded as DECISIONS.md D87 (the number may shift at
landing).

Changeset

.changeset/block-id-html-wrapper-id.md (@lexical/html patch, [fix] with a Verify: paragraph).

Decision for review

DECISIONS.md D87: a separate attribute on the nested list (additive) rather than changing what data-block-id means; the page case is documented instead of fixed.

Test plan

  • BlockIdHTMLExtension.test.ts: a list with a nested list and a list nested
    two levels: the export writes data-block-wrapper-id on the nested <ul>
    and <ol>, every identified block's id is in the HTML exactly once, and the
    import gives every block, the two wrapper items included, the id it had;
    pasted twice, the second copy gets fresh ids; export: false writes no
    wrapper id and import: false reads none; a wrapper id on a top-level list
    goes on no block (not the root); handles mode writes none.
  • C7Html.test.ts (the HTML contract over every node shape): the three
    nested-list cells now pass; the page cell stays pinned (documented).
  • BlockIdHTMLWrapperImport.test.ts: the import's cost grows linearly with wrapper ids that go on no block, with unknown elements that hand the same list up again, and with lists that grow per element; an item filled after its list was produced still takes the id; a nested import does not lose the outer one's wrapper id.
  • Mutations: without the export of the wrapper id, or without its import,
    5 cases fail.

Before

main (2e2acb7; the branch is now on c7f6139), C7Html.test.ts with MATRIX_FULL=1:

Tests  179 passed | 4 expected fail (183)
  (list/bullet-nested, list/number-nested, list/check-nested, page/content
   pinned as failing: "id <wrapper> written 0 times")

After

$ MATRIX_FULL=1 vitest --run --project unit packages/lexical-y/src/__tests__/unit/matrix/C7Html.test.ts
Tests  182 passed | 1 expected fail (183)
$ vitest --run --project unit packages/lexical-html/src/__tests__/unit/BlockIdHTMLExtension.test.ts
Tests  22 passed (22)

Unit tests of lexical-html, lexical-y, lexical-list, lexical-extension,
lexical and lexical-playground: 24,127 passed, 185 expected fail, 0 failed.
pnpm run ci-check passes.

@vercel

vercel Bot commented Oct 31, 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 31, 2026 4:00am UTC
lexical-playground Ready Ready Preview Oct 31, 2026 4:00am 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 31, 2026
@meta-code-review

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

Copy link
Copy Markdown

AI review status for this pull request.

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

@pkg-pr-new

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

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: f364a26

@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: the wrapper item's id now survives an HTML round trip, and data-block-id keeps its meaning for every other reader. The suggestion below is optional and non-blocking.

What I checked

  • Run against the previous implementation, the updated C7Html.test.ts fails the three nested-list cells with export on (id … written 0 times). On this branch it passes (182 passed, 1 expected fail for the documented page case).
  • Disabling the export of data-block-wrapper-id, its import, or the parent/grandparent look in the resolver each makes 5 to 7 of the new and updated tests fail, so both halves are pinned.
  • Edge cases behave as documented. A wrapper item in first position keeps data-block-id on its own <li>. Pretty-printed HTML and <li><p>…</p><ul …> still restore every id. A partial-selection copy keeps each id once. A wrapper id on a top-level list, or on a list alone in a table cell, goes on no block. An <li data-block-id> that holds only the nested list keeps its own id.
  • The changeset follows CONTRIBUTING.md ('@lexical/html': patch, [fix], with a Verify: paragraph). The new Flow declaration needs no suppression, and the new module-scope calls are side-effect-free factories, so the tree-shaking source test still passes.

Optional suggestion

  • packages/lexical-html/src/BlockIdHTMLExtension.ts: the JSDoc of BlockIdHTMLConfig.export (lines 81-87) and BlockIdHTMLConfig.import (lines 120-129) still mentions only BLOCK_ID_ATTRIBUTE. Both switches now also control BLOCK_WRAPPER_ID_ATTRIBUTE. One sentence on each would tell someone setting export: false or import: false that the wrapper attribute follows the same switch.

…nested list through an HTML round trip

Closes #9606.

With `BlockIdHTMLExtension`, every identified block's element carries its
id in `data-block-id`. A list item that only wraps a nested list (Lexical's
shape for a nested list) has no element of its own in exported HTML: its
export writes the nested list inside the previous item's element and removes
its own `<li>`, with the id on it. So the HTML had no id for it, and on import
it got a fresh id while every other block kept its own.

Export now puts that id on the first element the block's element held (the
nested list) in a separate attribute, `data-block-wrapper-id`
(`BLOCK_WRAPPER_ID_ATTRIBUTE`), when the block's export removes its own
element and hands its children to another element. Import records the id for
the node that element produces, and the block that ends up holding that node
alone (the item the list importer wraps a nested list in) takes it, through
the same uniqueness rules as an imported `data-block-id`. The root and shadow
roots never take one. The resolver only runs when the HTML carries the
attribute. `data-block-id` keeps its meaning (the block this element is) for
every other reader. Handles mode writes no wrapper id.

A block whose node exports no element at all (the playground's page, of
which only the content is written) still keeps no id through HTML; the Block
IDs page now says so. Recorded as DECISIONS.md D87 (the number may shift at
landing).

- `BlockIdHTMLExtension.test.ts`: a list with a nested list and a list nested
  two levels: the export writes `data-block-wrapper-id` on the nested `<ul>`
  and `<ol>`, every identified block's id is in the HTML exactly once, and the
  import gives every block, the two wrapper items included, the id it had;
  pasted twice, the second copy gets fresh ids; `export: false` writes no
  wrapper id and `import: false` reads none; a wrapper id on a top-level list
  goes on no block (not the root); handles mode writes none.
- `C7Html.test.ts` (the HTML contract over every node shape): the three
  nested-list cells now pass; the page cell stays pinned (documented).
- Mutations: without the export of the wrapper id, or without its import,
  5 cases fail.

main (2e2acb7), `C7Html.test.ts` with `MATRIX_FULL=1`:

```
Tests  179 passed | 4 expected fail (183)
  (list/bullet-nested, list/number-nested, list/check-nested, page/content
   pinned as failing: "id <wrapper> written 0 times")
```

```
$ MATRIX_FULL=1 vitest --run --project unit packages/lexical-y/src/__tests__/unit/matrix/C7Html.test.ts
Tests  182 passed | 1 expected fail (183)
$ vitest --run --project unit packages/lexical-html/src/__tests__/unit/BlockIdHTMLExtension.test.ts
Tests  22 passed (22)
```

Unit tests of lexical-html, lexical-y, lexical-list, lexical-extension,
lexical and lexical-playground: 24,127 passed, 185 expected fail, 0 failed.
`pnpm run ci-check` passes.
…ch element produced, per import session

## Description

Review of the first commit found two flaws in importing `data-block-wrapper-id`:

- Every element's import looked at every waiting id, so ids that go on no block (on top-level lists, say) made an import quadratic: 256 such lists cost 65,792 parent lookups. The resolver now looks only at the children of the nodes each element produced (the outer list holds the wrapper item it made), once each.
- The waiting ids lived in one module-wide map that every import cleared, so an import a rule runs inside another lost the outer import's ids. They are now an import-session state (`createImportState`), set by the preprocess step that installs the rules.

## Test plan

`BlockIdHTMLWrapperImport.test.ts`: parent lookups for 64, 128 and 256 top-level lists with wrapper ids grow by about 2x per doubling (the previous head: about 4x); a nested import does not change what the outer import gives (the previous head lost the wrapper id); HTML without wrapper ids reads the same with a nested import. Both fail on the previous head, pass here.

Units of lexical-html, lexical-y, lexical-list, lexical-extension, lexical and lexical-playground: 24,130 passed, 185 expected fail, 0 failed. `pnpm run ci-check` passes.
… once their number changed

## Description

Unknown elements around a list each hand the same list node up again, and while a wrapper id on it waited (it goes on no block at the top level), the resolver looked at that list's children once per such element: 128 elements around a 128-item list cost 16,770 child-count reads. The resolver now remembers, per import session, each produced node it looked at and its number of children then, and looks again only once that number changed.

## Test plan

`BlockIdHTMLWrapperImport.test.ts`: child-count reads for 32, 64 and 128 elements around a list of as many items grow by about 2x per doubling (the previous head: about 4x). Fails on the previous head, passes here.

Units of lexical-html, lexical-y, lexical-list, lexical-extension, lexical and lexical-playground: 24,131 passed, 185 expected fail, 0 failed. `pnpm run ci-check` passes.
… element up to its grandparent, once each

## Description

The previous head looked at a produced node's children again only once their number changed; a list that grows by one item per element around it defeated that (quadratic), and an item filled after its list was produced kept the list's child count, so the wrapper id was lost.

Each waiting wrapper id is now looked for from its own element's import up to its grandparent's (where the list importer makes the item that wraps a nested list, in the outer list): after each of those, if the id's node is held alone by a block without an id, that block takes it; after the grandparent, the id is dropped (that block gets a new id). Each id is looked at three times at most, so the cost is linear in the ids, whatever other rules produce or move.

## Test plan

`BlockIdHTMLWrapperResolve.test.ts`: a list growing by one item per element around it (32, 64, 128) costs about 2x per doubling; an item filled after its list was produced takes the wrapper id as when filled early. `BlockIdHTMLWrapperImport.test.ts`: 32 to 128 unknown elements around as many lists with wrapper ids: parent lookups 2x per doubling. The first two fail on the previous head.

Units of lexical-html, lexical-y, lexical-list, lexical-extension, lexical and lexical-playground: 24,135 passed, 185 expected fail, 0 failed. `pnpm run ci-check` passes.
…page and in D87

## Description

HTML a Lexical editor exports keeps every wrapper id; in HTML another tool changed, extra elements between the nested list and its item can make the item get a new id (as on main). This was only in the JSDoc and the PR body.

## Test plan

Docs only. BlockIdDocs.test.ts 16 passed; `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.

This looks good: the wrapper item's id now survives an HTML round trip, data-block-id keeps its meaning for every other reader, and the page case is documented rather than left silent. The two suggestions below are optional and non-blocking.

Optional suggestions

  1. Pin the "holds it alone" guard in a test (packages/lexical-html/src/__tests__/unit/BlockIdHTMLExtension.test.ts:722). The test a wrapper id goes only on a block that holds that list alone gives the text <li> its own data-block-id="item", so the $getBlockId(holder) === null check already keeps the wrapper id off it. If holder.getChildrenSize() === 1 is removed from the resolver (BlockIdHTMLExtension.ts:236), every test still passes, while <ul><li>text<ul data-block-wrapper-id="w"><li>inner</li></ul></li></ul> then gives w to the text item instead of the wrapper. Dropping data-block-id="item" from that fixture, or adding a case without it, and asserting that the text item does not take w would pin it.
  2. One JSDoc sentence on each switch (packages/lexical-html/src/BlockIdHTMLExtension.ts: BlockIdHTMLConfig.export at lines 81-86, BlockIdHTMLConfig.import at lines 120-128). Both still name only BLOCK_ID_ATTRIBUTE, but they now govern BLOCK_WRAPPER_ID_ATTRIBUTE too. Saying so tells a caller who sets export: false or import: false that the wrapper attribute follows the same switch.

What I checked

  • With the previous implementation, the updated C7Html.test.ts fails the three nested-list cells with export on, and three of the new BlockIdHTMLExtension.test.ts cases fail. On this branch they pass (182 passed, 1 expected fail for the documented page case).
  • Taking out the export of data-block-wrapper-id, its import, or the parent/grandparent look each makes 5 to 7 tests fail. The operation-count tests catch a resolver that never drops ids.
  • Every id comes back for pretty-printed HTML, two nested lists of different types under one item, <li><p>…</p><ul …>, a partial-selection copy and a nested import. An invalid value, an id already in the document, or an element between the nested list and its item gives a fresh id, as the Block IDs page says.
  • The changeset follows CONTRIBUTING.md ('@lexical/html': patch, [fix], with a Verify: paragraph). The new Flow declaration needs no suppression, and the new module-scope calls are side-effect-free factories.

'<ul data-block-wrapper-id="top"><li>a</li></ul>' +
// A nested list after the item's text: the importer wraps it in an
// item of its own, which takes the id.
'<ul><li data-block-id="item">text' +

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: because this <li> carries data-block-id="item", the $getBlockId(holder) === null check already keeps the wrapper id off it, so the holder.getChildrenSize() === 1 guard (BlockIdHTMLExtension.ts:236) is not pinned. Removing that guard leaves every test green, while <ul><li>text<ul data-block-wrapper-id="w">… then gives w to the text item instead of the wrapper. Dropping the attribute here, or adding a second case without it, would cover it.

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

Merges main at c0c7e42 (#9640, #9600, #9652, #9659, #9648 and #9657). The one conflict is in the link list of `packages/lexical-y/DECISIONS.md`, where both sides added a reference (#9597 here, #9606 on main); both are kept.

## Test plan

### Before

The branch was on main f59f74d.

### After

`pnpm run ci-check` and `pnpm exec vitest run --project unit --project scripts-unit` pass on the merged tree (outputs in the pull request).

This branch was successfully deployed

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

1 participant