Skip to content

Commit 941e596

Browse files
author
Daniel Morse
committed
feat: add helper 'containsTagName'
1 parent ba18349 commit 941e596

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Loops through a list of nodes to check if it contains a specific tag name (or names).
3+
*
4+
* @param {NodeList} nodeList - A list of nodes in the DOM tree.
5+
* @param {(string|string[])} tags - A tag name or a list of tag names.
6+
* @returns {Boolean} - True if list contains any of the specified tags.
7+
*/
8+
9+
export function containsTagName(nodeList, tags) {
10+
let hasElement = false;
11+
12+
if (typeof tags === 'string') {
13+
tags = [tags];
14+
} else if (!Array.isArray(tags)) {
15+
return null;
16+
}
17+
18+
tags = tags.map(name => name.toUpperCase());
19+
20+
nodeList.forEach((childElement, i) => {
21+
if (childElement.tagName && tags.includes(childElement.tagName)) {
22+
hasElement = true;
23+
}
24+
});
25+
26+
return hasElement;
27+
}

packages/core/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './color-contrast';
22
export * from './contains-any';
3+
export * from './contains-tag-name';
34
export * from './css';
45
export * from './declarative-click-handler';
56
export * from './environment';

0 commit comments

Comments
 (0)