redom-jsx
Version:
Tiny turboboosted JavaScript library for creating user interfaces. 100 % test coverage!
77 lines (67 loc) • 1.78 kB
JavaScript
/* global SVGElement */
import { setStyle } from './setstyle.js';
import { getEl } from './util.js';
const xlinkns = 'http://www.w3.org/1999/xlink';
export function setAttr (view, arg1, arg2) {
setAttrInternal(view, arg1, arg2);
}
export function setAttrInternal (view, arg1, arg2, initial) {
const el = getEl(view);
const isObj = typeof arg1 === 'object';
if (isObj) {
for (const key in arg1) {
setAttrInternal(el, key, arg1[key], initial);
}
} else {
const isSVG = el instanceof SVGElement;
const isFunc = typeof arg2 === 'function';
if (arg1 === 'style' && typeof arg2 === 'object') {
setStyle(el, arg2);
} else if (isSVG && isFunc) {
el[arg1] = arg2;
} else if (arg1 === 'dataset') {
setData(el, arg2);
} else if (!isSVG && (arg1 in el || isFunc) && (arg1 !== 'list')) {
el[arg1] = arg2;
} else {
if (isSVG && (arg1 === 'xlink')) {
setXlink(el, arg2);
return;
}
if (initial && arg1 === 'class') {
arg2 = el.className + ' ' + arg2;
}
if (arg2 == null) {
el.removeAttribute(arg1);
} else {
el.setAttribute(arg1, arg2);
}
}
}
}
export function setXlink (el, arg1, arg2) {
if (typeof arg1 === 'object') {
for (const key in arg1) {
setXlink(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.setAttributeNS(xlinkns, arg1, arg2);
} else {
el.removeAttributeNS(xlinkns, arg1, arg2);
}
}
}
export function setData (el, arg1, arg2) {
if (typeof arg1 === 'object') {
for (const key in arg1) {
setData(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.dataset[arg1] = arg2;
} else {
delete el.dataset[arg1];
}
}
}