Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Point's isBefore calculation can be incorrect #3181

Open
thegreatercurve opened this issue Oct 14, 2022 · 0 comments · May be fixed by #7256
Open

Bug: Point's isBefore calculation can be incorrect #3181

thegreatercurve opened this issue Oct 14, 2022 · 0 comments · May be fixed by #7256
Assignees
Labels
all-platforms-bug core Reconciler, DOM, Selection, Node, Events, Composition

Comments

@thegreatercurve
Copy link
Contributor

Reported internally by @olegbl:

As far as I can tell, the calculation inside Point.isBefore is not quite right when comparing an Element Point to a Text Point:

if ($isElementNode(aNode)) {
  const aNodeDescendant = aNode.getDescendantByIndex(aOffset);
  aNode = aNodeDescendant != null ? aNodeDescendant : aNode;
}

changes the value of aNode but does not change aOffset.
As a result, when it gets used down below in

if (aNode === bNode) {
  return aOffset < bOffset;
}

The aOffset is being applied twice and leads to an incorrect result.
e.g.:

// nodes
1 (element node)
  2 (text node)
  3 (element node)
    4 (text node)
    5 (text node)
                  <-- point1 is here
    6 (text node) <-- point2 is inside here
  7 (text node)

// point1
key = 3
offset = 2

// point2
key = 6
offset = 1

// test:
point1.isBefore(point2) // this should be true

// aNode = node 3
// aOffset = 2
// bNode = node 6
// bOffset = 1

if ($isElementNode(aNode)) {
  const aNodeDescendant = aNode.getDescendantByIndex(aOffset);
  aNode = aNodeDescendant != null ? aNodeDescendant : aNode;
}

// aNode = node 6

if (aNode === bNode) { // true
  return aOffset < bOffset; // 2 < 1 = false (this is wrong)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
all-platforms-bug core Reconciler, DOM, Selection, Node, Events, Composition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants