Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(service): avoid sanitizing of functions
Browse files Browse the repository at this point in the history
Fixes #1529
  • Loading branch information
yjaaidi authored and knalli committed Jul 17, 2016
1 parent c0b0809 commit 492d8e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/service/sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ function $translateSanitizationProvider () {

stack.push(value);
angular.forEach(value, function (propertyValue, propertyKey) {

/* Skipping function properties. */
if (angular.isFunction(propertyValue)) {
return;
}

result[propertyKey] = mapInterpolationParameters(propertyValue, iteratee, stack);
});
stack.splice(-1, 1); // remove last
Expand Down
26 changes: 26 additions & 0 deletions test/unit/service/sanitization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ describe('pascalprecht.translate', function () {
expectedText = text;
expect($translateSanitization.sanitize(text, 'text')).toEqual(expectedText);
});

it('should not escape functions', function () {

var sanitizedUser;
var user = {
firstName: '<b>Foo</b>',
save: angular.noop
};

var spyAngularElementReturnValue = jasmine.createSpyObj('angularElement', ['html', 'off', 'text', 'data']);

spyOn(angular, 'element').and.returnValue(spyAngularElementReturnValue);

/* Sanitized user should not have a save property. */

sanitizedUser = $translateSanitization.sanitize({user: user}, 'params').user;

expect('firstName' in sanitizedUser).toEqual(true);
expect('save' in sanitizedUser).toEqual(false);

/* `user.save` should not be called. */
expect(spyAngularElementReturnValue.text.calls.count()).toEqual(1);
expect(spyAngularElementReturnValue.text.calls.argsFor(0)).toEqual(['<b>Foo</b>']);

});

});

describe('with the (legacy, deprecated) escaped strategy', function () {
Expand Down

0 comments on commit 492d8e5

Please sign in to comment.