Skip to content

Commit

Permalink
Fix: facebook#5976 Passing an empty ParagraphNode to $dfs incorrectly…
Browse files Browse the repository at this point in the history
… returns content from subsequent paragraphs (facebook#5977)
  • Loading branch information
matsuyama-k1 authored May 2, 2024
1 parent 6bb75a4 commit a346764
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,34 @@ describe('LexicalNodeHelpers tests', () => {
]);
});
});

test('DFS of empty ParagraphNode returns only itself', async () => {
const editor: LexicalEditor = testEnv.editor;

let paragraphKey;

await editor.update(() => {
const root = $getRoot();

const paragraph = $createParagraphNode();
const paragraph2 = $createParagraphNode();
const text = $createTextNode('test');

paragraphKey = paragraph.getKey();

paragraph2.append(text);
root.append(paragraph, paragraph2);
});
await editor.update(() => {
const paragraph = $getNodeByKey(paragraphKey);

expect($dfs(paragraph)).toEqual([
{
depth: 1,
node: paragraph.getLatest(),
},
]);
});
});
});
});
3 changes: 2 additions & 1 deletion packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export function $dfs(
const nodes = [];
const start = (startingNode || $getRoot()).getLatest();
const end =
endingNode || ($isElementNode(start) ? start.getLastDescendant() : start);
endingNode ||
($isElementNode(start) ? start.getLastDescendant() || start : start);
let node: LexicalNode | null = start;
let depth = $getDepth(node);

Expand Down

0 comments on commit a346764

Please sign in to comment.