Skip to content

Commit c921c22

Browse files
authored
Merge pull request #43 from thanhbican/feature/fix-slot-processing-with-nested-elements
fix: slot processing with nested elements when disableShadowDOM: true
2 parents 5f13064 + ee7aa02 commit c921c22

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

package/src/api-custom-element.ts

+48-5
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,58 @@ export class VueElement extends BaseClass {
321321
// replace slot
322322
if (!this._config.shadowRoot) {
323323
this._slots = {};
324-
for (const child of Array.from(this.children)) {
325-
const slotName = child.getAttribute('slot') || 'default';
324+
325+
const processChildNodes = (nodes: NodeList): (VNode | string)[] => {
326+
return Array.from(nodes)
327+
.map((node) : VNode | string | null => {
328+
if (node.nodeType === Node.ELEMENT_NODE) {
329+
const element = node as HTMLElement;
330+
const attributes = Object.fromEntries(
331+
Array.from(element.attributes).map((attr) => [attr.name, attr.value])
332+
);
333+
return h(
334+
element.tagName.toLowerCase(),
335+
attributes,
336+
processChildNodes(element.childNodes)
337+
);
338+
} else if (node.nodeType === Node.TEXT_NODE) {
339+
return node.textContent?.trim() || null;
340+
}
341+
return null;
342+
})
343+
.filter((node): node is VNode | string => node != null);
344+
};
345+
346+
for (const child of Array.from(this.childNodes)) {
347+
const slotName = child.nodeType === Node.ELEMENT_NODE
348+
? (child as HTMLElement).getAttribute('slot') || 'default'
349+
: 'default';
350+
326351
if (!this._slots[slotName]) {
327352
this._slots[slotName] = [];
328353
}
329-
this._slots[slotName].push(
330-
h(child.tagName.toLowerCase(), {}, child.innerHTML)
331-
);
354+
355+
if (child.nodeType === Node.ELEMENT_NODE) {
356+
const element = child as HTMLElement;
357+
const attributes = Object.fromEntries(
358+
Array.from(element.attributes).map((attr) => [attr.name, attr.value])
359+
);
360+
this._slots[slotName].push(
361+
h(
362+
element.tagName.toLowerCase(),
363+
attributes,
364+
processChildNodes(element.childNodes)
365+
)
366+
);
367+
} else if (child.nodeType === Node.TEXT_NODE) {
368+
const textContent = child.textContent?.trim();
369+
if (textContent) {
370+
// @ts-ignore
371+
this._slots[slotName].push(textContent);
372+
}
373+
}
332374
}
375+
333376
this.replaceChildren();
334377
}
335378

0 commit comments

Comments
 (0)