Skip to content

Commit

Permalink
fix(event): uses Object.prototype.toString.call on objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Stobbart authored and Eric Stobbart committed Nov 20, 2016
1 parent 836fb1f commit 1251c10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,21 @@ describe('Observable.fromEvent', () => {

send(1, 2, 3);
});

it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => {
// NOTE: Can not test with Object.create(null) or `class Foo extends null`
// due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108
class NullProtoEventTarget {
on() { /*noop*/ }
off() { /*noop*/ }
}
NullProtoEventTarget.prototype.toString = null;
const obj: NullProtoEventTarget = new NullProtoEventTarget();

expect(() => {
Observable.fromEvent(obj, 'foo').subscribe();
done();
}).to.not.throw(TypeError);
});

});
6 changes: 4 additions & 2 deletions src/observable/FromEventObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { errorObject } from '../util/errorObject';
import { Subscription } from '../Subscription';
import { Subscriber } from '../Subscriber';

const toString: Function = Object.prototype.toString;

export type NodeStyleEventEmmitter = {
addListener: (eventName: string, handler: Function) => void;
removeListener: (eventName: string, handler: Function) => void;
Expand All @@ -22,11 +24,11 @@ function isJQueryStyleEventEmitter(sourceObj: any): sourceObj is JQueryStyleEven
}

function isNodeList(sourceObj: any): sourceObj is NodeList {
return !!sourceObj && sourceObj.toString() === '[object NodeList]';
return !!sourceObj && toString.call(sourceObj) === '[object NodeList]';
}

function isHTMLCollection(sourceObj: any): sourceObj is HTMLCollection {
return !!sourceObj && sourceObj.toString() === '[object HTMLCollection]';
return !!sourceObj && toString.call(sourceObj) === '[object HTMLCollection]';
}

function isEventTarget(sourceObj: any): sourceObj is EventTarget {
Expand Down

0 comments on commit 1251c10

Please sign in to comment.