Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-bees-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tiptap/extension-floating-menu': patch
---

Added a safeguard to avoid `TypeError: Cannot read properties of null (reading 'domFromPos')` being thrown when the editor was being destroyed
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,24 @@ describe('FloatingMenuView cross-contamination', () => {
editor.destroy()
})
})

describe('FloatingMenuView destroy safety', () => {
it('updatePosition should not call coordsAtPos when the editor view is detached from the DOM', () => {
const editor = createEditor()
const view = createFloatingMenuView(editor)
const coordsSpy = vi.spyOn(editor.view, 'coordsAtPos')

try {
// Simulate the real-world teardown race: the editor is destroyed while a
// pending updatePosition call (debounced resize/scroll) is still in flight.
// ProseMirror's destroy removes view.dom from its parent and nulls docView;
// without a guard, posToDOMRect -> coordsAtPos throws on the null docView.
editor.destroy()

expect(() => view.updatePosition()).not.toThrow(/Cannot read properties of null \(reading 'domFromPos'\)/)
expect(coordsSpy).not.toHaveBeenCalled()
} finally {
view.destroy()
}
})
})
4 changes: 4 additions & 0 deletions packages/extension-floating-menu/src/floating-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ export class FloatingMenuView {
}

updatePosition() {
if (!this.view?.dom?.parentNode) {
return
}

const { selection } = this.editor.state

const domRect = posToDOMRect(this.view, selection.from, selection.to)
Expand Down
Loading