2021-06-23 14:21:33 +00:00
|
|
|
/*
|
|
|
|
A functional HTML generation library.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
html.label(
|
|
|
|
html.span("Delete everything", {class: ["warning", "important"]}),
|
|
|
|
html.button("Click", {onClick: e => document.body.innerHTML=""}),
|
|
|
|
)
|
|
|
|
or
|
|
|
|
html.ul([1, 2, 3, 4, 5].map(x => html.li(x)), {class: "numbers"})
|
|
|
|
*/
|
|
|
|
|
2023-12-23 16:31:01 +00:00
|
|
|
const weakReferences = new WeakMap()
|
|
|
|
const untilDeathDoThemPart = (referrer, reference) => {
|
|
|
|
if (!weakReferences.has(referrer)) {
|
|
|
|
weakReferences.set(referrer, new Set())
|
|
|
|
}
|
|
|
|
weakReferences.get(referrer).add(reference)
|
|
|
|
}
|
|
|
|
|
2022-03-26 11:40:50 +00:00
|
|
|
export const empty = Symbol("Explicit empty argument for Skooma")
|
|
|
|
|
2023-09-18 09:45:36 +00:00
|
|
|
const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase())
|
|
|
|
|
2021-08-16 15:57:10 +00:00
|
|
|
const insertStyles = (rule, styles) => {
|
2023-09-18 06:57:14 +00:00
|
|
|
for (const [key, value] of Object.entries(styles))
|
2021-08-16 15:57:10 +00:00
|
|
|
if (typeof value == "undefined")
|
|
|
|
rule.removeProperty(keyToPropName(key))
|
|
|
|
else
|
|
|
|
rule.setProperty(keyToPropName(key), value.toString())
|
|
|
|
}
|
|
|
|
|
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
|
2022-04-12 20:35:44 +00:00
|
|
|
else if (attribute && "join" in attribute)
|
2021-02-19 20:37:13 +00:00
|
|
|
return attribute.join(" ")
|
|
|
|
else
|
|
|
|
return JSON.stringify(attribute)
|
|
|
|
}
|
|
|
|
|
2022-04-12 20:36:35 +00:00
|
|
|
const defined = (value, fallback) => typeof value != "undefined" ? value : fallback
|
2023-12-14 23:10:45 +00:00
|
|
|
const getCustom = args => args.reduce(
|
|
|
|
(current, argument) => Array.isArray(argument)
|
|
|
|
? defined(getCustom(argument), current)
|
|
|
|
: (argument && typeof argument == "object")
|
|
|
|
? defined(argument.is, current)
|
|
|
|
: current
|
|
|
|
,undefined
|
2022-04-12 20:36:35 +00:00
|
|
|
)
|
|
|
|
|
2023-12-21 22:36:48 +00:00
|
|
|
const isReactive = object => object
|
|
|
|
&& (typeof object == "object")
|
|
|
|
&& ("addEventListener" in object)
|
|
|
|
&& ("value" in object)
|
|
|
|
|
|
|
|
const toChild = arg => {
|
|
|
|
if (typeof arg == "string" || typeof arg == "number") {
|
|
|
|
return document.createTextNode(arg)
|
|
|
|
} else if ("nodeName" in arg) {
|
|
|
|
return arg
|
|
|
|
} else if (isReactive(arg)) {
|
|
|
|
return reactiveChild(arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reactiveChild = reactive => {
|
|
|
|
const ref = new WeakRef(toChild(reactive.value))
|
|
|
|
reactive.addEventListener("change", () => {
|
|
|
|
const value = ref.deref()
|
|
|
|
if (value)
|
|
|
|
value.replaceWith(reactiveChild(reactive))
|
|
|
|
}, {once: true})
|
2023-12-23 16:31:01 +00:00
|
|
|
untilDeathDoThemPart(ref.deref(), reactive)
|
2023-12-21 22:36:48 +00:00
|
|
|
return ref.deref()
|
|
|
|
}
|
|
|
|
|
|
|
|
const specialAttributes = {
|
|
|
|
value: {
|
|
|
|
get: element => element.value,
|
|
|
|
set: (element, value) => {
|
|
|
|
element.setAttribute("value", value)
|
|
|
|
element.value = value
|
|
|
|
},
|
|
|
|
hook: (element, callback) => { element.addEventListener("input", callback) }
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
set: (element, value) => { insertStyles(element.style, value) }
|
|
|
|
},
|
|
|
|
dataset: {
|
|
|
|
set: (element, value) => {
|
|
|
|
for (const [attribute2, value2] of Object.entries(value)) {
|
|
|
|
element.dataset[attribute2] = parseAttribute(value2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
shadowRoot: {
|
|
|
|
set: (element, value) => {
|
|
|
|
parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), null, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const setAttribute = (element, attribute, value, cleanupSignal) => {
|
|
|
|
const special = specialAttributes[attribute]
|
|
|
|
if (isReactive(value))
|
|
|
|
setReactiveAttribute(element, attribute, value)
|
|
|
|
else if (typeof value === "function")
|
|
|
|
element.addEventListener(attribute.replace(/^on[A-Z]/, x => x.charAt(x.length-1).toLowerCase()), value, {signal: cleanupSignal})
|
|
|
|
else if (special) {
|
|
|
|
special.set(element, value)
|
|
|
|
}
|
|
|
|
else if (value === true)
|
|
|
|
{if (!element.hasAttribute(attribute)) element.setAttribute(attribute, '')}
|
|
|
|
else if (value === false)
|
|
|
|
element.removeAttribute(attribute)
|
|
|
|
else {
|
|
|
|
element.setAttribute(attribute, parseAttribute(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const setReactiveAttribute = (element, attribute, reactive, abortController) => {
|
2023-12-23 16:31:01 +00:00
|
|
|
untilDeathDoThemPart(element, reactive)
|
|
|
|
|
2023-12-21 22:36:48 +00:00
|
|
|
if (abortController) abortController.abort()
|
|
|
|
abortController = new AbortController()
|
|
|
|
|
|
|
|
const ref = new WeakRef(element)
|
|
|
|
setAttribute(element, attribute, reactive.value, abortController.signal)
|
|
|
|
|
|
|
|
reactive.addEventListener("change", () => {
|
|
|
|
const element = ref.deref()
|
|
|
|
if (element)
|
|
|
|
setReactiveAttribute(element, attribute, reactive, abortController)
|
|
|
|
}, {once: true})
|
|
|
|
|
|
|
|
const special = specialAttributes[attribute]
|
|
|
|
if (special?.hook) {
|
|
|
|
special.hook(element, () => {
|
|
|
|
const value = special.get(element, attribute)
|
|
|
|
if (value != reactive.value) reactive.value = value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:02:49 +00:00
|
|
|
const parseArgs = (element, before, ...args) => {
|
2021-02-23 17:55:32 +00:00
|
|
|
if (element.content) element = element.content
|
2023-12-21 22:36:48 +00:00
|
|
|
for (const arg of args) if (arg !== empty) {
|
|
|
|
const child = toChild(arg)
|
|
|
|
if (child)
|
|
|
|
element.insertBefore(child, before)
|
2022-03-26 11:40:50 +00:00
|
|
|
else if (arg === undefined || arg == null)
|
2023-03-01 10:01:49 +00:00
|
|
|
console.warn(`An argument of type ${typeof arg} has been ignored`, element)
|
2021-09-13 16:54:07 +00:00
|
|
|
else if (typeof arg == "function")
|
|
|
|
arg(element)
|
2021-02-17 10:03:21 +00:00
|
|
|
else if ("length" in arg)
|
2021-08-27 14:02:49 +00:00
|
|
|
parseArgs(element, before, ...arg)
|
2021-02-17 10:03:21 +00:00
|
|
|
else
|
2023-09-18 06:57:14 +00:00
|
|
|
for (const key in arg)
|
2023-12-21 22:36:48 +00:00
|
|
|
setAttribute(element, key, arg[key])
|
|
|
|
}
|
2021-02-17 10:03:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 20:36:35 +00:00
|
|
|
const node = (name, args, options) => {
|
2021-03-05 12:29:21 +00:00
|
|
|
let element
|
2023-09-18 06:57:14 +00:00
|
|
|
const custom = getCustom(args)
|
2023-12-14 23:10:45 +00:00
|
|
|
const opts = custom && {is: String(custom)}
|
|
|
|
|
2022-04-28 07:11:11 +00:00
|
|
|
if ("nameFilter" in options) name = options.nameFilter(name)
|
2021-07-30 10:12:00 +00:00
|
|
|
if (options.xmlns)
|
2023-12-14 23:10:45 +00:00
|
|
|
element = document.createElementNS(options.xmlns, name, opts)
|
2021-02-23 18:11:25 +00:00
|
|
|
else
|
2023-12-14 23:10:45 +00:00
|
|
|
element = document.createElement(name, opts)
|
2021-08-27 14:02:49 +00:00
|
|
|
parseArgs(element, null, args)
|
2021-02-17 10:03:21 +00:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:00:00 +00:00
|
|
|
const nameSpacedProxy = (options={}) => new Proxy(Window, {
|
2023-09-18 06:57:14 +00:00
|
|
|
get: (_target, prop, _receiver) => { return (...args) => node(prop, args, options) },
|
|
|
|
has: (_target, _prop) => true,
|
2021-08-04 15:00:00 +00:00
|
|
|
})
|
2021-02-23 18:11:25 +00:00
|
|
|
|
2023-09-18 09:45:36 +00:00
|
|
|
export const html = nameSpacedProxy({nameFilter: name => name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()})
|
|
|
|
export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})
|
|
|
|
|
|
|
|
// Other utility exports
|
|
|
|
|
|
|
|
// Wraps an event handler in a function that calls preventDefault on the event
|
|
|
|
export const handle = fn => event => { fn(event); event.preventDefault() }
|
2021-11-23 15:27:25 +00:00
|
|
|
|
2023-09-18 09:45:36 +00:00
|
|
|
// Wraps a list of elements in a document fragment
|
2022-06-22 13:51:03 +00:00
|
|
|
export const fragment = (...elements) => {
|
|
|
|
const fragment = new DocumentFragment()
|
2023-09-18 06:57:14 +00:00
|
|
|
for (const element of elements)
|
2022-06-22 13:51:03 +00:00
|
|
|
fragment.append(element)
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:45:36 +00:00
|
|
|
// Turns a template literal into document fragment.
|
|
|
|
// Strings are returned as text nodes.
|
|
|
|
// Elements are inserted in between.
|
2021-12-26 16:54:04 +00:00
|
|
|
const textFromTemplate = (literals, items) => {
|
|
|
|
const fragment = new DocumentFragment()
|
|
|
|
for (const key in items) {
|
|
|
|
fragment.append(document.createTextNode(literals[key]))
|
|
|
|
fragment.append(items[key])
|
|
|
|
}
|
|
|
|
fragment.append(document.createTextNode(literals.at(-1)))
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
export const text = (data="", ...items) =>
|
2022-02-06 11:56:54 +00:00
|
|
|
typeof data == "object" && "at" in data
|
|
|
|
? textFromTemplate(data, items)
|
|
|
|
: document.createTextNode(data)
|