From beb2c7f577a4e46f15e94a97cd1b4ce32978c182 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sun, 26 Nov 2023 02:16:15 +0100 Subject: [PATCH] Squire: Handle empty nodes in moveRangeBoundariesDownTree https://github.com/fastmail/Squire/commit/b85754ca50de679f5735ad2be752a8222750da25 --- vendors/squire/build/squire-raw.js | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/vendors/squire/build/squire-raw.js b/vendors/squire/build/squire-raw.js index 5530e40ec..4e032b683 100644 --- a/vendors/squire/build/squire-raw.js +++ b/vendors/squire/build/squire-raw.js @@ -780,24 +780,36 @@ const }, moveRangeBoundariesDownTree = range => { - let startContainer = range.startContainer, - startOffset = range.startOffset, - endContainer = range.endContainer, - endOffset = range.endOffset, - maySkipBR = true, + let { startContainer, startOffset, endContainer, endOffset } = range; + let maySkipBR = true, child; - while (startContainer.nodeType !== TEXT_NODE) { - child = startContainer.childNodes[ startOffset ]; + while (!(startContainer instanceof Text)) { + child = 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; + } + } break; } startContainer = child; startOffset = 0; } if (endOffset) { - while (endContainer.nodeType !== TEXT_NODE) { - child = endContainer.childNodes[ endOffset - 1 ]; + while (!(endContainer instanceof Text)) { + child = endContainer.childNodes[endOffset - 1]; if (!child || isLeaf(child)) { if (maySkipBR && child?.nodeName === 'BR') { --endOffset; @@ -810,7 +822,7 @@ const endOffset = getLength(endContainer); } } else { - while (endContainer.nodeType !== TEXT_NODE) { + while (!(endContainer instanceof Text)) { child = endContainer.firstChild; if (!child || isLeaf(child)) { break;