@@ -115,7 +115,7 @@ function dispatchEventsForPlugins(
115115 eventSystemFlags : EventSystemFlags ,
116116 nativeEvent : AnyNativeEvent ,
117117 targetInst : null | Fiber ,
118- rootContainer : EventTarget ,
118+ targetContainer : null | EventTarget ,
119119) : void {
120120 const nativeEventTarget = getEventTarget ( nativeEvent ) ;
121121 const syntheticEvents : Array < ReactSyntheticEvent > = [ ] ;
@@ -129,7 +129,7 @@ function dispatchEventsForPlugins(
129129 nativeEvent ,
130130 nativeEventTarget ,
131131 eventSystemFlags ,
132- rootContainer ,
132+ targetContainer ,
133133 ) ;
134134 if ( isArray ( extractedEvents ) ) {
135135 // Flow complains about @@iterator being missing in ReactSyntheticEvent,
@@ -209,7 +209,7 @@ function willDeferLaterForFBLegacyPrimer(nativeEvent: any): boolean {
209209 const legacyFBSupport = true ;
210210 const isCapture = nativeEvent . eventPhase === 1 ;
211211 addTrappedEventListener (
212- document ,
212+ null ,
213213 ( ( type : any ) : DOMTopLevelEventType ) ,
214214 isCapture ,
215215 legacyFBSupport ,
@@ -223,18 +223,18 @@ function willDeferLaterForFBLegacyPrimer(nativeEvent: any): boolean {
223223
224224function isMatchingRootContainer (
225225 grandContainer : Element ,
226- rootContainer : EventTarget,
226+ targetContainer : EventTarget ,
227227) : boolean {
228228 return (
229- grandContainer === rootContainer ||
229+ grandContainer === targetContainer ||
230230 ( grandContainer . nodeType === COMMENT_NODE &&
231- grandContainer . parentNode === rootContainer )
231+ grandContainer . parentNode === targetContainer )
232232 ) ;
233233}
234234
235235export function isDOMElement ( target : EventTarget ) : boolean {
236236 const nodeType = ( ( target : any ) : Node ) . nodeType ;
237- return nodeType != null && nodeType === ELEMENT_NODE ;
237+ return ( nodeType : any ) && nodeType === ELEMENT_NODE ;
238238}
239239
240240export function isDOMDocument ( target : EventTarget ) : boolean {
@@ -247,14 +247,22 @@ export function dispatchEventForPluginEventSystem(
247247 eventSystemFlags : EventSystemFlags ,
248248 nativeEvent : AnyNativeEvent ,
249249 targetInst : null | Fiber ,
250- rootContainer: EventTarget,
250+ targetContainer : null | EventTarget ,
251251) : void {
252252 let ancestorInst = targetInst ;
253- // Given the rootContainer can be any EventTarget, if the
254- // target is not a valid DOM element then we'll skip this part.
255- if ( isDOMElement ( rootContainer ) ) {
253+ if ( targetContainer !== null ) {
254+ const possibleTargetContainerNode = ( ( targetContainer : any ) : Node ) ;
255+ // Given the rootContainer can be any EventTarget, if the
256+ // target is not a valid DOM element then we'll skip this part.
257+ if (
258+ possibleTargetContainerNode === window ||
259+ ! isDOMElement ( possibleTargetContainerNode )
260+ ) {
261+ // TODO: useEvent for document and window
262+ return ;
263+ }
256264 // If we detect the FB legacy primer system, we
257- // defer the event to the "document" with a one
265+ // defer the event to the null with a one
258266 // time event listener so we can defer the event.
259267 if (
260268 enableLegacyFBPrimerSupport &&
@@ -281,7 +289,7 @@ export function dispatchEventForPluginEventSystem(
281289 }
282290 if ( node . tag === HostRoot || node . tag === HostPortal ) {
283291 const container = node . stateNode . containerInfo ;
284- if ( isMatchingRootContainer ( container , rootContainer ) ) {
292+ if ( isMatchingRootContainer ( container , possibleTargetContainerNode ) ) {
285293 break ;
286294 }
287295 if ( node . tag === HostPortal ) {
@@ -293,7 +301,12 @@ export function dispatchEventForPluginEventSystem(
293301 while ( grandNode !== null ) {
294302 if ( grandNode . tag === HostRoot || grandNode . tag === HostPortal ) {
295303 const grandContainer = grandNode . stateNode . containerInfo ;
296- if ( isMatchingRootContainer ( grandContainer , rootContainer ) ) {
304+ if (
305+ isMatchingRootContainer (
306+ grandContainer ,
307+ possibleTargetContainerNode ,
308+ )
309+ ) {
297310 // This is the rootContainer we're looking for and we found it as
298311 // a parent of the Portal. That means we can ignore it because the
299312 // Portal will bubble through to us.
@@ -320,7 +333,7 @@ export function dispatchEventForPluginEventSystem(
320333 eventSystemFlags ,
321334 nativeEvent ,
322335 ancestorInst ,
323- rootContainer ,
336+ targetContainer ,
324337 ) ,
325338 ) ;
326339}
@@ -347,11 +360,13 @@ export function attachElementListener(listener: ReactDOMListener): void {
347360 // find the nearest root/portal contained to attach the event listener
348361 // to. If it's not managed, i.e. the window, then we just attach
349362 // the listener to the target.
350- const possibleManagedTarget = ((target: any): Element);
351- if (getClosestInstanceFromNode(possibleManagedTarget)) {
352- containerEventTarget = getNearestRootOrPortalContainer (
353- possibleManagedTarget ,
354- ) ;
363+ if ( isDOMElement ( target ) ) {
364+ const possibleManagedTarget = ( ( target : any ) : Element ) ;
365+ if ( getClosestInstanceFromNode ( possibleManagedTarget ) ) {
366+ containerEventTarget = getNearestRootOrPortalContainer (
367+ possibleManagedTarget ,
368+ ) ;
369+ }
355370 }
356371 const listenerMap = getListenerMapForElement ( containerEventTarget ) ;
357372 // Add the event listener to the target container (falling back to
0 commit comments