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,
} 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;
}
/**
- * 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);
+}
+
+/**
+ * Find the closest scroll container parent for the given element
+ * otherwise will default to the body element.
+ */
+export function findClosestScrollContainer(start: HTMLElement): HTMLElement {
+ let el: HTMLElement|null = start;
+ do {
+ const computed = window.getComputedStyle(el);
+ if (computed.overflowY === 'scroll') {
+ return el;
+ }
+
+ el = el.parentElement;
+ } while (el);
+
+ return document.body;
}
\ No newline at end of file