From e727d11861825a5bc4b9e514080f2df1c029cc2a Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Tue, 3 Sep 2024 05:50:04 +0530 Subject: [PATCH] feat: get all descendant selectors --- server/src/workflow-management/selector.ts | 25 +++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/server/src/workflow-management/selector.ts b/server/src/workflow-management/selector.ts index 3b4e7c07..aacc9a5e 100644 --- a/server/src/workflow-management/selector.ts +++ b/server/src/workflow-management/selector.ts @@ -784,12 +784,9 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates return { generalSelector: '' }; } }; -interface SelectorNode { - selector: string; - children: SelectorNode[]; -} -export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise => { + +export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise => { try { const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => { function getNonUniqueSelector(element: HTMLElement): string { @@ -822,22 +819,24 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD return path.join(' > '); } - function getDescendantSelectors(element: HTMLElement, currentDepth: number = 0): SelectorNode[] { + function getAllDescendantSelectors(element: HTMLElement, currentDepth: number = 0): string[] { if (currentDepth >= maxDepth) return []; + let selectors: string[] = []; const children = Array.from(element.children) as HTMLElement[]; - return children.map(child => { - return { - selector: getSelectorPath(child), - children: getDescendantSelectors(child, currentDepth + 1) - }; - }); + + for (const child of children) { + selectors.push(getSelectorPath(child)); + selectors = selectors.concat(getAllDescendantSelectors(child, currentDepth + 1)); + } + + return selectors; } const parentElement = document.querySelector(parentSelector) as HTMLElement; if (!parentElement) return []; - return getDescendantSelectors(parentElement); + return getAllDescendantSelectors(parentElement); }, { parentSelector, maxDepth }); return childSelectors || [];