10BC0 fix: Allow line breaks in interpolated expressions. · angular-translate/angular-translate@70957a3 · 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 70957a3

Browse files
bobischknalli
authored andcommitted
fix: Allow line breaks in interpolated expressions.
Line breaks can now appear in interpolated AngularJS expressions inside the body or translated elements and inside the value of translate attributes. Closes #1884, #1824 (cherry picked from commit 982980c)
1 parent a14918d commit 70957a3

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/directive/translate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function translateDirective($translate, $interpolate, $compile, $parse, $rootSco
181181
}
182182

183183
if (angular.equals(translationId , '') || !angular.isDefined(translationId)) {
184-
var iElementText = trim.apply(iElement.text());
184+
var iElementText = trim.apply(iElement.text()).replace(/\n/g, ' ');
185185

186186
// Resolve translation id by inner html if required
187187
var interpolateMatches = iElementText.match(interpolateRegExp);

test/unit/directive/translate.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,4 +904,68 @@ describe('pascalprecht.translate', function () {
904904
expect(element.html()).toBe('My content');
905905
});
906906
});
907+
908+
describe('handling newlines in interpolation', function () {
909+
910+
var $compile, $rootScope, element;
911+
912+
beforeEach(module('pascalprecht.translate', function ($translateProvider) {
913+
$translateProvider
914+
.translations('en', {
915+
'TRANSLATION_ID' : 'foo',
916+
'abcfoodef': 'BOGUS?',
917+
'foo': 'FOOGUS?'
918+
})
919+
.preferredLanguage('en');
920+
}));
921+
922+
beforeEach(inject(function (_$compile_, _$rootScope_) {
923+
$compile = _$compile_;
924+
$rootScope = _$rootScope_;
925+
}));
926+
927+
it('should handle newlines embedded in interpolation expression', function () {
928+
$rootScope.translationId = 'TRANSLATION_ID';
929+
element = $compile('<div translate>{{\ntranslationId\n}}</div>')($rootScope);
930+
$rootScope.$digest();
931+
expect(element.text()).toBe('foo');
932+
});
933+
934+
it('should handle newlines embedded in interpolation expression for translate element', function () {
935+
$rootScope.translationId = 'TRANSLATION_ID';
936+
element = $compile('<translate>{{\ntranslationId\n}}</translate>')($rootScope);
937+
$rootScope.$digest();
938+
expect(element.text()).toBe('foo');
939+
});
940+
941+
it('should handle newlines embedded in interpolation expression with surrounding text', function () {
942+
$rootScope.translationId = 'TRANSLATION_ID';
943+
element = $compile('<div translate>abc{{\ntranslationId\n}}def</div>')($rootScope);
944+
$rootScope.$digest();
945+
expect(element.text()).toBe('abcfoodef');
946+
});
947+
948+
it('should handle newlines embedded in and surrounding interpolation expression', function () {
949+
$rootScope.translationId = 'TRANSLATION_ID';
950+
element = $compile('<div translate>abc\n{{\ntranslationId\n}}\ndef</div>')($rootScope);
951+
$rootScope.$digest();
952+
expect(element.text()).toBe('abc foo def');
953+
});
954+
955+
it('should handle newlines embedded in complex interpolation expression', function () {
956+
$rootScope.translationId = 'TRANSLATION_ID';
957+
$rootScope.var1 = 'TRANSLATION';
958+
$rootScope.var2 = 'ID';
959+
element = $compile('<div translate>\nabc{{\n var1 + \n "_" + var2\n }}def\n </div>')($rootScope);
960+
$rootScope.$digest();
961+
expect(element.text()).toBe('abcfoodef');
962+
});
963+
964+
it('should handle newlines embedded in interpolation expression in attribute', function () {
965+
$rootScope.translationId = 'TRANSLATION_ID';
966+
element = $compile('<div translate="{{\ntranslationId\n}}">...</div>')($rootScope);
967+
$rootScope.$digest();
968+
expect(element.text()).toBe('foo');
969+
});
970+
});
907971
});

0 commit comments

Comments
 (0)
0