From 973a229843ef16f5056c69fc84d89ef5e0a6b49c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 09:49:34 -0400 Subject: html: tweak attribute validators, actually enable singlet validator --- src/util/html.js | 108 +++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 54 deletions(-) (limited to 'src/util/html.js') diff --git a/src/util/html.js b/src/util/html.js index 7ba49ae2..dd6c3b05 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -13,6 +13,7 @@ const { isNumber, isString, isSymbol, + looseArrayOf, oneOf, validateAllPropertyValues, validateArrayItems, @@ -149,60 +150,6 @@ export const validators = { }, }; -const isAttributeKey = - oneOf(isString, isSymbol); - -const isAttributeValue = - oneOf(isString, isNumber, isBoolean, isArray); - -const isAttributesAdditionPair = pair => { - isArray(pair); - - if (pair.length !== 2) { - throw new TypeError(`Expected attributes pair to have two items`); - } - - withAggregate(({push}) => { - try { - isAttributeKey(pair[0]); - } catch (caughtError) { - push(new Error(`Error validating key`, {cause: caughtError})); - } - - try { - isAttributeValue(pair[1]); - } catch (caughtError) { - push(new Error(`Error validating value`, {cause: caughtError})); - } - }); - - return true; -}; - -const isAttributesAdditionSingletValue = value => - oneOf( - validators.isTemplate, - validateAllPropertyValues(isAttributeValue), - validateArrayItems( - oneOf( - is(null, undefined, false), - isAttributesAdditionSingletValue))); - -const isAttributesAdditionSinglet = singlet => { - isArray(singlet); - - if (singlet.length !== 1) { - throw new TypeError(`Expected attributes singlet to have one item`); - } - - isAttributesAdditionSingletValue(singlet[0]); - - return true; -} - -const isAttributesAddition = - oneOf(isAttributesAdditionSinglet, isAttributesAdditionPair); - export function blank() { return []; } @@ -1396,3 +1343,56 @@ export const isHTML = }, isArrayOfHTML); + +export const isAttributeKey = + oneOf(isString, isSymbol); + +export const isAttributeValue = + oneOf( + isString, isNumber, isBoolean, isArray, + isTag, isTemplate); + +export const isAttributesAdditionPair = pair => { + isArray(pair); + + if (pair.length !== 2) { + throw new TypeError(`Expected attributes pair to have two items`); + } + + withAggregate(({push}) => { + try { + isAttributeKey(pair[0]); + } catch (caughtError) { + push(new Error(`Error validating key`, {cause: caughtError})); + } + + try { + isAttributeValue(pair[1]); + } catch (caughtError) { + push(new Error(`Error validating value`, {cause: caughtError})); + } + }); + + return true; +}; + +export const isAttributesAdditionSingletValue = + oneOf( + validateInstanceOf(Template), + validateAllPropertyValues(isAttributeValue), + looseArrayOf(value => isAttributesAdditionSingletValue(value))); + +export const isAttributesAdditionSinglet = singlet => { + isArray(singlet); + + if (singlet.length !== 1) { + throw new TypeError(`Expected attributes singlet to have one item`); + } + + isAttributesAdditionSingletValue(singlet[0]); + + return true; +} + +export const isAttributesAddition = + oneOf(isAttributesAdditionSinglet, isAttributesAdditionPair); -- cgit 1.3.0-6-gf8a5