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

Commit

Permalink
feat(sanitize): Allow sanitize strategy defined as a service
Browse files Browse the repository at this point in the history
 This change allows to define a strategy like this one:

 ```js
 app
 config(function($translateProvider, $translateSanitizationProvider) {
   $translateSanitizationProvider.addStrategy('myStrategy', 'myStrategyService');
   $translateProvider.useSanitizeValueStrategy('myStrategy');
 })
 .factory('myStrategyService', function ($sce) {
   return function (value, mode) {
     if (mode === 'text') {
       return 'something in text mode';
     }
     return 'something in params mode'';
   };
 })
 ```
  • Loading branch information
knalli committed Sep 15, 2015
1 parent c4b16d3 commit 8a6cc07
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/service/sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,24 @@ function $translateSanitizationProvider () {
*/
this.$get = function ($injector, $log) {

var cachedStrategyMap = {};

var applyStrategies = function (value, mode, selectedStrategies) {
angular.forEach(selectedStrategies, function (selectedStrategy) {
if (angular.isFunction(selectedStrategy)) {
value = selectedStrategy(value, mode);
} else if (angular.isFunction(strategies[selectedStrategy])) {
value = strategies[selectedStrategy](value, mode);
} else if (angular.isString(strategies[selectedStrategy])) {
if (!cachedStrategyMap[strategies[selectedStrategy]]) {
try {
cachedStrategyMap[strategies[selectedStrategy]] = $injector.get(strategies[selectedStrategy]);
} catch (e) {
cachedStrategyMap[strategies[selectedStrategy]] = function() {};
throw new Error('pascalprecht.translate.$translateSanitization: Unknown sanitization strategy: \'' + selectedStrategy + '\'');
}
}
value = cachedStrategyMap[strategies[selectedStrategy]](value, mode);
} else {
throw new Error('pascalprecht.translate.$translateSanitization: Unknown sanitization strategy: \'' + selectedStrategy + '\'');
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/service/sanitization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,35 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translateSanitization', function () {
var $translateSanitization;

beforeEach(module('ngSanitize'));
beforeEach(module('pascalprecht.translate', function ($provide, $translateSanitizationProvider) {
$provide.factory('mySanitizeService', function () {
return function (value, mode) {
return '1' + value + '2' + mode;
};
});
$translateSanitizationProvider.addStrategy('mySanitize', 'mySanitizeService');
$translateSanitizationProvider.useStrategy('mySanitize');
}));
beforeEach(inject(function (_$translateSanitization_) {
$translateSanitization = _$translateSanitization_;
}));

describe('should allow specifying a different strategy which is the alias of an existing service', function () {
it('for text', function () {
expect($translateSanitization.sanitize('Donald <strong>Duck</strong>', 'text')).toEqual('1Donald <strong>Duck</strong>2text');
});

it('for params', function () {
expect($translateSanitization.sanitize('Donald <strong>Duck</strong>', 'params')).toEqual('1Donald <strong>Duck</strong>2params');
});
});

});

describe('$translateSanitization#sanitize without ngSanitize', function () {
var $translateSanitization;

Expand Down

0 comments on commit 8a6cc07

Please sign in to comment.