« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js40
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)}`);
   }