Skip to content

Commit

Permalink
fix(mdCompiler): prevent resolve and locals overwrite
Browse files Browse the repository at this point in the history


Objects passed to a function should be considered immutable

 to avoid unexpected side-effects

Closes angular#2614
  • Loading branch information
rochdev committed May 2, 2015
1 parent 7d0aeef commit 45e7719
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/core/services/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function mdCompilerService($q, $http, $injector, $compile, $controller, $templat
var template = options.template || '';
var controller = options.controller;
var controllerAs = options.controllerAs;
var resolve = options.resolve || {};
var locals = options.locals || {};
var resolve = angular.copy(options.resolve || {});
var locals = angular.copy(options.locals || {});
var transformTemplate = options.transformTemplate || angular.identity;
var bindToController = options.bindToController;

Expand Down
50 changes: 32 additions & 18 deletions src/core/services/compiler/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,41 @@ describe('$mdCompiler service', function() {
expect(data.element.html()).toBe('hello world');
});

it('resolve and locals should work', function() {
module(function($provide) {
$provide.constant('StrawberryColor', 'red');
});
describe('with resolve and locals options', function() {
var options;

var data = compile({
resolve: {
//Resolve a factory inline
fruit: function($q) {
return $q.when('apple');
beforeEach(function() {
module(function($provide) {
$provide.constant('StrawberryColor', 'red');
});

options = {
resolve: {
//Resolve a factory inline
fruit: function($q) {
return $q.when('apple');
},
//Resolve a DI token's value
color: 'StrawberryColor'
},
//Resolve a DI token's value
color: 'StrawberryColor'
},
locals: {
vegetable: 'carrot'
}
locals: {
vegetable: 'carrot'
}
};
});

it('should work', function() {
var data = compile(options);
expect(data.locals.fruit).toBe('apple');
expect(data.locals.vegetable).toBe('carrot');
expect(data.locals.color).toBe('red');
});

it('should not overwrite the original values', function() {
var clone = angular.copy(options);
compile(options);
expect(options).toEqual(clone);
});
expect(data.locals.fruit).toBe('apple');
expect(data.locals.vegetable).toBe('carrot');
expect(data.locals.color).toBe('red');
});

describe('after link()', function() {
Expand Down

0 comments on commit 45e7719

Please sign in to comment.