2024-01-15 10:50:27 +00:00
|
|
|
// Keep a referee alive until a referrer is collected
|
2023-12-23 16:31:01 +00:00
|
|
|
const weakReferences = new WeakMap()
|
2024-07-27 14:03:24 +00:00
|
|
|
|
|
|
|
/** Keeps the referenced value alive until the referrer is collected
|
|
|
|
* @param {Object} referrer
|
|
|
|
* @param {Object} reference
|
|
|
|
*/
|
2023-12-23 16:31:01 +00:00
|
|
|
const untilDeathDoThemPart = (referrer, reference) => {
|
2024-01-24 13:50:51 +00:00
|
|
|
if (!weakReferences.has(referrer)) weakReferences.set(referrer, new Set())
|
2023-12-23 16:31:01 +00:00
|
|
|
weakReferences.get(referrer).add(reference)
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** Like AbortController, but resets after each abort */
|
2024-01-24 13:50:51 +00:00
|
|
|
class MultiAbortController {
|
|
|
|
#controller = new AbortController()
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @return {AbortSignal} */
|
2024-01-24 13:50:51 +00:00
|
|
|
get signal() { return this.#controller.signal }
|
|
|
|
abort() { this.#controller.abort(); this.#controller = new AbortController() }
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:38:57 +00:00
|
|
|
/** A symbol representing nothing to be appended to an element */
|
2022-03-26 11:40:50 +00:00
|
|
|
export const empty = Symbol("Explicit empty argument for Skooma")
|
|
|
|
|
2024-02-12 12:38:57 +00:00
|
|
|
/** Converts a snake-case string to a CSS property name
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {string} key
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
const snakeToCSS = key => key.replace(/^[A-Z]/, a => "-" + a).replace(/[A-Z]/g, a => '-' + a.toLowerCase())
|
2021-08-16 15:57:10 +00:00
|
|
|
|
2024-02-12 12:38:57 +00:00
|
|
|
/** @typedef SpecialAttributeDescriptor
|
2024-07-29 09:35:06 +00:00
|
|
|
* @type {object}
|
|
|
|
* @property {function(Node):void} [get]
|
|
|
|
* @property {function(Node,any):void} [set]
|
|
|
|
* @property {function(Node,function(any):void):void} [subscribe]
|
|
|
|
* @property {function(Node):boolean} [filter]
|
|
|
|
*/
|
2024-02-12 12:38:57 +00:00
|
|
|
|
|
|
|
/**
|
2024-07-17 14:37:30 +00:00
|
|
|
* Returns a fallback if value is fallback
|
|
|
|
* @param {any} value
|
|
|
|
* @param {any} whenUndefined
|
|
|
|
*/
|
|
|
|
const fallback = (value, whenUndefined) => typeof value != "undefined" ? value : whenUndefined
|
2024-02-12 12:38:57 +00:00
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @typedef {EventTarget & {value: any}} Observable */
|
2024-02-12 12:38:57 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Cancelable event triggered when a reactive element gets replaced with something else */
|
|
|
|
export class BeforeReplaceEvent extends Event {
|
2024-02-29 14:29:20 +00:00
|
|
|
/** @param {Element|Text} next */
|
|
|
|
constructor(next) {
|
2024-09-07 10:26:34 +00:00
|
|
|
super("skooma:beforereplace", { cancelable: true })
|
2024-02-29 14:29:20 +00:00
|
|
|
this.next = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Event triggered when a reactive element was replaced */
|
|
|
|
export class AfterReplaceEvent extends Event {
|
2024-02-29 14:29:20 +00:00
|
|
|
/** @param {Element|Text} next */
|
|
|
|
constructor(next) {
|
2024-09-07 10:26:34 +00:00
|
|
|
super("skooma:afterreplace")
|
2024-02-29 14:29:20 +00:00
|
|
|
this.next = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Event triggered when a new element replaces an old one */
|
|
|
|
export class ReplacedEvent extends Event {
|
|
|
|
/** @param {Element|Text} old */
|
|
|
|
constructor(old) {
|
2024-09-07 10:26:34 +00:00
|
|
|
super("skooma:replaced", { bubbles: true })
|
2024-07-17 14:37:30 +00:00
|
|
|
this.old = old
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 14:47:20 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
// Other utility exports
|
|
|
|
|
|
|
|
/** Wraps an event handler in a function that calls preventDefault on the event
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {function(event) : event} fn
|
|
|
|
* @return {function(event)}
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
export const handle = fn => event => { event.preventDefault(); return fn(event) }
|
|
|
|
|
|
|
|
|
|
|
|
/** A reference to an element that follows it around through replacements */
|
|
|
|
export class Ref {
|
|
|
|
/** @type {WeakMap<Text|Element,Text|Element>} */
|
|
|
|
static #map = new WeakMap()
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @type {Element|Text} */
|
2024-07-17 14:37:30 +00:00
|
|
|
#element
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {Element|Text} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
constructor(element) {
|
|
|
|
this.#element = element
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @return {Element|Text} */
|
2024-07-17 14:37:30 +00:00
|
|
|
deref() {
|
2024-07-27 14:03:24 +00:00
|
|
|
const next = Ref.newer(this.#element)
|
2024-07-17 14:37:30 +00:00
|
|
|
if (next) {
|
|
|
|
this.#element = next
|
|
|
|
return this.deref()
|
|
|
|
} else {
|
|
|
|
return this.#element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {Element|Text} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
static newer(element) {
|
|
|
|
return this.#map.get(element)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-27 14:03:24 +00:00
|
|
|
* @param {Element|Text} previous
|
|
|
|
* @param {Element|Text} next
|
2024-07-17 14:37:30 +00:00
|
|
|
*/
|
|
|
|
static replace(previous, next) {
|
|
|
|
if (this.newer(previous))
|
|
|
|
throw "Element has already been replaced with newer one"
|
|
|
|
this.#map.set(previous, next)
|
|
|
|
}
|
2023-12-21 22:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Main class doing all the rendering */
|
|
|
|
export class Renderer {
|
|
|
|
static proxy() {
|
|
|
|
return new Proxy(new this(), {
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {string} prop */
|
|
|
|
get: (renderer, prop) => /** @param {any[]} args */ (...args) => renderer.node(prop, args),
|
2024-07-17 14:37:30 +00:00
|
|
|
has: (renderer, prop) => renderer.nodeSupported(prop),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {string} name */
|
2024-07-17 14:37:30 +00:00
|
|
|
node(name, ...args) {
|
|
|
|
throw "Attempting to use an abstract Renderer"
|
|
|
|
}
|
|
|
|
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {string|symbol} name */
|
2024-07-17 14:37:30 +00:00
|
|
|
nodeSupported(name) {
|
2024-07-27 14:03:24 +00:00
|
|
|
if (typeof(name) != "string") return false
|
2024-07-17 14:37:30 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Turns an attribute value into a string */
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {any} value */
|
2024-07-17 14:37:30 +00:00
|
|
|
static serialiseAttributeValue(value) {
|
|
|
|
if (typeof value == "string" || typeof value == "number")
|
|
|
|
return value
|
|
|
|
else if (value && "join" in value)
|
|
|
|
return value.join(" ")
|
|
|
|
else if (Object.getPrototypeOf(value) == Object.prototype)
|
|
|
|
return JSON.stringify(value)
|
|
|
|
else
|
|
|
|
return value.toString()
|
2023-12-21 22:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
export class DomRenderer extends Renderer {
|
|
|
|
/** @type {Object<string,SpecialAttributeDescriptor>} */
|
|
|
|
static specialAttributes = Object.freeze({})
|
|
|
|
|
|
|
|
/** Processes a list of arguments for an HTML Node
|
2024-07-27 14:03:24 +00:00
|
|
|
* @param {Element|ShadowRoot} element
|
2024-07-17 14:37:30 +00:00
|
|
|
* @param {Array} args
|
|
|
|
*/
|
|
|
|
static apply(element, ...args) {
|
|
|
|
for (const arg of args) if (arg !== empty) {
|
|
|
|
if (Array.isArray(arg)) {
|
|
|
|
this.apply(element, ...arg)
|
|
|
|
} else {
|
|
|
|
const child = this.toElement(arg)
|
|
|
|
if (child)
|
|
|
|
element.append(child)
|
|
|
|
else if (typeof arg == "function")
|
|
|
|
this.apply(element, arg(element) || empty)
|
2024-07-31 07:30:17 +00:00
|
|
|
else if (arg instanceof DocumentFragment)
|
|
|
|
element.append(arg)
|
2024-07-17 14:37:30 +00:00
|
|
|
else if (arg && typeof(arg)=="object")
|
|
|
|
for (const key in arg)
|
2024-07-27 14:03:24 +00:00
|
|
|
if (element instanceof Element)
|
|
|
|
this.setAttribute(element, key, arg[key])
|
|
|
|
else
|
|
|
|
throw `Attempting to set attributes on a non-element (${element.constructor.name})`
|
2024-07-17 14:37:30 +00:00
|
|
|
else
|
|
|
|
console.warn(`An argument of type ${typeof arg} has been ignored`, element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new node
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Array} args
|
|
|
|
*/
|
|
|
|
node(name, args) {
|
2024-07-29 09:35:06 +00:00
|
|
|
const element = this.createElement(name)
|
2024-07-17 14:37:30 +00:00
|
|
|
this.constructor.apply(element, args)
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Node}
|
|
|
|
*/
|
2024-07-29 09:35:06 +00:00
|
|
|
createElement(name, options={}) {
|
2024-07-17 14:37:30 +00:00
|
|
|
return document.createElement(name, options)
|
|
|
|
}
|
2024-02-06 16:07:31 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Turns an argument into something that can be inserted as a child into a DOM node
|
|
|
|
* @protected
|
|
|
|
* @param {any} value
|
|
|
|
* @return {Element|Text}
|
|
|
|
*/
|
|
|
|
static toElement(value) {
|
|
|
|
if (typeof value == "string" || typeof value == "number")
|
|
|
|
return document.createTextNode(value.toString())
|
|
|
|
else if (value instanceof Element)
|
|
|
|
return value
|
|
|
|
else if (this.isObservable(value))
|
|
|
|
return this.toReactiveElement(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @param {Observable} observable
|
|
|
|
* @return {Element|Text}
|
|
|
|
*/
|
|
|
|
static toReactiveElement(observable) {
|
2024-07-31 07:30:17 +00:00
|
|
|
if (observable.value instanceof DocumentFragment) {
|
|
|
|
throw "Failed to create reactive element: Document fragments cannot be replaced dynamically"
|
|
|
|
}
|
2024-07-17 14:37:30 +00:00
|
|
|
const element = this.toElement(observable.value)
|
2024-02-06 16:07:31 +00:00
|
|
|
untilDeathDoThemPart(element, observable)
|
2024-07-17 14:37:30 +00:00
|
|
|
let ref = new WeakRef(element)
|
|
|
|
|
|
|
|
const handleChange = () => {
|
|
|
|
const element = ref.deref()
|
|
|
|
|
|
|
|
if (!element) return
|
|
|
|
|
|
|
|
const next = this.toElement(observable.value)
|
|
|
|
if (element?.dispatchEvent(new BeforeReplaceEvent(next))) {
|
|
|
|
element.replaceWith(next)
|
|
|
|
Ref.replace(element, next)
|
|
|
|
next.dispatchEvent(new ReplacedEvent(element))
|
|
|
|
element.dispatchEvent(new AfterReplaceEvent(next))
|
|
|
|
ref = new WeakRef(next)
|
|
|
|
}
|
|
|
|
observable.addEventListener("change", handleChange, {once: true})
|
|
|
|
}
|
|
|
|
observable.addEventListener("change", handleChange, {once: true})
|
|
|
|
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set an attribute on an element
|
|
|
|
* @protected
|
|
|
|
* @param {Element} element
|
|
|
|
* @param {string} attribute
|
|
|
|
* @param {any} value
|
|
|
|
* @param {AbortSignal} [cleanupSignal]
|
|
|
|
*/
|
|
|
|
static setAttribute(element, attribute, value, cleanupSignal) {
|
|
|
|
const special = this.getSpecialAttribute(element, attribute)
|
|
|
|
|
|
|
|
if (this.isObservable(value))
|
|
|
|
this.setReactiveAttribute(element, attribute, value)
|
|
|
|
else if (typeof value === "function")
|
|
|
|
element.addEventListener(attribute, value, { signal: cleanupSignal })
|
|
|
|
else if (special?.set)
|
|
|
|
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, this.serialiseAttributeValue(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set up a binding between an attribute and an observable
|
|
|
|
* @protected
|
|
|
|
* @param {Element} element
|
|
|
|
* @param {string} attribute
|
|
|
|
* @param {Observable} observable
|
|
|
|
*/
|
|
|
|
static setReactiveAttribute(element, attribute, observable) {
|
|
|
|
const multiAbort = new MultiAbortController()
|
|
|
|
|
|
|
|
observable.addEventListener("change", () => {
|
|
|
|
multiAbort.abort()
|
|
|
|
this.setAttribute(element, attribute, observable.value, multiAbort.signal)
|
2023-12-21 22:36:48 +00:00
|
|
|
})
|
2024-07-17 14:37:30 +00:00
|
|
|
this.setAttribute(element, attribute, observable.value, multiAbort.signal)
|
|
|
|
|
|
|
|
const special = this.getSpecialAttribute(element, attribute)
|
|
|
|
|
|
|
|
if (special?.subscribe) {
|
|
|
|
untilDeathDoThemPart(element, observable)
|
|
|
|
special.subscribe(element, value => {
|
|
|
|
if (value != observable.value) observable.value = value
|
|
|
|
})
|
|
|
|
}
|
2023-12-21 22:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/**
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {CSSStyleDeclaration} style The style property of a node
|
|
|
|
* @param {object} rules A map of snake case property names to css values
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
static insertStyles(style, rules) {
|
|
|
|
for (const [key, value] of Object.entries(rules))
|
|
|
|
if (typeof value == "undefined")
|
|
|
|
style.removeProperty(snakeToCSS(key))
|
2024-01-24 08:51:49 +00:00
|
|
|
else
|
2024-07-17 14:37:30 +00:00
|
|
|
style.setProperty(snakeToCSS(key), value.toString())
|
2023-12-21 22:36:48 +00:00
|
|
|
}
|
2021-02-17 10:03:21 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Returns whether an object is an observable according to skooma's contract
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {any} object
|
|
|
|
* @return {object is Observable}
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
static isObservable(object) {
|
|
|
|
return object && object.observable
|
|
|
|
}
|
2021-02-17 10:03:21 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Wraps a list of elements in a document fragment
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {Array<Element|String>} elements
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
static documentFragment(...elements) {
|
|
|
|
const fragment = new DocumentFragment()
|
|
|
|
for (const element of elements)
|
|
|
|
fragment.append(this.toElement(element))
|
|
|
|
return fragment
|
|
|
|
}
|
2021-02-23 18:11:25 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @param {Element} element
|
|
|
|
* @param {String} attribute
|
|
|
|
*/
|
|
|
|
static getSpecialAttribute(element, attribute) {
|
|
|
|
const special = this.specialAttributes[attribute]
|
|
|
|
if (special?.filter == undefined)
|
|
|
|
return special
|
2024-07-27 14:03:24 +00:00
|
|
|
if (special.filter(element))
|
2024-07-17 14:37:30 +00:00
|
|
|
return special
|
|
|
|
return undefined
|
|
|
|
}
|
2023-09-18 09:45:36 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/**
|
2024-07-29 09:35:06 +00:00
|
|
|
* @param {String|Array<String>} data
|
|
|
|
* @param {Array<String|Element>} items
|
|
|
|
*/
|
2024-07-17 14:37:30 +00:00
|
|
|
static createTextOrFragment(data = "", ...items) {
|
|
|
|
return Array.isArray(data)
|
|
|
|
? this.textFromTemplate(data, items)
|
|
|
|
: document.createTextNode(data)
|
|
|
|
}
|
2023-09-18 09:45:36 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Turns a template literal into document fragment.
|
|
|
|
* Strings are returned as text nodes.
|
|
|
|
* Elements are inserted in between.
|
|
|
|
* @param {Array<String>} literals
|
|
|
|
* @param {Array<any>} items
|
|
|
|
* @return {DocumentFragment}
|
|
|
|
*/
|
|
|
|
static textFromTemplate(literals, items) {
|
|
|
|
const fragment = new DocumentFragment()
|
|
|
|
for (const key in items) {
|
|
|
|
fragment.append(document.createTextNode(literals[key]))
|
2024-07-27 14:03:24 +00:00
|
|
|
fragment.append(this.toElement(items[key]))
|
2024-07-17 14:37:30 +00:00
|
|
|
}
|
|
|
|
fragment.append(document.createTextNode(literals[literals.length - 1]))
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 15:27:25 +00:00
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Renderer for normal HTML nodes targetting a browser's DOM */
|
|
|
|
export class DomHtmlRenderer extends DomRenderer {
|
|
|
|
/**
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Node}
|
|
|
|
*/
|
|
|
|
createElement(name, options) {
|
|
|
|
return document.createElement(name.replace(/([a-z])([A-Z])/g, "$1-$2"), options)
|
|
|
|
}
|
|
|
|
|
2024-07-29 09:35:06 +00:00
|
|
|
/** Creates a new node and make it a custom element if necessary
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Array} args
|
|
|
|
*/
|
|
|
|
node(name, args) {
|
|
|
|
const custom = this.getCustom(args)
|
|
|
|
const opts = custom && { is: String(custom) }
|
|
|
|
|
|
|
|
const element = this.createElement(name, opts)
|
|
|
|
this.constructor.apply(element, args)
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Recursively finds the last 'is' attribute in a list nested array of objects
|
|
|
|
* @param {Array} args
|
|
|
|
*/
|
|
|
|
getCustom(args) {
|
|
|
|
return args.reduce(
|
|
|
|
(current, argument) => Array.isArray(argument)
|
|
|
|
? fallback(this.getCustom(argument), current)
|
|
|
|
: (argument && typeof argument == "object")
|
|
|
|
? fallback(argument.is, current)
|
|
|
|
: current
|
|
|
|
, undefined
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** @type {Object<string,SpecialAttributeDescriptor>} */
|
|
|
|
static specialAttributes = {
|
|
|
|
value: {
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLInputElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
get(element) { return element.value },
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLInputElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
set(element, value) {
|
|
|
|
element.setAttribute("value", value)
|
|
|
|
element.value = value
|
|
|
|
},
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLInputElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
subscribe(element, callback) {
|
|
|
|
element.addEventListener("input", () => {
|
|
|
|
callback(this.get(element))
|
|
|
|
})
|
|
|
|
},
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLElement} element */
|
|
|
|
filter(element) {
|
|
|
|
return element.nodeName.toLowerCase() == "input"
|
|
|
|
}
|
2024-07-17 14:37:30 +00:00
|
|
|
},
|
|
|
|
style: {
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
set(element, value) { DomRenderer.insertStyles(element.style, value) }
|
|
|
|
},
|
|
|
|
dataset: {
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
set(element, value) {
|
|
|
|
for (const [attribute2, value2] of Object.entries(value)) {
|
|
|
|
element.dataset[attribute2] = DomRenderer.serialiseAttributeValue(value2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
shadowRoot: {
|
2024-07-27 14:03:24 +00:00
|
|
|
/** @param {HTMLElement} element */
|
2024-07-17 14:37:30 +00:00
|
|
|
set(element, value) {
|
2024-07-27 14:03:24 +00:00
|
|
|
DomRenderer.apply(
|
|
|
|
(element.shadowRoot || element.attachShadow({ mode: "open" })),
|
|
|
|
value
|
|
|
|
)
|
2024-07-17 14:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 13:51:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
/** Renderer for normal SVG nodes targetting a browser's DOM */
|
|
|
|
export class DomSvgRenderer extends DomRenderer {
|
|
|
|
/**
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Node}
|
|
|
|
*/
|
|
|
|
createElement(name, options) {
|
|
|
|
return document.createElementNS("http://www.w3.org/2000/svg", name, options)
|
2021-12-26 16:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:37:30 +00:00
|
|
|
export const html = DomHtmlRenderer.proxy()
|
|
|
|
export const svg = DomSvgRenderer.proxy()
|
|
|
|
|
|
|
|
export const fragment = DomRenderer.documentFragment.bind(DomRenderer)
|
|
|
|
export const text = DomRenderer.createTextOrFragment.bind(DomRenderer)
|
|
|
|
|
|
|
|
export default html
|