Skip to content

fix(slate-dom): propagate suppressThrow to toSlateNode in toSlatePoint#6072

Open
pangoyal-gong wants to merge 3 commits into
ianstormtaylor:mainfrom
pangoyal-gong:fix/toSlatePoint-suppress-throw-in-toSlateNode
Open

fix(slate-dom): propagate suppressThrow to toSlateNode in toSlatePoint#6072
pangoyal-gong wants to merge 3 commits into
ianstormtaylor:mainfrom
pangoyal-gong:fix/toSlatePoint-suppress-throw-in-toSlateNode

Conversation

@pangoyal-gong

Copy link
Copy Markdown

Problem

toSlatePoint accepts a suppressThrow option that is supposed to prevent it from throwing when a DOM node cannot be resolved to a Slate point. However, the implementation only suppresses errors from findPath — errors thrown by toSlateNode (called on the line immediately before) are not guarded.

This means that callers passing suppressThrow: true can still receive an unhandled Error: Cannot resolve a Slate node from DOM node exception thrown by toSlateNode, defeating the purpose of the option.

Affected code path

In packages/slate-dom/src/plugin/dom-editor.ts, the toSlatePoint implementation:

// BEFORE (buggy)
const slateNode = DOMEditor.toSlateNode(editor, textNode!)  // throws unconditionally
let path
try {
  path = DOMEditor.findPath(editor, slateNode)  // only this is guarded
} catch (e) {
  if (suppressThrow) {
    return null
  }
  throw e
}

A common trigger: the document-level selectionchange listener in slate-react calls toSlatePoint with suppressThrow: true, but receives the error anyway because toSlateNode is not guarded. This produces high-volume Sentry noise for applications using Slate in a React portal or with multiple editors on the page.

Relationship to PR #6004

PR #6004 added suppressThrow handling to findPath inside toSlatePoint. This fix applies the same pattern one step earlier, to the toSlateNode call that precedes it.

Fix

Wrap the toSlateNode call in the same try/catch pattern already used for findPath:

// AFTER (fixed)
let slateNode
try {
  slateNode = DOMEditor.toSlateNode(editor, textNode!)
} catch (e) {
  if (suppressThrow) {
    return null
  }
  throw e
}
let path
try {
  path = DOMEditor.findPath(editor, slateNode)
} catch (e) {
  if (suppressThrow) {
    return null
  }
  throw e
}

Testing

The change is minimal and mirrors the existing pattern in the same function. The behavior when suppressThrow is false (or omitted) is unchanged — errors still propagate as before.

…ePoint

When toSlatePoint is called with suppressThrow: true, errors thrown by
toSlateNode were not being suppressed — only errors from findPath were
handled. Wrap the toSlateNode call in the same try/catch pattern already
used for findPath.

This is the gap left by PR ianstormtaylor#6004 which guarded findPath but not the
preceding toSlateNode call.
@changeset-bot

changeset-bot Bot commented Jun 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5a47415

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-dom Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dylans dylans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pangoyal-gong needs a quick prettier run please on the changeset, likely needs a single-quote change and a shorter line.

@dylans dylans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot this still needs prettier changes to land it please.

NikunjSonigara added a commit to NikunjSonigara/slate that referenced this pull request Jul 1, 2026
…Throw

Address review feedback on ianstormtaylor#6073: instead of silently swallowing the
error, add an opt-in `suppressThrow` option to `useSelected` so the hook
keeps throwing by default and callers opt in to returning `false` when
the element can no longer be resolved. Follows the `suppressThrow`
pattern from ianstormtaylor#6072. Also trim the explanatory comments.
@pangoyal-gong

Copy link
Copy Markdown
Author

@dylans pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants