Repository navigation
[lexical][lexical-html] Bug Fix: emit a terminating <br> so a trailing line break survives HTML export - #8765
Conversation
…naged line breaks Follow-up to meta#8750 implementing @etrepum's approach from meta#7600. meta#6395 dropped every trailing <br> in a block on import to match HTML's non-rendering behavior, but that loses authored trailing breaks on round-trip serialization. meta#8750 narrowed the drop to Safari's Apple-interchange-newline clipboard artifact, but that gate doesn't fire for ordinary structural/authored trailing breaks (or non-Safari pastes), so the phantom-blank-line regression persists for those. Instead, drop a trailing <br> only when it is a *managed* line break: - data-lexical-managed-linebreak="true": the terminator Lexical's reconciler already injects via LexicalDOMSlot.setManagedLineBreak, so re-importing Lexical's own exported HTML stays idempotent. - class="Apple-interchange-newline": Safari's clipboard transport artifact. An ordinary authored trailing <br> carries neither marker and is now preserved, so round-trip serialization to HTML is lossless while rendering of external pasted HTML stays faithful. Browser-agnostic for the Lexical-managed case. Applies the same gate to both LineBreakNode.importDOM and the @lexical/html coreImportRules LineBreakRule. Adds test coverage for the preserved / managed / Apple-interchange cases.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Addresses review feedback: the new isManagedLineBreak in LexicalLineBreakNode (inspects serialized markup — the data-lexical-managed-linebreak attribute / Apple-interchange class to decide whether to drop a parsed <br> on import) collided by name with the pre-existing isManagedLineBreak in LexicalMutations (identifies a line break the editor is actively managing in the live DOM via the __lexicalLineBreak slot reference / node key). They work differently, so the mutations one is renamed to isEditorManagedLineBreak and both now carry cross-referencing comments.
|
Looks like everything's ready here? |
|
Is there anything that prevents this PR from being merged now, other than the draft status? @etrepum @potatowagon |
|
Claude claims that this is missing a case where the Code review — gate trailing
|
…reak on export ## Description A `<br>` that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it — `isLastChildInBlockNode` and `isOnlyChildInBlockNode` in `LexicalLineBreakNode`, used by both `LineBreakNode.importDOM` and `@lexical/html`'s `LineBreakRule`. The reconciler already compensates for that rule in the live DOM: when an element ends with a `LineBreakNode` it appends a managed terminator `<br>` (`ElementDOMSlot.insertManagedLineBreak`) so the break actually renders. The HTML exporter had no equivalent, so `$generateHtmlFromNodes` wrote a paragraph ending in a `LineBreakNode` as `<p>a<br></p>` — HTML that renders as a single line, and that every importer reads back without the break at all. A paragraph holding a single `LineBreakNode` came back empty. Emit the same terminator on export, when a block element's last exported child came from a `LineBreakNode` and the DOM it produced is a `<br>`. The importers drop the terminator and keep the authored break, so the round trip is lossless with no change to the import rules — externally authored HTML such as `<p>a<br></p>`, which a browser renders as one line, still imports without a phantom blank line. Export and import gate on the same `isBlockDomNode` predicate, so a `<br>` gets a terminator exactly when an importer would have dropped it. That keeps the two sides consistent for a break trailing an inline element (`<a>x<br></a>` — no terminator, nothing dropped), for tags outside `BLOCK_TAG_RE` such as `<th>`, and for a `domOverride` that wraps the `<br>` in another element. Code blocks, which export as `<pre>`, were losing a trailing newline the same way and are fixed by the same change. This supersedes meta#8765, which addressed the same round-trip loss by relaxing the import rules to keep unmarked trailing `<br>`s. Fixing the export side instead avoids changing how externally authored HTML is pasted, needs no new public API, and also covers the sole-child case that the import-side gate left lossy. ## Test plan New test: `packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts`, covering both importers (`$generateNodesFromDOM` and `$generateNodesFromDOMViaExtension`). Four of its twelve cases are guards that must pass either way: they assert the export shape is unchanged for an interior break and for a break trailing an inline element, and that `<p>a<br></p>` from outside Lexical still imports without a line break. ### Before $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts × a trailing line break exports with a terminating <br> 27ms × a lone line break exports with a terminating <br> 4ms × restores a trailing line break 32ms × restores a lone line break 11ms × restores consecutive trailing line breaks 11ms × restores a trailing line break 12ms × restores a lone line break 9ms × restores consecutive trailing line breaks 10ms ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…' // Object.is equality AssertionError: expected '<p><br></p>' to be '<p><br><br></p>' // Object.is equality AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] AssertionError: expected [] to deeply equal [ 'linebreak' ] AssertionError: expected [ 'text', 'linebreak' ] to deeply equal [ 'text', 'linebreak', 'linebreak' ] Test Files 1 failed (1) Tests 8 failed | 4 passed (12) ### After $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts Test Files 1 passed (1) Tests 12 passed (12) $ npx vitest run # full jsdom unit suite Test Files 316 passed (340) Tests 5839 passed | 1 skipped (5840) $ pnpm run test-browser # vitest browser mode, chromium Test Files 24 passed (24) Tests 151 passed | 1 skipped (152) $ npx cross-env E2E_BROWSER=chromium playwright test --project=chromium 34 skipped 834 passed (18.3m) $ npx tsc --noEmit -p . # clean $ npx flow check No errors! The 24 unit test files not run by `npx vitest run` are the vitest browser-mode set, which `pnpm run test-browser` covers. Firefox and WebKit E2E were not exercised. The rendering rule this change relies on was measured rather than assumed. At `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` also emit `<p>a<br><br></p>` for a trailing soft break, so the terminator emitted here is the platform's own encoding of that content, and Lexical's existing import rules already read it back as a single line break. WebKit could not be launched in this environment, so Safari is unverified on both points.
## Description meta#8765 branched from 335410a and is 185 commits behind `main`, so it no longer applies. Merge `main` in (rather than rebasing, which would invalidate the contributor's checkout) and resolve the three conflicts its diff creates: - `LexicalLineBreakNode.ts`: `main` moved the `<br>` importer out of `static importDOM()` into `$config()`, and dropped `static importJSON()`. Take `main`'s shape and carry meta#8765's managed-line-break gate into the `$config()` importer. - `LexicalMutations.ts`: meta#8765 renamed the local helper to `isEditorManagedLineBreak`; `main` added an adjacent `isDecoratorBoundaryAnchorDOM` check for the decorator boundary anchors from meta#8922. Keep both. - `coreImportRules.ts`: keep meta#8765's updated `LineBreakRule` comment, but take `main`'s `defineImportRule(` without the hand-written `/* @__PURE__ */` — those annotations are injected by `@lexical/compiler` at build time now and a hand-written one is a lint error. No behavior change beyond bringing the branch up to date; meta#8765's import-side gate is preserved exactly as its author wrote it.
…de trailing-<br> gate ## Description meta#8765 made both HTML importers keep a trailing `<br>` unless it carries `data-lexical-managed-linebreak="true"` or `class="Apple-interchange-newline"`, so that a trailing `LineBreakNode` survives an export/import round trip. Revert that; the following commit fixes the same round-trip loss on the export side instead. Three reasons the import-side gate is the wrong half: - It does not fix the case it targets. `exportDOM` never writes `data-lexical-managed-linebreak` — only the reconciler writes it, into the live DOM (`ElementDOMSlot.insertManagedLineBreak`). The marker is absent from exactly the HTML whose round trip the change is about. - The `Apple-interchange-newline` branch is unreachable for real Safari payloads. Safari puts that sentinel at the top level of the clipboard fragment, whose parent is `<body>`; `BODY` is not in `BLOCK_TAG_RE`, so `isLastChildInBlockNode` already returns false there. - It regresses the common paste path. Measured at `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` emit `<p>a<br><br></p>` for a trailing soft break, so `<br><br>` is the platform's encoding and the existing rules already read it back as one line break. Keeping unmarked trailing `<br>`s turns that into two, and gives externally authored `<p>a<br></p>` a blank line no browser renders. It also left `isOnlyChildInBlockNode` dropping unconditionally, so a paragraph holding a single `LineBreakNode` still came back empty. Reverted: the gate in `LineBreakNode`'s `$config()` importer and in `coreImportRules`' `LineBreakRule`, the `isManagedLineBreak` helper and its `lexical` export, and the repurposed `last br in a block node is ignored` case in `LexicalEventHelpers`. Kept: the `isEditorManagedLineBreak` rename in `LexicalMutations`, which is clearer on its own; its comment no longer cross-references the removed helper. After this commit the only delta from `main` is that rename. ## Test plan $ npx vitest run packages/lexical-utils/src/__tests__/unit/LexicalEventHelpers.test.tsx Test Files 1 passed (1)
…reak on export ## Description A `<br>` that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it — `isLastChildInBlockNode` and `isOnlyChildInBlockNode` in `LexicalLineBreakNode`, used by both `LineBreakNode.importDOM` and `@lexical/html`'s `LineBreakRule`. The reconciler already compensates for that rule in the live DOM: when an element ends with a `LineBreakNode` it appends a managed terminator `<br>` (`ElementDOMSlot.insertManagedLineBreak`) so the break actually renders. The HTML exporter had no equivalent, so `$generateHtmlFromNodes` wrote a paragraph ending in a `LineBreakNode` as `<p>a<br></p>` — HTML that renders as a single line, and that every importer reads back without the break at all. A paragraph holding a single `LineBreakNode` came back empty. Emit the same terminator on export, when a block element's last exported child came from a `LineBreakNode` and the DOM it produced is a `<br>`. The importers drop the terminator and keep the authored break, so the round trip is lossless with no change to the import rules — externally authored HTML such as `<p>a<br></p>`, which a browser renders as one line, still imports without a phantom blank line. Export and import gate on the same `isBlockDomNode` predicate, so a `<br>` gets a terminator exactly when an importer would have dropped it. That keeps the two sides consistent for a break trailing an inline element (`<a>x<br></a>` — no terminator, nothing dropped), for tags outside `BLOCK_TAG_RE` such as `<th>`, and for a `domOverride` that wraps the `<br>` in another element. Code blocks, which export as `<pre>`, were losing a trailing newline the same way and are fixed by the same change. This supersedes meta#8765, which addressed the same round-trip loss by relaxing the import rules to keep unmarked trailing `<br>`s. Fixing the export side instead avoids changing how externally authored HTML is pasted, needs no new public API, and also covers the sole-child case that the import-side gate left lossy. ## Test plan New test: `packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts`, covering both importers (`$generateNodesFromDOM` and `$generateNodesFromDOMViaExtension`). Four of its twelve cases are guards that must pass either way: they assert the export shape is unchanged for an interior break and for a break trailing an inline element, and that `<p>a<br></p>` from outside Lexical still imports without a line break. ### Before $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts × a trailing line break exports with a terminating <br> 27ms × a lone line break exports with a terminating <br> 4ms × restores a trailing line break 32ms × restores a lone line break 11ms × restores consecutive trailing line breaks 11ms × restores a trailing line break 12ms × restores a lone line break 9ms × restores consecutive trailing line breaks 10ms ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…' // Object.is equality AssertionError: expected '<p><br></p>' to be '<p><br><br></p>' // Object.is equality AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] AssertionError: expected [] to deeply equal [ 'linebreak' ] AssertionError: expected [ 'text', 'linebreak' ] to deeply equal [ 'text', 'linebreak', 'linebreak' ] Test Files 1 failed (1) Tests 8 failed | 4 passed (12) ### After $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts Test Files 1 passed (1) Tests 12 passed (12) $ npx vitest run # full jsdom unit suite Test Files 316 passed (340) Tests 5839 passed | 1 skipped (5840) $ pnpm run test-browser # vitest browser mode, chromium Test Files 24 passed (24) Tests 151 passed | 1 skipped (152) $ npx cross-env E2E_BROWSER=chromium playwright test --project=chromium 34 skipped 834 passed (18.3m) $ npx tsc --noEmit -p . # clean $ npx flow check No errors! The 24 unit test files not run by `npx vitest run` are the vitest browser-mode set, which `pnpm run test-browser` covers. Firefox and WebKit E2E were not exercised. The rendering rule this change relies on was measured rather than assumed. At `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` also emit `<p>a<br><br></p>` for a trailing soft break, so the terminator emitted here is the platform's own encoding of that content, and Lexical's existing import rules already read it back as a single line break. WebKit could not be launched in this environment, so Safari is unverified on both points.
…reak on export ## Description A `<br>` that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it — `isLastChildInBlockNode` and `isOnlyChildInBlockNode` in `LexicalLineBreakNode`, used by both `LineBreakNode.importDOM` and `@lexical/html`'s `LineBreakRule`. The reconciler already compensates for that rule in the live DOM: when an element ends with a `LineBreakNode` it appends a managed terminator `<br>` (`ElementDOMSlot.insertManagedLineBreak`) so the break actually renders. The HTML exporter had no equivalent, so `$generateHtmlFromNodes` wrote a paragraph ending in a `LineBreakNode` as `<p>a<br></p>` — HTML that renders as a single line, and that every importer reads back without the break at all. A paragraph holding a single `LineBreakNode` came back empty. Emit the same terminator on export, when a block element's last exported child came from a `LineBreakNode` and the DOM it produced is a `<br>`. The importers drop the terminator and keep the authored break, so the round trip is lossless with no change to the import rules — externally authored HTML such as `<p>a<br></p>`, which a browser renders as one line, still imports without a phantom blank line. Export and import gate on the same `isBlockDomNode` predicate, so a `<br>` gets a terminator exactly when an importer would have dropped it. That keeps the two sides consistent for a break trailing an inline element (`<a>x<br></a>` — no terminator, nothing dropped), for tags outside `BLOCK_TAG_RE` such as `<th>`, and for a `domOverride` that wraps the `<br>` in another element. Code blocks, which export as `<pre>`, were losing a trailing newline the same way and are fixed by the same change. This replaces the import-side gate reverted in the preceding commit (meta#8765's approach). Fixing the export side instead avoids changing how externally authored HTML is pasted, needs no new public API, and also covers the sole-child case that the import-side gate left lossy. ## Test plan New test: `packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts`, covering both importers (`$generateNodesFromDOM` and `$generateNodesFromDOMViaExtension`). Four of its twelve cases are guards that must pass either way: they assert the export shape is unchanged for an interior break and for a break trailing an inline element, and that `<p>a<br></p>` from outside Lexical still imports without a line break. ### Before $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts × a trailing line break exports with a terminating <br> 27ms × a lone line break exports with a terminating <br> 4ms × restores a trailing line break 32ms × restores a lone line break 11ms × restores consecutive trailing line breaks 11ms × restores a trailing line break 12ms × restores a lone line break 9ms × restores consecutive trailing line breaks 10ms ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 8 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…' // Object.is equality AssertionError: expected '<p><br></p>' to be '<p><br><br></p>' // Object.is equality AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] AssertionError: expected [] to deeply equal [ 'linebreak' ] AssertionError: expected [ 'text', 'linebreak' ] to deeply equal [ 'text', 'linebreak', 'linebreak' ] Test Files 1 failed (1) Tests 8 failed | 4 passed (12) ### After $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts Test Files 1 passed (1) Tests 12 passed (12) $ npx vitest run # full jsdom unit suite Test Files 345 passed (345) Tests 6034 passed | 2 skipped (6036) $ pnpm run test-browser # vitest browser mode, chromium Test Files 24 passed (24) Tests 151 passed | 1 skipped (152) $ npx cross-env E2E_BROWSER=chromium playwright test --project=chromium 34 skipped 835 passed (16.6m) $ npx tsc --noEmit -p . # clean $ npx flow check No errors! Firefox and WebKit E2E were not exercised. The rendering rule this change relies on was measured rather than assumed. At `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` also emit `<p>a<br><br></p>` for a trailing soft break, so the terminator emitted here is the platform's own encoding of that content, and Lexical's existing import rules already read it back as a single line break. WebKit could not be launched in this environment, so Safari is unverified on both points.
…ldrn7' into jv-managed-linebreak-import
…reak on export ## Description A `<br>` that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it — `isLastChildInBlockNode` and `isOnlyChildInBlockNode` in `LexicalLineBreakNode`, used by both `LineBreakNode.importDOM` and `@lexical/html`'s `LineBreakRule`. The reconciler already compensates for that rule in the live DOM: when an element ends with a `LineBreakNode` it appends a managed terminator `<br>` (`ElementDOMSlot.insertManagedLineBreak`) so the break actually renders. The HTML exporter had no equivalent, so `$generateHtmlFromNodes` wrote a paragraph ending in a `LineBreakNode` as `<p>a<br></p>` — HTML that renders as a single line, and that every importer reads back without the break at all. A paragraph holding a single `LineBreakNode` came back empty. Emit the same terminator on export, when a block element's last exported child came from a `LineBreakNode` and the DOM it produced is a `<br>`, carrying the same `data-lexical-managed-linebreak="true"` marker the reconciler writes. An export and a scrape of the live DOM then describe a trailing break identically, and a consumer can tell the terminator apart from authored content. The marker is metadata, not load-bearing: the importers match on position, so HTML that has been through a sanitizer that strips unknown `data-*` attributes still round-trips (covered by a test). The importers drop the terminator and keep the authored break, so the round trip is lossless with no change to the import rules — externally authored HTML such as `<p>a<br></p>`, which a browser renders as one line, still imports without a phantom blank line. Export and import gate on the same `isBlockDomNode` predicate, so a `<br>` gets a terminator exactly when an importer would have dropped it. That keeps the two sides consistent for a break trailing an inline element (`<a>x<br></a>` — no terminator, nothing dropped), for tags outside `BLOCK_TAG_RE` such as `<th>`, and for a `domOverride` that wraps the `<br>` in another element. Code blocks, which export as `<pre>`, were losing a trailing newline the same way and are fixed by the same change. This replaces the import-side gate reverted in the preceding commit (meta#8765's approach). Fixing the export side instead avoids changing how externally authored HTML is pasted, needs no new public API, and also covers the sole-child case that the import-side gate left lossy. ## Test plan New test: `packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts`, covering both importers (`$generateNodesFromDOM`, which runs `LineBreakNode`'s `$config()` `importDOM`, and `$generateNodesFromDOMViaExtension`, which runs `coreImportRules`' `LineBreakRule`). Mutation-tested to confirm the two are independently covered: breaking only `LineBreakRule` fails exactly the four `$generateNodesFromDOMViaExtension` cases, and breaking only the `$config()` importer fails exactly the four `$generateNodesFromDOM` cases. Four of its fifteen cases are guards that must pass either way: they assert the export shape is unchanged for an interior break and for a break trailing an inline element, and that `<p>a<br></p>` from outside Lexical still imports without a line break. ### Before $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts × a trailing line break exports with a terminating <br> 32ms × a lone line break exports with a terminating <br> 4ms × the exported terminator carries the reconciler marker 26ms × restores a trailing line break 22ms × restores a lone line break 15ms × restores consecutive trailing line breaks 13ms × restores a trailing line break with the marker stripped 14ms × restores a trailing line break 14ms × restores a lone line break 15ms × restores consecutive trailing line breaks 19ms × restores a trailing line break with the marker stripped 14ms ⎯⎯⎯⎯⎯⎯ Failed Tests 11 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…' // Object.is equality AssertionError: expected '<p><br></p>' to be '<p><br><br data-lexical-managed-lineb…' // Object.is equality AssertionError: expected <br></br> to have a length of 2 but got 1 AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] AssertionError: expected [] to deeply equal [ 'linebreak' ] AssertionError: expected [ 'text', 'linebreak' ] to deeply equal [ 'text', 'linebreak', 'linebreak' ] AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] Test Files 1 failed (1) Tests 11 failed | 4 passed (15) ### After $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ npx vitest run # full jsdom unit suite Test Files 345 passed (345) Tests 6037 passed | 2 skipped (6039) $ pnpm run test-browser # vitest browser mode, chromium Test Files 24 passed (24) Tests 151 passed | 1 skipped (152) $ npx cross-env E2E_BROWSER=chromium playwright test --project=chromium 34 skipped 835 passed (16.6m) $ npx tsc --noEmit -p . # clean $ npx flow check No errors! Firefox and WebKit E2E were not exercised. The rendering rule this change relies on was measured rather than assumed. At `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` also emit `<p>a<br><br></p>` for a trailing soft break, so the terminator emitted here is the platform's own encoding of that content, and Lexical's existing import rules already read it back as a single line break. WebKit could not be launched in this environment, so Safari is unverified on both points.
…reak on export ## Description A `<br>` that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it — `isLastChildInBlockNode` and `isOnlyChildInBlockNode` in `LexicalLineBreakNode`, used by both `LineBreakNode.importDOM` and `@lexical/html`'s `LineBreakRule`. The reconciler already compensates for that rule in the live DOM: when an element ends with a `LineBreakNode` it appends a managed terminator `<br>` (`ElementDOMSlot.insertManagedLineBreak`) so the break actually renders. The HTML exporter had no equivalent, so `$generateHtmlFromNodes` wrote a paragraph ending in a `LineBreakNode` as `<p>a<br></p>` — HTML that renders as a single line, and that every importer reads back without the break at all. A paragraph holding a single `LineBreakNode` came back empty. Emit the same terminator on export, when a block element's last exported child came from a `LineBreakNode` and the DOM it produced is a `<br>`, carrying the same `data-lexical-managed-linebreak="true"` marker the reconciler writes. An export and a scrape of the live DOM then describe a trailing break identically, and a consumer can tell the terminator apart from authored content. The marker is metadata, not load-bearing: the importers match on position, so HTML that has been through a sanitizer that strips unknown `data-*` attributes still round-trips (covered by a test). The importers drop the terminator and keep the authored break, so the round trip is lossless with no change to the import rules — externally authored HTML such as `<p>a<br></p>`, which a browser renders as one line, still imports without a phantom blank line. Export and import gate on the same `isBlockDomNode` predicate, so a `<br>` gets a terminator exactly when an importer would have dropped it. That keeps the two sides consistent for a break trailing an inline element (`<a>x<br></a>` — no terminator, nothing dropped), for tags outside `BLOCK_TAG_RE` such as `<th>`, and for a `domOverride` that wraps the `<br>` in another element. Code blocks, which export as `<pre>`, were losing a trailing newline the same way and are fixed by the same change. This replaces the import-side gate reverted in the preceding commit (meta#8765's approach). Fixing the export side instead avoids changing how externally authored HTML is pasted, needs no new public API, and also covers the sole-child case that the import-side gate left lossy. ## Test plan New test: `packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts`, covering both importers (`$generateNodesFromDOM`, which runs `LineBreakNode`'s `$config()` `importDOM`, and `$generateNodesFromDOMViaExtension`, which runs `coreImportRules`' `LineBreakRule`). Mutation-tested to confirm the two are independently covered: breaking only `LineBreakRule` fails exactly the four `$generateNodesFromDOMViaExtension` cases, and breaking only the `$config()` importer fails exactly the four `$generateNodesFromDOM` cases. Four of its fifteen cases are guards that must pass either way: they assert the export shape is unchanged for an interior break and for a break trailing an inline element, and that `<p>a<br></p>` from outside Lexical still imports without a line break. ### Before $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts × a trailing line break exports with a terminating <br> 32ms × a lone line break exports with a terminating <br> 4ms × the exported terminator carries the reconciler marker 26ms × restores a trailing line break 22ms × restores a lone line break 15ms × restores consecutive trailing line breaks 13ms × restores a trailing line break with the marker stripped 14ms × restores a trailing line break 14ms × restores a lone line break 15ms × restores consecutive trailing line breaks 19ms × restores a trailing line break with the marker stripped 14ms ⎯⎯⎯⎯⎯⎯ Failed Tests 11 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…' // Object.is equality AssertionError: expected '<p><br></p>' to be '<p><br><br data-lexical-managed-lineb…' // Object.is equality AssertionError: expected <br></br> to have a length of 2 but got 1 AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] AssertionError: expected [] to deeply equal [ 'linebreak' ] AssertionError: expected [ 'text', 'linebreak' ] to deeply equal [ 'text', 'linebreak', 'linebreak' ] AssertionError: expected [ 'text' ] to deeply equal [ 'text', 'linebreak' ] Test Files 1 failed (1) Tests 11 failed | 4 passed (15) ### After $ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ npx vitest run # full jsdom unit suite Test Files 345 passed (345) Tests 6037 passed | 2 skipped (6039) $ pnpm run test-browser # vitest browser mode, chromium Test Files 24 passed (24) Tests 151 passed | 1 skipped (152) $ npx cross-env E2E_BROWSER=chromium playwright test --project=chromium 34 skipped 835 passed (19.9m) $ npx tsc --noEmit -p . # clean $ npx flow check No errors! Firefox and WebKit E2E were not exercised. The rendering rule this change relies on was measured rather than assumed. At `line-height: 20px`, in both Chromium and Firefox, `<p>a</p>` and `<p>a<br></p>` are both 20px while `<p>a<br><br></p>` is 40px — a lone trailing `<br>` does not render. Both engines' own `contenteditable` also emit `<p>a<br><br></p>` for a trailing soft break, so the terminator emitted here is the platform's own encoding of that content, and Lexical's existing import rules already read it back as a single line break. WebKit could not be launched in this environment, so Safari is unverified on both points.
…ged line break
## Description
The terminating `<br>` the exporter appends after a trailing `LineBreakNode` is
the same construct the reconciler injects in the live DOM, but it was emitted
bare while the reconciler marks its own with
`data-lexical-managed-linebreak="true"`
(`ElementDOMSlot.insertManagedLineBreak`).
Write the same marker on export. An export and a scrape of the live DOM then
describe a trailing break identically, and a consumer reading the HTML can tell
the terminator apart from authored content rather than having to infer it from
position.
The marker is metadata, not load-bearing. Both importers still match on
position, so HTML that has passed through a sanitizer which strips unknown
`data-*` attributes round-trips exactly the same — there is a test for that. No
import rule keys off the marker, and no new public API: the attribute name is a
literal with a comment pointing at the reconciler, since `lexical` exports no
attribute-name constants today.
## Test plan
Three cases added to `LineBreakRoundTrip.test.ts` (12 -> 15): the exported
terminator carries the marker and the authored `<br>` does not; the two export
shapes include it; and a round trip still succeeds with the marker stripped.
### Before
$ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts
× a trailing line break exports with a terminating <br> 34ms
× a lone line break exports with a terminating <br> 4ms
× the exported terminator carries the reconciler marker 28ms
AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><span style="white-space: pre-wrap…'
AssertionError: expected '<p><br><br></p>' to be '<p><br><br data-lexical-managed-lineb…'
AssertionError: expected null to be 'true'
Test Files 1 failed (1)
Tests 3 failed | 12 passed (15)
### After
$ npx vitest run packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts
Test Files 1 passed (1)
Tests 15 passed (15)
Both importers are independently covered, confirmed by mutation testing:
breaking only `coreImportRules`' `LineBreakRule` fails exactly the four
`$generateNodesFromDOMViaExtension` cases, and breaking only `LineBreakNode`'s
`$config()` `importDOM` fails exactly the four `$generateNodesFromDOM` cases.
|
Will this be included in the 0.50 release? That would be great |
…havior changes and deprecations into CHANGELOG.md for v0.23 to v0.52, add an Upgrading page ## Description Depends on the changesets tooling in #9357. Past versions in CHANGELOG.md only list commits, so someone upgrading from an older version cannot find what broke, or how to migrate, without reading every GitHub release. Future versions get curated sections from changesets; this gives v0.23.0 to v0.52.0 the same sections. - Every version from v0.23.0 to v0.52.0 now starts with `### Breaking changes` (35 entries), `### Behavior changes` (43) and `### Deprecations` (29), each with packages and a PR link. The existing lines are kept, unchanged, under `### All changes` (the diff only adds lines). - Rule, stated in the preamble: a breaking change needs a code change (an API removed or renamed, a type that no longer checks, a call that now throws) and has `Detect:` and `Migration:`. A behavior change keeps building but behaves or renders differently and has `Verify:`. The entries come from a verified inventory of those releases, which marked each change as manual, mechanical, or runtime-only ("no type or build error points at it"). Runtime-only entries are behavior changes, except #7341 (now throws) and #8603 (a signature change), which stay breaking. - v0.50.0: the items its release notes list as "Behavior Changes to Watch" (#8765, #8908, #8884, #9059, #9061, #8940, #8283) are behavior changes; the listener return-type tightening (#8990), the new invariants (#6483) and the two items the release called breaking (#8938, #8995) stay breaking. Checked against the v0.50.0 release notes. - Added the v0.42.0 removal of `@lexical/react/LexicalContextMenuPlugin` (#8199; present in the v0.41.0 tag, absent in v0.42.0). - Deprecations come from a pass over every `@deprecated` tag added between consecutive tags v0.22.0 to v0.52.0, each checked against the previous tag and attributed with `git log -S`. Each has `Replacement:` and `Removal:`: the version that removed it, "no earlier than v0.53.0" for the ones #9316 removes after v0.52.0, otherwise "not scheduled". Skipped: tags that only moved (`trimTextContentFromAnchor`, deprecated before v0.23.0) or restated earlier ones (v0.46.0 `HorizontalRulePlugin`, v0.51.0 `getScrollParent`), and the v0.47.0 `applyFormatToDom` deprecation that v0.48.0 reversed. - Entries that mixed a breaking change and a deprecation are split with no double counting (#7357, #8360, #8661); #8350 and #8662 only deprecated aliases, so they are listed under Deprecations alone. - `docs/upgrading.md` ("Upgrading" in the sidebar) is a short page: upgrade all packages together, read CHANGELOG.md between your version and the target (also at `node_modules/lexical/CHANGELOG.md`) or print that range, and a copy-paste `AGENTS.md` snippet that points coding agents at the installed changelog. Nothing is installed automatically. - The repository CHANGELOG.md test checks every backfilled entry: `Detect:` and `Migration:` on breaking changes, `Verify:` on behavior changes, `Replacement:` and a valid `Removal:` on deprecations, a PR link on all. ## Test plan ### Before ``` $ pnpm run changelog:range -- --from 0.49.0 --breaking Note: v0.50.0, v0.51.0, v0.52.0 have no curated sections (the changelog only lists their commits), so --breaking cannot show what changed there. Run without --breaking to read them in full. No breaking changes, behavior changes or deprecations recorded after 0.49.0. ``` ### After ``` $ git diff --numstat --diff-algorithm=patience HEAD~1 -- CHANGELOG.md 1412 0 CHANGELOG.md $ pnpm run changelog:range -- --from 0.49.0 --breaking | grep '^## \|^### ' ## v0.50.0 (2026-09-02) ### Breaking changes ### Behavior changes ### Deprecations ## v0.51.0 (2026-09-17) ### Breaking changes ### Deprecations ## v0.52.0 (2026-09-27) ### Breaking changes ### Behavior changes ### Deprecations $ pnpm run changelog:range -- --from 0.49.0 --to 0.50.0 --breaking --json # (kind, [(pr, behavior)]) [('breaking', [(8938, False), (8995, False), (8990, False), (6483, False)]), ('behavior', [(8765, True), (8908, True), (8884, True), (9059, True), (9061, True), (8940, True), (8283, True)]), ('deprecation', [(8918, False)])] $ pnpm run test-unit scripts/__tests__/unit/changelog.test.ts scripts/__tests__/unit/changesets.test.ts Tests 63 passed (63) $ pnpm -C packages/lexical-website run docusaurus build [SUCCESS] Generated static files in "build". ```
…havior changes and deprecations into CHANGELOG.md for v0.23 to v0.52, add an Upgrading page ## Description Depends on the changesets tooling in #9357. Past versions in CHANGELOG.md only list commits, so someone upgrading from an older version cannot find what broke, or how to migrate, without reading every GitHub release. Future versions get curated sections from changesets; this gives v0.23.0 to v0.52.0 the same sections. - Every version from v0.23.0 to v0.52.0 now starts with `### Breaking changes` (35 entries), `### Behavior changes` (43) and `### Deprecations` (29), each with packages and a PR link. The existing lines are kept, unchanged, under `### All changes` (the diff only adds lines). - Rule, stated in the preamble: a breaking change needs a code change (an API removed or renamed, a type that no longer checks, a call that now throws) and has `Detect:` and `Migration:`. A behavior change keeps building but behaves or renders differently and has `Verify:`. The entries come from a verified inventory of those releases, which marked each change as manual, mechanical, or runtime-only ("no type or build error points at it"). Runtime-only entries are behavior changes, except #7341 (now throws) and #8603 (a signature change), which stay breaking. - v0.50.0: the items its release notes list as "Behavior Changes to Watch" (#8765, #8908, #8884, #9059, #9061, #8940, #8283) are behavior changes; the listener return-type tightening (#8990), the new invariants (#6483) and the two items the release called breaking (#8938, #8995) stay breaking. Checked against the v0.50.0 release notes. - Added the v0.42.0 removal of `@lexical/react/LexicalContextMenuPlugin` (#8199; present in the v0.41.0 tag, absent in v0.42.0). - Deprecations come from a pass over every `@deprecated` tag added between consecutive tags v0.22.0 to v0.52.0, each checked against the previous tag and attributed with `git log -S`. Each has `Replacement:` and `Removal:`: the version that removed it, "no earlier than v0.53.0" for the ones #9316 removes after v0.52.0, otherwise "not scheduled". Skipped: tags that only moved (`trimTextContentFromAnchor`, deprecated before v0.23.0) or restated earlier ones (v0.46.0 `HorizontalRulePlugin`, v0.51.0 `getScrollParent`), and the v0.47.0 `applyFormatToDom` deprecation that v0.48.0 reversed. - Entries that mixed a breaking change and a deprecation are split with no double counting (#7357, #8360, #8661); #8350 and #8662 only deprecated aliases, so they are listed under Deprecations alone. - `docs/upgrading.md` ("Upgrading" in the sidebar) is a short page: upgrade all packages together, read CHANGELOG.md between your version and the target (also at `node_modules/lexical/CHANGELOG.md`) or print that range, and a copy-paste `AGENTS.md` snippet that points coding agents at the installed changelog. Nothing is installed automatically. - The repository CHANGELOG.md test checks every backfilled entry: `Detect:` and `Migration:` on breaking changes, `Verify:` on behavior changes, `Replacement:` and a valid `Removal:` on deprecations, a PR link on all. ## Test plan ### Before ``` $ pnpm run changelog:range -- --from 0.49.0 --breaking Note: v0.50.0, v0.51.0, v0.52.0 have no curated sections (the changelog only lists their commits), so --breaking cannot show what changed there. Run without --breaking to read them in full. No breaking changes, behavior changes or deprecations recorded after 0.49.0. ``` ### After ``` $ git diff --numstat --diff-algorithm=patience HEAD~1 -- CHANGELOG.md 1412 0 CHANGELOG.md $ pnpm run changelog:range -- --from 0.49.0 --breaking | grep '^## \|^### ' ## v0.50.0 (2026-09-02) ### Breaking changes ### Behavior changes ### Deprecations ## v0.51.0 (2026-09-17) ### Breaking changes ### Deprecations ## v0.52.0 (2026-09-27) ### Breaking changes ### Behavior changes ### Deprecations $ pnpm run changelog:range -- --from 0.49.0 --to 0.50.0 --breaking --json # (kind, [(pr, behavior)]) [('breaking', [(8938, False), (8995, False), (8990, False), (6483, False)]), ('behavior', [(8765, True), (8908, True), (8884, True), (9059, True), (9061, True), (8940, True), (8283, True)]), ('deprecation', [(8918, False)])] $ pnpm run test-unit scripts/__tests__/unit/changelog.test.ts scripts/__tests__/unit/changesets.test.ts Tests 63 passed (63) $ pnpm -C packages/lexical-website run docusaurus build [SUCCESS] Generated static files in "build". ```
…havior changes and deprecations into CHANGELOG.md for v0.23 to v0.52, add an Upgrading page Depends on the changesets tooling in #9357. Past versions in CHANGELOG.md only list commits, so someone upgrading from an older version cannot find what broke, or how to migrate, without reading every GitHub release. Future versions get curated sections from changesets; this gives v0.23.0 to v0.52.0 the same sections. - Every version from v0.23.0 to v0.52.0 now starts with `### Breaking changes` (35 entries), `### Behavior changes` (43) and `### Deprecations` (29), each with packages and a PR link. The existing lines are kept, unchanged, under `### All changes` (the diff only adds lines). - Rule, stated in the preamble: a breaking change needs a code change (an API removed or renamed, a type that no longer checks, a call that now throws) and has `Detect:` and `Migration:`. A behavior change keeps building but behaves or renders differently and has `Verify:`. The entries come from a verified inventory of those releases, which marked each change as manual, mechanical, or runtime-only ("no type or build error points at it"). Runtime-only entries are behavior changes, except #7341 (now throws) and #8603 (a signature change), which stay breaking. - v0.50.0: the items its release notes list as "Behavior Changes to Watch" (#8765, #8908, #8884, #9059, #9061, #8940, #8283) are behavior changes; the listener return-type tightening (#8990), the new invariants (#6483) and the two items the release called breaking (#8938, #8995) stay breaking. Checked against the v0.50.0 release notes. - Added the v0.42.0 removal of `@lexical/react/LexicalContextMenuPlugin` (#8199; present in the v0.41.0 tag, absent in v0.42.0). - Deprecations come from a pass over every `@deprecated` tag added between consecutive tags v0.22.0 to v0.52.0, each checked against the previous tag and attributed with `git log -S`. Each has `Replacement:` and `Removal:`: the version that removed it, "no earlier than v0.53.0" for the ones #9316 removes after v0.52.0, otherwise "not scheduled". Skipped: tags that only moved (`trimTextContentFromAnchor`, deprecated before v0.23.0) or restated earlier ones (v0.46.0 `HorizontalRulePlugin`, v0.51.0 `getScrollParent`), and the v0.47.0 `applyFormatToDom` deprecation that v0.48.0 reversed. - Entries that mixed a breaking change and a deprecation are split with no double counting (#7357, #8360, #8661); #8350 and #8662 only deprecated aliases, so they are listed under Deprecations alone. - `docs/upgrading.md` ("Upgrading" in the sidebar) is a short page: upgrade all packages together, read CHANGELOG.md between your version and the target (also at `node_modules/lexical/CHANGELOG.md`) or print that range, and a copy-paste `AGENTS.md` snippet that points coding agents at the installed changelog. Nothing is installed automatically. - The repository CHANGELOG.md test checks every backfilled entry: `Detect:` and `Migration:` on breaking changes, `Verify:` on behavior changes, `Replacement:` and a valid `Removal:` on deprecations, a PR link on all. ``` $ pnpm run changelog:range -- --from 0.49.0 --breaking Note: v0.50.0, v0.51.0, v0.52.0 have no curated sections (the changelog only lists their commits), so --breaking cannot show what changed there. Run without --breaking to read them in full. No breaking changes, behavior changes or deprecations recorded after 0.49.0. ``` ``` $ git diff --numstat --diff-algorithm=patience HEAD~1 -- CHANGELOG.md 1412 0 CHANGELOG.md $ pnpm run changelog:range -- --from 0.49.0 --breaking | grep '^## \|^### ' $ pnpm run changelog:range -- --from 0.49.0 --to 0.50.0 --breaking --json # (kind, [(pr, behavior)]) [('breaking', [(8938, False), (8995, False), (8990, False), (6483, False)]), ('behavior', [(8765, True), (8908, True), (8884, True), (9059, True), (9061, True), (8940, True), (8283, True)]), ('deprecation', [(8918, False)])] $ pnpm run test-unit scripts/__tests__/unit/changelog.test.ts scripts/__tests__/unit/changesets.test.ts Tests 63 passed (63) $ pnpm -C packages/lexical-website run docusaurus build [SUCCESS] Generated static files in "build". ```
Description
A
<br>that is the last (or only) child of a block element is not rendered by browsers, so both HTML importers deliberately drop it (isLastChildInBlockNode/isOnlyChildInBlockNode). That rule is correct for pasted HTML, but it meant a trailingLineBreakNodewas lost on every export → import round trip, and a paragraph holding a singleLineBreakNodecame back empty.Earlier revisions of this PR fixed that on the import side, keeping an unmarked trailing
<br>. This revision reverts that and fixes the export side instead, because measurement showed the import-side gate regressed the common paste path.Why the export side
Measured at
line-height: 20px:<p>a</p><p>a<br></p><p>a<br><br></p>A lone trailing
<br>renders as nothing, so an application cannot express a visible trailing break that way — there is no content there to preserve. And both engines' owncontenteditableemit<p>a<br><br></p>for a trailing soft break (a+ Shift+Enter).<br><br>is the platform's annotation-free encoding, and Lexical's existing import rules already read it back as exactly one line break.The defect was that Lexical's exporter wrote
<p>a<br></p>— markup no browser produces for that content. The reconciler already solves this in the live DOM by appending a managed terminator<br>(ElementDOMSlot.insertManagedLineBreak); the exporter had no equivalent.What this PR does
Emit the same terminator on export, when a block element's last exported child came from a
LineBreakNodeand the DOM it produced is a<br>, carrying the samedata-lexical-managed-linebreak="true"marker the reconciler writes. An export and a scrape of the live DOM then describe a trailing break identically, and a consumer can tell the terminator apart from authored content.The marker is metadata, not load-bearing — both importers still match on position, so HTML through a sanitizer that strips unknown
data-*attributes round-trips unchanged (there is a test). No import rule keys off it, and no new public API: the attribute name is a literal with a comment pointing at the reconciler.This lives in the
@lexical/htmlexporter walk rather than aLineBreakNode.exportDOMoverride, because block-ness of the parent is only knowable there — when a leaf'sexportDOMruns, the parent's element does not exist yet. Export and import gate on the sameisBlockDomNodepredicate, so a<br>gets a terminator exactly when an importer would have dropped it.Behaviour
Measured on this branch, both importers agreeing:
LineBreakNodes<p><br></p><p>2<br><br data-lexical-managed-linebreak="true"></p><p>2<br><br><br data-lexical-managed-linebreak="true"></p><p>2<br><br><br><br data-lexical-managed-linebreak="true"></p>Externally authored HTML is unchanged from
main:<p>2<br></p>[text]— no phantom blank line<p><br></p>[]— empty block<p>2<br><br></p>[text, linebreak]— the platform's encoding, one breakThe import rules are untouched. Code blocks, which export as
<pre>, were losing a trailing newline the same way and are fixed by the same change. A break ending an inline element (<a>x<br></a>) gets no terminator, since nothing would drop it.Cost
Exported HTML gains one
<br>per trailing break. A naive html-to-text converter will count two newlines fora<br><br>— but it would do the same for Chromium's and Firefox's owncontenteditableoutput, so this is no worse than the platform, which is the right bar for an interop format. The marker makes the terminator identifiable for consumers that want to skip it.Also in this branch
mainthat this PR needed (it was 185 commits behind, andLineBreakNodehad since moved fromstatic importDOM()to$config()).isManagedLineBreakhelper and itslexicalexport, and the repurposedLexicalEventHelperscase.isManagedLineBreak→isEditorManagedLineBreakrename inLexicalMutations, which is clearer on its own.Test plan
New
packages/lexical-html/src/__tests__/unit/LineBreakRoundTrip.test.ts, 15 cases covering both importers —$generateNodesFromDOM(which runsLineBreakNode's$config()importDOM) and$generateNodesFromDOMViaExtension(which runscoreImportRules'LineBreakRule). Four cases are guards that must pass either way, including that<p>a<br></p>from outside Lexical still imports without a line break.Both importers are independently covered, confirmed by mutation testing: breaking only
LineBreakRulefails exactly the four$generateNodesFromDOMViaExtensioncases; breaking only the$config()importer fails exactly the four$generateNodesFromDOMcases.Before
After
WebKit could not be launched in the environment used, so Safari is unverified on both the rendering rule and the
contenteditableconvention above; Firefox and WebKit E2E were not run.