lexical
lexical copied to clipboard
Bug: Is the LexicalNode isBefore logic correct?
I was looking at the logic and it looks like it should be backwards, I think. Or I could be wrong and open to be corrected.
As per the comment, the code should return true if this node is BEFORE the target node.
The question occurs when one is the parent of another.
If this node is a parent of the target node, doesn't that mean the parent is before the child?
In the code, the logic is such that if a node is a child of the parent, the child is before the parent.
If I'm just misunderstanding it, I apologize in advance.
Below is how the code is written in the codebase.
/**
* Returns true if this node logical precedes the target node in the editor state.
*
* @param targetNode - the node we're testing to see if it's after this one.
*/
isBefore(targetNode: LexicalNode): boolean {
if (this === targetNode) {
return false;
}
if (targetNode.isParentOf(this)) {
return true; // <-- isn't this backwards?
}
if (this.isParentOf(targetNode)) {
return false; // <-- same with this?
}
I was also confused by this logic the last time I was looking into it. IIRC it might have something to do with the node being entirely before, not just starting before. You really have to look at how it's used to see why it's done this way and it's been a few months since I've looked at that part of the code.