Skip to content

Commit

Permalink
fix: children and childNodes not returning top-level slotted children (
Browse files Browse the repository at this point in the history
…#4098)

* fix: children and childNodes not returning top-level slotted children

* fix: revert getAllMatches and do no filtering

* Update packages/@lwc/synthetic-shadow/src/faux-shadow/node.ts

Co-authored-by: Eugene Kashida <ekashida@gmail.com>

* fix: comment

---------

Co-authored-by: Eugene Kashida <ekashida@gmail.com>
  • Loading branch information
abdulsattar and ekashida committed Mar 27, 2024
1 parent ea41be4 commit 8643706
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
Expand Up @@ -78,5 +78,7 @@ describe('root light element', () => {
expect(nodes['x-list'].querySelector('button')).toEqual(nodes.button);
expect(nodes['x-list'].getElementsByTagName('button')[0]).toEqual(nodes.button);
expect(nodes['x-list'].getElementsByClassName('button')[0]).toEqual(nodes.button);
expect(nodes['x-list'].childNodes[0]).toEqual(nodes.button);
expect(nodes['x-list'].children[0]).toEqual(nodes.button);
});
});
6 changes: 5 additions & 1 deletion packages/@lwc/synthetic-shadow/src/faux-shadow/element.ts
Expand Up @@ -92,7 +92,11 @@ function shadowRootGetterPatched(this: Element): ShadowRoot | null {

function childrenGetterPatched(this: Element): HTMLCollectionOf<Element> {
const owner = getNodeOwner(this);
const childNodes = isNull(owner) ? [] : getAllMatches(owner, getFilteredChildNodes(this));
const filteredChildNodes = getFilteredChildNodes(this);
// No need to filter by owner for non-shadowed nodes
const childNodes = isNull(owner)
? filteredChildNodes
: getAllMatches(owner, filteredChildNodes);
return createStaticHTMLCollection(
ArrayFilter.call(childNodes, (node: Node | Element) => node instanceof Element)
);
Expand Down
6 changes: 5 additions & 1 deletion packages/@lwc/synthetic-shadow/src/faux-shadow/node.ts
Expand Up @@ -173,7 +173,11 @@ function cloneNodePatched(this: Node, deep?: boolean): Node {
function childNodesGetterPatched(this: Node): NodeListOf<Node> {
if (isSyntheticShadowHost(this)) {
const owner = getNodeOwner(this);
const childNodes = isNull(owner) ? [] : getAllMatches(owner, getFilteredChildNodes(this));
const filteredChildNodes = getFilteredChildNodes(this);
// No need to filter by owner for non-shadowed nodes
const childNodes = isNull(owner)
? filteredChildNodes
: getAllMatches(owner, filteredChildNodes);
return createStaticNodeList(childNodes);
}
// nothing to do here since this does not have a synthetic shadow attached to it
Expand Down
2 changes: 2 additions & 0 deletions packages/@lwc/synthetic-shadow/src/shared/node-ownership.ts
Expand Up @@ -58,6 +58,8 @@ export function getNodeNearestOwnerKey(node: Node): number | undefined {
}
host = parentNodeGetter.call(host) as ShadowedNode | null;

// Elements slotted from top level light DOM into synthetic shadow
// reach the slot tag from the shadow element first
if (!isNull(host) && isSyntheticSlotElement(host)) {
return undefined;
}
Expand Down

0 comments on commit 8643706

Please sign in to comment.