Skip to content

Commit 6fa6e2a

Browse files
Added unit test for empty string
1 parent 732850a commit 6fa6e2a

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

dist/angular-gettext.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
177177
post: function (scope, element, attrs) {
178178
var countFn = $parse(attrs.translateN);
179179
var pluralScope = null;
180+
var linking = true;
180181

181182
function update() {
182183
// Fetch correct translated string.
@@ -189,11 +190,27 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
189190
translated = gettextCatalog.getString(msgid, null, translateContext);
190191
}
191192

193+
var oldContents = element.contents();
194+
195+
// if (!oldContents.length){
196+
// $compile(oldContents)(scope);
197+
// return;
198+
// }
199+
200+
// Avoid redundant swaps
201+
if (translated === oldContents.html()){
202+
// Take care of unlinked content
203+
if (linking){
204+
$compile(oldContents)(scope);
205+
}
206+
return;
207+
}
208+
192209
// Swap in the translation
193210
var newWrapper = angular.element('<span>' + translated + '</span>');
194211
$compile(newWrapper.contents())(scope);
195-
var oldContents = element.contents();
196212
var newContents = newWrapper.contents();
213+
197214
$animate.enter(newContents, element);
198215
$animate.leave(oldContents);
199216
}
@@ -205,6 +222,7 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
205222
scope.$on('gettextLanguageChanged', update);
206223

207224
update();
225+
linking = false;
208226
}
209227
};
210228
}

dist/angular-gettext.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
5959

6060
var oldContents = element.contents();
6161

62-
if(!oldContents){
63-
return;
64-
}
65-
6662
// Avoid redundant swaps
6763
if (translated === oldContents.html()){
6864
// Take care of unlinked content
@@ -77,13 +73,8 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
7773
$compile(newWrapper.contents())(scope);
7874
var newContents = newWrapper.contents();
7975

80-
// No animation is required before anything shows
81-
if (linking){
82-
oldContents.replaceWith(newContents);
83-
} else {
84-
$animate.enter(newContents, element);
85-
$animate.leave(oldContents);
86-
}
76+
$animate.enter(newContents, element);
77+
$animate.leave(oldContents);
8778
}
8879

8980
if (attrs.translateN) {

test/unit/directive.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ describe("Directive", function () {
2020
});
2121
}));
2222

23+
it("Should work on empty strings", function () {
24+
var el = $compile("<div><h1 translate></h1></div>")($rootScope);
25+
$rootScope.$digest();
26+
assert.equal(el.text(), "");
27+
});
28+
2329
it("Should return string unchanged when no translation is available", function () {
2430
var el = $compile("<div><h1 translate>Hello!</h1></div>")($rootScope);
2531
$rootScope.$digest();

0 commit comments

Comments
 (0)