|
1 | | -import { describe, expect, it } from "vite-plus/test"; |
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 |
|
3 | 3 | import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; |
4 | 4 | import { SuggestionMenu } from "./SuggestionMenu.js"; |
@@ -325,4 +325,46 @@ describe("SuggestionMenu", () => { |
325 | 325 |
|
326 | 326 | editor._tiptapEditor.destroy(); |
327 | 327 | }); |
| 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 | + }); |
328 | 370 | }); |
0 commit comments