forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toSVG.ts
28 lines (23 loc) · 799 Bytes
/
toSVG.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Copyright IBM Corp. 2018, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import getAttributes from './getAttributes';
import IconDescriptor from './types';
/**
* Convert an icon descriptor to a DOM node.
*/
export default function toSVG(descriptor: IconDescriptor): SVGElement {
const { elem = 'svg', attrs = {}, content = [] } = descriptor;
const node = document.createElementNS('http://www.w3.org/2000/svg', elem);
const attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
Object.keys(attributes).forEach((key) => {
node.setAttribute(key, attrs[key]);
});
for (let i = 0; i < content.length; i++) {
node.appendChild(toSVG(content[i]));
}
return node;
}