lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: underline strikethrough HTML serialization does not match editor

Open JavoByte opened this issue 2 years ago • 9 comments

Generated HTML with $generateHtmlFromNodes from @lexical/html does not match with Editor original content when having Strikethrough and Underline.

Lexical version: 0.12.5

Steps To Reproduce

  1. Use RichText Code Sandbox
  2. Apply strikethrough and Underline to a text node
  3. Inspect the generated element: You will notice that under the paragraph, only a span exists with editor-text-underlineStrikethrough class applied
  4. Export DOM using the provided button: You will notice that additional u and s tags were created.

Link to code example:

https://codesandbox.io/p/sandbox/lexical-rich-text-example-5tncvy

The current behavior

Additional u and s tags are created in the generated HTML.

The expected behavior

the generated html only contains the original span with the editor-text-underlineStrikethrough class applied

JavoByte avatar Dec 16 '23 03:12 JavoByte

Can I work on this?

mrAJAY1 avatar Dec 16 '23 21:12 mrAJAY1

If no one is working on this bug, please assign it to me. 🙂

mrAJAY1 avatar Dec 16 '23 21:12 mrAJAY1

@JavoByte the styles on the playground are being picked up from the theme object which is passed on in the initalConfig. That's why those are getting applied in the playground. When you are serializing the contents to html, the styles from theme theme classes are not applied only the styles which are applied inline would remain in the serialized html. You can use the theme css file to the document where you are rendering the serialized html. This should work.

inaseem avatar Dec 22 '23 07:12 inaseem

@JavoByte the styles on the playground are being picked up from the theme object which is passed on in the initalConfig. That's why those are getting applied in the playground. When you are serializing the contents to html, the styles from theme theme classes are not applied only the styles which are applied inline would remain in the serialized html. You can use the theme css file to the document where you are rendering the serialized html. This should work.

I don't think that's the case. I see the classes applied in the generated HTML. I see additional tags that shouldn't be there IMO. I would be ok with classes not applied but since they are, a mix of styling is being applied that does not match with what user entered

JavoByte avatar Jan 11 '24 21:01 JavoByte

@JavoByte I meant the css associated with those css classes are not applied to the serialized html. For this to work correctly, you will have to include the css file either as as import or as style in the head of serialized html.

inaseem avatar Jan 13 '24 02:01 inaseem

Styles of course are not added (if you mean inline styling) but the classes are. So, I'm having this in the editor:

<p><span class="underline-strikethrough">this text is underlined and striked through</span></p>

but when I serialize that, I end up with:

<p><u><s><span class="underline-strikethrough">this text is underlined and striked through</span></s></u></p>

My point is that the u and s tags should not be there together with the class name.

JavoByte avatar Jan 23 '24 03:01 JavoByte

@JavoByte that's how it's implemented to support both underline and strike-through, as far is I got. Wrapping are applied during export but merged back on import. This functionality is controlled within the exportDOM method of the TextNode class. You can customize this behavior by replacing the TextNode class. For example,

TextNode:

exportDOM(editor: LexicalEditor) {
      if (this.hasFormat("strikethrough")) {
        element = wrapElementWith(element, "s");
      }
      if (this.hasFormat("underline")) {
        element = wrapElementWith(element, "u");
      }

CustomTextNode:

exportDOM(editor: LexicalEditor) {
    const element = super.createDOM(editor._config);
    if (element instanceof HTMLElement) {
      if (element.className.match("underline-strikethrough")) {
        element.style.textDecoration = "underline line-through";
      } else if (this.hasFormat("bold")) {

will apply inline styles, instead of wrapping it with respective element. But than hasFormat behaviour should be adjusted as well, to support both from inline.

oleza avatar Jan 31 '24 10:01 oleza

1 year later hehe.

I understood now that this is by design to maximize compatibility with most html renderers, as purpose of the export is not to be reused with Lexical. For lexical reusage Lexical State should be preferred.

I found this comment in the source code when appending the s, u, i and b tags:

    // This is the only way to properly add support for most clients,
    // even if it's semantically incorrect to have to resort to using
    // <b>, <u>, <s>, <i> elements.

I fixed my "issue" using a custom text node as @oleza suggested.

JavoByte avatar Jan 13 '25 15:01 JavoByte

Hello,

I’m experiencing a similar issue with underline and strikethrough styles. I store text content as a string in my database, and it looks like this:

<p class="editor-paragraph" dir="ltr"><span class="editor-text-underline">ONE</span></p>

This works as expected—the underline is applied correctly. However, when the content is reloaded into the editor, the class ''editor-text-underline'' disappears, and the underline is lost:

<span data-lexical-text="true">656565</span>

I’m using $generateNodesFromDOM to convert the stored content into nodes, and based on my investigation, the issue seems to stem from this function.

Any thoughts?

galbam avatar Mar 09 '25 18:03 galbam