« get me outta code hell

html: expand resolve to take "normalize" option - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-08-20 22:04:40 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-08-20 22:07:42 -0300
commit040e0d3a1d3890f7c45e2839ca949d353a160efc (patch)
tree3c4f00bfd9a64bf13a1d9e6ad342c4a8638b1146
parentef8acc5d50fa3c23bd7c9d4bb720b7ff78581981 (diff)
html: expand resolve to take "normalize" option
-rw-r--r--src/util/html.js27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 7ed363c..a311bbb 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) {