From bbb470ee09a2803ffe3c8465c6dad9e50080fe19 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 14:06:41 -0400 Subject: html: refactor attributes addition recursion logic --- src/util/html.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/util/html.js b/src/util/html.js index ecc2b065..8cb411b3 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -665,37 +665,45 @@ export class Attributes { } #addMultipleAttributes(attributes) { - if (attributes === null) return; - if (attributes === undefined) return; - if (attributes === false) return; + const flatInputAttributes = + [attributes].flat(Infinity).filter(Boolean); - if (Array.isArray(attributes)) { - return attributes.map(item => this.#addMultipleAttributes(item)); + const attributeSets = + flatInputAttributes.map(attributes => this.#getAttributeSet(attributes)); + + const resultList = []; + + for (const set of attributeSets) { + const setResults = {}; + + for (const key of Reflect.ownKeys(set)) { + const value = set[key]; + setResults[key] = this.#addOneAttribute(key, value); + } + + resultList.push(setResults); } + return resultList; + } + + #getAttributeSet(attributes) { if (attributes instanceof Attributes) { - return this.#addMultipleAttributes(attributes.attributes); + return attributes.attributes; } if (attributes instanceof Template) { const resolved = Template.resolve(attributes); isAttributesAdditionSingletValue(resolved); - return this.#addMultipleAttributes(resolved); + return 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; + return attributes; } throw new Error( - `Expected an array, object, or template, ` + + `Expected Attributes, Template, or object, ` + `got ${typeAppearance(attribute)}`); } -- cgit 1.3.0-6-gf8a5