E57C feat(directive): introduce attr translate-sanitize-strategy · angular-translate/angular-translate@41c7e1f · GitHub
[go: up one dir, main page]

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

Commit 41c7e1f

Browse files
Anberknalli
authored andcommitted
feat(directive): introduce attr translate-sanitize-strategy
1 parent 4a2c3ab commit 41c7e1f

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

src/directive/translate-attr.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ angular.module('pascalprecht.translate')
1111
*
1212
* @param {string=} translate-attr Object literal mapping attributes to translation ids.
1313
* @param {string=} translate-values Values to pass into the translation ids. Can be passed as object literal string.
14+
* @param {string=} translate-sanitize-strategy defines locally sanitize strategy
1415
*
1516
* @example
1617
<example module="ngView">
@@ -67,6 +68,7 @@ function translateAttrDirective($translate, $rootScope) {
6768

6869
var translateAttr,
6970
translateValues,
71+
translateSanitizeStrategy,
7072
previousAttributes = {};
7173

7274
// Main update translations function
@@ -81,7 +83,7 @@ function translateAttrDirective($translate, $rootScope) {
8183
if (scope.translateNamespace && translationId.charAt(0) === '.') {
8284
translationId = scope.translateNamespace + translationId;
8385
}
84-
$translate(translationId, translateValues, attr.translateInterpolation, undefined, scope.translateLanguage)
86+
$translate(translationId, translateValues, attr.translateInterpolation, undefined, scope.translateLanguage, translateSanitizeStrategy)
8587
.then(function (translation) {
8688
element.attr(attributeName, translation);
8789
}, function (translationId) {
@@ -112,6 +114,13 @@ function translateAttrDirective($translate, $rootScope) {
112114
function (newValue) { translateValues = newValue; },
113115
updateTranslations
114116
);
117+
// Watch for sanitize strategy changes
118+
watchAttribute(
119+
scope,
120+
attr.translateSanitizeStrategy,
121+
function (newValue) { translateSanitizeStrategy = newValue; },
122+
updateTranslations
123+
);
115124

116125
if (attr.translateValues) {
117126
scope.$watch(attr.translateValues, updateTranslations, true);

src/directive/translate.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ angular.module('pascalprecht.translate')
1717
* @param {string=} translate-values Values to pass into translation id. Can be passed as object literal string or interpolated object.
1818
* @param {string=} translate-attr-ATTR translate Translation id and put it into ATTR attribute.
1919
* @param {string=} translate-default will be used unless translation was successful
20+
* @param {string=} translate-sanitize-strategy defines locally sanitize strategy
2021
* @param {boolean=} translate-compile (default true if present) defines locally activation of {@link pascalprecht.translate.$translateProvider#methods_usePostCompiling}
2122
* @param {boolean=} translate-keep-content (default true if present) defines that in case a KEY could not be translated, that the existing content is left in the innerHTML}
2223
*
@@ -123,6 +124,9 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
123124
var translateInterpolation = (tAttr.translateInterpolation) ?
124125
tAttr.translateInterpolation : undefined;
125126

127+
var translateSanitizeStrategyExist = (tAttr.translateSanitizeStrategy) ?
128+
tAttr.translateSanitizeStrategy : undefined;
129+
126130
var translateValueExist = tElement[0].outerHTML.match(/translate-value-+/i);
127131

128132
var interpolateRegExp = '^(.*)(' + $interpolate.startSymbol() + '.*' + $interpolate.endSymbol() + ')(.*)',
@@ -226,6 +230,13 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
226230
updateTranslations();
227231
});
228232

233+
if (translateSanitizeStrategyExist) {
234+
iAttr.$observe('translateSanitizeStrategy', function (value) {
235+
scope.sanitizeStrategy = $parse(value)(scope.$parent);
236+
updateTranslations();
237+
});
238+
}
239+
229240
if (translateValuesExist) {
230241
iAttr.$observe('translateValues', function (interpolateParams) {
231242
if (interpolateParams) {
@@ -267,7 +278,7 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
267278
translationId = translateNamespace + translationId;
268279
}
269280

270-
$translate(translationId, interpolateParams, translateInterpolation, defaultTranslationText, scope.translateLanguage)
281+
$translate(translationId, interpolateParams, translateInterpolation, defaultTranslationText, scope.translateLanguage, scope.sanitizeStrategy)
271282
.then(function (translation) {
272283
applyTranslation(translation, scope, true, translateAttr);
273284
}, function (translationId) {

test/unit/directive/translate-attr.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,42 @@ describe('pascalprecht.translate', function () {
172172
});
173173
});
174174

175+
describe('translate-sanitize-strategy', function () {
176+
it('should sanitize with default strategy', function () {
177+
$rootScope.attributes = {'attr' : 'with_value'};
178+
$rootScope.values = {'value' : '<b>test</b>'};
179+
180+
element = $compile('<div translate-attr="attributes" translate-values="values" />')($rootScope);
181+
182+
$rootScope.$digest();
183+
expect(element.attr('attr')).toEqual('This is <b>test</b>');
184+
});
185+
186+
it('should use overridden strategy', function () {
187+
$rootScope.attributes = {'attr' : 'with_value'};
188+
$rootScope.values = {'value' : '<b>test</b>'};
189+
$rootScope.strategy = 'escaped';
190+
191+
element = $compile('<div translate-attr="attributes" translate-values="values" translate-sanitize-strategy="strategy" />')($rootScope);
192+
193+
$rootScope.$digest();
194+
expect(element.attr('attr')).toEqual('This is &lt;b&gt;test&lt;/b&gt;');
195+
});
196+
197+
it('should support changed strategy', function () {
198+
$rootScope.attributes = {'attr' : 'with_value'};
199+
$rootScope.values = {'value' : '<b>test</b>'};
200+
$rootScope.strategy = 'escaped';
201+
202+
element = $compile('<div translate-attr="attributes" translate-values="values" translate-sanitize-strategy="strategy" />')($rootScope);
203+
204+
$rootScope.$digest();
205+
expect(element.attr('attr')).toEqual('This is &lt;b&gt;test&lt;/b&gt;');
206+
207+
$rootScope.strategy = null;
208+
$rootScope.$digest();
209+
expect(element.attr('attr')).toEqual('This is <b>test</b>');
210+
});
211+
});
175212
});
176213
});

test/unit/directive/translate.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,33 @@ describe('pascalprecht.translate', function () {
559559
});
560560
});
561561

562+
describe('translate-sanitize-strategy extension', function () {
563+
564+
var $rootScope, $compile, element;
565+
566+
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
567+
568+
$translateProvider.translations('en', {
569+
'hacking' : '{{v}}'
570+
});
571< B451 /td>+
572+
$translateProvider.preferredLanguage('en');
573+
}));
574+
575+
beforeEach(inject(function (_$rootScope_, _$compile_) {
576+
$rootScope = _$rootScope_;
577+
$compile = _$compile_;
578+
}));
579+
580+
it('should use overridden strategy', function () {
581+
element = $compile('<p translate="hacking" translate-values="{v: \'<u>test</u>\'}" translate-sanitize-strategy="\'escaped\'"></p>')($rootScope);
582+
$rootScope.$digest();
583+
// Verify content is escaped.
584+
expect(element.text()).toEqual('<u>test</u>'); // possible because text
585+
expect(element.html()).toEqual('&lt;u&gt;test&lt;/u&gt;');
586+
});
587+
});
588+
562589
describe('translate-compile extension (globally disabled)', function () {
563590

564591
var $rootScope, $compile, element, $translate;

0 commit comments

Comments
 (0)
0