Skip to content

Commit 3cfbb81

Browse files
fix(core): ignore suggestion menu scrolls from another realm
The scroll guard used the host page's Node constructor, so a scroll event whose target came from a same-origin iframe skipped the filter and repositioned the menu. Check for contains() instead.
1 parent bd39cd4 commit 3cfbb81

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎packages/core/src/extensions/SuggestionMenu/SuggestionMenu.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { describe, expect, it, vi } from "vite-plus/test";
22

33
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
44
import { SuggestionMenu } from "./SuggestionMenu.js";
@@ -325,4 +325,46 @@ describe("SuggestionMenu", () => {
325325

326326
editor._tiptapEditor.destroy();
327327
});
328+
it("ignores scroll events from a menu node created in another realm", () => {
329+
const editor = BlockNoteEditor.create();
330+
const div = document.createElement("div");
331+
document.body.appendChild(div);
332+
editor.mount(div);
333+
// jsdom's DOMRect has no toJSON(), which the menu uses for its position.
334+
const rectSpy = vi
335+
.spyOn(Element.prototype, "getBoundingClientRect")
336+
.mockReturnValue({ toJSON: () => ({}) } as DOMRect);
337+
const sm = editor.getExtension(SuggestionMenu)!;
338+
editor.setTextCursorPosition(editor.document[0].id, "end");
339+
sm.openSuggestionMenu("/");
340+
expect(sm.store.state?.show).toBe(true);
341+
342+
let updates = 0;
343+
const unsubscribe = sm.store.subscribe(() => {
344+
updates++;
345+
});
346+
347+
// A node from a same-origin iframe is not an instance of the host page's
348+
// `Node`, which is the case when host-page code manages an editor inside
349+
// an iframe. The menu does not contain the decoration.
350+
const iframe = document.createElement("iframe");
351+
document.body.appendChild(iframe);
352+
const menu = iframe.contentDocument!.createElement("div");
353+
expect(menu instanceof Node).toBe(false);
354+
355+
const menuScroll = new Event("scroll");
356+
Object.defineProperty(menuScroll, "target", { value: menu });
357+
document.dispatchEvent(menuScroll);
358+
expect(updates).toBe(0);
359+
360+
// Scrolling an ancestor of the decoration still updates the position.
361+
document.body.dispatchEvent(new Event("scroll"));
362+
expect(updates).toBe(1);
363+
364+
unsubscribe();
365+
rectSpy.mockRestore();
366+
editor._tiptapEditor.destroy();
367+
div.remove();
368+
iframe.remove();
369+
});
328370
});

‎packages/core/src/extensions/SuggestionMenu/SuggestionMenu.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ class SuggestionMenuView {
5959
}
6060
// Only a scroll of an ancestor of the cursor can move the menu's
6161
// reference. The menu's own scroll (it is in the editor's subtree, so
62-
// this capture listener sees it) must not reposition it.
62+
// this capture listener sees it) must not reposition it. `instanceof
63+
// Node` is false for nodes from another realm (e.g. a same-origin
64+
// iframe managed by host-page code), so check for `contains` instead.
65+
const target = event.target;
6366
if (
64-
event.target instanceof Node &&
65-
!event.target.contains(decorationNode)
67+
target !== null &&
68+
"contains" in target &&
69+
typeof target.contains === "function" &&
70+
!target.contains(decorationNode)
6671
) {
6772
return;
6873
}

0 commit comments

Comments
 (0)