question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Lexical nodes not persisting styles present in original HTML

See original GitHub issue

Lexical version: 0.4.1

Steps To Reproduce

  1. Generate HTML using Lexical APIs
  2. Use generated HTML from step 1 and provide to Lexical editor. use generateNode API provided by Lexical.
  3. Resulting content in editor will not have style which it supposed to have

Following HTML was generated by Lexical and saved in DB. We provide it as is to Lexical and generate nodes (https://lexical.dev/docs/concepts/serialization) from it

<p class="RMXContentTheme__paragraph"
 data-lexical-node-type="paragraph"
 data-lexical-node-json='{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}'
 data-lexical-editor-key="rpoel">
 <span
   data-lexical-node-type="text"
   data-lexical-node-json='{"detail":0,"format":0,"mode":"normal","style":"color: #e00000;background-color: #f8e71c;","text":"manish","type":"text","version":1}'
   data-lexical-editor-key="rpoel"
   style="color: rgb(224, 0, 0); background-color: rgb(248, 231, 28)">
       Hello World
   </span>
</p>

The current behavior

After deserializing HTML content its NOT PERSISTING style properties which were present in HTML

<p
  class="RMXContentTheme__paragraph"
  data-lexical-node-type="paragraph"
  data-lexical-node-json='{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}'
  data-lexical-editor-key="qktmf">
  <span
    data-lexical-node-type="text"
    data-lexical-node-json='{"detail":0,"format":0,"mode":"normal","style":"","text":"manish","type":"text","version":1}'
    data-lexical-editor-key="qktmf">
    Hello World
  </span>
</p>

The expected behavior

After deserializing HTML content it SHOULD PERSIST style properties which were present in HTML

<p class="RMXContentTheme__paragraph"
 data-lexical-node-type="paragraph"
 data-lexical-node-json='{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}'
 data-lexical-editor-key="rpoel">
 <span
   data-lexical-node-type="text"
   data-lexical-node-json='{"detail":0,"format":0,"mode":"normal","style":"color: #e00000;background-color: #f8e71c;","text":"manish","type":"text","version":1}'
   data-lexical-editor-key="rpoel"
   style="color: rgb(224, 0, 0); background-color: rgb(248, 231, 28)">
       Hello World
   </span>
</p>

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Manish2612commented, Sep 15, 2022

@zurfyx Thanks for prompt response and fix! It will be great if you can let us know way to persist text-alignment?

0reactions
Manish2612commented, Sep 15, 2022

@zurfyx Thanks for prompt response and fix! It will be great if you can let us know way to persist text-alignment?

Can you store this on the parent (i.e. ParagraphNode)? paragraphNode.setFormat('center')

We also have a command for this -> .dispatchCommand(FORMAT_ELEMENT_COMMAND, 'center');

It’s already being stored in parent by lexical itself. When we use generated HTML as initial state, aligned text is not rendered by editor even if data-lexical-json attributes is present on parent tag.

<p
  class="RMXContentTheme__paragraph"
  data-lexical-node-type="paragraph"
  data-lexical-node-json='{"children":[],"direction":"ltr","format":"center","indent":0,"type":"paragraph","version":1}'
  data-lexical-editor-key="tcrre"
>
  <span
    data-lexical-node-type="text"
    data-lexical-node-json='{"detail":0,"format":0,"mode":"normal","style":"","text":"Manish","type":"text","version":1}'
    data-lexical-editor-key="tcrre"
    >This should be aligned center</span
  >
</p>

let me know your thoughts on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Lexical nodes not persisting styles present in original ...
I am using Lexical APIs for serialisation and deserialisation to generate following HTML from editor. <p class="RMXContentTheme__paragraph" data ...
Read more >
Bug: $generateNodesFromDOM does not apply styles ...
When using $generateNodesFromDOM to convert HTML to Lexical, styles such as underline or align are not being applied.
Read more >
3.2.5 Content models — HTML5 - W3C
Inter-element whitespace, comment nodes, and processing instruction nodes must be ignored when establishing whether an element's contents match the element's ...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >
Troubleshooting Geo - GitLab Docs
If the user interface is not working, or you are unable to sign in, ... found a secondary node named "Shanghai" GitLab Geo...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found