Repository navigation
[lexical][lexical-mark] Bug Fix: Keep the textFormat and textStyle of elements a full reconcile does not change - #9553
Conversation
… elements a full reconcile does not change
## Description
The reconciler derives an element's `textFormat` and `textStyle` from its first text (`reconcileTextFormat` / `reconcileTextStyle`) whenever it reconciles the element's children. A full reconcile treats every node as dirty to render it again, so it also derived them on elements the update did not change, and `setTextFormat` there changed a node in an update that changes none. `$fullReconcile` promises that no mutation or collaboration listener observes a change, and `MarkStateExtension` relies on it to re-render when `isMarkIDShown` changes. In a document whose paragraph's stored `textFormat` (0) was not its bold first text's format, as a client that never rendered it (headless, an agent) writes it, assigning a new `isMarkIDShown` changed that paragraph: it pushed a history entry (the next undo took back nothing visible) and, bound with `@lexical/y`, wrote the paragraph's text format and block id to every collaborator.
A full reconcile now derives them only for an element this update changed (a node the new state does not share with the previous one) or changed inside (a dirty element, such as a paragraph whose first text changed format). Updates that are not full reconciles are unchanged, and so is `setEditorState` (its state is read only, where nothing was derived before either).
## Changeset
`.changeset/lexical-full-reconcile-keeps-text-fields.md` (`lexical`, `[fix]`).
## Test plan
New `DerivedTextFields.test.ts` (`lexical`): a full reconcile changes no element and marks only the root; a full reconcile in an update that also changes a paragraph's first text derives that paragraph's fields and leaves the other one; an element whose first text changes still takes its format and style. New `isMarkIDShown` case in `MarkStateExtension.test.ts`: assigning a new filter in that document changes no node, and undo takes back the last edit.
```
pnpm exec vitest run --project unit --reporter=verbose packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts packages/lexical-mark/src/__tests__/unit/MarkStateExtension.test.ts -t "derived textFormat|assigning a new filter changes no node"
```
### Before
```
× |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > a full reconcile changes no element that the update did not change
× |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > a full reconcile with an edit derives them for the element it changes only
✓ |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > an element whose first text changes takes its format and style
× |unit| packages/lexical-mark/src/__tests__/unit/MarkStateExtension.test.ts > MarkStateExtension > isMarkIDShown > assigning a new filter changes no node, also where an element's stored text format is not its first text's
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected [ 1, 'color: red' ] to deeply equal [ +0, '' ]
AssertionError: expected [ [ 1, 'color: red' ], …(1) ] to deeply equal [ [ +0, '' ], [ 2, 'color: blue' ] ]
AssertionError: expected 1 to be +0 // Object.is equality
Test Files 2 failed (2)
Tests 3 failed | 1 passed | 112 skipped (116)
```
### After
```
✓ |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > a full reconcile changes no element that the update did not change
✓ |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > a full reconcile with an edit derives them for the element it changes only
✓ |unit| packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts > derived textFormat and textStyle of an element > an element whose first text changes takes its format and style
✓ |unit| packages/lexical-mark/src/__tests__/unit/MarkStateExtension.test.ts > MarkStateExtension > isMarkIDShown > assigning a new filter changes no node, also where an element's stored text format is not its first text's
Test Files 2 passed (2)
Tests 4 passed | 112 skipped (116)
```
Full unit suite (`pnpm exec vitest run --project unit`), `lexical` core included: 568 files, 0 failed. The two reproductions this came from (without collaboration, and bound with `@lexical/y`, where the binding wrote the paragraph's fields and an undo item) pass. `pnpm run ci-check` passes.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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: |
…s that #9573 fixed ## Description #9573 merged, so the slot-block list numbering cells pass on main: the 179 cells marked `test.fails` for it (139 in `C1ReadEqualsFresh.test.ts`, 16 in `C1Busy.test.ts`, the 24 seeds of `CatalogSoakFuzz.test.ts`) now report as passing unexpectedly. They are plain tests again. The other expected failures (#9553, #9568, #9540, #9581) stay. ## Test plan ``` pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix pnpm run ci-check ``` ### Before On main `cad9015` with the contracts as they were: ``` ❯ packages/lexical-y/src/__tests__/unit/matrix/CatalogSoakFuzz.test.ts (24 tests | 24 failed) ❯ packages/lexical-y/src/__tests__/unit/matrix/C1Busy.test.ts (96 tests | 16 failed) ❯ packages/lexical-y/src/__tests__/unit/matrix/C1ReadEqualsFresh.test.ts (435 tests | 139 failed) Tests 179 failed | 1973 passed | 32 expected fail (2184) ``` ### After On main `cad9015`: ``` Test Files 12 passed (12) Tests 2152 passed | 32 expected fail (2184) $ pnpm run ci-check Found 0 errors ```
## Description This commit changes no file. The repository's review bot reviews a pull request when its branch is pushed; this branch was last pushed before the bot started reviewing the repository, so it had no review yet.
…and count each id's holders in the HTML import check ## Description #9540 and #9584 merged, so the cells marked `test.fails` for them pass on main and report as passing unexpectedly: letters composed past the end of a marked run (26 cells in `MarksIME.test.ts`) and the `@lexical/yjs` v2 table row cell of #9581 (`C6Settles.test.ts`, `yjs-v2, table edits, follow-ups none, repair none, seed 11`). They are plain tests again. The marks for #9553, #9568, #9578 (and #9561) and #9606 stay. The import check of `C7Html.test.ts` collected the imported blocks' ids in a map, so two blocks holding the same id counted as one and "exactly one imported block" was never asserted. It now keeps every holder and requires exactly one, of the same type. ## Test plan ``` pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix pnpm run ci-check ``` ### Before On main `04e899d` with the marks: ``` ❯ packages/lexical-y/src/__tests__/unit/matrix/MarksIME.test.ts (56 tests | 26 failed) ❯ packages/lexical-y/src/__tests__/unit/matrix/C6Settles.test.ts (360 tests | 1 failed) ``` ### After On main `04e899d`: ``` Test Files 13 passed (13) Tests 2395 passed | 13 expected fail (2408) $ pnpm run ci-check Found 0 errors ```
…ixes as plain tests ## Description The catalog contracts that merged in #9588 pin the bug this change fixes with four `test.fails` cells in C1FullReconcile (a paragraph and a heading whose first text is formatted, bound through the binding and through the extension). With this change they pass, so they report "Expect test to fail" in CI. They are plain tests now, and the unused `FORMATTED_FIRST` list is gone. ## Test plan ### Before On this branch merged with main, without this commit: ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts FAIL |unit| packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts > C1: derived element fields do not depend on reconcile history > binding: paragraph/formatted-first-text FAIL |unit| packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts > C1: derived element fields do not depend on reconcile history > extension: paragraph/formatted-first-text FAIL |unit| packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts > C1: derived element fields do not depend on reconcile history > binding: heading/formatted-first-text FAIL |unit| packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts > C1: derived element fields do not depend on reconcile history > extension: heading/formatted-first-text Error: Expect test to fail Tests 4 failed | 178 passed | 1 expected fail (183) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix/C1FullReconcile.test.ts Tests 182 passed | 1 expected fail (183) $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix packages/lexical/src/__tests__/unit Tests 5016 passed | 140 expected fail (5156) ``` The remaining expected failure in C1FullReconcile is #9568.
There was a problem hiding this comment.
Thanks, this looks good. Gating the derivation on prevNode !== nextNode || activeDirtyElements.has(key) makes a full reconcile derive textFormat/textStyle for exactly the elements an ordinary reconcile of the same commit would, so re-rendering (isMarkIDShown, $fullReconcile, setEditable with slots) no longer adds an invisible undo step or a collaborator write, and updates that are not full reconciles are untouched. The new tests fail against the previous reconciler and pass here. A few optional, non-blocking suggestions:
setEditorStatechanges too, which the description says it does not (packages/lexical/src/LexicalReconciler.ts:1844). On a mounted editorsetEditorStateswaps a read-only state for a writable clone before committing (packages/lexical/src/LexicalEditor.ts:1824-1830), so until now every element the new state shared with the current one was re-derived, for example on every@lexical/historyundo and redo. With this change those shared elements come back exactly as saved, which looks like the right behavior, but AGENTS.md asks the description to state what the change makes the code do, so it is worth correcting there (and optionally naming undo in the changeset'sVerify:paragraph). ADerivedTextFields.test.tscase would pin it: keepeditor.getEditorState(), change the second paragraph,setEditorStatethe kept state, and expect the first paragraph to stay[0, '']with onlyrootdirty.- The JSON comparison at
packages/lexical-mark/src/__tests__/unit/MarkStateExtension.test.ts:3103cannot fail for this bug. A paragraph with a text child exports its first text's format rather than the stored field (packages/lexical/src/nodes/LexicalParagraphNode.ts:110-128), so the comparison also passes on the previous reconciler; the stored-format read just above it and the undo check carry the test. Consider dropping it or asserting the commit's dirty elements instead. - The comment at
packages/lexical/src/__tests__/unit/DerivedTextFields.test.ts:124.$fullReconcilemarks nothing itself:rootis dirty because this is the first writable commit after mounting, where the reconciler stores the root's text cache. A second$fullReconciledirties nothing, so asserting[]after one would test the claim directly. - Title: the diff now also touches
packages/lexical-y, and AGENTS.md asks the title's package list to name every package directory the diff touches, so[lexical-y]belongs in it.
| prevNode, | ||
| nextNode, | ||
| dom, | ||
| prevNode !== nextNode || activeDirtyElements.has(key), |
There was a problem hiding this comment.
Optional: this also changes setEditorState with an unparsed state, such as @lexical/history undo/redo. On a mounted editor setEditorState swaps a read-only state for a writable clone (LexicalEditor.ts:1824-1830), so until now every element the new state shared with the current one was re-derived there; with this gate those keep their stored fields. That looks like the right outcome, but the description says setEditorState is unchanged. Worth correcting, and a DerivedTextFields.test.ts case (keep the state, edit the second paragraph, setEditorState the kept state, expect [0, ''] with only root dirty) would pin it.
| isMarkIDShownSignal(editor).value = id => id !== 'c1'; | ||
| await Promise.resolve(); | ||
| expect(editor.read($textFormat)).toBe(0); | ||
| expect(editor.getEditorState().toJSON()).toEqual(editorState.toJSON()); |
There was a problem hiding this comment.
Optional: this comparison also passes on the previous reconciler. ParagraphNode.exportJSON writes the first text's format when the paragraph has a text child, so the stored field never reaches JSON; the read above and the undo below are what catch the bug. Consider dropping this line or asserting the commit's dirty elements instead.
| editor.update($fullReconcile, {discrete: true}); | ||
| expect(editor.read($firstParagraphTextFields)).toEqual([0, '']); | ||
| expect(listener).toHaveBeenCalledTimes(1); | ||
| // Only the root, which a full reconcile marks |
There was a problem hiding this comment.
Nit: $fullReconcile marks nothing itself. root is dirty here because this is the first writable commit after mounting, where the reconciler writes the root's text cache; a second $fullReconcile dirties nothing, so asserting [] after one would test the claim directly.
Description
The reconciler derives an element's
textFormatandtextStylefrom its first text (reconcileTextFormat/reconcileTextStyle) whenever it reconciles the element's children. A full reconcile treats every node as dirty to render it again, so it also derived them on elements the update did not change, andsetTextFormatthere changed a node in an update that changes none.$fullReconcilepromises that no mutation or collaboration listener observes a change, andMarkStateExtensionrelies on it to re-render whenisMarkIDShownchanges. In a document whose paragraph's storedtextFormat(0) was not its bold first text's format, as a client that never rendered it (headless, an agent) writes it, assigning a newisMarkIDShownchanged that paragraph: it pushed a history entry (the next undo took back nothing visible) and, bound with@lexical/y, wrote the paragraph's text format and block id to every collaborator.A full reconcile now derives them only for an element this update changed (a node the new state does not share with the previous one) or changed inside (a dirty element, such as a paragraph whose first text changed format). Updates that are not full reconciles are unchanged, and so is
setEditorState(its state is read only, where nothing was derived before either).Changeset
.changeset/lexical-full-reconcile-keeps-text-fields.md(lexical,[fix]).Test plan
New
DerivedTextFields.test.ts(lexical): a full reconcile changes no element and marks only the root; a full reconcile in an update that also changes a paragraph's first text derives that paragraph's fields and leaves the other one; an element whose first text changes still takes its format and style. NewisMarkIDShowncase inMarkStateExtension.test.ts: assigning a new filter in that document changes no node, and undo takes back the last edit.Before
After
Full unit suite (
pnpm exec vitest run --project unit),lexicalcore included: 568 files, 0 failed. The two reproductions this came from (without collaboration, and bound with@lexical/y, where the binding wrote the paragraph's fields and an undo item) pass.pnpm run ci-checkpasses.Related (not changed here)
A live collaboration test of the merged stack saw two clients with identical content disagree on the
textFormatof a paragraph whose only child is a bold and italic link (0 on one, 3 on the other) after an offline merge; a reload made them agree. That is the same root as this change, more generally: core derives an element'stextFormat/textStylewhile reconciling it, so the value depends on which elements each editor happened to reconcile, not on the document alone. This PR removes one trigger (a full reconcile rewriting elements no update changed); the general history dependence (which clients reconcile an element after a remote change) remains; it also happens on main, and is tracked in #9568.