From 5b10a908149af0587a353c63edc88bb7198c2f28 Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Tue, 28 Jan 2025 13:55:47 +0100 Subject: [PATCH] Fix out-of-bounds index in BesQuillService --- service.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/service.js b/service.js index 6dc3d21..e9758f8 100644 --- a/service.js +++ b/service.js @@ -1209,17 +1209,26 @@ class BesQuillService extends BesTreeService { }) if (reproofNeeded) { - const [leaf, offset] = this.quillInstance.getLeaf(index) - let domElement = leaf.domNode - while (domElement && domElement.tagName !== 'P') { - domElement = domElement.parentNode - } - this.clearProofing(domElement) + const editorLength = this.quillInstance.getLength() + const clampedIndex = Math.min(index, editorLength - 1) - setTimeout(() => { - this.repositionAllMarkup() - this.scheduleProofing(1000) - }, 0) + const [leaf, offset] = this.quillInstance.getLeaf(clampedIndex) + if (leaf) { + let domElement = leaf.domNode + while (domElement && domElement.tagName !== 'P') { + domElement = domElement.parentNode + } + this.clearProofing(domElement) + + setTimeout(() => { + this.repositionAllMarkup() + this.scheduleProofing(1000) + }, 0) + } else { + console.warn( + 'Leaf is null. The index might be out of bounds or the editor content is empty.' + ) + } } }