0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -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; let { startContainer, startOffset, endContainer, endOffset } = range;
while (!(startContainer instanceof Text)) { while (!(startContainer instanceof Text)) {
let child = startContainer.childNodes[startOffset]; let child: ChildNode | null = startContainer.childNodes[startOffset];
if (!child || isLeaf(child)) { if (!child || isLeaf(child)) {
if (startOffset) { if (startOffset) {
child = startContainer.childNodes[startOffset - 1]; 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) { if (child instanceof Text) {
startContainer = child; startContainer = child;
startOffset = child.data.length; startOffset = child.data.length;