Repository navigation
Description
When using $generateNodesFromDOM to convert HTML to Lexical, styles such as underline or align are not being applied.
In the case of HTML with styles that changes tag name such as bold(<strong>) or Italic(<em>), the style looks well when converted to lexical using $generateNodesFromDOM.
However, for HTML with styles that only add className(e.g.editor-text-*), such as underline or strikethrough, the styles don't seem to be applied when converted to lexical.
I referenced the convert code(HTML -> Lexical - lexical doc).
Lexical version: 0.3.5
Steps To Reproduce
(* example HTML string - No style applied to text(STYLE TEST))
<div class="editor-input" contenteditable="true" spellcheck="true" data-lexical-editor="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" role="textbox"><p class="editor-paragraph ltr" dir="ltr"><span data-lexical-text="true">STYLE TEST</span></p></div>
case 1. bold
- Select bold - it is applied to
<strong>tag.

- When convert it to lexical based on the html string obtained in step 1 and insert it, the bold style is well as shown below.

case 2. underline
-
Select underline - The tagname is not changed(still
<span>) and onlyeditor-text-underlineis added to the classname.

The underline is also visible in the editor.

-
However, when convert it to lexical based on html obtained in step 1 and insert, converted without underline applied.

case 3. bold + italic
-
Select bold and italic. The tagname is
<strong>, and italics only apply toeditor-text-italicclassname.

-
However, when convert and insert it, only applied
bold(<strong>). (where is theeditor-text-italic? 😢 )

