Skip to content

[lexical-html] Bug Fix: preserve blank blocks in the legacy HTML importer - #9271

Open
maximilliangrand wants to merge 1 commit into
meta:mainfrom
maximilliangrand:codex/preserve-html-empty-blocks
Open

maximilliangrand wants to merge 1 commit into
meta:mainfrom
maximilliangrand:codex/preserve-html-empty-blocks

Conversation

@maximilliangrand

Copy link
Copy Markdown
Contributor

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 DOMImportExtension pipeline is unchanged and needs separate work.

Test plan

Before

On unchanged 30772fe845a6fbd0efbd242bea9c3d701ea9e614, with the new regression tests:

./node_modules/.bin/vitest --project unit --run packages/lexical-html/src/__tests__/unit/EmptyBlockImport.test.ts
Test Files  1 failed (1)
     Tests  11 failed | 18 passed (29)

After

The same command with this change:

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.

./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
Test Files  61 passed (61)
     Tests  596 passed (596)

Also passed:

./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.

…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.
@vercel

vercel Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 29, 2026 10:49am UTC
lexical-playground Ready Ready Preview Sep 29, 2026 10:49am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 29, 2026
maximilliangrand added a commit to maximilliangrand/lexical that referenced this pull request Sep 30, 2026
…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.
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 30, 2026
maximilliangrand added a commit to maximilliangrand/lexical that referenced this pull request Oct 1, 2026
…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.

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. 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 $unwrapArtificialNodes puts a line break between adjacent wrappers. So <pre><div>a</div><div><br></div><div>b</div></pre> now imports as a\n\nb (it was a\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'] to EmptyBlockImport.test.ts and updating the description. AGENTS.md asks the Description to say what the change makes the code do.
  2. 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.

Comment on lines +585 to +594
childLexicalNodes = wrapContinuousInlines(
node,
childLexicalNodes,
() => {
const artificialNode = new ArtificialNode__DO_NOT_USE();
allArtificialNodes.push(artificialNode);
return artificialNode;
},
preserveEmptyBlock,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: 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.

This branch was successfully deployed

2 active deployments
Preview – lexical — 58c41c76 Deployed Sep 29, 2026 by vercel[bot]
Preview – lexical-playground — 58c41c76 Deployed Sep 29, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants