[lexical] Feature: Add html.isInlineDomNode API for custom inline DOM elements#8477
Closed
abhishekvishwakarma007 wants to merge 1 commit into
Closed
Conversation
…OM elements (facebook#8391) When importing HTML, Lexical only preserves whitespace around known native inline elements (span, b, a, etc.) or elements with explicit `display: inline` style. Custom elements like `<tooltip>` or `<mention>` are treated as block-level, causing adjacent whitespace to be stripped. This adds an `isInlineDomNode` option to `HTMLConfig` that accepts a predicate function, allowing users to declare which custom DOM elements should be treated as inline during HTML import. Usage: ```js createEditor({ html: { isInlineDomNode: (node) => node.nodeName.toLowerCase() === 'tooltip', }, }); ```
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Collaborator
|
I'm not sure this is a good idea, the importDOM machinery is really due for an overhaul, not more duct tape. |
Contributor
Although this is a patch to the current importDOM, there are tests here that will help take into account the current feature for the future redesigned HTML import API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #8391
When importing HTML into Lexical, whitespace is only preserved around known native HTML inline elements (like
<span>,<b>,<a>) or elements with explicitdisplay: inlineCSS. Custom elements like<tooltip>or<mention>are not recognized as inline, causing adjacent whitespace to be stripped during import.This PR adds an
isInlineDomNodeoption toHTMLConfigthat accepts a predicate function, allowing users to declare which custom DOM elements should be treated as inline during HTML import.Usage
Changes
packages/lexical/src/LexicalEditor.ts— AddedisInlineDomNodetoHTMLConfigtypepackages/lexical/src/LexicalUtils.ts— ModifiedisInlineDomNode()to check the active editor's custom predicate as a fallback when built-in checks failpackages/lexical/flow/Lexical.js.flow— Flow type paritypackages/lexical-extension/src/LexicalBuilder.ts— FixedLexicalBuilderto forwardisInlineDomNodeconfig through the Extension system (it was only forwardingimportandexport)packages/lexical-html/src/__tests__/unit/LexicalHtml.test.ts— 4 tests covering: default behavior (whitespace stripped), fix working (whitespace preserved), multiple custom elements, and native elements unaffectedBefore (bug)
Produces:
"Helloworldfoo"— spaces around<tooltip>are strippedAfter (fix)
With
isInlineDomNodeconfigured, produces:"Hello world foo"— spaces preservedTest plan