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

Commit

Permalink
feat(): add option to html escape all values
Browse files Browse the repository at this point in the history
In addition to fe94c1f the internal structure of escaping was replaced with a strategy pattern. That allows different kinds of sanitizing including standard html escaping.
  • Loading branch information
knalli committed Nov 24, 2013
1 parent a9c7db0 commit e042c44
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
34 changes: 23 additions & 11 deletions src/service/default-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ angular.module('pascalprecht.translate').factory('$translateDefaultInterpolation
var $translateInterpolator = {},
$locale,
$identifier = 'default',
$useSanitizedValues = false;

var sanitizeParams = function (params) {
var result = {};
for (var key in params) {
if (params.hasOwnProperty(key)) {
result[key] = angular.element('<div></div>').text(params[key]).html();
$sanitizeValueStrategy = null,
// map of all sanitize strategies
sanitizeValueStrategies = {
escaped: function (params) {
var result = {};
for (var key in params) {
if (params.hasOwnProperty(key)) {
result[key] = angular.element('<div></div>').text(params[key]).html();
}
}
return result;
}
return result;
};

var sanitizeParams = function (params) {
var result;
if (typeof sanitizeValueStrategies[$sanitizeValueStrategy] === 'function') {
result = sanitizeValueStrategies[$sanitizeValueStrategy](params);
} else {
result = params;
}
return result;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateDefaultInterpolation#setLocale
Expand Down Expand Up @@ -53,8 +65,8 @@ angular.module('pascalprecht.translate').factory('$translateDefaultInterpolation
return $identifier;
};

$translateInterpolator.useSanitizedValues = function (value) {
$useSanitizedValues = (value === true);
$translateInterpolator.useSanitizeValueStrategy = function (value) {
$sanitizeValueStrategy = value;
return this;
};

Expand All @@ -70,7 +82,7 @@ angular.module('pascalprecht.translate').factory('$translateDefaultInterpolation
* @returns {string} interpolated string.
*/
$translateInterpolator.interpolate = function (string, interpolateParams) {
if ($useSanitizedValues) {
if ($sanitizeValueStrategy) {
interpolateParams = sanitizeParams(interpolateParams);
}
return $interpolate(string)(interpolateParams);
Expand Down
14 changes: 7 additions & 7 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$missingTranslationHandlerFactory,
$interpolationFactory,
$interpolatorFactories = [],
$interpolationSanitization = false,
$interpolationSanitizationStrategy = false,
$loaderFactory,
$loaderOptions,
$notFoundIndicatorLeft,
Expand Down Expand Up @@ -175,8 +175,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
return this;
};

this.useSanitizedValues = function (value) {
$interpolationSanitization = value;
this.useSanitizeValueStrategy = function (value) {
$interpolationSanitizationStrategy = value;
return this;
};

Expand Down Expand Up @@ -573,8 +573,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}

// apply additional settings
if (typeof defaultInterpolator.useSanitizedValues === 'function') {
defaultInterpolator.useSanitizedValues($interpolationSanitization);
if (typeof defaultInterpolator.useSanitizeValueStrategy === 'function') {
defaultInterpolator.useSanitizeValueStrategy($interpolationSanitizationStrategy);
}

// if we have additional interpolations that were added via
Expand All @@ -587,8 +587,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
// setting initial locale for each interpolation service
interpolator.setLocale($preferredLanguage || $uses);
// apply additional settings
if (typeof interpolator.useSanitizedValues === 'function') {
interpolator.useSanitizedValues($interpolationSanitization);
if (typeof interpolator.useSanitizeValueStrategy === 'function') {
interpolator.useSanitizeValueStrategy($interpolationSanitizationStrategy);
}
// make'em recognizable through id
interpolatorHashMap[interpolator.getInterpolationIdentifier()] = interpolator;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ describe('pascalprecht.translate', function () {
});

$translateProvider.preferredLanguage('en');
$translateProvider.useSanitizedValues(true);
$translateProvider.useSanitizeValueStrategy('escaped');
}));

beforeEach(inject(function (_$rootScope_, _$compile_) {
Expand Down

0 comments on commit e042c44

Please sign in to comment.