Skip to content

[lexical][lexical-html] Bug Fix: Trim a space at a block boundary nex… - #9948

Open
Shriraj1901 wants to merge 3 commits into
meta:mainfrom
Shriraj1901:fix-html-import-block-boundary-space
Open

Shriraj1901 wants to merge 3 commits into
meta:mainfrom
Shriraj1901:fix-html-import-block-boundary-space

Conversation

@Shriraj1901

Copy link
Copy Markdown

…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 #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 #9750

Description

Describe the changes in this pull request

Closes #

Changeset

The changeset you added, or why none is needed

Test plan

Before

Insert relevant screenshots/recordings/automated-tests

After

Insert relevant screenshots/recordings/automated-tests

@vercel

vercel Bot commented Nov 19, 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 Dec 6, 2026 1:44pm UTC
lexical-playground Ready Ready Preview Dec 6, 2026 1:44pm UTC

Request Review

@meta-cla

meta-cla Bot commented Nov 19, 2026

Copy link
Copy Markdown

Hi @Shriraj1901!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@rootnode-bot

rootnode-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟡 Reviewed (for maintainers only) Dec 6, 2026, 2:02 PM UTC

@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 picking up #9750! The block-tag cases are fixed in both importers, and the new tests fail on main and pass here. One problem needs fixing before this can merge.

Words get joined next to display: block inline elements

packages/lexical/src/nodes/LexicalTextNode.ts:1689 and packages/lexical-html/src/import/coreImportRules.ts:316

isLineBreakingDomNode and isLineBreakingNode treat any element whose inline display is not inline* as the end of a line. Neither importer starts a new line at such an element unless its tag is a block tag ($createNodesFromDOM wraps only when isBlockDomNode holds), so the space being trimmed was the only separator between the words:

HTML main this PR
<p><span style="display:block">a </span><span>b</span></p> a b ab
<p>see <a href="…"><span style="display:block">here</span></a> now</p> see here now seeherenow

Both $generateNodesFromDOM and DOMImportExtension give these results. Browser copies do carry such inline styles (see the GitHub paste fixture in packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts). Stopping the climb and the descent only at block tags, the direction #9750 suggests, keeps every case from the issue fixed and passes all the tests in both changed test files:

// LexicalTextNode.ts
function isLineBreakingDomNode(node: HTMLElement): boolean {
  return isBlockDomNode(node);
}

// coreImportRules.ts (isLineBreakingNode)
return isHTMLElement(node) && !wsConfig.isInline(node) && isBlockDomNode(node);

Please update the comment above each helper to match, and add the display:block case above as a test in both CoreImportExtension.test.ts and LexicalHtml.test.ts so this branch stays covered.

Optional, non-blocking

  • The PR body's Test plan still holds the template placeholders. AGENTS.md ("Commits and Pull Requests") asks for the command you ran and its real Before and After output; the red-to-green run of your new tests would do.
  • The changeset looks right: [fix], both packages as patch, and a Verify: paragraph.

Automated review (maintainer link)

@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 the follow-up: the display:block cases from the last round are fixed, and stopping the walk at block tags fixes all three shapes from #9750 in both importers.

One regression to fix before this lands

When the descent into an inline sibling reaches a block tag, both walks now return null (packages/lexical/src/nodes/LexicalTextNode.ts:1720, packages/lexical-html/src/import/coreImportRules.ts:344), so the spaces on both sides of that inline element are trimmed. If that block produces no content, though, neither importer starts a new line there: $createNodesFromDOM only turns an empty block into a line break when its own siblings are inline (packages/lexical-html/src/index.ts:605), and TransparentBlockRule returns nothing for it. The words then merge:

HTML main this PR
<div>a <span><div></div></span> b</div> a b ab
<div>Click <a href="https://example.com"><div><svg><path d="M0 0"></path></svg></div>Download</a></div> Click Download ClickDownload

The second shape (an icon wrapped in a <div> inside a link) is one HTML copied from web pages carries. One way to keep every #9750 case is to stop the descent only at a block that holds text the importer will emit:

if (
  isHTMLElement(node) &&
  isLineBreakingDomNode(node) &&
  /[^ \t\r\n]/.test(node.textContent || '')
) {
  return null;
}

with the same condition next to isLineBreakingNode in findAdjacentTextOnLine, plus both rows above in the new test.each tables in CoreImportExtension.test.ts and LexicalHtml.test.ts.

