From 040e0d3a1d3890f7c45e2839ca949d353a160efc Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 20 Aug 2023 22:04:40 -0300 Subject: html: expand resolve to take "normalize" option --- src/util/html.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/util/html.js b/src/util/html.js index 7ed363c0..a311bbba 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -403,6 +403,21 @@ export class Tag { .join(joiner); } + static normalize(content) { + // Normalizes contents that are valid from an `isHTML` perspective so + // that it's always a pure, single Tag object. + + if (content instanceof Template) { + return Tag.normalize(Template.resolve(content)); + } + + if (content instanceof Tag) { + return content; + } + + return new Tag(null, null, content); + } + [inspect.custom]() { if (this.tagName) { if (empty(this.content)) { @@ -578,8 +593,16 @@ export class Attributes { } } -export function resolve(tagOrTemplate) { - return Template.resolve(tagOrTemplate); +export function resolve(tagOrTemplate, {normalize = null} = {}) { + if (normalize === 'tag') { + return Tag.normalize(tagOrTemplate); + } else if (normalize === 'string') { + return Tag.normalize(tagOrTemplate).toString(); + } else if (normalize) { + throw new TypeError(`Expected normalize to be 'tag', 'string', or null`); + } else { + return Template.resolve(tagOrTemplate); + } } export function template(description) { -- cgit 1.3.0-6-gf8a5