@samitouri / QOS-React-2 / commits / e028ce2ab7

Modern Event System: ensure target ancestors are only host nodes (#18827)

Dominic Gannaway committed May 5, 2020 at 13:10 UTC e028ce2ab7fbecb30d23cd34e91553ccbeb7bb8e
1 file changed +13 -4
packages/react-dom/src/events/DOMModernPluginEventSystem.js
+13 -4
@@ -39,6 +39,7 @@ import {
39 HostRoot,
40 HostPortal,
41 HostComponent,
42 + HostText,
43 } from 'react-reconciler/src/ReactWorkTags';
44
45 import getEventTarget from './getEventTarget';
@@ -420,19 +421,21 @@ export function dispatchEventForPluginEventSystem(
421 if (node === null) {
422 return;
423 }
423 - if (node.tag === HostRoot || node.tag === HostPortal) {
424 + const nodeTag = node.tag;
425 + if (nodeTag === HostRoot || nodeTag === HostPortal) {
426 const container = node.stateNode.containerInfo;
427 if (isMatchingRootContainer(container, targetContainerNode)) {
428 break;
429 }
428 - if (node.tag === HostPortal) {
430 + if (nodeTag === HostPortal) {
431 // The target is a portal, but it's not the rootContainer we're looking for.
432 // Normally portals handle their own events all the way down to the root.
433 // So we should be able to stop now. However, we don't know if this portal
434 // was part of *our* root.
435 let grandNode = node.return;
436 while (grandNode !== null) {
435 - if (grandNode.tag === HostRoot || grandNode.tag === HostPortal) {
437 + const grandTag = grandNode.tag;
438 + if (grandTag === HostRoot || grandTag === HostPortal) {
439 const grandContainer = grandNode.stateNode.containerInfo;
440 if (
441 isMatchingRootContainer(grandContainer, targetContainerNode)
@@ -450,7 +453,13 @@ export function dispatchEventForPluginEventSystem(
453 if (parentSubtreeInst === null) {
454 return;
455 }
453 - node = ancestorInst = parentSubtreeInst;
456 + const parentTag = parentSubtreeInst.tag;
457 + // getClosestInstanceFromNode can return a HostRoot or SuspenseComponent.
458 + // So we need to ensure we only set the ancestor to a HostComponent or HostText.
459 + if (parentTag === HostComponent || parentTag === HostText) {
460 + ancestorInst = parentSubtreeInst;
461 + }
462 + node = parentSubtreeInst;
463 continue;
464 }
465 node = node.return;