54 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-02-20 23:48:16 +01:00
export function setAttribute(el, name, value) {
2019-02-27 23:20:29 +01:00
if (name === "className") {
name = "class";
}
2019-02-20 23:48:16 +01:00
el.setAttribute(name, value);
}
export function el(elementName, attrs, children) {
const e = document.createElement(elementName);
2019-02-26 22:45:58 +01:00
if (typeof attrs === "object" && attrs !== null) {
2019-02-20 23:48:16 +01:00
for (let [name, value] of Object.entries(attrs)) {
setAttribute(e, name, value);
}
}
2019-02-26 22:45:58 +01:00
if (children) {
if (!Array.isArray(children)) {
children = [children];
}
2019-02-20 23:48:16 +01:00
// TODO: use fragment here?
for (let c of children) {
2019-02-26 20:49:32 +01:00
if (typeof c === "string") {
c = text(c);
}
2019-02-20 23:48:16 +01:00
e.appendChild(c);
}
}
return e;
}
export function text(str) {
return document.createTextNode(str);
}
export function ol(... params) { return el("ol", ... params); }
export function ul(... params) { return el("ul", ... params); }
export function li(... params) { return el("li", ... params); }
export function div(... params) { return el("div", ... params); }
export function h1(... params) { return el("h1", ... params); }
export function h2(... params) { return el("h2", ... params); }
export function h3(... params) { return el("h3", ... params); }
export function h4(... params) { return el("h4", ... params); }
export function h5(... params) { return el("h5", ... params); }
export function h6(... params) { return el("h6", ... params); }
export function p(... params) { return el("p", ... params); }
export function strong(... params) { return el("strong", ... params); }
export function em(... params) { return el("em", ... params); }
export function span(... params) { return el("span", ... params); }
export function img(... params) { return el("img", ... params); }
export function section(... params) { return el("section", ... params); }
export function main(... params) { return el("main", ... params); }
export function article(... params) { return el("article", ... params); }
export function aside(... params) { return el("aside", ... params); }
export function pre(... params) { return el("pre", ... params); }