Repository navigation
[lexical-html] Bug Fix: preserve blank blocks in the legacy HTML importer - #9271
maximilliangrand wants to merge 1 commit into
Conversation
…rter ## Description Pasting HTML with consecutive `<div><br></div>` blocks through the default rich-text clipboard path loses paragraph boundaries. The line-break importer correctly omits a sole placeholder `<br>`, but a transparent block then has no Lexical children and disappears. This affects the Gmail quote markup reported by @djheyson in meta#4830. Preserve an empty wrapper for these blocks in `$generateNodesFromDOM`, using the existing paragraph/artificial-wrapper logic and alignment handling. The change respects custom conversion callbacks and leaves converted containers, code content, and trailing line breaks unchanged. Related to meta#4830. This fixes the default/legacy HTML importer; the experimental `DOMImportExtension` pipeline is unchanged and needs separate work. ## Test plan ### Before On unchanged `30772fe845a6fbd0efbd242bea9c3d701ea9e614`, with the new regression tests: ```sh ./node_modules/.bin/vitest --project unit --run packages/lexical-html/src/__tests__/unit/EmptyBlockImport.test.ts ``` ```text Test Files 1 failed (1) Tests 11 failed | 18 passed (29) ``` ### After The same command with this change: ```text Test Files 1 passed (1) Tests 29 passed (29) ``` Coverage includes consecutive and nested blank blocks, alignment, rich-text clipboard insertion, custom `after`/`forChild` callbacks, and existing code/list/table/line-break behavior. ```sh ./node_modules/.bin/vitest --project unit --run \ packages/lexical-html/src/__tests__/unit \ packages/lexical-code-core/src/__tests__/unit \ packages/lexical-list/src/__tests__/unit \ packages/lexical-table/src/__tests__/unit \ packages/lexical-clipboard/src/__tests__/unit ``` ```text Test Files 61 passed (61) Tests 596 passed (596) ``` Also passed: ```sh ./node_modules/.bin/eslint packages/lexical-html/src/index.ts packages/lexical-html/src/__tests__/unit/EmptyBlockImport.test.ts ./node_modules/.bin/prettier --check packages/lexical-html/src/index.ts packages/lexical-html/src/__tests__/unit/EmptyBlockImport.test.ts ./node_modules/.bin/tsc -p tsconfig.test.json --noEmit git diff --check ``` Local validation used macOS arm64 and Node 26.7.0. The full monorepo, Windows, and Linux test matrices were not run locally. Implemented with Codex assistance; the reported reproduction is credited to @djheyson.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…ion HTML import ## Description The experimental HTML importer drops empty transparent blocks such as `<div><br></div>`. In the Gmail reproduction from meta#4830, pasting yields nine paragraphs instead of eleven. Preserve those authored blank boundaries at the root and in table cells, list items, and code content. The default BR rule retains a sole placeholder only within its owning transparent block. A custom rule that discards the BR, including middleware that discards `$next()`'s result, remains authoritative. List segments are joined by position so a leading empty segment retains its separator. Code imports use the normalization in meta#9272, including when a blank appears beside nested code or a custom paragraph replacement. This follow-up changes two production files and adds regression coverage; it adds no public API. Related to meta#4830 (thanks to @djheyson for the reproduction). meta#9271 addresses the default importer separately. Developed with Codex assistance. ## Test plan ### Before Copied the final unit and browser tests onto the prerequisite at `a6328b61bbb00dabf3d894dfba63ef9754275f0f`: ```sh ./node_modules/.bin/vitest --project unit --run packages/lexical-html/src/__tests__/unit/EmptyBlockImportExtension.test.ts packages/lexical-html/src/__tests__/unit/EmptyBlockImportOverrides.test.ts ``` ```text Test Files 2 failed (2) Tests 28 failed | 5 passed (33) ``` ```sh VITEST_BROWSER=chromium,firefox,webkit ./node_modules/.bin/vitest run --project browser packages/lexical-html/src/__tests__/browser/EmptyBlockImportExtension.test.ts ``` ```text Test Files 3 failed (3) Tests 3 failed (3) ``` ### After The same unit command: ```text Test Files 2 passed (2) Tests 33 passed (33) ``` Gmail paste and the prerequisite's code-paste regression together in all three engines: ```sh VITEST_BROWSER=chromium,firefox,webkit ./node_modules/.bin/vitest run --project browser packages/lexical-code-core/src/__tests__/browser/CodeContentImport.test.ts packages/lexical-html/src/__tests__/browser/EmptyBlockImportExtension.test.ts ``` ```text Test Files 6 passed (6) Tests 6 passed (6) ``` All 981 affected tests pass across HTML, code-core, code-prism, code-shiki, list, table, rich-text, and clipboard. Test TypeScript, Flow (zero errors), changed-file ESLint/Prettier, and `git diff --check` pass. Coverage includes leading/consecutive/trailing blanks, nested transparent blocks, table cells, list segments, all ordinary code import paths, exact blank-line counts beside nested code, custom deletion/replacement rules, paragraph replacements, and Gmail formatting/link preservation. Browser checks use native synthetic clipboard events dispatched to mounted contenteditables in Chromium, Firefox, and WebKit on macOS. The full playground end-to-end matrix was not run locally.
…ion HTML import ## Description The experimental HTML importer drops empty transparent blocks such as `<div><br></div>`. In the Gmail reproduction from meta#4830, pasting produces nine paragraphs instead of eleven. Preserve these authored blank boundaries at the root and in table cells, list items, and code content. The default BR rule retains a sole placeholder only within its owning transparent block. Custom rules that discard the BR, including middleware that discards `$next()`'s result, remain authoritative. List segments are joined by position so a leading empty segment retains its separator. Code imports use the normalization merged in meta#9272. The change touches two production files and adds regression coverage without adding public API. Related to meta#4830 (thanks to @djheyson for the reproduction); meta#9271 addresses the default importer separately. Developed with Codex assistance. ## Test plan ### Before Copied the three regression files onto unchanged upstream `7bfd3e4ef532f87bdace863afe3821a092693b51`, which includes meta#9272: ```sh ./node_modules/.bin/vitest --project unit --run packages/lexical-html/src/__tests__/unit/EmptyBlockImportExtension.test.ts packages/lexical-html/src/__tests__/unit/EmptyBlockImportOverrides.test.ts ``` ```text Test Files 2 failed (2) Tests 28 failed | 5 passed (33) ``` ```sh VITEST_BROWSER=chromium,firefox,webkit ./node_modules/.bin/vitest run --project browser packages/lexical-html/src/__tests__/browser/EmptyBlockImportExtension.test.ts ``` ```text Test Files 3 failed (3) Tests 3 failed (3) ``` ### After The same focused unit command: ```text Test Files 2 passed (2) Tests 33 passed (33) ``` Gmail paste and the merged code-paste regression together: ```sh VITEST_BROWSER=chromium,firefox,webkit ./node_modules/.bin/vitest run --project browser packages/lexical-code-core/src/__tests__/browser/CodeContentImport.test.ts packages/lexical-html/src/__tests__/browser/EmptyBlockImportExtension.test.ts ``` ```text Test Files 6 passed (6) Tests 6 passed (6) ``` All 988 affected unit tests pass across HTML, code-core, code-prism, code-shiki, list, table, rich-text, and clipboard. Test TypeScript, Flow (zero errors), changed-file ESLint/Prettier, and `git diff --check` pass. Coverage includes leading/consecutive/trailing blanks, nested transparent blocks, table cells, list segments, ordinary code import paths, exact blank-line counts beside nested code, custom deletion/replacement rules, paragraph replacements, and Gmail formatting/link preservation. Unit fixtures use DOMParser to match the current test environment. Browser checks dispatch native synthetic clipboard events to mounted contenteditables in Chromium, Firefox, and WebKit on macOS. The full playground end-to-end matrix was not run locally.
There was a problem hiding this comment.
Thanks for tracking this down. The change looks good to me. The guard in $generateNodesFromDOM is narrow: it only applies to unconverted blocks with a sole <br>, it respects custom after/forChild conversions, and it makes <div><br></div> behave like <p><br></p> already does. The new tests fail before the fix and pass after it. The suggestions below are optional and non-blocking.
- Blank lines inside code, quote and list blocks also change (
packages/lexical-html/src/index.ts:585-594). Under a block node, the preserved block becomes an artificial wrapper, and$unwrapArtificialNodesputs a line break between adjacent wrappers. So<pre><div>a</div><div><br></div><div>b</div></pre>now imports asa\n\nb(it wasa\nb), and<blockquote>and<li>with the same children change the same way. That matches what a browser renders and looks like an improvement. But the description says code content is unchanged, and the existing code tests only use a single blank block. Consider adding a case like['<pre><div>a</div><div><br></div><div>b</div></pre>', 'a\n\nb']toEmptyBlockImport.test.tsand updating the description.AGENTS.mdasks the Description to say what the change makes the code do. - Follow-up for #4830. The issue's first blank lines are formatted (
<div><b><br></b></div>). Those still import as a paragraph containing a line break, which renders two lines tall next to the new single-line empty paragraphs. Since the PR says "Related to #4830", it would help to note this remaining case on the issue so it stays open for it.
| childLexicalNodes = wrapContinuousInlines( | ||
| node, | ||
| childLexicalNodes, | ||
| () => { | ||
| const artificialNode = new ArtificialNode__DO_NOT_USE(); | ||
| allArtificialNodes.push(artificialNode); | ||
| return artificialNode; | ||
| }, | ||
| preserveEmptyBlock, | ||
| ); |
There was a problem hiding this comment.
Optional: under a block node such as <pre>, <blockquote> or <li>, the preserved block becomes an artificial wrapper, and $unwrapArtificialNodes inserts a line break between adjacent wrappers. So <pre><div>a</div><div><br></div><div>b</div></pre> now imports as a\n\nb (previously a\nb). That looks right, but the description says code content is unchanged and no test covers it. Consider adding this case to EmptyBlockImport.test.ts.
Description
Pasting HTML with consecutive
<div><br></div>blocks through the default rich-text clipboard path loses paragraph boundaries. The line-break importer correctly omits a sole placeholder<br>, but a transparent block then has no Lexical children and disappears. This affects the Gmail quote markup reported by @djheyson in #4830.Preserve an empty wrapper for these blocks in
$generateNodesFromDOM, using the existing paragraph/artificial-wrapper logic and alignment handling. The change respects custom conversion callbacks and leaves converted containers, code content, and trailing line breaks unchanged.Related to #4830. This fixes the default/legacy HTML importer; the experimental
DOMImportExtensionpipeline is unchanged and needs separate work.Test plan
Before
On unchanged
30772fe845a6fbd0efbd242bea9c3d701ea9e614, with the new regression tests:After
The same command with this change:
Coverage includes consecutive and nested blank blocks, alignment, rich-text clipboard insertion, custom
after/forChildcallbacks, and existing code/list/table/line-break behavior.Also passed:
Local validation used macOS arm64 and Node 26.7.0. The full monorepo, Windows, and Linux test matrices were not run locally.
Implemented with Codex assistance; the reported reproduction is credited to @djheyson.