[WIP] Add child generators and refactor

This commit is contained in:
Talia 2024-01-22 10:35:20 +01:00
parent f6e7c00944
commit dda6673f15
1 changed files with 7 additions and 5 deletions

View File

@ -98,7 +98,7 @@ const specialAttributes = {
}, },
shadowRoot: { shadowRoot: {
set: (element, value) => { set: (element, value) => {
parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), null, value) parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), value)
} }
} }
} }
@ -145,18 +145,20 @@ const setReactiveAttribute = (element, attribute, reactive, abortController) =>
} }
} }
const parseArgs = (element, before, ...args) => { const parseArgs = (element, ...args) => {
if (element.content) element = element.content if (element.content) element = element.content
for (const arg of args) if (arg !== empty) { for (const arg of args) if (arg !== empty) {
const child = toChild(arg) const child = toChild(arg)
if (child) if (child)
element.insertBefore(child, before) element.append(child)
else if (arg === undefined || arg == null) else if (arg === undefined || arg == null)
console.warn(`An argument of type ${typeof arg} has been ignored`, element) console.warn(`An argument of type ${typeof arg} has been ignored`, element)
else if (typeof arg == "function" && arg.length == 0)
parseArgs(element, arg())
else if (typeof arg == "function") else if (typeof arg == "function")
arg(element) arg(element)
else if ("length" in arg) else if ("length" in arg)
parseArgs(element, before, ...arg) parseArgs(element, ...arg)
else else
for (const key in arg) for (const key in arg)
setAttribute(element, key, arg[key]) setAttribute(element, key, arg[key])
@ -173,7 +175,7 @@ const node = (name, args, options) => {
element = document.createElementNS(options.xmlns, name, opts) element = document.createElementNS(options.xmlns, name, opts)
else else
element = document.createElement(name, opts) element = document.createElement(name, opts)
parseArgs(element, null, args) parseArgs(element, args)
return element return element
} }