X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/18ede9bbd3bef5e7b82ae52c0dac2a26fe2c24fd..refs/pull/5689/head:/resources/js/services/dom.ts diff --git a/resources/js/services/dom.ts b/resources/js/services/dom.ts index 661ed7ca3..c3817536c 100644 --- a/resources/js/services/dom.ts +++ b/resources/js/services/dom.ts @@ -225,7 +225,7 @@ export function findTargetNodeAndOffset(parentNode: HTMLElement, offset: number) if (currentNode.nodeType === Node.TEXT_NODE) { // For text nodes, count the length of their content // Returns if within range - const textLength = currentNode.textContent.length; + const textLength = (currentNode.textContent || '').length; if (currentOffset + textLength >= offset) { return { node: currentNode, @@ -237,9 +237,9 @@ export function findTargetNodeAndOffset(parentNode: HTMLElement, offset: number) } else if (currentNode.nodeType === Node.ELEMENT_NODE) { // Otherwise, if an element, track the text length and search within // if in range for the target offset - const elementTextLength = currentNode.textContent.length; + const elementTextLength = (currentNode.textContent || '').length; if (currentOffset + elementTextLength >= offset) { - return findTargetNodeAndOffset(currentNode, offset - currentOffset); + return findTargetNodeAndOffset(currentNode as HTMLElement, offset - currentOffset); } currentOffset += elementTextLength; @@ -251,9 +251,9 @@ export function findTargetNodeAndOffset(parentNode: HTMLElement, offset: number) } /** - * Create a hash for the given HTML element. + * Create a hash for the given HTML element content. */ export function hashElement(element: HTMLElement): string { - const normalisedElemHtml = element.outerHTML.replace(/\s{2,}/g, ''); - return cyrb53(normalisedElemHtml); + const normalisedElemText = (element.textContent || '').replace(/\s{2,}/g, ''); + return cyrb53(normalisedElemText); } \ No newline at end of file