Skip to content

Commit bd815a7

Browse files
committed
fix(addEventListener): when called from the global scope
fixes angular#190
1 parent e6124dc commit bd815a7

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/utils.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,18 @@ function patchEventTargetMethods(obj) {
110110
})(handler);
111111
} else {
112112
fn = handler;
113-
}
113+
}
114114

115115
handler[originalFnKey] = fn;
116116
handler[boundFnsKey] = handler[boundFnsKey] || {};
117117
handler[boundFnsKey][eventType] = handler[boundFnsKey][eventType] || zone.bind(fn);
118118
arguments[1] = handler[boundFnsKey][eventType];
119119
}
120120

121-
var target = isWebWorker() && !this ? self : this;
121+
// - Inside a Web Worker, `this` is undefined, the context is `global` (= `self`)
122+
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
123+
// see https://github.com/angular/zone.js/issues/190
124+
var target = this || global;
122125

123126
return global.zone.addEventListener.apply(target, arguments);
124127
};
@@ -129,11 +132,15 @@ function patchEventTargetMethods(obj) {
129132
var eventType = eventName + (useCapturing ? '$capturing' : '$bubbling');
130133
if (handler[boundFnsKey] && handler[boundFnsKey][eventType]) {
131134
var _bound = handler[boundFnsKey];
132-
133135
arguments[1] = _bound[eventType];
134136
delete _bound[eventType];
135137
}
136-
var target = isWebWorker() && !this ? self : this;
138+
139+
// - Inside a Web Worker, `this` is undefined, the context is `global` (= `self`)
140+
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
141+
// see https://github.com/angular/zone.js/issues/190
142+
var target = this || global;
143+
137144
var result = global.zone.removeEventListener.apply(target, arguments);
138145
global.zone.dequeueTask(handler[originalFnKey]);
139146
return result;

test/patch/element.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ describe('element', function () {
1414
document.body.removeChild(button);
1515
});
1616

17+
// https://github.com/angular/zone.js/issues/190
18+
it('should work when addEventListener is called in the global context', function () {
19+
var clickEvent = document.createEvent('Event');
20+
clickEvent.initEvent('click', true, true);
21+
22+
testZone.run(function() {
23+
// `this` would be null inside the method when `addEventListener` is called from strict mode
24+
// it would be `window`:
25+
// - when called from non strict-mode,
26+
// - when `window.addEventListener` is called explicitely.
27+
addEventListener('click', function (event) {
28+
expect(zone).toBeDirectChildOf(testZone);
29+
expect(event).toBe(clickEvent)
30+
});
31+
});
32+
33+
button.dispatchEvent(clickEvent);
34+
});
35+
1736
it('should work with addEventListener when called with a function listener', function () {
1837
var clickEvent = document.createEvent('Event');
1938
clickEvent.initEvent('click', true, true);

0 commit comments

Comments
 (0)