Skip to content

Commit

Permalink
fix: set correct type of "selectors" in options
Browse files Browse the repository at this point in the history
refs #48
  • Loading branch information
fczbkk committed Jan 15, 2022
1 parent 5761ace commit a845239
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import {getFallbackSelector} from './selector-fallback'
import {sanitizeOptions} from './utilities-options'
import {
getClosestIdentifiableParent,
sanitizeSelectorNeedle
sanitizeSelectorNeedle,
} from './utilities-selectors'
import {CssSelector, CssSelectorGeneratorOptions} from './types'
import {
CssSelector,
CssSelectorGeneratorOptionsInput,
} from './types'
import {testSelector} from './utilities-dom'
import {SELECTOR_SEPARATOR} from './constants'

Expand All @@ -13,7 +16,7 @@ import {SELECTOR_SEPARATOR} from './constants'
*/
export function getCssSelector (
needle: unknown,
custom_options: Partial<CssSelectorGeneratorOptions> = {}
custom_options: CssSelectorGeneratorOptionsInput = {},
): CssSelector {
const elements = sanitizeSelectorNeedle(needle)
const options = sanitizeOptions(elements[0], custom_options)
Expand All @@ -28,15 +31,15 @@ export function getCssSelector (
elements,
currentRoot,
partialSelector,
options
options,
)
}

let closestIdentifiableParent = updateIdentifiableParent()
while (closestIdentifiableParent) {
const {
foundElements,
selector
selector,
} = closestIdentifiableParent
if (testSelector(elements, selector, options.root)) {
return selector
Expand Down
11 changes: 8 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export enum CssSelectorType {
nthchild = 'nthchild',
nthoftype = 'nthoftype'
}

export type CssSelectorTypes = Array<CssSelectorType>

export type CssSelectorsByType = Record<CssSelectorType, CssSelectors>
Expand All @@ -66,9 +67,9 @@ export type CssSelectorData = {
[key in CssSelectorType]?: Array<string> | Array<Array<string>>
}

export type CssSelectorGeneratorOptions = {
export type CssSelectorGeneratorOptionsInput = Partial<{
// List of selector types to use. They will be prioritised by their order.
selectors: CssSelectorTypes,
selectors: (keyof typeof CssSelectorType)[],
// List of selectors that should be prioritised.
whitelist: Array<CssSelectorMatch>,
// List of selectors that should be ignored.
Expand All @@ -85,7 +86,11 @@ export type CssSelectorGeneratorOptions = {
maxCombinations: number,
// Maximum number of selector candidates to be tested for each element. This is handy for performance reasons, e.g. when elements can produce large number of combinations of various types of selectors.
maxCandidates: number
}
}>

export type CssSelectorGeneratorOptions =
Required<Omit<CssSelectorGeneratorOptionsInput, 'selectors'>
& { selectors: CssSelectorTypes }>

export type IdentifiableParent =
null
Expand Down

0 comments on commit a845239

Please sign in to comment.