From ecaf1169908ccc10992cbe34b99062c928d3e6a5 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 27 May 2026 08:31:43 -0400 Subject: [PATCH] fix: Guard against out-of-range position in scrollToAnchor (#12489) The MutationObserver callback could throw an uncaught RangeError when posAtDOM returned a position outside the document, since the existing try/catch only wrapped the observer setup, not the async callback. Co-authored-by: Claude Opus 4.7 --- app/editor/index.tsx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 0e9e7d39b8..f3e2027b40 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -572,12 +572,18 @@ export class Editor extends React.PureComponent< this.mutationObserver = observe( hash, (element) => { - const pos = this.view.posAtDOM(element, 0, 1); - this.view.dispatch( - this.view.state.tr.setSelection( - TextSelection.near(this.view.state.doc.resolve(pos), 1) - ) - ); + try { + const pos = this.view.posAtDOM(element, 0, 1); + if (pos >= 0 && pos <= this.view.state.doc.content.size) { + this.view.dispatch( + this.view.state.tr.setSelection( + TextSelection.near(this.view.state.doc.resolve(pos), 1) + ) + ); + } + } catch (_err) { + // posAtDOM may throw if the element is not part of the editor doc + } if (isVisible(element)) { element.scrollIntoView();