Optional, non-blocking

  • $generateNodesFromDOM imports <div><a href="https://example.com/"><div>see </div></a><span>more</span></div> as a paragraph nested inside the link (that nesting predates this PR); with the space now trimmed, getTextContent() reads seemore. A test pinning the intended result would document it.
  • The IsInlineForWhitespace JSDoc (packages/lexical-html/src/import/ImportContext.ts:168) and the ImportWhitespaceConfig section of packages/lexical-website/docs/serialization/dom-import.md still describe a walk over siblings only; the walk now also consults isInline for the ancestors it leaves and the blocks it enters (for block tags only). A sentence in each would help anyone writing a custom predicate.
  • AGENTS.md (Commits and Pull Requests) asks the PR body's Test plan to carry the command you ran with its real failing and passing output; the Before / After sections are still the template placeholders.
  • The changeset follows .changeset/README.md; once the fix above is in, its Verify: line can mention the empty-block case too.

Automated review (maintainer link)

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

The change looks good. It fixes #9750 in both importers, and the new tests pin the right cases: the spaces that should go, and the display:block, empty-block and <svg>-block cases where they must stay. The points below are optional and non-blocking.

1. Optional: holdsText also counts <style>/<script> text (packages/lexical-html/src/import/coreImportRules.ts:323, packages/lexical/src/nodes/LexicalTextNode.ts:1696)
textContent includes the contents of <style> and <script>, but both importers drop those elements (IGNORE_TAGS in packages/lexical-html/src/index.ts, IgnoreScriptStyleRule in coreImportRules.ts). A block that holds only one of them therefore still ends the line, so <div>a <span><div><style>.x{color:red}</style></div></span> b</div> now imports as ab, where it used to give a b. One fix: walk the block's descendants, skip STYLE/SCRIPT, and stop at the first text node with a non-whitespace character. That also avoids re-reading the subtree's textContent at each nesting level. A row for this input in the test.each at CoreImportExtension.test.ts:111 would cover it.

2. Merge conflict with main
Since this branch was cut, #9980 removed @experimental from the IsInlineForWhitespace JSDoc in packages/lexical-html/src/import/ImportContext.ts, so the new paragraph there no longer merges cleanly. Keep your paragraph and drop the @experimental line. Reproduced locally: with that resolved, the @lexical/html unit tests pass on top of current main.

3. Test plan (AGENTS.md, pull request template)
The Before/After sections still hold the template text. AGENTS.md asks the Test plan to carry the real output of the command you ran. Pasting the vitest output for CoreImportExtension.test.ts and LexicalHtml.test.ts (the new "trims a space" cases fail on main and pass on this branch) would document it for reviewers.

Automated review (maintainer link)

