Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better selection #396

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions server/src/workflow-management/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,31 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
}
}

if (element.parentElement) {
// Look for identical siblings
const siblings = Array.from(element.parentElement.children);
const identicalSiblings = siblings.filter(sibling => {
if (sibling === element) return false;

let siblingSelector = sibling.tagName.toLowerCase();
const siblingClassName = typeof sibling.className === 'string' ? sibling.className : '';
if (siblingClassName) {
const siblingClasses = siblingClassName.split(/\s+/).filter(Boolean);
const validSiblingClasses = siblingClasses.filter(cls => !cls.startsWith('!') && !cls.includes(':'));
if (validSiblingClasses.length > 0) {
siblingSelector += '.' + validSiblingClasses.map(cls => CSS.escape(cls)).join('.');
}
}

return siblingSelector === selector;
});

if (identicalSiblings.length > 0) {
const position = siblings.indexOf(element) + 1;
selector += `:nth-child(${position})`;
}
}

Comment on lines +1657 to +1681
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor duplicated selector generation logic into a shared function.

The identical sibling handling logic is duplicated in three locations. This violates the DRY principle and makes maintenance more difficult.

Consider extracting the common logic into a shared utility function:

+ function appendNthChildSelectorIfNeeded(element: HTMLElement, selector: string): string {
+   if (element.parentElement) {
+     const siblings = Array.from(element.parentElement.children);
+     const identicalSiblings = siblings.filter(sibling => {
+       if (sibling === element) return false;
+       
+       let siblingSelector = sibling.tagName.toLowerCase();
+       const siblingClassName = typeof sibling.className === 'string' ? sibling.className : '';
+       if (siblingClassName) {
+         const siblingClasses = siblingClassName.split(/\s+/).filter(Boolean);
+         const validSiblingClasses = siblingClasses.filter(cls => !cls.startsWith('!') && !cls.includes(':'));
+         if (validSiblingClasses.length > 0) {
+           siblingSelector += '.' + validSiblingClasses.map(cls => CSS.escape(cls)).join('.');
+         }
+       }
+       
+       return siblingSelector === selector;
+     });
+ 
+     if (identicalSiblings.length > 0) {
+       const position = siblings.indexOf(element) + 1;
+       selector += `:nth-child(${position})`;
+     }
+   }
+   return selector;
+ }

  function getNonUniqueSelector(element: HTMLElement): string {
    let selector = element.tagName.toLowerCase();
    // ... existing class handling logic ...
-   if (element.parentElement) {
-     // ... duplicate sibling handling logic ...
-   }
+   selector = appendNthChildSelectorIfNeeded(element, selector);
    return selector;
  }

Also applies to: 1922-1946, 2078-2102

return selector;
}

Expand Down Expand Up @@ -1894,6 +1919,31 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
}
}

if (element.parentElement) {
// Look for identical siblings
const siblings = Array.from(element.parentElement.children);
const identicalSiblings = siblings.filter(sibling => {
if (sibling === element) return false;

let siblingSelector = sibling.tagName.toLowerCase();
const siblingClassName = typeof sibling.className === 'string' ? sibling.className : '';
if (siblingClassName) {
const siblingClasses = siblingClassName.split(/\s+/).filter(Boolean);
const validSiblingClasses = siblingClasses.filter(cls => !cls.startsWith('!') && !cls.includes(':'));
if (validSiblingClasses.length > 0) {
siblingSelector += '.' + validSiblingClasses.map(cls => CSS.escape(cls)).join('.');
}
}

return siblingSelector === selector;
});

if (identicalSiblings.length > 0) {
const position = siblings.indexOf(element) + 1;
selector += `:nth-child(${position})`;
}
}

return selector;
}

Expand Down Expand Up @@ -2025,6 +2075,31 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
}
}

if (element.parentElement) {
// Look for identical siblings
const siblings = Array.from(element.parentElement.children);
const identicalSiblings = siblings.filter(sibling => {
if (sibling === element) return false;

let siblingSelector = sibling.tagName.toLowerCase();
const siblingClassName = typeof sibling.className === 'string' ? sibling.className : '';
if (siblingClassName) {
const siblingClasses = siblingClassName.split(/\s+/).filter(Boolean);
const validSiblingClasses = siblingClasses.filter(cls => !cls.startsWith('!') && !cls.includes(':'));
if (validSiblingClasses.length > 0) {
siblingSelector += '.' + validSiblingClasses.map(cls => CSS.escape(cls)).join('.');
}
}

return siblingSelector === selector;
});

if (identicalSiblings.length > 0) {
const position = siblings.indexOf(element) + 1;
selector += `:nth-child(${position})`;
}
}

return selector;
}

Expand Down