Skip to content

Bug: $generateNodesFromDOM creates empty list items from whitespace between actual list items #2807

Closed

Description

@knpwrs

Lexical version: 0.3.8

Steps To Reproduce

  1. Use the following code to initialize Lexical:
  editorState: (editor) => {
    const parser = new DOMParser();
    const dom = parser.parseFromString(
      "<ul><li>foo</li> <li>bar</li> <li>banana</li></ul>",
      "text/html"
    );
    const nodes = $generateNodesFromDOM(editor, dom);
    const rootNode = $getRoot();
    rootNode.append(...nodes);
  }
  1. Observe that the empty spaces between the <li> elements get converted to empty list item nodes:

image

  1. If you delete the extra spaces, the list will have the correct number of list items.

Link to code example: https://codesandbox.io/s/lexical-list-parsing-bug-ktssb4?file=%2Fsrc%2FEditor.js%3A1849-2160

The current behavior

Empty list items are created from whitespace text nodes between list item nodes.

The expected behavior

The empty whitespace between list items should be ignored.

Activity

  1. marwan38 commented on Nov 15, 2022

    @marwan38

    I came here to post this issue. If it is intentional then this PR will get closed anyways, but, worth sharing my use-case. Unlike OP, I am getting an error thrown.

      editorState: (editor) => {
        const parser = new DOMParser();
        const dom = parser.parseFromString(
          "<p>foo</p> <p>bar</p>", // NOTE THE SPACE BETWEEN THE P TAGS
          "text/html"
        );
        const nodes = $generateNodesFromDOM(editor, dom);
        const rootNode = $getRoot();
        rootNode.append(...nodes); // ERROR
      }

    Error + stack trace

    Error: rootNode.append: Only element or decorator nodes can be appended to the root node
        at RootNode.append (**/node_modules/lexical/Lexical.dev.js:7667:17)

    That empty space is creating empty nodes in between the paragraph ones which is why they then can't be inserted into the root node.

  2. SzDani3l commented on Dec 20, 2022

    @SzDani3l

    I came here to post this issue. If it is intentional then this PR will get closed anyways, but, worth sharing my use-case. Unlike OP, I am getting an error thrown.

      editorState: (editor) => {
        const parser = new DOMParser();
        const dom = parser.parseFromString(
          "<p>foo</p> <p>bar</p>", // NOTE THE SPACE BETWEEN THE P TAGS
          "text/html"
        );
        const nodes = $generateNodesFromDOM(editor, dom);
        const rootNode = $getRoot();
        rootNode.append(...nodes); // ERROR
      }

    Error + stack trace

    Error: rootNode.append: Only element or decorator nodes can be appended to the root node
        at RootNode.append (**/node_modules/lexical/Lexical.dev.js:7667:17)

    That empty space is creating empty nodes in between the paragraph ones which is why they then can't be inserted into the root node.

    I had the same issue, for now I fixed it by skipping the invalid nodes:

    import { $getRoot, $isDecoratorNode, $isElementNode } from 'lexical'
    [...]
    const nodes = $generateNodesFromDOM(editor, dom)
    const root = $getRoot()
    nodes.forEach((node, i) => {
    	if ($isElementNode(node) || $isDecoratorNode(node)) {
    		root.append(node)
    	}
    })
  3. kevinansfield commented on Jan 8, 2025

    @kevinansfield
    Contributor

    I believe this can be closed now, I have code that uses $generateNodesFromDOM and added a test for this case in our code which shows it no longer creates blank list items, at least when using 0.13.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions