Skip to content

Commit

Permalink
Fix add the missing slot attribute to child nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ba55ie committed Jan 7, 2023
1 parent ec5a39c commit b8b14c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/integrations/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"test": "mocha"
},
"dependencies": {
"@lit-labs/ssr": "^2.2.0"
"@lit-labs/ssr": "^2.2.0",
"parse5": "^7.1.2"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
21 changes: 16 additions & 5 deletions packages/integrations/lit/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './server-shim.js';
import '@lit-labs/ssr/lib/render-lit-html.js';
import { LitElementRenderer } from '@lit-labs/ssr/lib/lit-element-renderer.js';
import * as parse5 from 'parse5';

function isCustomElementTag(name) {
return typeof name === 'string' && /-/.test(name);
Expand Down Expand Up @@ -58,12 +59,22 @@ function* render(Component, attrs, slots) {
yield '</template>';
}
if (slots) {
for (const [slot, value] of Object.entries(slots)) {
if (slot === 'default') {
yield `<astro-slot>${value || ''}</astro-slot>`;
} else {
yield `<astro-slot slot="${slot}">${value || ''}</astro-slot>`;
for (let [slot, value = ''] of Object.entries(slots)) {
if (slot !== 'default' && value) {
// Parse the value as a concatenated string
const fragment = parse5.parseFragment(`${value}`);

// Add the missing slot attribute to child Element nodes
for (const node of fragment.childNodes) {
if (node.tagName && !node.attrs.some(({ name }) => name === 'slot')) {
node.attrs.push({ name: 'slot', value: slot});
}
}

value = parse5.serialize(fragment);
}

yield value;
}
}
yield `</${tagName}>`;
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8b14c7

Please sign in to comment.