Skip to content

Commit

Permalink
fix: add children with id to id map even if their parent was teleport…
Browse files Browse the repository at this point in the history
…ed (#8321)
  • Loading branch information
vursen authored Dec 11, 2024
1 parent edb03f3 commit 3d4f3c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/component-base/src/polylit-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ const PolylitMixinImplementation = (superclass) => {
this.$ = {};
}

this.renderRoot.querySelectorAll('[id]').forEach((node) => {
this.$[node.id] = node;
[...Object.values(this.$), this.renderRoot].forEach((node) => {
node.querySelectorAll('[id]').forEach((node) => {
this.$[node.id] = node;
});
});
}

Expand Down
16 changes: 12 additions & 4 deletions packages/component-base/test/polylit-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ describe('PolylitMixin', () => {
}

render() {
return html`Teleported`;
return html`<slot></slot>`;
}
},
);
Expand All @@ -1206,7 +1206,10 @@ describe('PolylitMixin', () => {
render() {
return html`
<div id="title">Title</div>
<${unsafeStatic(teleportedTag)} id="teleported" />
<${unsafeStatic(teleportedTag)} id="teleported">
<div id="teleportedContent">Teleported content</div>
</${unsafeStatic(teleportedTag)}>
`;
}
},
Expand All @@ -1223,12 +1226,17 @@ describe('PolylitMixin', () => {

it('should register elements with id', () => {
expect(element.$.title).to.be.instanceOf(HTMLDivElement);
expect(element.$.title.id).to.equal('title');
expect(element.$.title.textContent.trim()).to.equal('Title');
});

it('should register teleported elements with id', () => {
expect(element.$.teleported).to.be.instanceOf(HTMLElement);
expect(element.$.teleported.id).to.equal('teleported');
expect(element.$.teleported.textContent.trim()).to.equal('Teleported content');
});

it('should register children with id whose parent was teleported', () => {
expect(element.$.teleportedContent).to.be.instanceOf(HTMLElement);
expect(element.$.teleportedContent.textContent.trim()).to.equal('Teleported content');
});
});

Expand Down

0 comments on commit 3d4f3c0

Please sign in to comment.