2021-02-19 20:37:13 +00:00
|
|
|
const parseAttribute = (attribute) => {
|
2021-05-22 12:34:17 +00:00
|
|
|
if (typeof attribute == "string" || typeof attribute == "number")
|
2021-02-19 20:37:13 +00:00
|
|
|
return attribute
|
|
|
|
else if ("join" in attribute)
|
|
|
|
return attribute.join(" ")
|
|
|
|
else
|
|
|
|
return JSON.stringify(attribute)
|
|
|
|
}
|
|
|
|
|
2021-02-17 10:03:21 +00:00
|
|
|
const parseArgs = (element, args) => {
|
2021-02-23 17:55:32 +00:00
|
|
|
if (element.content) element = element.content
|
2021-03-05 12:29:21 +00:00
|
|
|
for (let arg of args)
|
2021-05-22 12:34:17 +00:00
|
|
|
if (typeof arg == "string" || typeof arg == "number")
|
2021-02-17 10:03:21 +00:00
|
|
|
element.appendChild(document.createTextNode(arg))
|
|
|
|
else if ("nodeName" in arg)
|
|
|
|
element.appendChild(arg)
|
|
|
|
else if ("length" in arg)
|
|
|
|
parseArgs(element, arg)
|
|
|
|
else
|
2021-05-17 15:19:32 +00:00
|
|
|
for (let key in arg)
|
2021-05-26 09:26:44 +00:00
|
|
|
if (typeof arg[key] == "function")
|
|
|
|
element.addEventListener(key.replace(/^on[A-Z]/, x => x.charAt(x.length-1).toLowerCase()), arg[key])
|
|
|
|
else
|
|
|
|
element.setAttribute(key, parseAttribute(arg[key]))
|
2021-02-17 10:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:11:25 +00:00
|
|
|
const node = (name, args, xmlns) => {
|
2021-03-05 12:29:21 +00:00
|
|
|
let element
|
2021-02-23 18:11:25 +00:00
|
|
|
if (xmlns)
|
2021-05-26 09:26:12 +00:00
|
|
|
element = document.createElementNS(xmlns, name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase())
|
2021-02-23 18:11:25 +00:00
|
|
|
else
|
2021-05-26 09:26:12 +00:00
|
|
|
element = document.createElement(name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase())
|
2021-02-17 10:03:21 +00:00
|
|
|
parseArgs(element, args)
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:11:25 +00:00
|
|
|
const nameSpacedProxy = (xmlns) => new Proxy(Window, { get: (target, prop, receiver) => { return (...args) => node(prop, args, xmlns) }})
|
|
|
|
|
|
|
|
export const html = nameSpacedProxy()
|
|
|
|
export const svg = nameSpacedProxy("http://www.w3.org/2000/svg")
|