Skip to content

[lexical][lexical-html][lexical-website] Bug Fix: Keep the spaces around inline tags such as <strike> and <font> on HTML import - #9744

Merged
potatowagon merged 1 commit into
meta:mainfrom
vjeux:html-import-inline-tag-spaces
Nov 3, 2026

Conversation

@vjeux

@vjeux vjeux commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Pasting HTML that uses <strike>, <font>, <small> or a few other inline tags deletes the spaces on both sides of them, so words run together: is <strike>old</strike> now imports as isoldnow. Gmail's composer writes <strike> and <font>, so pasting from an email hits this.

Both HTML importers ($generateNodesFromDOM and DOMImportExtension) collapse a text node's leading and trailing space against its inline neighbors, and they ask isInlineDomNode what is inline. Its tag list, INLINE_TAG_RE, was missing these standard inline elements, so they were taken for block edges and the spaces next to them were trimmed.

This adds bdi, bdo, big, data, dfn, font, nobr, small and strike to that list, and to the copy of the list in the DOM import guide. Unknown custom elements are still treated as blocks by default, so the <tooltip> case from #8391 keeps its behavior and its test. This keeps the words and their spaces; it adds no formatting for these tags. isInlineDomNode is exported, so its callers now see these tags as inline too: $generateNodesFromDOM turns an empty block between two of them into a line break, as it already did between two <span>s, and a new test pins that.

Test plan

Before

The new tests on main:

$ pnpm exec vitest run --project unit packages/lexical-html/src/__tests__/unit/CoreImportExtension.test.ts packages/lexical-html/src/__tests__/unit/LexicalHtml.test.ts
     × keeps the spaces around an inline <bdi>
     × keeps the spaces around an inline <bdo>
     × keeps the spaces around an inline <big>
     × keeps the spaces around an inline <data>
     × keeps the spaces around an inline <dfn>
     × keeps the spaces around an inline <font>
     × keeps the spaces around an inline <nobr>
     × keeps the spaces around an inline <small>
     × keeps the spaces around an inline <strike>
     × $generateNodesFromDOM keeps the spaces around an inline <bdi>
     × $generateNodesFromDOM keeps the spaces around an inline <bdo>
     × $generateNodesFromDOM keeps the spaces around an inline <big>
     × $generateNodesFromDOM keeps the spaces around an inline <data>
     × $generateNodesFromDOM keeps the spaces around an inline <dfn>
     × $generateNodesFromDOM keeps the spaces around an inline <font>
     × $generateNodesFromDOM keeps the spaces around an inline <nobr>
     × $generateNodesFromDOM keeps the spaces around an inline <small>
     × $generateNodesFromDOM keeps the spaces around an inline <strike>
AssertionError: expected 'isoldnow' to be 'is old now' // Object.is equality
      Tests  18 failed | 34 passed (52)
$ pnpm exec vitest run --project unit packages/lexical-html/src/__tests__/unit/LexicalHtml.test.ts -t "breaks the line"
     × $generateNodesFromDOM breaks the line at an empty block between two inline <font>s, as between two <span>s
AssertionError: expected 'ab' to be 'a\nb' // Object.is equality

After

$ pnpm exec vitest run --project unit packages/lexical-html
      Tests  189 passed (189)
$ pnpm exec vitest run --project unit
 Test Files  679 passed | 4 skipped (683)
      Tests  29186 passed | 373 expected fail | 136 skipped (29695)

pnpm run tsc, pnpm run tsc-test, pnpm run flow, pnpm run check-changesets, and ESLint and Prettier on the changed files pass. Not run in a browser: the change is the tag list both importers read, and these tests drive both importers with HTML parsed by DOMParser, as pasted HTML is.

@vercel

vercel Bot commented Nov 3, 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 Nov 3, 2026 6:59am UTC
lexical-playground Ready Ready Preview Nov 3, 2026 6:59am UTC

Request Review

@vjeux
vjeux requested a review from etrepum as a code owner November 3, 2026 05:52
@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 Nov 3, 2026
@meta-code-review

meta-code-review Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 3, 2026, 7:10 AM UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9744

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9744

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9744

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9744

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9744

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9744

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9744

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9744

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9744

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9744

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9744

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9744

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9744

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9744

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9744

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9744

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9744

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9744

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9744

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9744

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9744

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9744

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9744

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9744

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9744

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9744

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9744

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9744

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9744

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9744

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9744

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9744

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9744

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9744

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9744

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9744

commit: 039eca9

@meta-code-review meta-code-review 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.

