« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/util/html.js37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 913206d8..0fe424df 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -1395,8 +1395,13 @@ export class Attributes {
   }
 }
 
-export function resolve(tagOrTemplate, {normalize = null} = {}) {
-  if (normalize === 'tag') {
+export function resolve(tagOrTemplate, {
+  normalize = null,
+  slots = null,
+} = {}) {
+  if (slots) {
+    return Template.resolveForSlots(tagOrTemplate, slots);
+  } else if (normalize === 'tag') {
     return Tag.normalize(tagOrTemplate);
   } else if (normalize === 'string') {
     return Tag.normalize(tagOrTemplate).toString();
@@ -1861,6 +1866,34 @@ export class Template {
     return content;
   }
 
+  static resolveForSlots(tagOrTemplate, slots) {
+    if (!slots || typeof slots !== 'object') {
+      throw new Error(
+        `Expected slots to be an object or array, ` +
+        `got ${typeAppearance(slots)}`);
+    }
+
+    if (!Array.isArray(slots)) {
+      return Template.resolveForSlots(tagOrTemplate, Object.keys(slots)).slots(slots);
+    }
+
+    while (tagOrTemplate && tagOrTemplate instanceof Template) {
+      try {
+        for (const slot of slots) {
+          tagOrTemplate.getSlotDescription(slot);
+        }
+
+        return tagOrTemplate;
+      } catch {
+        tagOrTemplate = tagOrTemplate.content;
+      }
+    }
+
+    throw new Error(
+      `Didn't find slots ${inspect(slots, {compact: true})} ` +
+      `resolving ${inspect(tagOrTemplate, {compact: true})}`);
+  }
+
   [inspect.custom]() {
     const {annotation} = this.description;