1
1
import { BUILD } from '@app-data' ;
2
- import { consoleError , doc , plt , supportsListenerOptions , win } from '@platform' ;
2
+ import { consoleError , plt , supportsListenerOptions , win } from '@platform' ;
3
3
import { HOST_FLAGS , LISTENER_FLAGS } from '@utils' ;
4
4
5
5
import type * as d from '../declarations' ;
@@ -10,7 +10,7 @@ export const addHostEventListeners = (
10
10
listeners ?: d . ComponentRuntimeHostListener [ ] ,
11
11
attachParentListeners ?: boolean ,
12
12
) => {
13
- if ( BUILD . hostListener && listeners ) {
13
+ if ( BUILD . hostListener && listeners && win . document ) {
14
14
// this is called immediately within the element's constructor
15
15
// initialize our event listeners on the host element
16
16
// we do this now so that we can listen to events that may
@@ -32,7 +32,7 @@ export const addHostEventListeners = (
32
32
}
33
33
34
34
listeners . map ( ( [ flags , name , method ] ) => {
35
- const target = BUILD . hostListenerTarget ? getHostListenerTarget ( elm , flags ) : elm ;
35
+ const target = BUILD . hostListenerTarget ? getHostListenerTarget ( win . document , elm , flags ) : elm ;
36
36
const handler = hostListenerProxy ( hostRef , method ) ;
37
37
const opts = hostListenerOpts ( flags ) ;
38
38
plt . ael ( target , name , handler , opts ) ;
@@ -58,12 +58,20 @@ const hostListenerProxy = (hostRef: d.HostRef, methodName: string) => (ev: Event
58
58
}
59
59
} ;
60
60
61
- const getHostListenerTarget = ( elm : Element , flags : number ) : EventTarget => {
62
- if ( BUILD . hostListenerTargetDocument && flags & LISTENER_FLAGS . TargetDocument ) return doc ;
63
- if ( BUILD . hostListenerTargetWindow && flags & LISTENER_FLAGS . TargetWindow ) return win ;
64
- if ( BUILD . hostListenerTargetBody && flags & LISTENER_FLAGS . TargetBody ) return doc . body ;
65
- if ( BUILD . hostListenerTargetParent && flags & LISTENER_FLAGS . TargetParent && elm . parentElement )
61
+ const getHostListenerTarget = ( doc : Document , elm : Element , flags : number ) : EventTarget => {
62
+ if ( BUILD . hostListenerTargetDocument && flags & LISTENER_FLAGS . TargetDocument ) {
63
+ return doc ;
64
+ }
65
+ if ( BUILD . hostListenerTargetWindow && flags & LISTENER_FLAGS . TargetWindow ) {
66
+ return win ;
67
+ }
68
+ if ( BUILD . hostListenerTargetBody && flags & LISTENER_FLAGS . TargetBody ) {
69
+ return doc . body ;
70
+ }
71
+ if ( BUILD . hostListenerTargetParent && flags & LISTENER_FLAGS . TargetParent && elm . parentElement ) {
66
72
return elm . parentElement ;
73
+ }
74
+
67
75
return elm ;
68
76
} ;
69
77
0 commit comments