Looks good to me, thanks for the clear before/after. None of these nine elements has a block display by default, so treating them as inline matches how browsers render the pasted HTML. The new tests cover both $generateNodesFromDOM and DOMImportExtension, and your test plan shows them failing on main. Unknown tags such as the <tooltip> from #8391 still behave as before.

A few optional, non-blocking suggestions:

  1. .changeset/html-import-inline-tag-spaces.md: isInlineDomNode is exported from lexical (and re-exported from @lexical/utils), and it now returns true for these tags. CONTRIBUTING.md asks a fix that changes observable behaviour to say what to test in Verify:, and callers of the helper are affected too. Something like "isInlineDomNode from lexical and @lexical/utils now returns true for these tags" would cover them.
  2. packages/lexical-html/src/index.ts:592-595: the same tag list feeds the legacy importer's rule that turns an empty block between two inline siblings into a line break. So <div><font>a</font><div></div><font>b</font></div> now imports as a, line break, b instead of ab. That matches what <span> already gets and how a browser lays the HTML out, but one test would pin it.
  3. Follow-up idea (out of scope here): <strike> still imports without strikethrough formatting. Only <s> is mapped, in InlineFormatRule (packages/lexical-html/src/import/coreImportRules.ts) and in nodeNameToTextFormat (packages/lexical/src/nodes/LexicalTextNode.ts). A strikethrough pasted from Gmail now keeps its words and spaces but still loses the line.


[fix] Keep the spaces around `<strike>`, `<font>`, `<small>`, `<big>`, `<nobr>`, `<dfn>`, `<bdi>`, `<bdo>` and `<data>` when importing HTML, so pasted text such as `is <strike>old</strike> now` no longer becomes `isoldnow`

Verify: HTML import of text around these tags, for example pasted email, keeps one space on each side of them.

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: isInlineDomNode is exported from lexical (and re-exported from @lexical/utils) and now returns true for these tags, so direct callers see a change too. Per CONTRIBUTING.md's note on Verify:, you could add a clause such as: "isInlineDomNode from lexical and @lexical/utils now returns true for these tags."

…und inline tags such as <strike> and <font> on HTML import

## Description

Pasting HTML that uses `<strike>`, `<font>`, `<small>` or a few other inline tags deletes the spaces on both sides of them, so words run together: `is <strike>old</strike> now` imports as `isoldnow`. Gmail's composer writes `<strike>` and `<font>`, so pasting from an email hits this.

Both HTML importers (`$generateNodesFromDOM` and `DOMImportExtension`) collapse a text node's leading and trailing space against its inline neighbors, and they ask `isInlineDomNode` what is inline. Its tag list, `INLINE_TAG_RE`, was missing these standard inline elements, so they were taken for block edges and the spaces next to them were trimmed.

This adds `bdi`, `bdo`, `big`, `data`, `dfn`, `font`, `nobr`, `small` and `strike` to that list, and to the copy of the list in the DOM import guide. Unknown custom elements are still treated as blocks by default, so the `<tooltip>` case from meta#8391 keeps its behavior and its test. This keeps the words and their spaces; it adds no formatting for these tags. `isInlineDomNode` is exported, so its callers now see these tags as inline too: `$generateNodesFromDOM` turns an empty block between two of them into a line break, as it already did between two `<span>`s, and a new test pins that.

## Test plan

### Before

The new tests on `main`:

```
$ pnpm exec vitest run --project unit packages/lexical-html/src/__tests__/unit/CoreImportExtension.test.ts packages/lexical-html/src/__tests__/unit/LexicalHtml.test.ts
     × keeps the spaces around an inline <bdi>
     × keeps the spaces around an inline <bdo>
     × keeps the spaces around an inline <big>
     × keeps the spaces around an inline <data>
     × keeps the spaces around an inline <dfn>
     × keeps the spaces around an inline <font>
     × keeps the spaces around an inline <nobr>
     × keeps the spaces around an inline <small>
     × keeps the spaces around an inline <strike>
     × $generateNodesFromDOM keeps the spaces around an inline <bdi>
     × $generateNodesFromDOM keeps the spaces around an inline <bdo>
     × $generateNodesFromDOM keeps the spaces around an inline <big>
     × $generateNodesFromDOM keeps the spaces around an inline <data>
     × $generateNodesFromDOM keeps the spaces around an inline <dfn>
     × $generateNodesFromDOM keeps the spaces around an inline <font>
     × $generateNodesFromDOM keeps the spaces around an inline <nobr>
     × $generateNodesFromDOM keeps the spaces around an inline <small>
     × $generateNodesFromDOM keeps the spaces around an inline <strike>
AssertionError: expected 'isoldnow' to be 'is old now' // Object.is equality
      Tests  18 failed | 34 passed (52)
$ pnpm exec vitest run --project unit packages/lexical-html/src/__tests__/unit/LexicalHtml.test.ts -t "breaks the line"
     × $generateNodesFromDOM breaks the line at an empty block between two inline <font>s, as between two <span>s
AssertionError: expected 'ab' to be 'a\nb' // Object.is equality
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-html
      Tests  189 passed (189)
$ pnpm exec vitest run --project unit
 Test Files  679 passed | 4 skipped (683)
      Tests  29186 passed | 373 expected fail | 136 skipped (29695)
```

