From c85010654da2607627c00822e1a29b6bf956185c Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 27 Feb 2015 16:13:51 +0100 Subject: [PATCH] fix(Angular): properly compare RegExp with other objects for equality Fixes #11204 --- src/Angular.js | 7 ++++--- test/AngularSpec.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 0a64cd016273..2a12f4c8e538 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -860,10 +860,11 @@ function equals(o1, o2) { } else if (isDate(o1)) { if (!isDate(o2)) return false; return equals(o1.getTime(), o2.getTime()); - } else if (isRegExp(o1) && isRegExp(o2)) { - return o1.toString() == o2.toString(); + } else if (isRegExp(o1)) { + return isRegExp(o2) ? o1.toString() == o2.toString() : false; } else { - if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false; + if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || + isArray(o2) || isDate(o2) || isRegExp(o2)) return false; keySet = {}; for (key in o1) { if (key.charAt(0) === '$' || isFunction(o1[key])) continue; diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 4a94e1720024..5d1835401b15 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -547,6 +547,7 @@ describe('angular', function() { expect(equals(new Date(undefined), new Date(0))).toBe(false); expect(equals(new Date(undefined), new Date(null))).toBe(false); expect(equals(new Date(undefined), new Date('wrong'))).toBe(true); + expect(equals(new Date(), /abc/)).toBe(false); }); it('should correctly test for keys that are present on Object.prototype', function() { @@ -564,12 +565,22 @@ describe('angular', function() { expect(equals(/abc/, /def/)).toBe(false); expect(equals(/^abc/, /abc/)).toBe(false); expect(equals(/^abc/, '/^abc/')).toBe(false); + expect(equals(/abc/, new Date())).toBe(false); }); it('should return false when comparing an object and an array', function() { expect(equals({}, [])).toBe(false); expect(equals([], {})).toBe(false); }); + + it('should return false when comparing an object and a RegExp', function() { + expect(equals({}, /abc/)).toBe(false); + expect(equals({}, new RegExp('abc', 'i'))).toBe(false); + }); + + it('should return false when comparing an object and a Date', function() { + expect(equals({}, new Date())).toBe(false); + }); });