diff --git a/src/selector-tag.ts b/src/selector-tag.ts index d5fb9c94e..8767b84d8 100644 --- a/src/selector-tag.ts +++ b/src/selector-tag.ts @@ -1,9 +1,16 @@ -import {sanitizeSelectorItem} from './utilities-selectors' -import {CssSelector} from './types' +import { + sanitizeSelectorItem, + sanitizeSelectorNeedle +} from './utilities-selectors'; +import {CssSelector, SelectorNeedle} from './types'; /** * Get tag selector for an element. */ -export function getTagSelector (element: Element): Array { - return [sanitizeSelectorItem(element.tagName.toLowerCase())] +export function getTagSelector (needle: SelectorNeedle): Array { + const elements = sanitizeSelectorNeedle(needle) + const selectors = [...new Set(elements.map((element) => { + return sanitizeSelectorItem(element.tagName.toLowerCase()) + }))] + return (selectors.length === 0 || selectors.length > 1) ? [] : [selectors[0]] } diff --git a/test/selector-tag.spec.js b/test/selector-tag.spec.js index b4325fe87..8450ce1d6 100644 --- a/test/selector-tag.spec.js +++ b/test/selector-tag.spec.js @@ -17,7 +17,7 @@ describe('selector - tag', function () { root.innerHTML = '
' const element = root.firstChild const selector = getTagSelector(element) - assert.equal(selector, 'div') + assert.sameMembers(selector, ['div']) assert.equal(root.querySelector(selector), element) }) @@ -29,4 +29,11 @@ describe('selector - tag', function () { assert.equal(root.querySelector(selector), element) }) + it('should generate tag selector for multiple elements', () => { + root.innerHTML = '
' + const elements = [...root.querySelectorAll('div')] + const result = getTagSelector(elements) + assert.sameMembers(result, ['div']) + }) + })