// around an <svg> icon) produces no content and no line break (see
// TransparentBlockRule), so the line carries on past it.
function holdsText(node: Node): boolean {
return /[^ \t\r\n]/.test(node.textContent || '');

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: textContent includes the contents of <style> and <script>, which both importers drop (IGNORE_TAGS in index.ts, IgnoreScriptStyleRule here), so <div>a <span><div><style>.x{color:red}</style></div></span> b</div> now imports as ab (it was a b). Walking the descendants, skipping STYLE/SCRIPT and returning at the first text node with a non-whitespace character would fix that here and in holdsText in LexicalTextNode.ts, and would avoid re-reading the subtree's textContent at every nesting level.

…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
The walk treated any element with a non-inline display as the end of a
line, but the importer only starts a new block at block tags. A span
styled display:block stays on the same line, so trimming the space next
to it joined words: <p><span style="display:block">a </span><span>b</span></p>
imported as "ab". Use isBlockDomNode alone in both importers and add
tests for the display:block cases.
Stopping the whitespace walk at every block tag went too far. A block that
yields nothing, such as an empty <div> or a <div> around an <svg> icon, does
not start a new line in either importer, so trimming the spaces on both
sides of the inline element around it joined the words:
<div>a <span><div></div></span> b</div> imported as "ab", and
<div>Click <a href="..."><div><svg>...</svg></div>Download</a></div> as
"ClickDownload".

Entering a block nested in an inline sibling now ends the line only when
the block holds text the importer will emit. Leaving a block we walked into
no longer ends the line either, only leaving a block that holds the text
being decided does, so the walk can step over a whitespace-only text inside
such a block. Both importers get the same change, the new cases are added
to the two display:block tables, and the IsInlineForWhitespace docs and the
DOM import guide describe what the walk does.

@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 picking up #9750. The block-boundary trimming handles every case in the issue, in both importers, and the new rows fail without the change and pass with it (reproduced locally). One regression in the legacy importer needs a fix before this can merge.

  1. Spaces around a block inside a link are dropped by the legacy importer (packages/lexical/src/nodes/LexicalTextNode.ts:1739-1746). findTextInLine now ends the line when it enters a block tag that holds text. The comment at line 1686 gives the reason: the importer starts a new block there. $generateNodesFromDOM does not when the block sits inside a link. $createNodesFromDOM wraps it in an artificial node inside the inline LinkNode, and $unwrapArtificialNodes (packages/lexical-html/src/index.ts:660) adds a line break only between two adjacent artificial nodes. So the link text stays on the line while the spaces on both sides are trimmed. The default HTML paste handler takes this path. Reproduced locally with LinkNode, ListNode/ListItemNode, HeadingNode and QuoteNode registered:

    HTML Before this PR With this PR
    <ul><li><a href="/"><div>Home</div></a> <a href="/about"><div>About</div></a></li></ul> Home About HomeAbout
    <h2>Title <a href="…"><div>Link</div></a> more</h2> Title Link more TitleLinkmore
    <blockquote>See <a href="…"><div>docs</div></a> here</blockquote> See docs here Seedocshere

    Links that wrap a <div> are common in pasted navigation, cards and buttons. Glued words are a bigger change than the stray boundary space the issue reports, and AGENTS.md ("Backwards Compatibility") asks to keep existing behavior intact when in doubt. The DOMImportExtension path is fine: its link rule puts the link on its own line. One fix: return isBlockDomNode(node) && node.closest('a') === null; in isLineBreakingDomNode (line 1688). Locally, this restores all three inputs, and every unit test in packages/lexical-html, HTMLCopyAndPaste.test.ts and @lexical/clipboard still passes. Please also add the first row to the "keeps a space" table at LexicalHtml.test.ts:434, with LinkNode, ListNode and ListItemNode registered.

  2. <style>/<script> text counts as text (optional). holdsText (packages/lexical-html/src/import/coreImportRules.ts:323 and LexicalTextNode.ts:1696) reads textContent, which includes the contents of <style> and <script>. Both importers drop those elements, so <div>a <span><div><script>var x = 1;</script></div></span> b</div> imports as ab. Walking the descendants, skipping STYLE/SCRIPT, and stopping at the first non-whitespace text would fix both copies.

  3. The trimming tables can't tell a line break from glued words (optional). CoreImportExtension.test.ts:92 and LexicalHtml.test.ts:403 compare the getAllTextNodes() contents. Those are the same whether a, b and c end up in three paragraphs or side by side in one, which is the shape of item 1. Asserting $getRoot().getTextContent() (e.g. 'a\n\nb\n\nc') would catch that.

  4. Test plan output (nit). The Before and After sections still hold the template placeholders. AGENTS.md ("Commits and Pull Requests") asks for the command you ran and its real failing and passing output.

The changeset follows CONTRIBUTING.md (a [fix] entry with a Verify: paragraph). The rewritten <hr> override test is a fair input change, since the parser closes the <p> before the <hr>.

Automated review (maintainer link)

Comment on lines +1739 to +1746
// Entering a block nested in an inline starts a new line
if (
isHTMLElement(node) &&
isLineBreakingDomNode(node) &&
holdsText(node)
) {
return null;
}

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 ends the line at a block nested in an inline, but $generateNodesFromDOM keeps such a block on the line when it sits inside a link: the artificial node inside the inline LinkNode is unwrapped with no line break ($unwrapArtificialNodes only breaks between two adjacent ones). The spaces on both sides are trimmed anyway, so with LinkNode and the list nodes registered, <ul><li><a href="/"><div>Home</div></a> <a href="/about"><div>About</div></a></li></ul> imports as HomeAbout (it was Home About), and <h2>Title <a href="…"><div>Link</div></a> more</h2> as TitleLinkmore. return isBlockDomNode(node) && node.closest('a') === null; in isLineBreakingDomNode restores those (it also covers text inside the nested block, through the leaving check at line 1720), and the unit tests in packages/lexical-html, HTMLCopyAndPaste.test.ts and @lexical/clipboard still pass with it (reproduced locally).

Comment on lines +423 to +429
expect(
editor.read(() =>
$getRoot()
.getAllTextNodes()
.map(node => node.getTextContent()),
),
).toEqual(expected);

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: getAllTextNodes() gives ['a', 'b', 'c'] whether the three land in separate paragraphs or side by side in one paragraph with the spaces dropped (the link-wrapped-block case on LexicalTextNode.ts:1739). Asserting $getRoot().getTextContent() ('a\n\nb\n\nc' for the third row) would tell the two apart.

This branch was successfully deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [lexical-html] A space at a block boundary next to an inline tag is kept on HTML import (regression of #9744)

1 participant