Repository navigation
[lexical][lexical-html][lexical-website] Bug Fix: Keep the spaces around inline tags such as <strike> and <font> on HTML import - #9744
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
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:
.changeset/html-import-inline-tag-spaces.md:isInlineDomNodeis exported fromlexical(and re-exported from@lexical/utils), and it now returnstruefor these tags. CONTRIBUTING.md asks a fix that changes observable behaviour to say what to test inVerify:, and callers of the helper are affected too. Something like "isInlineDomNodefromlexicaland@lexical/utilsnow returnstruefor these tags" would cover them.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 asa, line break,binstead ofab. That matches what<span>already gets and how a browser lays the HTML out, but one test would pin it.- Follow-up idea (out of scope here):
<strike>still imports without strikethrough formatting. Only<s>is mapped, inInlineFormatRule(packages/lexical-html/src/import/coreImportRules.ts) and innodeNameToTextFormat(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. |
There was a problem hiding this comment.
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.
17a9593 to
039eca9
Compare
There was a problem hiding this comment.
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:
<font>around block content (follow-up, not for this PR). Now thatfontcounts 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) andisDomNodeBetweenTwoInlineNodes(packages/lexical-html/src/index.ts:663-670). As a result<div>Intro <font><div>x</div></font></div>now keeps a trailing space afterIntro, 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 onmain, so this is not a regression againstspan, but older email HTML wraps blocks in<font>more often. Stopping the descent atisBlockDomNodedescendants would fix both tags.<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; mappingstrikenext tosin both importers would be a natural next step.- 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 asa binstead ofab, but no test covers it. Adding it to one of thetest.eachblocks would lock it in.
…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>
…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
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> nowimports asisoldnow. Gmail's composer writes<strike>and<font>, so pasting from an email hits this.Both HTML importers (
$generateNodesFromDOMandDOMImportExtension) collapse a text node's leading and trailing space against its inline neighbors, and they askisInlineDomNodewhat 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,smallandstriketo 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.isInlineDomNodeis exported, so its callers now see these tags as inline too:$generateNodesFromDOMturns 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:After
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 byDOMParser, as pasted HTML is.