@soil/arch
Version:
Architectural constructs for web applications.
38 lines • 1.2 kB
JavaScript
import { isPlainObject } from './isPlainObject';
/**
* Assign properties from an object literal to an object of type `HTMLElement`
* or `SVGElement`.
*/
export function assignProps(elem, props) {
for (var p in props) {
if (props.hasOwnProperty(p)) {
if (p === 'role' || p.startsWith('aria-')) {
// First-class support for accessibility attributes.
elem.setAttribute(p, props[p] || '');
}
else if (isPlainObject(props[p])) {
// Go deeper for properties such as `style` or SVG-specific properties.
assignNestedProps(elem[p], props[p]);
}
else {
elem[p] = props[p];
}
}
}
}
function assignNestedProps(target, props) {
for (var p in props) {
if (props.hasOwnProperty(p)) {
if (isPlainObject(props[p])) {
if (target[p] === undefined) {
target[p] = {};
}
assignNestedProps(target[p], props[p]);
}
else {
target[p] = props[p];
}
}
}
}
//# sourceMappingURL=assignProps.js.map