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

Commit

Permalink
feat(directive): introduce attr translate-sanitize-strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber authored and knalli committed Jul 17, 2017
1 parent 4a2c3ab commit 41c7e1f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/directive/translate-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module('pascalprecht.translate')
*
* @param {string=} translate-attr Object literal mapping attributes to translation ids.
* @param {string=} translate-values Values to pass into the translation ids. Can be passed as object literal string.
* @param {string=} translate-sanitize-strategy defines locally sanitize strategy
*
* @example
<example module="ngView">
Expand Down Expand Up @@ -67,6 +68,7 @@ function translateAttrDirective($translate, $rootScope) {

var translateAttr,
translateValues,
translateSanitizeStrategy,
previousAttributes = {};

// Main update translations function
Expand All @@ -81,7 +83,7 @@ function translateAttrDirective($translate, $rootScope) {
if (scope.translateNamespace && translationId.charAt(0) === '.') {
translationId = scope.translateNamespace + translationId;
}
$translate(translationId, translateValues, attr.translateInterpolation, undefined, scope.translateLanguage)
$translate(translationId, translateValues, attr.translateInterpolation, undefined, scope.translateLanguage, translateSanitizeStrategy)
.then(function (translation) {
element.attr(attributeName, translation);
}, function (translationId) {
Expand Down Expand Up @@ -112,6 +114,13 @@ function translateAttrDirective($translate, $rootScope) {
function (newValue) { translateValues = newValue; },
updateTranslations
);
// Watch for sanitize strategy changes
watchAttribute(
scope,
attr.translateSanitizeStrategy,
function (newValue) { translateSanitizeStrategy = newValue; },
updateTranslations
);

if (attr.translateValues) {
scope.$watch(attr.translateValues, updateTranslations, true);
Expand Down
13 changes: 12 additions & 1 deletion src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ angular.module('pascalprecht.translate')
* @param {string=} translate-values Values to pass into translation id. Can be passed as object literal string or interpolated object.
* @param {string=} translate-attr-ATTR translate Translation id and put it into ATTR attribute.
* @param {string=} translate-default will be used unless translation was successful
* @param {string=} translate-sanitize-strategy defines locally sanitize strategy
* @param {boolean=} translate-compile (default true if present) defines locally activation of {@link pascalprecht.translate.$translateProvider#methods_usePostCompiling}
* @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}
*
Expand Down Expand Up @@ -123,6 +124,9 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
var translateInterpolation = (tAttr.translateInterpolation) ?
tAttr.translateInterpolation : undefined;

var translateSanitizeStrategyExist = (tAttr.translateSanitizeStrategy) ?
tAttr.translateSanitizeStrategy : undefined;

var translateValueExist = tElement[0].outerHTML.match(/translate-value-+/i);

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

if (translateSanitizeStrategyExist) {
iAttr.$observe('translateSanitizeStrategy', function (value) {
scope.sanitizeStrategy = $parse(value)(scope.$parent);
updateTranslations();
});
}

if (translateValuesExist) {
iAttr.$observe('translateValues', function (interpolateParams) {
if (interpolateParams) {
Expand Down Expand Up @@ -267,7 +278,7 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
translationId = translateNamespace + translationId;
}

$translate(translationId, interpolateParams, translateInterpolation, defaultTranslationText, scope.translateLanguage)
$translate(translationId, interpolateParams, translateInterpolation, defaultTranslationText, scope.translateLanguage, scope.sanitizeStrategy)
.then(function (translation) {
applyTranslation(translation, scope, true, translateAttr);
}, function (translationId) {
Expand Down
37 changes: 37 additions & 0 deletions test/unit/directive/translate-attr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,42 @@ describe('pascalprecht.translate', function () {
});
});

describe('translate-sanitize-strategy', function () {
it('should sanitize with default strategy', function () {
$rootScope.attributes = {'attr' : 'with_value'};
$rootScope.values = {'value' : '<b>test</b>'};

element = $compile('<div translate-attr="attributes" translate-values="values" />')($rootScope);

$rootScope.$digest();
expect(element.attr('attr')).toEqual('This is <b>test</b>');
});

it('should use overridden strategy', function () {
$rootScope.attributes = {'attr' : 'with_value'};
$rootScope.values = {'value' : '<b>test</b>'};
$rootScope.strategy = 'escaped';

element = $compile('<div translate-attr="attributes" translate-values="values" translate-sanitize-strategy="strategy" />')($rootScope);

$rootScope.$digest();
expect(element.attr('attr')).toEqual('This is &lt;b&gt;test&lt;/b&gt;');
});

it('should support changed strategy', function () {
$rootScope.attributes = {'attr' : 'with_value'};
$rootScope.values = {'value' : '<b>test</b>'};
$rootScope.strategy = 'escaped';

element = $compile('<div translate-attr="attributes" translate-values="values" translate-sanitize-strategy="strategy" />')($rootScope);

$rootScope.$digest();
expect(element.attr('attr')).toEqual('This is &lt;b&gt;test&lt;/b&gt;');

$rootScope.strategy = null;
$rootScope.$digest();
expect(element.attr('attr')).toEqual('This is <b>test</b>');
});
});
});
});
27 changes: 27 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,33 @@ describe('pascalprecht.translate', function () {
});
});

describe('translate-sanitize-strategy extension', function () {

var $rootScope, $compile, element;

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

$translateProvider.translations('en', {
'hacking' : '{{v}}'
});

$translateProvider.preferredLanguage('en');
}));

beforeEach(inject(function (_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
}));

it('should use overridden strategy', function () {
element = $compile('<p translate="hacking" translate-values="{v: \'<u>test</u>\'}" translate-sanitize-strategy="\'escaped\'"></p>')($rootScope);
$rootScope.$digest();
// Verify content is escaped.
expect(element.text()).toEqual('<u>test</u>'); // possible because text
expect(element.html()).toEqual('&lt;u&gt;test&lt;/u&gt;');
});
});

describe('translate-compile extension (globally disabled)', function () {

var $rootScope, $compile, element, $translate;
Expand Down

0 comments on commit 41c7e1f

Please sign in to comment.