Bug: underline strikethrough HTML serialization does not match editor
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
- Use RichText Code Sandbox
- Apply strikethrough and Underline to a text node
- Inspect the generated element: You will notice that under the paragraph, only a
spanexists witheditor-text-underlineStrikethroughclass applied - Export DOM using the provided button: You will notice that additional
uandstags 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
Can I work on this?
If no one is working on this bug, please assign it to me. 🙂
@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.
@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 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.
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 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.
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.
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?