Confused about ResolvedPos.start API

Suppose I have HTML DOM like this(ignore the outter doc container): <ol><li><p>12345</p></li></ol>.

besides, for purpose of debug, I set window.selection = state.selection in editor view’s dispatchTransaction function

then, i put cursor into the position after 2 and before 3. after all these things done, i try to print the result of selection.$from.start(4) and it return 4. But when i print selection.$from.end(4), I got an error.

That confused me. In fact i think, the $from’s depth is 3, when start(3) invoked, $from is in a TextNode( it’s 12345), so it return the TextNode start position before 1 and after <p>, so it be 3, it’s OK. And besides, end(3) return position in TextNode’s end, so it’s after 5 and before </p>, it’s 8, That’s OK, too. but start(4) should got an error, just like end(4).

Or I got some misunderstand about the ResolvedPos.start API?

thank in advance!

The dispatchTransaction function is:

dispatchTransaction(tr) {
    let newState = window.view.state.apply(tr);
    window.selection = newState.selection;

.start(4) not erroring is just an accident—it’s not a valid query for a position with depth 3. Text nodes don’t count as a new level in this system—a position inside a text node is considered to be a position inside the text node’s parent. So you have only three wrapper nodes (ol, li, p) around your position.

whether it means that i should never pass on 4 to start function in this situation cause it not make any sense?

Yes, depth shouldn’t be bigger than the .depth property.

got it, thanks!