const MAX_NESTING_PARENTS = 5; export function isSelectionInsideInput(selectionRange: Range, inputId: string) { const { commonAncestorContainer } = selectionRange; let parentNode: HTMLElement | null = commonAncestorContainer as HTMLElement; let iterations = 1; while (parentNode && parentNode.id !== inputId && iterations < MAX_NESTING_PARENTS) { parentNode = parentNode.parentElement; iterations++; } return Boolean(parentNode && parentNode.id === inputId); }