From 22e074bad12a25993c884b53f0d460a408efa814 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 08:26:06 -0400 Subject: html: clean up #addHelper-related logic --- src/util/html.js | 58 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/util/html.js b/src/util/html.js index 30960ca6..7ba49ae2 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -708,27 +708,44 @@ export class Attributes { #addHelper(...args) { if (args.length === 1) { - const arg = args[0]; - if (arg === null || arg === undefined || arg === false) { - return; - } else if (Array.isArray(arg)) { - return arg.map(item => this.#addHelper(item)); - } else if (arg instanceof Template) { - return this.#addHelper(Template.resolve(arg)); - } else if (typeof arg === 'object') { - const results = {}; - for (const key of Reflect.ownKeys(arg)) { - results[key] = this.#addHelper(key, arg[key]); - } - return results; - } else { - throw new Error(`Expected an array, object, or template, got ${typeAppearance(args[0])}`); - } + return this.#addMultipleAttributes(args[0]); } else if (args.length === 2) { return this.#addOneAttribute(args[0], args[1]); } else { - throw new Error(`Expected array or object, or attribute and value`); + throw new Error( + `Expected array or object, or attribute and value`); + } + } + + #addMultipleAttributes(attributes) { + if (attributes === null) return; + if (attributes === undefined) return; + if (attributes === false) return; + + if (Array.isArray(attributes)) { + return attributes.map(item => this.#addMultipleAttributes(item)); + } + + if (attributes instanceof Template) { + const resolved = Template.resolve(attributes); + isAttributesAdditionSingletValue(resolved); + return this.#addMultipleAttributes(resolved); } + + if (typeof attributes === 'object') { + const results = {}; + + for (const key of Reflect.ownKeys(attributes)) { + const value = attributes[key]; + results[key] = this.#addOneAttribute(key, value); + } + + return results; + } + + throw new Error( + `Expected an array, object, or template, ` + + `got ${typeAppearance(attribute)}`); } #addOneAttribute(attribute, value) { @@ -741,8 +758,7 @@ export class Attributes { } if (!this.has(attribute)) { - this.set(attribute, value); - return value; + return this.set(attribute, value); } const descriptor = attributeSpec[attribute]; @@ -772,9 +788,7 @@ export class Attributes { } } - this.set(attribute, newValue); - - return newValue; + return this.set(attribute, newValue); } get(attribute) { -- cgit 1.3.0-6-gf8a5