Mildly refactor skooma.js

This commit is contained in:
Talia 2023-09-18 08:57:14 +02:00
parent e6e158b2f4
commit 0e63167c92

View file

@ -15,7 +15,7 @@ const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g,
export const empty = Symbol("Explicit empty argument for Skooma") export const empty = Symbol("Explicit empty argument for Skooma")
const insertStyles = (rule, styles) => { const insertStyles = (rule, styles) => {
for (let [key, value] of Object.entries(styles)) for (const [key, value] of Object.entries(styles))
if (typeof value == "undefined") if (typeof value == "undefined")
rule.removeProperty(keyToPropName(key)) rule.removeProperty(keyToPropName(key))
else else
@ -46,7 +46,7 @@ const getCustom = args => String(
const parseArgs = (element, before, ...args) => { const parseArgs = (element, before, ...args) => {
if (element.content) element = element.content if (element.content) element = element.content
for (let arg of args) if (arg !== empty) for (const arg of args) if (arg !== empty)
if (typeof arg == "string" || typeof arg == "number") if (typeof arg == "string" || typeof arg == "number")
element.insertBefore(document.createTextNode(arg), before) element.insertBefore(document.createTextNode(arg), before)
else if (arg === undefined || arg == null) else if (arg === undefined || arg == null)
@ -58,11 +58,11 @@ const parseArgs = (element, before, ...args) => {
else if ("length" in arg) else if ("length" in arg)
parseArgs(element, before, ...arg) parseArgs(element, before, ...arg)
else else
for (let key in arg) for (const key in arg)
if (key == "style" && typeof(arg[key])=="object") if (key == "style" && typeof(arg[key])=="object")
insertStyles(element.style, arg[key]) insertStyles(element.style, arg[key])
else if (key == "dataset" && typeof(arg[key])=="object") else if (key == "dataset" && typeof(arg[key])=="object")
for (let [key2, value] of Object.entries(arg[key])) for (const [key2, value] of Object.entries(arg[key]))
element.dataset[key2] = parseAttribute(value) element.dataset[key2] = parseAttribute(value)
else if (key == "shadowRoot") else if (key == "shadowRoot")
parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), null, arg[key]) parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), null, arg[key])
@ -76,10 +76,9 @@ const parseArgs = (element, before, ...args) => {
element.setAttribute(key, parseAttribute(arg[key])) element.setAttribute(key, parseAttribute(arg[key]))
} }
const nop = object => object
const node = (name, args, options) => { const node = (name, args, options) => {
let element let element
let custom = getCustom(args) const custom = getCustom(args)
if ("nameFilter" in options) name = options.nameFilter(name) if ("nameFilter" in options) name = options.nameFilter(name)
if (options.xmlns) if (options.xmlns)
element = document.createElementNS(options.xmlns, name, {is: custom}) element = document.createElementNS(options.xmlns, name, {is: custom})
@ -90,8 +89,8 @@ const node = (name, args, options) => {
} }
const nameSpacedProxy = (options={}) => new Proxy(Window, { const nameSpacedProxy = (options={}) => new Proxy(Window, {
get: (target, prop, receiver) => { return (...args) => node(prop, args, options) }, get: (_target, prop, _receiver) => { return (...args) => node(prop, args, options) },
has: (target, prop) => true, has: (_target, _prop) => true,
}) })
export const bind = transform => { export const bind = transform => {
@ -115,7 +114,7 @@ export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})
export const fragment = (...elements) => { export const fragment = (...elements) => {
const fragment = new DocumentFragment() const fragment = new DocumentFragment()
for (element of elements) for (const element of elements)
fragment.append(element) fragment.append(element)
return fragment return fragment
} }