Skip to content

Commit

Permalink
[@astrojs/lit] Fix slotted content into main (#5791)
Browse files Browse the repository at this point in the history
* Fix  add the missing slot attribute to child nodes

* Add lit slot tests

* Add changeset
  • Loading branch information
ba55ie authored Jan 7, 2023
1 parent ec5a39c commit f7aa1ec
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changeset/kind-beers-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/lit': patch
---

Fix Lit slotted content
30 changes: 27 additions & 3 deletions packages/astro/test/fixtures/lit-element/src/pages/slots.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@ import {MyElement} from '../components/my-element.js';
<title>LitElement | Slot</title>
</head>
<body>
<MyElement>
<div>default</div>
<div slot="named">named</div>
<MyElement id="root">
<div class="default">my-element default 1</div>
<div class="default">my-element default 2</div>

<h1 slot="named">my-element named 1</h1>
<h2 slot="named">my-element named 2</h2>

<ul slot="named" id="list">
<li>Custom elements</li>
<li>Shadow DOM</li>
<li>HTML templates</li>
</ul>

<my-element id="slotted" slot="named">
<h3 class="default">slotted my-element default</h3>

<div slot="named">slotted my-element named 1</div>
<div slot="named">slotted my-element named 2</div>

<MyElement id="slotted-slotted" slot="named">
<h4 class="default">slotted slotted my-element default 1</h4>
<h5 class="default">slotted slotted my-element default 2</h5>

<div slot="named">slotted slotted my-element named 1</div>
<div slot="named">slotted slotted my-element named 2</div>
</MyElement>
</my-element>
</MyElement>
</body>
</html>
4 changes: 3 additions & 1 deletion packages/astro/test/fixtures/lit-element/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"experimentalDecorators": true
"compilerOptions": {
"experimentalDecorators": true
}
}
32 changes: 26 additions & 6 deletions packages/astro/test/lit-element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,35 @@ describe('LitElement test', function () {
const html = await fixture.readFile('/slots/index.html');
const $ = cheerio.load(html);

expect($('my-element').length).to.equal(1);
const $rootMyElement = $('#root');
const $slottedMyElement = $('#slotted');
const $slottedSlottedMyElement = $('#slotted-slotted');

const [defaultSlot, namedSlot] = $('template').siblings().toArray();
expect($('my-element').length).to.equal(3);

// has default slot content in lightdom
expect($(defaultSlot).text()).to.equal('default');
// Root my-element
expect($rootMyElement.children('.default').length).to.equal(2);
expect($rootMyElement.children('.default').eq(1).text()).to.equal('my-element default 2');

// has named slot content in lightdom
expect($(namedSlot).text()).to.equal('named');
expect($rootMyElement.children('[slot="named"]').length).to.equal(4);
expect($rootMyElement.children('[slot="named"]').eq(1).text()).to.equal('my-element named 2');
expect($rootMyElement.children('[slot="named"]').eq(2).attr('id')).to.equal('list');
expect($rootMyElement.children('[slot="named"]').eq(3).attr('id')).to.equal('slotted');

// Slotted my-element first level
expect($slottedMyElement.children('.default').length).to.equal(1);
expect($slottedMyElement.children('.default').eq(0).text()).to.equal('slotted my-element default');

expect($slottedMyElement.children('[slot="named"]').length).to.equal(3);
expect($slottedMyElement.children('[slot="named"]').eq(1).text()).to.equal('slotted my-element named 2');
expect($slottedMyElement.children('[slot="named"]').eq(2).attr('id')).to.equal('slotted-slotted');

// Slotted my-element second level
expect($slottedSlottedMyElement.children('.default').length).to.equal(2);
expect($slottedSlottedMyElement.children('.default').eq(1).text()).to.equal('slotted slotted my-element default 2');

expect($slottedSlottedMyElement.children('[slot="named"]').length).to.equal(2);
expect($slottedSlottedMyElement.children('[slot="named"]').eq(1).text()).to.equal('slotted slotted my-element named 2');
});

it('Is able to build when behind getStaticPaths', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/ssr-lit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Lit integration in SSR', () => {
}

it('Is able to load', async () => {
delete globalThis.window;
delete globalThis.window; // On Windows this results in `ReferenceError: window is not defined`
const html = await fetchHTML('/');
const $ = cheerioLoad(html);
expect($('#win').text()).to.equal('function');
Expand Down
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 f7aa1ec

Please sign in to comment.