Considering the above cases, when converting html string to lexical, style or classname is ignored and it seems to be applied only based on tagname.
Link to code example:
HTML -> Lexical
const LoadHtmlPlugin = ({htmlString}: Props) => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
editor.update(() => {
const parser = new DOMParser();
const dom = parser.parseFromString(htmlString, 'text/html');
const nodes = $generateNodesFromDOM(editor, dom);
// Select the root
$getRoot().select();
// Insert them at a selection.
const selection = $getSelection() as RangeSelection;
selection.insertNodes(nodes);
});
}, [editor]);
return null;
};The current behavior
$generateNodesFromDOM does not apply styles properly
The expected behavior
If $generateNodesFromDOM is used, the style and className of the dom must be maintained.
Activity
harish-sethuraman commentedon Jun 17, 2022
I think we are supposed to write styles for those classes.(I wrote styles for when I used this) It helps to apply multiple formatting to same text? instead of wrapping them inside different tags.
In case you wanted properties being used inside the lexical playground here it is
patches11 commentedon Jun 22, 2022
I believe I have the same issue, and I do not believe it is just a styling concern.
It seems that $generateHtmlFromNodes correctly applies style classes to the HTML, for me it is something like this (snippet):
<span class="textUnderline">aaaa</span>However as far as i can tell $generateNodesFromDOM does not have any logic to apply this in reverse (take the class and generate an underline node). So in my case where I am saving the output of $generateHtmlFromNodes and then reloading it via $generateNodesFromDOM I lose some subset of my styling on reload
kevin-jenson commentedon Jul 19, 2022
Has anybody figured out any work around for this? I'm running into the same issue where most of my styles are getting dropped from the conversion from HTML back into lexical nodes.
pbrown-iris-construction commentedon Jul 20, 2022
So I think what you are looking for is exportDOM/importDOM, see here: https://lexical.dev/docs/concepts/serialization#lexicalnodeexportdom
For built in nodes I don't think think there is a great way to do this besides monkey patching, maybe I'm wrong, see this issue here: #1262
This is what I did for exportDOM for the build in TextNode:
For my use case importDOM is already set up to handle u elements, it was just not exporting that way, but not sure of your use case.
ChanhuiSeok commentedon Aug 7, 2022
I resolved this issue by saving the Editor State instead of
$generateNodesFromDOMand then usingsetEditorStateto load the editor.The
editorStatecontains all the styles such as the formatting and alignments, so there is no issue that the style is missing as before!getEditorState.Load the editor from within useEffect hook, and set editorState by
setEditorState.knpwrs commentedon Aug 8, 2022
Is it just me or is
IS_UNDERLINEonly exported from the dev build and not the prod build? Lexical ships with aLexicalConstants.d.tsfile but no matching module. Storybook ends up complaining that it can't findlexical/LexicalConstants, and runningrg IS_UNDERLINE node_modules/lexicalreturns the following:It looks like the solution from @pbrown-iris-construction works as long as I define
const IS_UNDERLINE = 1 << 3. It would be great to see Lexical support exporting underlines in HTML as a core feature.BanannaLopal commentedon Sep 20, 2022
@pbrown-iris-construction do u have a solution for 'editor-text-strikethrough' and text alignment?
byTimo commentedon Dec 5, 2022
I've implemented a patch-function that adds missed tag conversion (for [email protected]:
sub,sup,s) andbackground-colorandcolorstyles transfer from the DOM node to the Lexical nodeUPD
After some investigation I figured out that the way of determining of applied formats for the node wasn't correct for each cases, so I found the another way and updated the code snippet above using this
rahulgoyal8312 commentedon Dec 14, 2022
@byTimo I've a query regarding these patches, where do we have to include them?
Because currently when I'm implementing directly these patches into my code I'm getting attached error
byTimo commentedon Dec 15, 2022
Actually these patches are just some kind of global monkey-patches, so you can call
applyHtmlToRichContentPatchessomewhere in the module where your rich editor component is declared.In my case, the patch is being called in the file with the Lexical editor configuration, but you can call it something like this
src/path/to/your/component/filename.ts
BTW, I've updated the code snippet above a bit. Please, take a look if you're using this.
GabrielOrdonesDuarte commentedon Jan 12, 2023
Hello @ChanhuiSeok! Can you give me a detailed example? I'm having the same problem but didn't found a solution yet.
Thanks!
matthiasmeerhof commentedon Feb 18, 2023
I ran into similar problems as described here but found a few different fixes and improved a bit on what @byTimo did. First it wasnt clear to me that I had to add styling for the specific theme classes, a lot of patches weren't necessary for me after that. (Note: I only had issues with underlining) Coloring and background-color weren't working but I think that is intentional, so instead of patching I extended and override the TextNode like this:
the extende node:
tejas-hosamani commentedon Apr 20, 2023
I am facing styling issues as well.
I have styles applied to all the elements except
imgelement.When I resize the image in the editor and try to render the HTML generated by
$generateHtmlFromNodes, it overflows in x direction and none of the width, height are applied. I can see the values in editorState json but I don't see that coveted in the HTML.I tried upgrading to the latest version. It didn't fix.
I have version
0.10.0right now.Please help!
--
Edit: NVM.
I just had to make the following update in
ImageNode.tsxexportDOM(): DOMExportOutput { const element = document.createElement('img'); element.setAttribute('src', this.__src); element.setAttribute('alt', this.__altText); + element.setAttribute('width', this.__width.toString()); + element.setAttribute('height', this.__height.toString()); return { element }; }acywatson commentedon Apr 20, 2023
This is the canonical way to do this.
wangkekekexili commentedon Oct 23, 2023
#2452 (comment) This works well. A small note that we may need to handle
emandstrongin addition tospanbecause of https://github.com/facebook/lexical/blob/74471ab9cfca443d3ba15fefbe487cd1d4a24049/packages/lexical/src/nodes/LexicalTextNode.ts#L118-L126ivailop7 commentedon Oct 23, 2023
strong and em are handled in the docs example: https://lexical.dev/docs/concepts/serialization#handling-extended-html-styling
HilmanKhairr commentedon Apr 4, 2025
it worked for me, thanks. but i got a new issue where when i add a table. then each cell has a <p> initiation with child <span> not <br>. this causes when i click on the cell, then the active is the next cell. how to solve it?