diff --git a/src/selector-nth-of-type.ts b/src/selector-nth-of-type.ts index 413a884cb..0579766f0 100644 --- a/src/selector-nth-of-type.ts +++ b/src/selector-nth-of-type.ts @@ -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 { +export function getElementNthOfTypeSelector (element: Element): Array { const tag = getTagSelector(element)[0] const parentElement = element.parentElement @@ -18,3 +20,11 @@ export function getNthOfTypeSelector (element: Element): Array { return [] } + +/** + * Get Nth-of-type selector matching all elements. + */ +export function getNthOfTypeSelector (needle: SelectorNeedle): Array { + const elements = sanitizeSelectorNeedle(needle) + return getIntersection(elements.map(getElementNthOfTypeSelector)) +} diff --git a/test/selector-nth-of-type.spec.js b/test/selector-nth-of-type.spec.js index 2472225c1..369e9ad71 100644 --- a/test/selector-nth-of-type.spec.js +++ b/test/selector-nth-of-type.spec.js @@ -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 = ` +
+
+ ` + const elements = [...root.querySelectorAll('span')] + const result = getNthOfTypeSelector(elements) + assert.sameMembers(result, ['span:nth-of-type(1)']) + }) + })