Improve HTML render helper

This commit is contained in:
Talia 2021-02-19 21:37:13 +01:00
parent cf4455ca7a
commit f740bf3957
Signed by: darkwiiplayer
GPG key ID: 7808674088232B3E

View file

@ -1,3 +1,12 @@
const parseAttribute = (attribute) => {
if (typeof(attribute) == "string")
return attribute
else if ("join" in attribute)
return attribute.join(" ")
else
return JSON.stringify(attribute)
}
const parseArgs = (element, args) => { const parseArgs = (element, args) => {
for (arg of args) for (arg of args)
if (typeof(arg) == "string") if (typeof(arg) == "string")
@ -8,11 +17,11 @@ const parseArgs = (element, args) => {
parseArgs(element, arg) parseArgs(element, arg)
else else
for (key in arg) for (key in arg)
element.setAttribute(key, arg[key]) element.setAttribute(key.replace("_", "-"), parseAttribute(arg[key]))
} }
export const node = (name, args) => { export const node = (name, args) => {
const element = document.createElement(name) const element = document.createElement(name.replace("_", "-"))
parseArgs(element, args) parseArgs(element, args)
return element return element
} }