Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(event): uses Object.prototype.toString.call on objects #2143

Merged
merged 1 commit into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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