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

Commit

Permalink
feat(service): introduce new sanitize strategies: sce/sceParameters
Browse files Browse the repository at this point in the history
Both `sce` and `sceParameters` are based on `$sce.trustAsHtml()`.

Relates #1101
  • Loading branch information
knalli committed Sep 5, 2016
1 parent 8504c60 commit 1624df5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/service/sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function $translateSanitizationProvider () {
'use strict';

var $sanitize,
$sce,
currentStrategy = null, // TODO change to either 'sanitize', 'escape' or ['sanitize', 'escapeParameters'] in 3.0.
hasConfiguredStrategy = false,
hasShownNoStrategyConfiguredWarning = false,
Expand Down Expand Up @@ -72,6 +73,23 @@ function $translateSanitizationProvider () {
value = mapInterpolationParameters(value, htmlEscapeValue);
}
return value;
},
sce: function (value, mode, context) {
if (mode === 'text') {
value = htmlTrustValue(value);
} else if (mode === 'params') {
if (context !== 'filter') {
// do html escape in filter context #1101
value = mapInterpolationParameters(value, htmlEscapeValue);
}
}
return value;
},
sceParameters: function (value, mode/*, context*/) {
if (mode === 'params') {
value = mapInterpolationParameters(value, htmlTrustValue);
}
return value;
}
};
// Support legacy strategy name 'escaped' for backwards compatibility.
Expand Down Expand Up @@ -176,6 +194,9 @@ function $translateSanitizationProvider () {
if ($injector.has('$sanitize')) {
$sanitize = $injector.get('$sanitize');
}
if ($injector.has('$sce')) {
$sce = $injector.get('$sce');
}

return {
/**
Expand Down Expand Up @@ -244,6 +265,13 @@ function $translateSanitizationProvider () {
return $sanitize(value);
};

var htmlTrustValue = function (value) {
if (!$sce) {
throw new Error('pascalprecht.translate.$translateSanitization: Error cannot find $sce service.');
}
return $sce.trustAsHtml(value);
};

var mapInterpolationParameters = function (value, iteratee, stack) {
if (angular.isDate(value)) {
return value;
Expand Down

0 comments on commit 1624df5

Please sign in to comment.