From 7d629c0cb7f5b02289c92d8a6017e5bddfa69df8 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 15:32:42 -0400 Subject: html: make isHTML a proper (albeit hard-coded) validator --- src/util/html.js | 58 ++++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 31 deletions(-) (limited to 'src/util/html.js') diff --git a/src/util/html.js b/src/util/html.js index 32350236..edc210b0 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -7,6 +7,7 @@ import {empty, typeAppearance} from '#sugar'; import * as commonValidators from '#validators'; const { + validateArrayItems, validateInstanceOf, } = commonValidators; @@ -76,28 +77,6 @@ export function isBlank(value) { return value.length === 0; } -export function isHTML(value) { - if (typeof value === 'string') { - return true; - } - - if (value === null || value === undefined || value === false) { - return true; - } - - if (isBlank(value) || value instanceof Tag || value instanceof Template) { - return true; - } - - if (Array.isArray(value)) { - if (value.every(isHTML)) { - return true; - } - } - - return false; -} - export function isAttributes(value) { if (typeof value !== 'object' || Array.isArray(value)) { return false; @@ -136,11 +115,7 @@ export const validators = { }, isHTML(value) { - if (!isHTML(value)) { - throw new TypeError(`Expected HTML content`); - } - - return true; + return isHTML(value); }, isAttributes(value) { @@ -1056,10 +1031,7 @@ export class Template { 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}`); - - return true; + return isHTML(value); } case 'string': { @@ -1240,3 +1212,27 @@ export const isTag = export const isTemplate = validateInstanceOf(Template); + +export function isHTML(value) { + if (typeof value === 'string') { + return true; + } + + if (value === null || value === undefined || value === false) { + return true; + } + + if (isBlank(value) || value instanceof Tag || value instanceof Template) { + return true; + } + + if (isArrayOfHTML(value)) { + return true; + } + + throw new TypeError( + `Expected html (tag, template, or blank), got ${typeAppearance(value)}`); +} + +export const isArrayOfHTML = + validateArrayItems(isHTML); -- cgit 1.3.0-6-gf8a5