Repository navigation
[lexical][lexical-html] Bug Fix: Trim a space at a block boundary nex… - #9948
Shriraj1901 wants to merge 3 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @Shriraj1901! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
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 aspatch, and aVerify:paragraph.
There was a problem hiding this comment.
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
$generateNodesFromDOMimports<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()readsseemore. A test pinning the intended result would document it.- The
IsInlineForWhitespaceJSDoc (packages/lexical-html/src/import/ImportContext.ts:168) and theImportWhitespaceConfigsection ofpackages/lexical-website/docs/serialization/dom-import.mdstill describe a walk over siblings only; the walk now also consultsisInlinefor 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, itsVerify:line can mention the empty-block case too.
There was a problem hiding this comment.
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.
| // 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 || ''); |
There was a problem hiding this comment.
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.
0f7a247 to
838032b
Compare
There was a problem hiding this comment.
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.
-
Spaces around a block inside a link are dropped by the legacy importer (
packages/lexical/src/nodes/LexicalTextNode.ts:1739-1746).findTextInLinenow 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.$generateNodesFromDOMdoes not when the block sits inside a link.$createNodesFromDOMwraps it in an artificial node inside the inlineLinkNode, 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 withLinkNode,ListNode/ListItemNode,HeadingNodeandQuoteNoderegistered: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 AboutHomeAbout<h2>Title <a href="…"><div>Link</div></a> more</h2>Title Link moreTitleLinkmore<blockquote>See <a href="…"><div>docs</div></a> here</blockquote>See docs hereSeedocshereLinks 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. TheDOMImportExtensionpath is fine: its link rule puts the link on its own line. One fix:return isBlockDomNode(node) && node.closest('a') === null;inisLineBreakingDomNode(line 1688). Locally, this restores all three inputs, and every unit test inpackages/lexical-html,HTMLCopyAndPaste.test.tsand@lexical/clipboardstill passes. Please also add the first row to the "keeps a space" table atLexicalHtml.test.ts:434, withLinkNode,ListNodeandListItemNoderegistered. -
<style>/<script>text counts as text (optional).holdsText(packages/lexical-html/src/import/coreImportRules.ts:323andLexicalTextNode.ts:1696) readstextContent, 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 asab. Walking the descendants, skippingSTYLE/SCRIPT, and stopping at the first non-whitespace text would fix both copies. -
The trimming tables can't tell a line break from glued words (optional).
CoreImportExtension.test.ts:92andLexicalHtml.test.ts:403compare thegetAllTextNodes()contents. Those are the same whethera,bandcend 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. -
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>.
| // Entering a block nested in an inline starts a new line | ||
| if ( | ||
| isHTMLElement(node) && | ||
| isLineBreakingDomNode(node) && | ||
| holdsText(node) | ||
| ) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
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).
| expect( | ||
| editor.read(() => | ||
| $getRoot() | ||
| .getAllTextNodes() | ||
| .map(node => node.getTextContent()), | ||
| ), | ||
| ).toEqual(expected); |
There was a problem hiding this comment.
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.
…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