@@ -7,11 +7,11 @@ const OnBeforeUnloadEventHandlerNonNull = require("../generated/OnBeforeUnloadEv
7
7
const OnErrorEventHandlerNonNull = require ( "../generated/OnErrorEventHandlerNonNull.js" ) ;
8
8
const reportException = require ( "./runtime-script-errors" ) ;
9
9
10
- exports . appendHandler = function appendHandler ( el , eventName ) {
10
+ exports . appendHandler = ( el , eventName ) => {
11
11
// tryImplForWrapper() is currently required due to use in Window.js
12
12
idlUtils . tryImplForWrapper ( el ) . addEventListener ( eventName , event => {
13
13
// https://html.spec.whatwg.org/#the-event-handler-processing-algorithm
14
- const callback = el [ "on" + eventName ] ;
14
+ const callback = exports . getCurrentEventHandlerValue ( el , eventName ) ;
15
15
if ( callback === null ) {
16
16
return ;
17
17
}
@@ -74,103 +74,106 @@ exports.setupForSimpleEventAccessors = (prototype, events) => {
74
74
}
75
75
} ;
76
76
77
- // https://html.spec.whatwg.org/#event-handler-idl-attributes
78
- exports . createEventAccessor = function createEventAccessor ( obj , event ) {
79
- Object . defineProperty ( obj , "on" + event , {
80
- configurable : true ,
81
- enumerable : true ,
82
- get ( ) {
83
- // TODO: Extract this into a helper function
84
- // https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler
85
- const value = this . _getEventHandlerFor ( event ) ;
86
- if ( ! value ) {
87
- return null ;
88
- }
77
+ // https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler
78
+ exports . getCurrentEventHandlerValue = ( target , event ) => {
79
+ const value = target . _getEventHandlerFor ( event ) ;
80
+ if ( ! value ) {
81
+ return null ;
82
+ }
89
83
90
- if ( value . body !== undefined ) {
91
- let element ;
92
- let document ;
93
- if ( this . constructor . name === "Window" ) {
94
- element = null ;
95
- document = idlUtils . implForWrapper ( this . document ) ;
96
- } else {
97
- element = this ;
98
- document = element . ownerDocument ;
99
- }
100
- const { body } = value ;
101
-
102
- const formOwner = element !== null && element . form ? element . form : null ;
103
- const window = this . constructor . name === "Window" && this . _document ? this : document . defaultView ;
104
-
105
- try {
106
- // eslint-disable-next-line no-new-func
107
- Function ( body ) ; // properly error out on syntax errors
108
- // Note: this won't execute body; that would require `Function(body)()`.
109
- } catch ( e ) {
110
- if ( window ) {
111
- reportException ( window , e ) ;
112
- }
113
- this . _setEventHandlerFor ( event , null ) ;
114
- return null ;
115
- }
84
+ if ( value . body !== undefined ) {
85
+ let element ;
86
+ let document ;
87
+ if ( target . constructor . name === "Window" ) {
88
+ element = null ;
89
+ document = idlUtils . implForWrapper ( target . document ) ;
90
+ } else {
91
+ element = target ;
92
+ document = element . ownerDocument ;
93
+ }
94
+ const { body } = value ;
95
+
96
+ const formOwner = element !== null && element . form ? element . form : null ;
97
+ const window = target . constructor . name === "Window" && target . _document ? target : document . defaultView ;
98
+
99
+ try {
100
+ // eslint-disable-next-line no-new-func
101
+ Function ( body ) ; // properly error out on syntax errors
102
+ // Note: this won't execute body; that would require `Function(body)()`.
103
+ } catch ( e ) {
104
+ if ( window ) {
105
+ reportException ( window , e ) ;
106
+ }
107
+ target . _setEventHandlerFor ( event , null ) ;
108
+ return null ;
109
+ }
116
110
117
- // Note: the with (window) { } is not necessary in Node, but is necessary in a browserified environment.
111
+ // Note: the with (window) { } is not necessary in Node, but is necessary in a browserified environment.
118
112
119
- let fn ;
120
- const createFunction = document . defaultView . Function ;
121
- if ( event === "error" && element === null ) {
122
- const wrapperBody = document ? body + `\n//# sourceURL=${ document . URL } ` : body ;
113
+ let fn ;
114
+ const createFunction = document . defaultView . Function ;
115
+ if ( event === "error" && element === null ) {
116
+ const wrapperBody = document ? body + `\n//# sourceURL=${ document . URL } ` : body ;
123
117
124
- // eslint-disable-next-line no-new-func
125
- fn = createFunction ( "window" , `with (window) { return function onerror(event, source, lineno, colno, error) {
118
+ // eslint-disable-next-line no-new-func
119
+ fn = createFunction ( "window" , `with (window) { return function onerror(event, source, lineno, colno, error) {
126
120
${ wrapperBody }
127
121
}; }` ) ( window ) ;
128
122
129
- fn = OnErrorEventHandlerNonNull . convert ( fn ) ;
130
- } else {
131
- const argNames = [ ] ;
132
- const args = [ ] ;
133
-
134
- argNames . push ( "window" ) ;
135
- args . push ( window ) ;
136
-
137
- if ( element !== null ) {
138
- argNames . push ( "document" ) ;
139
- args . push ( idlUtils . wrapperForImpl ( document ) ) ;
140
- }
141
- if ( formOwner !== null ) {
142
- argNames . push ( "formOwner" ) ;
143
- args . push ( idlUtils . wrapperForImpl ( formOwner ) ) ;
144
- }
145
- if ( element !== null ) {
146
- argNames . push ( "element" ) ;
147
- args . push ( idlUtils . wrapperForImpl ( element ) ) ;
148
- }
149
- let wrapperBody = `
123
+ fn = OnErrorEventHandlerNonNull . convert ( fn ) ;
124
+ } else {
125
+ const argNames = [ ] ;
126
+ const args = [ ] ;
127
+
128
+ argNames . push ( "window" ) ;
129
+ args . push ( window ) ;
130
+
131
+ if ( element !== null ) {
132
+ argNames . push ( "document" ) ;
133
+ args . push ( idlUtils . wrapperForImpl ( document ) ) ;
134
+ }
135
+ if ( formOwner !== null ) {
136
+ argNames . push ( "formOwner" ) ;
137
+ args . push ( idlUtils . wrapperForImpl ( formOwner ) ) ;
138
+ }
139
+ if ( element !== null ) {
140
+ argNames . push ( "element" ) ;
141
+ args . push ( idlUtils . wrapperForImpl ( element ) ) ;
142
+ }
143
+ let wrapperBody = `
150
144
return function on${ event } (event) {
151
145
${ body }
152
146
};` ;
153
- for ( let i = argNames . length - 1 ; i >= 0 ; -- i ) {
154
- wrapperBody = `with (${ argNames [ i ] } ) { ${ wrapperBody } }` ;
155
- }
156
- if ( document ) {
157
- wrapperBody += `\n//# sourceURL=${ document . URL } ` ;
158
- }
159
- argNames . push ( wrapperBody ) ;
160
- fn = createFunction ( ...argNames ) ( ...args ) ;
161
-
162
- if ( event === "beforeunload" ) {
163
- fn = OnBeforeUnloadEventHandlerNonNull . convert ( fn ) ;
164
- } else {
165
- fn = EventHandlerNonNull . convert ( fn ) ;
166
- }
167
- }
147
+ for ( let i = argNames . length - 1 ; i >= 0 ; -- i ) {
148
+ wrapperBody = `with (${ argNames [ i ] } ) { ${ wrapperBody } }` ;
149
+ }
150
+ if ( document ) {
151
+ wrapperBody += `\n//# sourceURL=${ document . URL } ` ;
152
+ }
153
+ argNames . push ( wrapperBody ) ;
154
+ fn = createFunction ( ...argNames ) ( ...args ) ;
168
155
169
- this . _setEventHandlerFor ( event , fn ) ;
156
+ if ( event === "beforeunload" ) {
157
+ fn = OnBeforeUnloadEventHandlerNonNull . convert ( fn ) ;
158
+ } else {
159
+ fn = EventHandlerNonNull . convert ( fn ) ;
170
160
}
161
+ }
162
+
163
+ target . _setEventHandlerFor ( event , fn ) ;
164
+ }
165
+
166
+ return target . _getEventHandlerFor ( event ) ;
167
+ } ;
171
168
172
- // tryWrapperForImpl() is currently required due to use in Window.js
173
- return idlUtils . tryWrapperForImpl ( this . _getEventHandlerFor ( event ) ) ;
169
+ // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributes
170
+ // TODO: Consider replacing this with `[ReflectEvent]`
171
+ exports . createEventAccessor = ( obj , event ) => {
172
+ Object . defineProperty ( obj , "on" + event , {
173
+ configurable : true ,
174
+ enumerable : true ,
175
+ get ( ) {
176
+ return exports . getCurrentEventHandlerValue ( this , event ) ;
174
177
} ,
175
178
set ( val ) {
176
179
this . _setEventHandlerFor ( event , val ) ;
0 commit comments