Skip to content

Commit

Permalink
Ensure that queryAssignedNodes always returns an array (#1980)
Browse files Browse the repository at this point in the history
If the `<slot>` element isn't rendered, the decorator can return null
  • Loading branch information
dfreedm committed Aug 20, 2021
1 parent 9745bba commit 018f652
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-ducks-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/reactive-element': patch
---

fix queryAssignedNodes returning null if slot is not found
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export function queryAssignedNodes(
slotName ? `[name=${slotName}]` : ':not([name])'
}`;
const slot = this.renderRoot?.querySelector(slotSelector);
let nodes = (slot as HTMLSlotElement)?.assignedNodes({flatten});
if (nodes && selector) {
let nodes = (slot as HTMLSlotElement)?.assignedNodes({flatten}) ?? [];
if (selector) {
nodes = nodes.filter(
(node) =>
node.nodeType === Node.ELEMENT_NODE &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const flush =
assignedNodesEl!: D;
assignedNodesEl2!: E;
assignedNodesEl3!: S;
@queryAssignedNodes() missingSlotAssignedNodes!: Node[];

render() {
return html`
Expand Down Expand Up @@ -225,4 +226,8 @@ const flush =
Object.defineProperty(Element.prototype, 'matches', descriptor);
}
});

test('always returns an array, even if the slot is not rendered', () => {
assert.isArray(el.missingSlotAssignedNodes);
});
});

0 comments on commit 018f652

Please sign in to comment.