From 79af2b74d8c38559aef559a4af6e90cec9e290ab Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 17 Feb 2021 11:03:21 +0100 Subject: [PATCH] Add skooma-like functional DOM rendering helper --- render.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 render.js diff --git a/render.js b/render.js new file mode 100644 index 0000000..bbfff8d --- /dev/null +++ b/render.js @@ -0,0 +1,26 @@ +const parseArgs = (element, args) => { + for (arg of args) + if (typeof(arg) == "string") + element.appendChild(document.createTextNode(arg)) + else if ("nodeName" in arg) + element.appendChild(arg) + else if ("length" in arg) + parseArgs(element, arg) + else + for (key in arg) + element.setAttribute(key, arg[key]) +} + +export const node = (name, args) => { + const element = document.createElement(name) + parseArgs(element, args) + return element +} + +export const html = new Proxy(Window, { get: (target, prop, receiver) => { + // This distinction is only helpful because javascript is fucking restarted + if (prop.search(/^[A-Z]/)+1) + return (arg) => node(prop, [arg]) + else + return (...args) => node(prop, args) +}})