From 21a270ca6efa561cad3e87048cf8deb8a166d55f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 8 Sep 2023 08:51:20 -0300 Subject: fix miscellaneous eslint errors --- src/util/html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/util/html.js') diff --git a/src/util/html.js b/src/util/html.js index a311bbba..b1668558 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -242,7 +242,7 @@ export class Tag { this.selfClosing && !(value === null || value === undefined || - !Boolean(value) || + !value || Array.isArray(value) && value.filter(Boolean).length === 0) ) { throw new Error(`Tag <${this.tagName}> is self-closing but got content`); -- cgit 1.3.0-6-gf8a5 From d878ab29f20c0727acafb4b1150d4e31d69c55c0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 11 Sep 2023 10:09:48 -0300 Subject: data, html, infra: supporting changes for sanitizing content --- src/util/html.js | 57 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 16 deletions(-) (limited to 'src/util/html.js') diff --git a/src/util/html.js b/src/util/html.js index a311bbba..f0c7bfdf 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -806,24 +806,43 @@ export class Template { } // Null is always an acceptable slot value. - if (value !== null) { - if ('validate' in description) { - description.validate({ - ...commonValidators, - ...validators, - })(value); - } + if (value === null) { + return true; + } + + if ('validate' in description) { + description.validate({ + ...commonValidators, + ...validators, + })(value); + } - if ('type' in description) { - const {type} = description; - if (type === 'html') { - if (!isHTML(value)) { + if ('type' in description) { + switch (description.type) { + case 'html': { + if (!isHTML(value)) throw new TypeError(`Slot expects html (tag, template or blank), got ${typeof value}`); - } - } else { - if (typeof value !== type) { - throw new TypeError(`Slot expects ${type}, got ${typeof value}`); - } + + return true; + } + + case 'string': { + // Tags and templates are valid in string arguments - they'll be + // stringified when exposed to the description's .content() function. + if (isTag(value) || isTemplate(value)) + return true; + + if (typeof value !== 'string') + throw new TypeError(`Slot expects string, got ${typeof value}`); + + return true; + } + + default: { + if (typeof value !== description.type) + throw new TypeError(`Slot expects ${description.type}, got ${typeof value}`); + + return true; } } } @@ -847,6 +866,12 @@ export class Template { return providedValue; } + if (description.type === 'string') { + if (isTag(providedValue) || isTemplate(providedValue)) { + return providedValue.toString(); + } + } + if (providedValue !== null) { return providedValue; } -- cgit 1.3.0-6-gf8a5 From 411c053dc4b314b2bc0d58d3899fd796ad0054c2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 28 Sep 2023 14:11:02 -0300 Subject: data, util: use typeAppearance in more places --- src/util/html.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/util/html.js') diff --git a/src/util/html.js b/src/util/html.js index c7395fbf..282a52da 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -2,7 +2,7 @@ import {inspect} from 'node:util'; -import {empty} from '#sugar'; +import {empty, typeAppearance} from '#sugar'; import * as commonValidators from '#validators'; // COMPREHENSIVE! @@ -633,7 +633,7 @@ export class Template { static validateDescription(description) { if (typeof description !== 'object') { - throw new TypeError(`Expected object, got ${typeof description}`); + throw new TypeError(`Expected object, got ${typeAppearance(description)}`); } if (description === null) { -- cgit 1.3.0-6-gf8a5