Refactor renderer class

This commit is contained in:
Talia 2024-07-29 11:35:06 +02:00
parent e2ec8312af
commit 6bd29f05dc

View file

@ -42,18 +42,6 @@ const snakeToCSS = key => key.replace(/^[A-Z]/, a => "-" + a).replace(/[A-Z]/g,
*/
const fallback = (value, whenUndefined) => typeof value != "undefined" ? value : whenUndefined
/** Recursively finds the last 'is' attribute in a list nested array of objects
* @param {Array} args
*/
const getCustom = args => args.reduce(
(current, argument) => Array.isArray(argument)
? fallback(getCustom(argument), current)
: (argument && typeof argument == "object")
? fallback(argument.is, current)
: current
, undefined
)
/** @typedef {EventTarget & {value: any}} Observable */
/** Cancelable event triggered when a reactive element gets replaced with something else */
@ -202,10 +190,7 @@ export class DomRenderer extends Renderer {
* @param {Array} args
*/
node(name, args) {
const custom = getCustom(args)
const opts = custom && { is: String(custom) }
const element = this.createElement(name, opts)
const element = this.createElement(name)
this.constructor.apply(element, args)
return element
}
@ -216,7 +201,7 @@ export class DomRenderer extends Renderer {
* @param {Object} options
* @return {Node}
*/
createElement(name, options) {
createElement(name, options={}) {
return document.createElement(name, options)
}
@ -397,6 +382,33 @@ export class DomHtmlRenderer extends DomRenderer {
return document.createElement(name.replace(/([a-z])([A-Z])/g, "$1-$2"), options)
}
/** 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
)
}
/** @type {Object<string,SpecialAttributeDescriptor>} */
static specialAttributes = {
value: {