Skip to content

Commit

Permalink
feat: multi elements support for tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Aug 15, 2021
1 parent f481ca6 commit df4ec59
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/selector-tag.ts
Original file line number Diff line number Diff line change
@@ -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<CssSelector> {
return [sanitizeSelectorItem(element.tagName.toLowerCase())]
export function getTagSelector (needle: SelectorNeedle): Array<CssSelector> {
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]]
}
9 changes: 8 additions & 1 deletion test/selector-tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('selector - tag', function () {
root.innerHTML = '<div></div>'
const element = root.firstChild
const selector = getTagSelector(element)
assert.equal(selector, 'div')
assert.sameMembers(selector, ['div'])
assert.equal(root.querySelector(selector), element)
})

Expand All @@ -29,4 +29,11 @@ describe('selector - tag', function () {
assert.equal(root.querySelector(selector), element)
})

it('should generate tag selector for multiple elements', () => {
root.innerHTML = '<div></div><div></div>'
const elements = [...root.querySelectorAll('div')]
const result = getTagSelector(elements)
assert.sameMembers(result, ['div'])
})

})

0 comments on commit df4ec59

Please sign in to comment.