From 4306cecb6eccaaad2e3dc084e048331863c9ef39 Mon Sep 17 00:00:00 2001 From: Neil Jenkins Date: Mon, 20 Feb 2023 09:57:31 +1100 Subject: [PATCH] Remove multiple adjacent empty text nodes Fix the loop condition so this will remove multiple adjacent empty text nodes if necessary. --- source/range/Boundaries.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/source/range/Boundaries.ts b/source/range/Boundaries.ts index c12ca04..2908bc2 100644 --- a/source/range/Boundaries.ts +++ b/source/range/Boundaries.ts @@ -48,22 +48,23 @@ const moveRangeBoundariesDownTree = (range: Range): void => { 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; + // Need a new variable to satisfy TypeScript's type checker + // for some reason. + let textChild: Text = child; + // If we have an empty text node next to another text node, + // just skip and remove it. + let prev: ChildNode | null; + while ( + !textChild.length && + (prev = textChild.previousSibling) && + prev instanceof Text + ) { + textChild.remove(); + textChild = prev; + } + startContainer = textChild; + startOffset = textChild.data.length; } } break;