lexical icon indicating copy to clipboard operation
lexical copied to clipboard

ElementNode should have a way to control when other nodes can be inserted inside

Open zurfyx opened this issue 3 years ago • 2 comments

Use case:

You have a link, you're anchored inside and want to prepend some additional nodes. You might not necessarily want these nodes to be part of the link. A valid node would be an Image, an invalid node would a LineBreakNode. We have a way to control text via canInsertTextBefore/After but we don't have such equivalent for full-fledges nodes.

Reference test (see issue in code):

    test.only('can insert a linebreak node before an inline element node', async () => {
      const editor = createTestEditor();
      const element = document.createElement('div');
      editor.setRootElement(element);

      await editor.update(() => {
        const root = $getRoot();
        const paragraph = $createParagraphNode();
        root.append(paragraph);
        const link = $createLinkNode('https://lexical.dev/');
        paragraph.append(link);
        const text = $createTextNode('Lexical');
        link.append(text);
        text.select(0, 0);

        $insertNodes([$createLineBreakNode()]);
      });

      // TODO ElementNode should have a way to control when other nodes can be inserted inside
      expect(element.innerHTML).toBe(
        '<p><a href="https://lexical.dev/" dir="ltr"><br><span data-lexical-text="true">Lexical</span></a></p>',
      );
    });

zurfyx avatar Oct 10 '23 19:10 zurfyx

I ran into this issue while looking for things to work on. Tested the reference test from the description against the current main and the behavior has changed — $insertNodes now places nodes outside inline elements rather than inside them.

// cursor at offset 0 inside LinkNode
text.select(0, 0);
$insertNodes([$createLineBreakNode()]);

// 2023 (issue filed): LineBreakNode ended up inside the link
// <a><br><span>Lexical</span></a>

// current main: LineBreakNode goes before the link
// <br><a><span>Lexical</span></a>

Same for mid-text insertion — the link gets split and the node lands between the two halves:

text.select(3, 3);
$insertNodes([$createLineBreakNode()]);
// <a>Lex</a><br><a>ical</a>

This comes from $removeTextAndSplitBlock in insertNodes, which now splits up through inline parents until it reaches the block ancestor. So the splice target is always the ParagraphNode, not the LinkNode.

Direct mutation (link.append($createLineBreakNode())) still puts the node inside, but that's intentional — if you call append, you mean it.

Does this resolve the issue, or is the broader "generic canAcceptChild mechanism for ElementNode" still desired as a separate feature?

mayrang avatar Jul 20 '26 09:07 mayrang

canAcceptChild sounds more like one way to describe the potential relationship scheme and allowed nodes within a particular node class. There is an old feature request for the schemas, but the discussion has long since stopped https://github.com/facebook/lexical/issues/3833

But today we have an Extensions API than 3 years ago, which does not require changes to the core itself

levensta avatar Jul 25 '26 01:07 levensta