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

Commit

Permalink
fix(directive): newlines before/after translation ids should be ignored
Browse files Browse the repository at this point in the history
Fixes #909
  • Loading branch information
knalli committed Feb 8, 2015
1 parent b0b7716 commit 8dcf3e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ angular.module('pascalprecht.translate')
*/
.directive('translate', ['$translate', '$q', '$interpolate', '$compile', '$parse', '$rootScope', function ($translate, $q, $interpolate, $compile, $parse, $rootScope) {

/**
* @name trim
* @private
*
* @description
* trim polyfill
*
* @returns {string} The string stripped of whitespace from both ends
*/
var trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};

return {
restrict: 'AE',
scope: true,
Expand Down Expand Up @@ -124,7 +137,7 @@ angular.module('pascalprecht.translate')

if (angular.equals(translationId , '') || !angular.isDefined(translationId)) {
// Resolve translation id by inner html if required
var interpolateMatches = iElement.text().match(interpolateRegExp);
var interpolateMatches = trim.apply(iElement.text()).match(interpolateRegExp);
// Interpolate translation id if required
if (angular.isArray(interpolateMatches)) {
scope.preText = interpolateMatches[1];
Expand Down
16 changes: 16 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ describe('pascalprecht.translate', function () {
expect(element.text()).toBe('TEXT');
});

it('should return translation id if translation doesn\'t exist (innerHTML with newlines)', function () {
element = $compile("<div translate>\nTEXT\n</div>")($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('TEXT');
});

it('should return translation if translation id exist', function () {
element = $compile('<div translate>TRANSLATION_ID</div>')($rootScope);
$rootScope.$digest();
Expand All @@ -103,6 +109,16 @@ describe('pascalprecht.translate', function () {
expect(element.text()).toBe('');
});

it('should return translation if translation id exist (innerHTML with newlines)', function () {
element = $compile("<div translate>\nTRANSLATION_ID\n</div>")($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');

element = $compile("<div translate>\nBLANK_VALUE\n</div>")($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});

it('should return translation id if translation doesn\'t exist and is passed as interpolation', function () {
$rootScope.translationId = 'TEXT';
element = $compile('<div translate>{{translationId}}</div>')($rootScope);
Expand Down

0 comments on commit 8dcf3e2

Please sign in to comment.