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-06-14 22:40:18 +02:00
|
|
|
if (value === false) {
|
|
|
|
el.removeAttribute(name);
|
|
|
|
} else {
|
|
|
|
if (value === true) {
|
|
|
|
value = name;
|
|
|
|
}
|
|
|
|
el.setAttribute(name, value);
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:41:45 +02:00
|
|
|
export const TAG_NAMES = [
|
|
|
|
"ol", "ul", "li", "div", "h1", "h2", "h3", "h4", "h5", "h6",
|
|
|
|
"p", "strong", "em", "span", "img", "section", "main", "article", "aside",
|
|
|
|
"pre", "button"];
|
|
|
|
|
2019-02-20 23:48:16 +01:00
|
|
|
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); }
|
2019-02-27 22:50:08 +01:00
|
|
|
export function pre(... params) { return el("pre", ... params); }
|
2019-06-02 14:59:30 +02:00
|
|
|
export function button(... params) { return el("button", ... params); }
|