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

Commit

Permalink
fix(service): fix an issue with default interpolator and expressions
Browse files Browse the repository at this point in the history
fix default interpolation of {{ value + value }} when "escaped" is used as sanitize value strategy

PR #884
  • Loading branch information
felixmosh authored and knalli committed Feb 8, 2015
1 parent 570617c commit 75b7381
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/service/default-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ angular.module('pascalprecht.translate').factory('$translateDefaultInterpolation
var result = {};
for (var key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
result[key] = angular.element('<div></div>').text(params[key]).html();
if (angular.isNumber(params[key])) {
result[key] = params[key];
} else {
result[key] = angular.element('<div></div>').text(params[key]).html();
}
}
}
return result;
Expand Down
21 changes: 19 additions & 2 deletions test/unit/service/default-interpolation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,28 @@ describe('pascalprecht.translate', function () {
});

it('should replace interpolateParams with concrete values', function () {
expect($translateDefaultInterpolation.interpolate('Foo bar {{value}}', { value: 5 })).toEqual('Foo bar 5');
expect($translateDefaultInterpolation.interpolate('Foo bar {{value}}', {value: 5})).toEqual('Foo bar 5');
});

it('should evaluate interpolateParams with concrete values the right way', function () {
expect($translateDefaultInterpolation.interpolate('Foo bar {{ value + value }}', { value: 5 })).toEqual('Foo bar 10');
expect($translateDefaultInterpolation.interpolate('Foo bar {{ value + value }}', {value: 5})).toEqual('Foo bar 10');
});

it('should evaluate interpolateParams with concrete values the right way when in escaped mode', function () {
$translateDefaultInterpolation.useSanitizeValueStrategy('escaped');
expect($translateDefaultInterpolation.interpolate('Foo bar {{ value + value }}', {value: 5})).toEqual('Foo bar 10');
});

it('should evaluate interpolateParams with values that contains html tags', function () {
$translateDefaultInterpolation.useSanitizeValueStrategy('escaped');

expect($translateDefaultInterpolation.interpolate('Foo bar {{ value + value }} with {{name}}', {value: 5, name: 'CaTz <xss>'})).toEqual('Foo bar 10 with CaTz &lt;xss&gt;');
});
});

describe('$translateDefaultInterpolation#useSanitizeValueStrategy', function () {
it('should be a function ', function () {
expect(typeof $translateDefaultInterpolation.useSanitizeValueStrategy).toBe('function');
});
});
});

0 comments on commit 75b7381

Please sign in to comment.