From 71b571f22c6eaaa75054237ea6fccdaf39167a6b Mon Sep 17 00:00:00 2001 From: Aleksey Novikov Date: Thu, 15 Feb 2024 13:51:34 +0300 Subject: [PATCH] fixed sanitization - cursor position https://github.com/surveyjs/survey-creator/issues/5185 --- src/utils/utils.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 9d995a343a..da1c0f00de 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -388,10 +388,21 @@ export function sanitizeEditableContent(element: any, cleanLineBreaks: boolean = element.innerText = innerText; range = document.createRange(); - range.setStart(element.childNodes[0], innerText.length - tail_len); - range.collapse(true); - selection.removeAllRanges(); - selection.addRange(range); + + const newPosition = innerText.length - tail_len; + let sumLen = 0; + for (let i = 0; i < element.childNodes.length; i++) { + const elementLen = element.childNodes[i].textContent.length; + if (elementLen == 0) continue; + if (sumLen + elementLen >= newPosition) { + range.setStart(element.childNodes[i], newPosition - sumLen); + range.collapse(true); + selection.removeAllRanges(); + selection.addRange(range); + return; + } + sumLen += elementLen; + } } } function mergeValues(src: any, dest: any) {