diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-12-19 12:10:56 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-12-19 13:25:16 -0400 |
commit | 5bae11ecd9d828eb2dd4715d8e6a27dcc42a767f (patch) | |
tree | 889a79348694c676b168535c0711adb2c3f354a9 | |
parent | 2a5b1da848a4d4337abd45bae04d7be616d46dd1 (diff) |
html: Template.resolveForSlots
-rw-r--r-- | src/util/html.js | 37 |
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; |