- From: Cyril Auburtin <notifications@github.com>
- Date: Wed, 26 Jul 2017 01:34:36 +0000 (UTC)
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Wednesday, 26 July 2017 01:35:00 UTC
it's this more or less?
```js
const safeAttrs = new Set(['textContent', 'id', 'className', 'htmlFor', 'disabled', 'checked', 'autocomplete', 'crossorigin', 'async', 'innerHTML']); // probably missing some
// html helper, usage:: var span = $('span', 'Hello'), div = h('div', {id:'test', onClick:console.log}, span, 'test2')
function $(tag, ...o){
const el = document.createElement(tag);
const childrenIndex = o.findIndex(x => typeof x=='string' || typeof x=='number' || x instanceof Node);
const props = Object.assign({}, ...o.slice(0,childrenIndex);
for (var k in props) {
const value = props[k];
if (typeof value=='function') {
const name = k.slice(2).toLowerCase();
el.addEventListener(name, value);
} else {
if (k=='style') Object.assign(el.style, value);
else if (safeAttrs.has(k)) el[k] = value;
else if (typeof value=='string') el.setAttribute(k, value);
}
}
el.append(...o.slice(childrenIndex));
return el;
}
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/150#issuecomment-317920802
Received on Wednesday, 26 July 2017 01:35:00 UTC