Repository navigation
[lexical-html] Bug Fix: Keep the id of a list item that only wraps a nested list through an HTML round trip - #9648
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
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.tsfails 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-idon 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 aVerify: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 ofBlockIdHTMLConfig.export(lines 81-87) andBlockIdHTMLConfig.import(lines 120-129) still mentions onlyBLOCK_ID_ATTRIBUTE. Both switches now also controlBLOCK_WRAPPER_ID_ATTRIBUTE. One sentence on each would tell someone settingexport: falseorimport: falsethat 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.
5b020c1 to
f364a26
Compare
There was a problem hiding this comment.
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
- Pin the "holds it alone" guard in a test (
packages/lexical-html/src/__tests__/unit/BlockIdHTMLExtension.test.ts:722). The testa wrapper id goes only on a block that holds that list alonegives the text<li>its owndata-block-id="item", so the$getBlockId(holder) === nullcheck already keeps the wrapper id off it. Ifholder.getChildrenSize() === 1is 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 giveswto thetextitem instead of the wrapper. Droppingdata-block-id="item"from that fixture, or adding a case without it, and asserting that the text item does not takewwould pin it. - One JSDoc sentence on each switch (
packages/lexical-html/src/BlockIdHTMLExtension.ts:BlockIdHTMLConfig.exportat lines 81-86,BlockIdHTMLConfig.importat lines 120-128). Both still name onlyBLOCK_ID_ATTRIBUTE, but they now governBLOCK_WRAPPER_ID_ATTRIBUTEtoo. Saying so tells a caller who setsexport: falseorimport: falsethat the wrapper attribute follows the same switch.
What I checked
- With the previous implementation, the updated
C7Html.test.tsfails the three nested-list cells with export on, and three of the newBlockIdHTMLExtension.test.tscases 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 aVerify: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' + |
There was a problem hiding this comment.
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.
## 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).
Description
Closes #9606.
With
BlockIdHTMLExtension, every identified block's element carries itsid in
data-block-id. A list item that only wraps a nested list (Lexical'sshape 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 importit 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 ownelement 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 shadowroots 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-idkeeps its meaning (the block this element is) forevery 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/htmlpatch,[fix]with aVerify:paragraph).Decision for review
DECISIONS.md D87: a separate attribute on the nested list (additive) rather than changing what
data-block-idmeans; the page case is documented instead of fixed.Test plan
BlockIdHTMLExtension.test.ts: a list with a nested list and a list nestedtwo levels: the export writes
data-block-wrapper-idon the nested<ul>and
<ol>, every identified block's id is in the HTML exactly once, and theimport gives every block, the two wrapper items included, the id it had;
pasted twice, the second copy gets fresh ids;
export: falsewrites nowrapper id and
import: falsereads none; a wrapper id on a top-level listgoes on no block (not the root); handles mode writes none.
C7Html.test.ts(the HTML contract over every node shape): the threenested-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.5 cases fail.
Before
main (2e2acb7; the branch is now on c7f6139),
C7Html.test.tswithMATRIX_FULL=1:After
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-checkpasses.