Skip to content

Commit 9daa7aa

Browse files
fix: restore depth guard in getParentBlockInfo to prevent RangeError (blo-1103) (#2585)
1 parent 1ae8de7 commit 9daa7aa

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
22

33
import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
44
import { setupTestEnv } from "../../setupTestEnv.js";
5-
import { mergeBlocksCommand } from "./mergeBlocks.js";
5+
import { getParentBlockInfo, mergeBlocksCommand } from "./mergeBlocks.js";
66

77
const getEditor = setupTestEnv();
88

@@ -77,6 +77,20 @@ describe("Test mergeBlocks", () => {
7777
expect(anchorIsAtOldFirstBlockEndPos).toBeTruthy();
7878
});
7979

80+
it("getParentBlockInfo returns undefined for top-level block", () => {
81+
getEditor().setTextCursorPosition("paragraph-0");
82+
83+
const beforePos = getPosBeforeSelectedBlock();
84+
const doc = getEditor()._tiptapEditor.state.doc;
85+
const $pos = doc.resolve(beforePos);
86+
87+
expect($pos.depth - 1).toBeLessThan(1);
88+
89+
const result = getParentBlockInfo(doc, beforePos);
90+
91+
expect(result).toBeUndefined();
92+
});
93+
8094
// We expect a no-op for each of the remaining tests as merging should only
8195
// happen for blocks which both have inline content. We also expect
8296
// `mergeBlocks` to return false as TipTap commands should do that instead of

packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const getParentBlockInfo = (
1616
): BlockInfo | undefined => {
1717
const $pos = doc.resolve(beforePos);
1818
const depth = $pos.depth - 1;
19+
20+
if (depth < 1) {
21+
return undefined;
22+
}
23+
1924
const parentBeforePos = $pos.before(depth);
2025
const parentNode = doc.resolve(parentBeforePos).nodeAfter;
2126

0 commit comments

Comments
 (0)