Skip to content

Commit

Permalink
feat: multi elements support for nth-of-type
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Aug 15, 2021
1 parent 74443c2 commit f481ca6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/selector-nth-of-type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {getTagSelector} from './selector-tag'
import {CssSelector} from './types'
import {CssSelector, SelectorNeedle} from './types';
import {sanitizeSelectorNeedle} from './utilities-selectors';
import {getIntersection} from './utilities-data';

/**
* Get nth-of-type selector for an element.
*/
export function getNthOfTypeSelector (element: Element): Array<CssSelector> {
export function getElementNthOfTypeSelector (element: Element): Array<CssSelector> {
const tag = getTagSelector(element)[0]
const parentElement = element.parentElement

Expand All @@ -18,3 +20,11 @@ export function getNthOfTypeSelector (element: Element): Array<CssSelector> {

return []
}

/**
* Get Nth-of-type selector matching all elements.
*/
export function getNthOfTypeSelector (needle: SelectorNeedle): Array<CssSelector> {
const elements = sanitizeSelectorNeedle(needle)
return getIntersection(elements.map(getElementNthOfTypeSelector))
}
10 changes: 10 additions & 0 deletions test/selector-nth-of-type.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ describe('selector - nth-of-type', function () {
assert.doesNotThrow(fn)
})

it('should generate nth-of-type selector for multiple elements', () => {
root.innerHTML = `
<div><div></div><span></span></div>
<div><span></span></div>
`
const elements = [...root.querySelectorAll('span')]
const result = getNthOfTypeSelector(elements)
assert.sameMembers(result, ['span:nth-of-type(1)'])
})

})

0 comments on commit f481ca6

Please sign in to comment.