From 5bae11ecd9d828eb2dd4715d8e6a27dcc42a767f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 19 Dec 2024 12:10:56 -0400 Subject: html: Template.resolveForSlots --- src/util/html.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'src/util') 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; -- cgit 1.3.0-6-gf8a5