From c592c6045eb1c90d81bda3c2b403192e273b8e73 Mon Sep 17 00:00:00 2001 From: BobChao87 Date: Sat, 12 Mar 2016 03:26:12 -0800 Subject: [PATCH 1/5] Fix broken expection in Safari for serializing invalid dates. All browsers should consistently serialize to 'null'. --- src/Angular.js | 7 ++++++- test/AngularSpec.js | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index c3e326d756fe..a979532817a9 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1195,7 +1195,12 @@ function toJson(obj, pretty) { if (!isNumber(pretty)) { pretty = pretty ? 2 : null; } - return JSON.stringify(obj, toJsonReplacer, pretty); + try { + return JSON.stringify(obj, toJsonReplacer, pretty); + } catch (RangeError) { + // In Safari, 'Invalid Date's throw a range error. 'null' is returned in all other browsers. + return 'null'; + } } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 314ff17c0119..ad7c7c1ce0e3 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -2058,6 +2058,10 @@ describe('angular', function() { it('should serialize undefined as undefined', function() { expect(toJson(undefined)).toEqual(undefined); }); + + it('should serialize invalid dates to null', function() { + expect(toJson(new Date(1/0))).toEqual('null'); + }); }); describe('isElement', function() { From 2d3cebfd9c22974b10369bda004a559b611c4199 Mon Sep 17 00:00:00 2001 From: BobChao87 Date: Sat, 12 Mar 2016 03:26:12 -0800 Subject: [PATCH 2/5] fix(toJson): correct serialization of invalid dates Fix for error thrown by Safari when an invalid date is serialized. Use 'null' always. Closes #13415 --- src/Angular.js | 7 ++++++- test/AngularSpec.js | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index c3e326d756fe..a979532817a9 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1195,7 +1195,12 @@ function toJson(obj, pretty) { if (!isNumber(pretty)) { pretty = pretty ? 2 : null; } - return JSON.stringify(obj, toJsonReplacer, pretty); + try { + return JSON.stringify(obj, toJsonReplacer, pretty); + } catch (RangeError) { + // In Safari, 'Invalid Date's throw a range error. 'null' is returned in all other browsers. + return 'null'; + } } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 314ff17c0119..ad7c7c1ce0e3 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -2058,6 +2058,10 @@ describe('angular', function() { it('should serialize undefined as undefined', function() { expect(toJson(undefined)).toEqual(undefined); }); + + it('should serialize invalid dates to null', function() { + expect(toJson(new Date(1/0))).toEqual('null'); + }); }); describe('isElement', function() { From 9c9eb498d386608d25abda8cd35ed9491f12a544 Mon Sep 17 00:00:00 2001 From: BobChao87 Date: Sat, 12 Mar 2016 04:13:57 -0800 Subject: [PATCH 3/5] fix(toJson): correct serialization of invalid dates Fix for error thrown by Safari when an invalid date is serialized. Use 'null' always. Closes #13415 --- test/AngularSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AngularSpec.js b/test/AngularSpec.js index ad7c7c1ce0e3..ffb445030309 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -2058,7 +2058,7 @@ describe('angular', function() { it('should serialize undefined as undefined', function() { expect(toJson(undefined)).toEqual(undefined); }); - + it('should serialize invalid dates to null', function() { expect(toJson(new Date(1/0))).toEqual('null'); }); From b0ae534306282bf1783dfc0649e89f7b8db900d1 Mon Sep 17 00:00:00 2001 From: BobChao87 Date: Sun, 13 Mar 2016 17:28:15 -0700 Subject: [PATCH 4/5] fix(toJson): correct serialization of invalid dates Fix for error thrown by Safari when an invalid date is serialized. Use 'null' always. Closes #13415 --- test/AngularSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AngularSpec.js b/test/AngularSpec.js index ffb445030309..326a15bef83e 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -2060,7 +2060,7 @@ describe('angular', function() { }); it('should serialize invalid dates to null', function() { - expect(toJson(new Date(1/0))).toEqual('null'); + expect(toJson(new Date(1 / 0))).toEqual('null'); }); }); From a859cdaf8874b50edde43d3321287bb9273fbab5 Mon Sep 17 00:00:00 2001 From: BobChao87 Date: Tue, 22 Mar 2016 23:44:30 -0700 Subject: [PATCH 5/5] fix(toJson): correct serialization of invalid dates Fix for error thrown by Safari when an invalid date is serialized. Use 'null' always. Closes #13415 --- src/Angular.js | 9 +++------ test/AngularSpec.js | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index a979532817a9..630b90df40fe 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1169,6 +1169,8 @@ function toJsonReplacer(key, value) { val = '$DOCUMENT'; } else if (isScope(value)) { val = '$SCOPE'; + } else if (isDate(value) && isNaN(value.getMilliseconds())) { + val = null; } return val; @@ -1195,12 +1197,7 @@ function toJson(obj, pretty) { if (!isNumber(pretty)) { pretty = pretty ? 2 : null; } - try { - return JSON.stringify(obj, toJsonReplacer, pretty); - } catch (RangeError) { - // In Safari, 'Invalid Date's throw a range error. 'null' is returned in all other browsers. - return 'null'; - } + return JSON.stringify(obj, toJsonReplacer, pretty); } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 326a15bef83e..6babe2360e44 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -2060,7 +2060,7 @@ describe('angular', function() { }); it('should serialize invalid dates to null', function() { - expect(toJson(new Date(1 / 0))).toEqual('null'); + expect(toJson({when: new Date(1 / 0)})).toEqual('{"when":null}'); }); });