Skip to content

Commit

Permalink
👌 simplify getPositionSelector a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Oct 19, 2022
1 parent a9a5445 commit 3441d27
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ export function getSelectorsFromElement(element: Element, actionNameAttribute: s
)
}
const globallyUniqueSelectorStrategies = attributeSelectors.concat(getIDSelector)
const uniqueAmongChildrenSelectorStrategies = attributeSelectors.concat(getClassSelector)
const uniqueAmongChildrenSelectorStrategies = attributeSelectors.concat([getClassSelector, getTagNameSelector])
return {
selector: getSelectorFromElement(element, globallyUniqueSelectorStrategies, uniqueAmongChildrenSelectorStrategies),
selector_combined: getSelectorFromElement(
element,
globallyUniqueSelectorStrategies,
uniqueAmongChildrenSelectorStrategies.concat(getTagNameSelector),
uniqueAmongChildrenSelectorStrategies,
{ useCombinedSelectors: true }
),
selector_stopping_when_unique: getSelectorFromElement(
element,
globallyUniqueSelectorStrategies.concat([getClassSelector, getTagNameSelector]),
uniqueAmongChildrenSelectorStrategies.concat(getTagNameSelector)
uniqueAmongChildrenSelectorStrategies
),
selector_all_together: getSelectorFromElement(
element,
globallyUniqueSelectorStrategies.concat([getClassSelector, getTagNameSelector]),
uniqueAmongChildrenSelectorStrategies.concat(getTagNameSelector),
uniqueAmongChildrenSelectorStrategies,
{ useCombinedSelectors: true }
),
}
Expand Down Expand Up @@ -93,7 +93,7 @@ function getSelectorFromElement(
useCombinedSelectors ? targetElementSelector : undefined
)
targetElementSelector = combineSelector(
uniqueSelectorAmongChildren ?? getPositionSelector(element),
uniqueSelectorAmongChildren || getPositionSelector(element) || getTagNameSelector(element),
targetElementSelector
)

Expand Down Expand Up @@ -145,7 +145,7 @@ function getAttributeSelector(attributeName: string, element: Element): string |
}
}

function getPositionSelector(element: Element): string {
function getPositionSelector(element: Element): string | undefined {
const parent = element.parentElement!
let sibling = parent.firstElementChild
let currentIndex = 0
Expand All @@ -167,7 +167,7 @@ function getPositionSelector(element: Element): string {
sibling = sibling.nextElementSibling
}

return currentIndex === 1 ? element.tagName : `${element.tagName}:nth-of-type(${elementIndex!})`
return currentIndex > 1 ? `${element.tagName}:nth-of-type(${elementIndex!})` : undefined
}

function findSelector(
Expand Down

0 comments on commit 3441d27

Please sign in to comment.