0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Handle empty nodes in moveRangeBoundariesDownTree

Empty text nodes next to another text node can always be safely
eliminated.
This commit is contained in:
Neil Jenkins 2023-02-01 14:19:52 +11:00
parent e4ae7c2b81
commit b85754ca50

View file

@ -44,10 +44,23 @@ const moveRangeBoundariesDownTree = (range: Range): void => {
let { startContainer, startOffset, endContainer, endOffset } = range;
while (!(startContainer instanceof Text)) {
let child = startContainer.childNodes[startOffset];
let child: ChildNode | null = startContainer.childNodes[startOffset];
if (!child || isLeaf(child)) {
if (startOffset) {
child = startContainer.childNodes[startOffset - 1];
let prev = child.previousSibling;
// If we have an empty text node next to another text node,
// just skip and remove it.
while (
child instanceof Text &&
!child.length &&
prev &&
prev instanceof Text
) {
child.remove();
child = prev;
continue;
}
if (child instanceof Text) {
startContainer = child;
startOffset = child.data.length;