Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: children and childNodes not returning top-level slotted children #4098

Merged
merged 4 commits into from Mar 27, 2024

Conversation

abdulsattar
Copy link
Contributor

Details

Fixes #4076
Light DOM nodes aren't owned by any shadow root. We change isNodeOwedBy to account for owner being null.

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

GUS work item

@abdulsattar abdulsattar requested a review from a team as a code owner March 25, 2024 16:45
// ownerKey is undefined for such nodes
const ownerKey = getNodeNearestOwnerKey(node);
if (isNull(owner)) {
return isUndefined(ownerKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so sure about this logic. If the owner is null but ownerKey is defined, then isNodeOwnedBy() returns true. By relaxing the input constraints, it seems that we're muddying the job of this function. Maybe this is a sign that we should be handling the light DOM case elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think of this as node being owned by a shadowRoot or document (in which case it's null). It's owned by document, in case of light DOM parents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would having more tests for childNodes/children help build confidence? I suspect we don’t have a lot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both childrenGetterPatched and childNodesGetterPatched, if there is no owner, then it means that the element or node is not shadowed, correct? If so, then it seems like we should immediately return getFilteredChildNodes(this) instead of drilling down into getAllMatches() and isNodeOwnedBy().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Done.

@@ -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 nodes by owner in case of light DOM
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also applies to root shadow DOM components:

Suggested change
// no need to filter nodes by owner in case of light DOM
// No need to filter by owner for non-shadowed elements

packages/@lwc/synthetic-shadow/src/faux-shadow/node.ts Outdated Show resolved Hide resolved
Comment on lines +61 to +62
// Elements slotted from top level light DOM into synthetic shadow
// reach the slot tag from the shadow element first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand this comment. Are we short-circuiting here so that we don't need to traverse to the root?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <slot> element will belong to some other shadow root and check if the node belongs to the owner of the <slot> element. This check will fail and it won't return any elements.

abdulsattar and others added 2 commits March 27, 2024 17:15
@jmsjtu jmsjtu merged commit 8643706 into master Mar 27, 2024
9 checks passed
@jmsjtu jmsjtu deleted the abdulsattar/fix-childnodes branch March 27, 2024 21:58
@jmsjtu jmsjtu mentioned this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants