From 07a1bfe53ba583e25512fce1e678e9ef41908119 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 00:00:40 -0400 Subject: html: support templates in attribute operations --- src/util/html.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src') 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; -- cgit 1.3.0-6-gf8a5