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 <noreply@anthropic.com>
This commit is contained in:
Tom Moor
2026-05-27 08:31:43 -04:00
committed by GitHub
parent e6f9b48530
commit ecaf116990
+12 -6
View File
@@ -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();