Skip to content

Commit

Permalink
Fix non-ElementNode regression in getCommonAncestor
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Mar 4, 2025
1 parent 1cfa654 commit 61224b4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/lexical/src/LexicalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ export class LexicalNode {
getCommonAncestor<T extends ElementNode = ElementNode>(
node: LexicalNode,
): T | null {
const result = $getCommonAncestor(this, node);
const a = $isElementNode(this) ? this : this.getParent();
const b = $isElementNode(node) ? node : node.getParent();
const result = a && b ? $getCommonAncestor(a, b) : null;
return result
? (result.commonAncestor as T) /* TODO this type cast is a lie, but fixing it would break backwards compatibility */
: null;
Expand Down
4 changes: 4 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,13 +766,17 @@ describe('LexicalNode tests', () => {
bazParagraphNode = new ParagraphNode();
bazTextNode = new TextNode('baz');
bazTextNode.toggleUnmergeable();
expect(bazTextNode.getCommonAncestor(bazTextNode)).toBe(null);
quxTextNode = new TextNode('qux');
quxTextNode.toggleUnmergeable();
paragraphNode.append(quxTextNode);
expect(barTextNode.getCommonAncestor(bazTextNode)).toBe(null);
barParagraphNode.append(barTextNode);
bazParagraphNode.append(bazTextNode);
expect(barTextNode.getCommonAncestor(bazTextNode)).toBe(null);
expect(bazTextNode.getCommonAncestor(bazTextNode)).toBe(
bazParagraphNode,
);
rootNode.append(barParagraphNode, bazParagraphNode);
});

Expand Down

0 comments on commit 61224b4

Please sign in to comment.