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(engine): bail retargeting if target is detached #682

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions packages/lwc-engine/src/faux-shadow/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ const EventPatchDescriptors: PropertyDescriptorMap = {
return outerMostElement;
}
const eventContext = eventToContextMap.get(this);
// Executing event listener on component, target is always currentTarget

// Retarget to currentTarget if the listener was added to a custom element.
if (eventContext === EventListenerContext.CUSTOM_ELEMENT_LISTENER) {
return patchShadowDomTraversalMethods(currentTarget as Element);
}
const currentTargetRootNode = getRootNode.call(currentTarget, GET_ROOT_NODE_CONFIG_FALSE); // x-child

// Before we can process any events, we need to first determine three things:
// 1) What VM context was the event attached to? (e.g. in what VM context was addEventListener called in).
// 2) What VM owns the context where the event was attached? (e.g. who rendered the VM context from step 1).
const currentTargetRootNode = getRootNode.call(currentTarget, GET_ROOT_NODE_CONFIG_FALSE);

// We need to determine three things in order to retarget correctly:
// 1) What VM context was the listener added? (e.g. in what VM context was addEventListener called in).
// 2) What VM owns the context where the listener was added? (e.g. who rendered the VM context from step 1).
// 3) What is the event's original target's relationship to 1 and 2?

// Determining Number 1:
Expand All @@ -86,8 +88,8 @@ const EventPatchDescriptors: PropertyDescriptorMap = {
// // x-parent.html
// <template>
// <!--
// The event below is attached inside of x-parent's template
// so vm context will be <x-parent>'s owner VM
// This listener is added inside of <x-parent>'s template
// so its VM context will be <x-parent>'s VM
// -->
// <div onclick={handleClick}</div>
// </template>
Expand Down Expand Up @@ -145,8 +147,15 @@ const EventPatchDescriptors: PropertyDescriptorMap = {
// </template>
//
let closestTarget = originalTarget;
while (getNodeOwnerKey(closestTarget as Node) !== myCurrentShadowKey && getNodeOwnerKey(closestTarget as Node) !== myOwnerKey) {
let nodeOwnerKey = getNodeOwnerKey(closestTarget as Node);
while (nodeOwnerKey !== myCurrentShadowKey && nodeOwnerKey !== myOwnerKey) {
closestTarget = parentNodeGetter.call(closestTarget);
// In this case, the original target is in a detached root, making it
// impossible to retarget (unless we figure out something clever).
if (closestTarget === null) {
return originalTarget;
}
nodeOwnerKey = getNodeOwnerKey(closestTarget as Node);
}

/**
Expand Down