super simple dom template function
let mydom = el`div#foo.bar.baz
${'foo'}
${el`${'bar'}`}
${'baz'}`;
function el(qs, ...children) { | |
let s = qs[0]; | |
let re = /([^#\.]+)?(?:#([^\.]+))?(?:\.(.+))*/; | |
let m = s.match(re); | |
let el = document.createElement(m[1] || 'div'); | |
let id = m[2]; if (id) el.id = id; | |
let cls = m[3]; if (cls) el.className = cls.replace(/\./g, ' '); | |
for (let child of children) { | |
el.appendChild( | |
typeof child === 'string' ? | |
document.createTextNode(child) : | |
child | |
); | |
} | |
return el; | |
} |