diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 14:06:41 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 16:26:36 -0400 |
commit | bbb470ee09a2803ffe3c8465c6dad9e50080fe19 (patch) | |
tree | 17e936091c04329d0c205b52ce2991c5baeccd87 /src | |
parent | 51b04c86147f1405c33829cc339b8c045dcafc4f (diff) |
html: refactor attributes addition recursion logic
Diffstat (limited to 'src')
-rw-r--r-- | src/util/html.js | 40 |
1 files changed, 24 insertions, 16 deletions
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)}`); } |