Skip to content

Commit

Permalink
Warn about invalid text selections
Browse files Browse the repository at this point in the history
FIX: Creating a `TextSelection` with endpoints that don't point at inline positions
now emits a warning. (It is technically an error, but crashing on it might be too
disruptive for some existing setups.)

See https://discuss.prosemirror.net/t/selection-changes-when-inline-decoration-is-applied/1027/11
See https://discuss.prosemirror.net/t/strange-behavior-with-textselection/4400
  • Loading branch information
marijnh committed Feb 14, 2022
1 parent 61bb964 commit 938448a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/selection.js
Expand Up @@ -230,6 +230,14 @@ export class SelectionRange {
}
}

let warnedAboutTextSelection = false
function checkTextSelection($pos) {
if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
warnedAboutTextSelection = true
console.warn("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")")
}
}

// ::- A text selection represents a classical editor selection, with
// a head (the moving side) and anchor (immobile side), both of which
// point into textblock nodes. It can be empty (a regular cursor
Expand All @@ -238,6 +246,8 @@ export class TextSelection extends Selection {
// :: (ResolvedPos, ?ResolvedPos)
// Construct a text selection between the given points.
constructor($anchor, $head = $anchor) {
checkTextSelection($anchor)
checkTextSelection($head)
super($anchor, $head)
}

Expand Down

0 comments on commit 938448a

Please sign in to comment.