setNodeSelection(non-leaf first node in doc with text) can create a NodeSelection for text #3090
Closed
2 tasks done
Labels
Type: Bug
The issue or pullrequest is related to a bug
What’s the bug you are facing?
If you try to
setNodeSelection()
on the very first node in the document, and that node is non-leaf, if there's a text selection able to be made within that node, Tiptap will end up creating aNodeSelection
for the first text node within that.The text
NodeSelection
doesn't behave well, and causes an error to be thrown in its deselect when trying to select anything else.Which browser was this experienced in? Are any special extensions installed?
Chrome 104.0.5112.79 (Official Build) (arm64), unlikely to be browser-specific.
How can we reproduce the bug on our side?
See this CodeSandbox which reproduces this in
onCreate
: https://codesandbox.io/s/tiptap-setnodeselection-repro-mtht1t?file=/src/App.jsThis is a contrived example using
paragraph
s for simplicity, but it happens with more complex custom node types which are both selectable themselves and contain editable content.Can you provide a CodeSandbox?
No response
What did you expect to happen?
The first node should be selected (if possible) OR no selection made at all. An invalid selection shouldn't be made, at any rate.
Anything to add? (optional)
This happens because
setNodeSelection
clamps theposition
parameter to selectable ranges in the document:tiptap/packages/core/src/commands/setNodeSelection.ts
Lines 20 to 23 in 57a9e86
This wasn't always the case; the change to clamp the parameter this way happened in 9425e72. Before that commit, it was just to the bounds of the whole document:
tiptap/packages/core/src/commands/setNodeSelection.ts
Lines 19 to 20 in f57466c
9425e72 made the same change (using
Selection.atStart(doc).from
andSelection.atEnd(doc).to
) to bothsetTextSelection
andsetNodeSelection
, but I'm inclined to believe that only thesetTextSelection
change was necessary to address #1588, and that furthermore thesetNodeSelection
change was incorrect.Selection.atStart
proceeds to find the first cursor position within the node (because it's not an atom) and returns aTextSelection
at position 1, which in turns becomesminPos
:I think we should just revert
setNodeSelection
to bounding within[0, doc.content.size]
again.Did you update your dependencies?
Are you sponsoring us?
The text was updated successfully, but these errors were encountered: