Skip to content

Commit e036e79

Browse files
estobbartjayphelps
authored andcommitted
fix(event): uses Object.prototype.toString.call on objects (#2143)
1 parent 836fb1f commit e036e79

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: spec/observables/fromEvent-spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,21 @@ describe('Observable.fromEvent', () => {
279279

280280
send(1, 2, 3);
281281
});
282+
283+
it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => {
284+
// NOTE: Can not test with Object.create(null) or `class Foo extends null`
285+
// due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108
286+
class NullProtoEventTarget {
287+
on() { /*noop*/ }
288+
off() { /*noop*/ }
289+
}
290+
NullProtoEventTarget.prototype.toString = null;
291+
const obj: NullProtoEventTarget = new NullProtoEventTarget();
292+
293+
expect(() => {
294+
Observable.fromEvent(obj, 'foo').subscribe();
295+
done();
296+
}).to.not.throw(TypeError);
297+
});
298+
282299
});

Diff for: src/observable/FromEventObservable.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { errorObject } from '../util/errorObject';
55
import { Subscription } from '../Subscription';
66
import { Subscriber } from '../Subscriber';
77

8+
const toString: Function = Object.prototype.toString;
9+
810
export type NodeStyleEventEmmitter = {
911
addListener: (eventName: string, handler: Function) => void;
1012
removeListener: (eventName: string, handler: Function) => void;
@@ -22,11 +24,11 @@ function isJQueryStyleEventEmitter(sourceObj: any): sourceObj is JQueryStyleEven
2224
}
2325

2426
function isNodeList(sourceObj: any): sourceObj is NodeList {
25-
return !!sourceObj && sourceObj.toString() === '[object NodeList]';
27+
return !!sourceObj && toString.call(sourceObj) === '[object NodeList]';
2628
}
2729

2830
function isHTMLCollection(sourceObj: any): sourceObj is HTMLCollection {
29-
return !!sourceObj && sourceObj.toString() === '[object HTMLCollection]';
31+
return !!sourceObj && toString.call(sourceObj) === '[object HTMLCollection]';
3032
}
3133

3234
function isEventTarget(sourceObj: any): sourceObj is EventTarget {

0 commit comments

Comments
 (0)