|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {patchFilteredProperties} from '../../lib/browser/property-descriptor';
|
| 10 | +import {patchEventTarget} from '../../lib/common/events'; |
10 | 11 | import {isBrowser, isIEOrEdge, isMix, zoneSymbol} from '../../lib/common/utils';
|
11 |
| -import {ifEnvSupports, ifEnvSupportsWithDone} from '../test-util'; |
| 12 | +import {getIEVersion, ifEnvSupports, ifEnvSupportsWithDone} from '../test-util'; |
12 | 13 |
|
13 | 14 | import Spy = jasmine.Spy;
|
14 | 15 | declare const global: any;
|
@@ -243,6 +244,74 @@ describe('Zone', function() {
|
243 | 244 | document.removeEventListener('mousedown', eventListenerSpy);
|
244 | 245 | }));
|
245 | 246 |
|
| 247 | + it('event handler with null context should use event.target', |
| 248 | + ifEnvSupports(canPatchOnProperty(Document.prototype, 'onmousedown'), function() { |
| 249 | + const ieVer = getIEVersion(); |
| 250 | + if (ieVer && ieVer === 9) { |
| 251 | + // in ie9, this is window object even we call func.apply(undefined) |
| 252 | + return; |
| 253 | + } |
| 254 | + const logs: string[] = []; |
| 255 | + const EventTarget = (window as any)['EventTarget']; |
| 256 | + let oriAddEventListener = EventTarget && EventTarget.prototype ? |
| 257 | + (EventTarget.prototype as any)['__zone_symbol__addEventListener'] : |
| 258 | + (HTMLSpanElement.prototype as any)['__zone_symbol__addEventListener']; |
| 259 | + |
| 260 | + if (!oriAddEventListener) { |
| 261 | + // no patched addEventListener found |
| 262 | + return; |
| 263 | + } |
| 264 | + let handler1: Function; |
| 265 | + let handler2: Function; |
| 266 | + |
| 267 | + const listener = function() { |
| 268 | + logs.push('listener1'); |
| 269 | + }; |
| 270 | + |
| 271 | + const listener1 = function() { |
| 272 | + logs.push('listener2'); |
| 273 | + }; |
| 274 | + |
| 275 | + HTMLSpanElement.prototype.addEventListener = function( |
| 276 | + eventName: string, callback: any) { |
| 277 | + if (eventName === 'click') { |
| 278 | + handler1 = callback; |
| 279 | + } else if (eventName === 'mousedown') { |
| 280 | + handler2 = callback; |
| 281 | + } |
| 282 | + return oriAddEventListener.apply(this, arguments); |
| 283 | + }; |
| 284 | + |
| 285 | + (HTMLSpanElement.prototype as any)['__zone_symbol__addEventListener'] = null; |
| 286 | + |
| 287 | + patchEventTarget(window, [HTMLSpanElement.prototype]); |
| 288 | + |
| 289 | + const span = document.createElement('span'); |
| 290 | + document.body.appendChild(span); |
| 291 | + |
| 292 | + zone.run(function() { |
| 293 | + span.addEventListener('click', listener); |
| 294 | + span.onmousedown = listener1; |
| 295 | + }); |
| 296 | + |
| 297 | + expect(handler1).toBe(handler2); |
| 298 | + |
| 299 | + handler1.apply(undefined, [{type: 'click', target: span}]); |
| 300 | + |
| 301 | + handler2.apply(undefined, [{type: 'mousedown', target: span}]); |
| 302 | + |
| 303 | + expect(hookSpy).toHaveBeenCalled(); |
| 304 | + expect(logs).toEqual(['listener1', 'listener2']); |
| 305 | + document.body.removeChild(span); |
| 306 | + if (EventTarget) { |
| 307 | + (EventTarget.prototype as any)['__zone_symbol__addEventListener'] = |
| 308 | + oriAddEventListener; |
| 309 | + } else { |
| 310 | + (HTMLSpanElement.prototype as any)['__zone_symbol__addEventListener'] = |
| 311 | + oriAddEventListener; |
| 312 | + } |
| 313 | + })); |
| 314 | + |
246 | 315 | it('SVGElement onclick should be in zone',
|
247 | 316 | ifEnvSupports(
|
248 | 317 | canPatchOnProperty(SVGElement && SVGElement.prototype, 'onmousedown'), function() {
|
|
0 commit comments