From 6eb9b01f27f2ca62953e3bb7bc9cd32fc752206d Mon Sep 17 00:00:00 2001 From: Raphael Jamet <rjamet@google.com> Date: Tue, 31 May 2016 14:29:09 +0200 Subject: [PATCH 1/2] fix(ngRoute): Don't deep-copy the route object, just copy it instead. This seems to fix #14478. No need for a deep copy, just a plain one is enough as ngRoute does not modify the fields in the route object. --- src/ngRoute/route.js | 2 +- test/ngRoute/routeSpec.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index cf5256c3dd51..1c66141a6e34 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -160,7 +160,7 @@ function $RouteProvider() { */ this.when = function(path, route) { //copy original route object to preserve params inherited from proto chain - var routeCopy = angular.copy(route); + var routeCopy = route; if (angular.isUndefined(routeCopy.reloadOnSearch)) { routeCopy.reloadOnSearch = true; } diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js index aa1d283fda83..68d8aac58696 100644 --- a/test/ngRoute/routeSpec.js +++ b/test/ngRoute/routeSpec.js @@ -1297,6 +1297,39 @@ describe('$route', function() { }); }); + + it('should not deep-copy or alter route fields', function() { + // Object.seal is used to block future modifications of the object: we do not want + // to alter the objects passed as parameters. + var canaryObject = Object.seal({}); + + // No point faking a full template, use the syntax from above + var canaryTemplate = function() { + return "<p>hi</p>"; + }; + canaryTemplate.someField = canaryObject; + Object.seal(canaryTemplate); // sealed as well, just in case + + module(function($routeProvider) { + $routeProvider.when('/foo', + { + template: canaryTemplate, + myObject: canaryObject, + }); + }); + + inject(function($route, $location, $rootScope) { + $location.path('/foo'); + $rootScope.$digest(); + + // Copies of the canaries won't be their originals. + expect($route.current.myObject).toBe(canaryObject); + expect($route.current.template).toBe(canaryTemplate); + expect($route.current.template.someField).toBe(canaryObject); + }); + }); + + describe('reload', function() { var $location; var $log; From f5275783a0e024a27fe29db27ebff88225fc49bf Mon Sep 17 00:00:00 2001 From: Raphael Jamet <rjamet@google.com> Date: Tue, 31 May 2016 14:43:11 +0200 Subject: [PATCH 2/2] fix(ngRoute): jshint complaints Forgot a comma. --- test/ngRoute/routeSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js index 68d8aac58696..d9e20297d85d 100644 --- a/test/ngRoute/routeSpec.js +++ b/test/ngRoute/routeSpec.js @@ -1314,7 +1314,7 @@ describe('$route', function() { $routeProvider.when('/foo', { template: canaryTemplate, - myObject: canaryObject, + myObject: canaryObject }); });