From 79c244acbf32bce681b2623a04648e0b4aee6360 Mon Sep 17 00:00:00 2001 From: SherSpock Date: Wed, 9 Oct 2019 08:08:20 -0400 Subject: [PATCH 1/2] Extract `preventOverlap/0` in `cleditHighlighter` Replaces comments, follows example of `highlight/1` in same file --- src/services/editor/cledit/cleditHighlighter.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/services/editor/cledit/cleditHighlighter.js b/src/services/editor/cledit/cleditHighlighter.js index 2b688f855..f7e89c1a6 100644 --- a/src/services/editor/cledit/cleditHighlighter.js +++ b/src/services/editor/cledit/cleditHighlighter.js @@ -120,10 +120,14 @@ function Highlighter(editor) { return false; }); - if (leftIndex - rightIndex > sectionList.length) { - // Prevent overlap - rightIndex = leftIndex - sectionList.length; - } + const preventOverlap = () => { + if (leftIndex - rightIndex > sectionList.length) { + rightIndex = leftIndex - sectionList.length; + } + return rightIndex; + }; + + rightIndex = preventOverlap(); const leftSections = sectionList.slice(0, leftIndex); modifiedSections = newSectionList.slice(leftIndex, newSectionList.length + rightIndex); From 7bb112fb4d2d129d3f01ab579f9c1bf01014d928 Mon Sep 17 00:00:00 2001 From: SherSpock Date: Wed, 9 Oct 2019 08:15:38 -0400 Subject: [PATCH 2/2] Extract node removal function and helper in `cleditHighlighter` Replaces comments and follows example of `highlight/1` in same file --- .../editor/cledit/cleditHighlighter.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/services/editor/cledit/cleditHighlighter.js b/src/services/editor/cledit/cleditHighlighter.js index f7e89c1a6..22071f360 100644 --- a/src/services/editor/cledit/cleditHighlighter.js +++ b/src/services/editor/cledit/cleditHighlighter.js @@ -176,18 +176,22 @@ function Highlighter(editor) { contentElt.appendChild(newSectionEltList); } - // Remove unauthorized nodes (text nodes outside of sections or - // duplicated sections via copy/paste) - let childNode = contentElt.firstChild; - while (childNode) { - const nextNode = childNode.nextSibling; - if (!childNode.section) { - contentElt.removeChild(childNode); + const lacksSection = node => !node.section; + + const removeUnauthorizedChildNodesFrom = (parentNode) => { + let childNode = parentNode.firstChild; + while (childNode) { + const nextNode = childNode.nextSibling; + if (lacksSection(childNode)) { + parentNode.removeChild(childNode); + } + childNode = nextNode; } - childNode = nextNode; - } - this.addTrailingNode(); - this.$trigger('highlighted'); + this.addTrailingNode(); + this.$trigger('highlighted'); + }; + + removeUnauthorizedChildNodesFrom(contentElt); if (editor.selectionMgr.hasFocus()) { editor.selectionMgr.restoreSelection();