« get me outta code hell

html: support templates in attribute operations - 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:
author(quasar) nebula <qznebula@protonmail.com>2023-12-30 00:00:40 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 16:26:35 -0400
commit07a1bfe53ba583e25512fce1e678e9ef41908119 (patch)
tree3bf90f21a5bcf6d9ed3fe26a4e6906871f2b210e /src/util
parentb100ef7b5807e24048ad15a48d18ea779f37779d (diff)
html: support templates in attribute operations
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/util/html.js b/src/util/html.js
index ab8e58fd..30960ca6 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -181,6 +181,7 @@ const isAttributesAdditionPair = pair => {
 
 const isAttributesAdditionSingletValue = value =>
   oneOf(
+    validators.isTemplate,
     validateAllPropertyValues(isAttributeValue),
     validateArrayItems(
       oneOf(
@@ -687,6 +688,10 @@ export class Attributes {
   }
 
   set(attribute, value) {
+    if (value instanceof Template) {
+      return this.set(attribute, Template.resolve(value));
+    }
+
     if (value === null || value === undefined) {
       this.remove(attribute);
     } else {
@@ -708,6 +713,8 @@ export class Attributes {
         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)) {
@@ -715,7 +722,7 @@ export class Attributes {
         }
         return results;
       } else {
-        throw new Error(`Expected an array or object, got ${typeAppearance(args[0])}`);
+        throw new Error(`Expected an array, object, or template, got ${typeAppearance(args[0])}`);
       }
     } else if (args.length === 2) {
       return this.#addOneAttribute(args[0], args[1]);
@@ -729,6 +736,10 @@ export class Attributes {
       return;
     }
 
+    if (value instanceof Template) {
+      return this.#addOneAttribute(attribute, Template.resolve(value));
+    }
+
     if (!this.has(attribute)) {
       this.set(attribute, value);
       return value;