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

Commit

Permalink
feat(service): provide for sanitize/escape strategy 3rd argument context
Browse files Browse the repository at this point in the history
Each strategy will be provided `context` which is either service (incl. directive) or filter.
  • Loading branch information
knalli committed Sep 5, 2016
1 parent bcb0f2c commit 8504c60
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/service/default-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ function $translateDefaultInterpolation ($interpolate, $translateSanitization) {
*
* @returns {string} interpolated string.
*/
$translateInterpolator.interpolate = function (value, interpolationParams) {
$translateInterpolator.interpolate = function (value, interpolationParams, context) {
interpolationParams = interpolationParams || {};
interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params');
interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params', undefined, context);

var interpolatedText;
if (angular.isNumber(value)) {
Expand All @@ -84,7 +84,7 @@ function $translateDefaultInterpolation ($interpolate, $translateSanitization) {
} else if (angular.isString(value)) {
// strings must be interpolated (that's the job here)
interpolatedText = $interpolate(value)(interpolationParams);
interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text');
interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text', undefined, context);
} else {
// neither a number or a string, cant interpolate => empty string
interpolatedText = '';
Expand Down
2 changes: 1 addition & 1 deletion src/service/messageformat-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function $translateMessageFormatInterpolation($translateSanitization, $cacheFact
*
* @returns {string} interpolated string.
*/
$translateInterpolator.interpolate = function (string, interpolationParams) {
$translateInterpolator.interpolate = function (string, interpolationParams/*, context*/) {
interpolationParams = interpolationParams || {};
interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params');

Expand Down
27 changes: 16 additions & 11 deletions src/service/sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ function $translateSanitizationProvider () {
*/

strategies = {
sanitize: function (value, mode) {
sanitize: function (value, mode/*, context*/) {
if (mode === 'text') {
value = htmlSanitizeValue(value);
}
return value;
},
escape: function (value, mode) {
escape: function (value, mode/*, context*/) {
if (mode === 'text') {
value = htmlEscapeValue(value);
}
return value;
},
sanitizeParameters: function (value, mode) {
sanitizeParameters: function (value, mode/*, context*/) {
if (mode === 'params') {
value = mapInterpolationParameters(value, htmlSanitizeValue);
}
return value;
},
escapeParameters: function (value, mode) {
escapeParameters: function (value, mode/*, context*/) {
if (mode === 'params') {
value = mapInterpolationParameters(value, htmlEscapeValue);
}
Expand Down Expand Up @@ -142,12 +142,12 @@ function $translateSanitizationProvider () {

var cachedStrategyMap = {};

var applyStrategies = function (value, mode, selectedStrategies) {
var applyStrategies = function (value, mode, context, selectedStrategies) {
angular.forEach(selectedStrategies, function (selectedStrategy) {
if (angular.isFunction(selectedStrategy)) {
value = selectedStrategy(value, mode);
value = selectedStrategy(value, mode, context);
} else if (angular.isFunction(strategies[selectedStrategy])) {
value = strategies[selectedStrategy](value, mode);
value = strategies[selectedStrategy](value, mode, context);
} else if (angular.isString(strategies[selectedStrategy])) {
if (!cachedStrategyMap[strategies[selectedStrategy]]) {
try {
Expand All @@ -157,7 +157,7 @@ function $translateSanitizationProvider () {
throw new Error('pascalprecht.translate.$translateSanitization: Unknown sanitization strategy: \'' + selectedStrategy + '\'');
}
}
value = cachedStrategyMap[strategies[selectedStrategy]](value, mode);
value = cachedStrategyMap[strategies[selectedStrategy]](value, mode, context);
} else {
throw new Error('pascalprecht.translate.$translateSanitization: Unknown sanitization strategy: \'' + selectedStrategy + '\'');
}
Expand Down Expand Up @@ -205,23 +205,28 @@ function $translateSanitizationProvider () {
* @param {string|object} value The value which should be sanitized.
* @param {string} mode The current sanitization mode, either 'params' or 'text'.
* @param {string|StrategyFunction|array} [strategy] Optional custom strategy which should be used instead of the currently selected strategy.
* @param {string} [context] The context of this call: filter, service. Default is service
* @returns {string|object} sanitized value
*/
sanitize: function (value, mode, strategy) {
sanitize: function (value, mode, strategy, context) {
if (!currentStrategy) {
showNoStrategyConfiguredWarning();
}

if (arguments.length < 3) {
if (!strategy && strategy !== null) {
strategy = currentStrategy;
}

if (!strategy) {
return value;
}

if (!context) {
context = 'service';
}

var selectedStrategies = angular.isArray(strategy) ? strategy : [strategy];
return applyStrategies(value, mode, selectedStrategies);
return applyStrategies(value, mode, context, selectedStrategies);
}
};
};
Expand Down
10 changes: 5 additions & 5 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
getFallbackTranslation(langKey, translation.substr(2), interpolateParams, Interpolator)
.then(deferred.resolve, deferred.reject);
} else {
var interpolatedValue = Interpolator.interpolate(translationTable[translationId], interpolateParams);
var interpolatedValue = Interpolator.interpolate(translationTable[translationId], interpolateParams, 'service');
interpolatedValue = applyPostProcessing(translationId, translationTable[translationId], interpolatedValue, interpolateParams, langKey);

deferred.resolve(interpolatedValue);
Expand Down Expand Up @@ -1402,7 +1402,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide

if (translationTable && Object.prototype.hasOwnProperty.call(translationTable, translationId)) {
Interpolator.setLocale(langKey);
result = Interpolator.interpolate(translationTable[translationId], interpolateParams);
result = Interpolator.interpolate(translationTable[translationId], interpolateParams, 'filter');
result = applyPostProcessing(translationId, translationTable[translationId], result, interpolateParams, langKey);
if (result.substr(0, 2) === '@:') {
return getFallbackTranslationInstant(langKey, result.substr(2), interpolateParams, Interpolator);
Expand Down Expand Up @@ -1558,7 +1558,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
.then(deferred.resolve, deferred.reject);
} else {
//
var resolvedTranslation = Interpolator.interpolate(translation, interpolateParams);
var resolvedTranslation = Interpolator.interpolate(translation, interpolateParams, 'service');
resolvedTranslation = applyPostProcessing(translationId, translation, resolvedTranslation, interpolateParams, uses);
deferred.resolve(resolvedTranslation);
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
if (translation.substr(0, 2) === '@:') {
result = determineTranslationInstant(translation.substr(2), interpolateParams, interpolationId, uses);
} else {
result = Interpolator.interpolate(translation, interpolateParams);
result = Interpolator.interpolate(translation, interpolateParams, 'filter');
result = applyPostProcessing(translationId, translation, result, interpolateParams, uses);
}
} else {
Expand Down Expand Up @@ -2177,7 +2177,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
result = applyNotFoundIndicators(translationId);
} else {
// Return translation of default interpolator if not found anything.
result = defaultInterpolator.interpolate(translationId, interpolateParams);
result = defaultInterpolator.interpolate(translationId, interpolateParams, 'filter');
if ($missingTranslationHandlerFactory && !pendingLoader) {
result = translateByHandler(translationId, interpolateParams);
}
Expand Down
8 changes: 4 additions & 4 deletions test/unit/service/default-interpolation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('pascalprecht.translate', function () {
spyOn($translateSanitization, 'sanitize').and.callThrough();

expect($translateDefaultInterpolation.interpolate(text, params)).toBe(sanitizedText);
expect($translateSanitization.sanitize).toHaveBeenCalledWith(params, 'params');
expect($translateSanitization.sanitize).toHaveBeenCalledWith(params, 'params', undefined, undefined);
}));

it('should sanitize the interpolation params', inject(function ($translateSanitization) {
Expand All @@ -96,7 +96,7 @@ describe('pascalprecht.translate', function () {
$translateDefaultInterpolation.useSanitizeValueStrategy('escapeParameters');

expect($translateDefaultInterpolation.interpolate(text, params)).toBe(sanitizedText);
expect($translateSanitization.sanitize).toHaveBeenCalledWith(params, 'params');
expect($translateSanitization.sanitize).toHaveBeenCalledWith(params, 'params', undefined, undefined);
}));

it('should not sanitize the interpolation params (defaults)', inject(function ($translateSanitization) {
Expand All @@ -108,7 +108,7 @@ describe('pascalprecht.translate', function () {
spyOn($translateSanitization, 'sanitize').and.callThrough();

expect($translateDefaultInterpolation.interpolate(text, params)).toBe(sanitizedText);
expect($translateSanitization.sanitize).toHaveBeenCalledWith(interpolatedText, 'text');
expect($translateSanitization.sanitize).toHaveBeenCalledWith(interpolatedText, 'text', undefined, undefined);
}));

it('should sanitize the interpolation params', inject(function ($translateSanitization) {
Expand All @@ -121,7 +121,7 @@ describe('pascalprecht.translate', function () {
$translateDefaultInterpolation.useSanitizeValueStrategy('escape');

expect($translateDefaultInterpolation.interpolate(text, params)).toBe(sanitizedText);
expect($translateSanitization.sanitize).toHaveBeenCalledWith(interpolatedText, 'text');
expect($translateSanitization.sanitize).toHaveBeenCalledWith(interpolatedText, 'text', undefined, undefined);
}));

it('should ignore a date param', inject(function ($translateSanitization) {
Expand Down

0 comments on commit 8504c60

Please sign in to comment.