Improve HTML render helper

This commit is contained in:
Talia 2021-02-19 21:37:13 +01:00
parent 79af2b74d8
commit da2f082939
No known key found for this signature in database
GPG key ID: AD727AD22802D0D6

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) => {
for (arg of args)
if (typeof(arg) == "string")
@ -8,11 +17,11 @@ const parseArgs = (element, args) => {
parseArgs(element, arg)
else
for (key in arg)
element.setAttribute(key, arg[key])
element.setAttribute(key.replace("_", "-"), parseAttribute(arg[key]))
}
export const node = (name, args) => {
const element = document.createElement(name)
const element = document.createElement(name.replace("_", "-"))
parseArgs(element, args)
return element
}