`pnpm run tsc`, `pnpm run tsc-test`, `pnpm run flow`, `pnpm run check-changesets`, and ESLint and Prettier on the changed files pass. Not run in a browser: the change is the tag list both importers read, and these tests drive both importers with HTML parsed by `DOMParser`, as pasted HTML is.

@meta-code-review meta-code-review 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.

This looks good to me. Thanks for the clear reproduction and the pasted before/after output. Adding the nine tags to INLINE_TAG_RE (packages/lexical/src/LexicalUtils.ts:2869) fixes the trimmed spaces in both $generateNodesFromDOM and DOMImportExtension, the new tests fail without the change and pass with it, unknown tags such as <tooltip> keep their #8391 behavior, and the list in packages/lexical-website/docs/serialization/dom-import.md matches the regex exactly. The changeset follows .changeset/README.md, including the Verify: paragraph for the line-break side effect.

A few optional, non-blocking suggestions:

  1. <font> around block content (follow-up, not for this PR). Now that font counts as inline, it goes through the same neighbor-descent code as <span>, and that code doesn't stop at block descendants: findTextInLine (packages/lexical/src/nodes/LexicalTextNode.ts:1709-1712), findAdjacentTextOnLine (packages/lexical-html/src/import/coreImportRules.ts:327-330) and isDomNodeBetweenTwoInlineNodes (packages/lexical-html/src/index.ts:663-670). As a result <div>Intro <font><div>x</div></font></div> now keeps a trailing space after Intro, and with $generateNodesFromDOM, <div><font><div>a</div></font><div></div><font><div>b</div></font></div> gets an extra paragraph holding a line break. <span> already does both of these on main, so this is not a regression against span, but older email HTML wraps blocks in <font> more often. Stopping the descent at isBlockDomNode descendants would fix both tags.
  2. <strike> formatting (follow-up). Only <s> maps to strikethrough (LexicalTextNode.ts:709, coreImportRules.ts:277), so pasted <strike> text now keeps its words and spaces but loses the line. You already scoped formatting out in the description; mapping strike next to s in both importers would be a natural next step.
  3. One more test row (nit). The pattern Gmail produces most often, a whitespace-only text node between two of these tags (<p><font>a</font> <font>b</font></p>), now imports as a b instead of ab, but no test covers it. Adding it to one of the test.each blocks would lock it in.

@potatowagon
potatowagon added this pull request to the merge queue Nov 3, 2026
Merged via the queue into meta:main with commit 4cf7d1c Nov 3, 2026
57 checks passed
@vjeux
vjeux deleted the html-import-inline-tag-spaces branch November 3, 2026 08:51
Vedthakar added a commit to Vedthakar/lexical that referenced this pull request Nov 3, 2026
…ock boundary

Adds <span> and <b> to the block-boundary whitespace tests, asserts that
the <p>a <hr>b</p> form now trims the space, matches the override test's
text exactly, and rewords the changeset: the fix applies next to any inline
tag, and the tags meta#9744 added made the bug visible.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
@potatowagon potatowagon mentioned this pull request Dec 2, 2026
Shriraj1901 added a commit to Shriraj1901/lexical that referenced this pull request Dec 6, 2026
…t to an inline tag on HTML import

The whitespace walk that decides whether a leading or trailing space in a
text node follows other text on the same line climbed out of a block and
descended into a block nested in an inline element without noticing. Since
meta#9744 added font, strike and other tags to the inline list, a space at the
edge of a paragraph next to one of those tags was kept, so
`<div><p>a </p><font>b</font></div>` imported as "a " and "b".

Both walks (the legacy importer in TextNode and the DOMImportExtension
rule) now stop when they leave a block or enter one. The `<hr>` test in
CoreImportExtension.test.ts relied on `<p>a <hr>b</p>` where the parser
closes the `<p>` before the `<hr>`, so it moves to a `<div>`.

Fixes meta#9750

This branch was successfully deployed

2 active deployments
Preview – lexical — 039eca91 Deployed Nov 3, 2026 by vercel[bot]
Preview – lexical-playground — 039eca91 Deployed Nov 3, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants