Skip to content

Commit 024b5fc

Browse files
fix(ngTransclude): use fallback content if only whitespace is provided
If the transcluded content is only whitespace then we should use the fallback content instead. This allows more flexibility in formatting your HTML. Closes angular#15077 BREAKING CHANGE: Previously whitespace only transclusion would be treated as the transclusion being "not empty", which meant that fallback content was not used in that case. Now if you only provide whitespace as the transclusion content, it will be assumed to be empty and the fallback content will be used instead. If you really do want whitespace then you can force it to be used by adding a comment to the whitespace.
1 parent 1547c75 commit 024b5fc

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/ng/directive/ngTransclude.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*
1414
* If the transcluded content is not empty (i.e. contains one or more DOM nodes, including whitespace text nodes), any existing
1515
* content of this element will be removed before the transcluded content is inserted.
16-
* If the transcluded content is empty, the existing content is left intact. This lets you provide fallback content in the case
17-
* that no transcluded content is provided.
16+
* If the transcluded content is empty (or only whitespace), the existing content is left intact. This lets you provide fallback
17+
* content in the case that no transcluded content is provided.
1818
*
1919
* @element ANY
2020
*
@@ -195,7 +195,7 @@ var ngTranscludeDirective = ['$compile', function($compile) {
195195
}
196196

197197
function ngTranscludeCloneAttachFn(clone, transcludedScope) {
198-
if (clone.length) {
198+
if (clone.length && notWhitespace(clone)) {
199199
$element.append(clone);
200200
} else {
201201
useFallbackContent();
@@ -212,6 +212,15 @@ var ngTranscludeDirective = ['$compile', function($compile) {
212212
$element.append(clone);
213213
});
214214
}
215+
216+
function notWhitespace(nodes) {
217+
for (var i = 0, ii = nodes.length; i < ii; i++) {
218+
var node = nodes[i];
219+
if (node.nodeType !== NODE_TYPE_TEXT || node.nodeValue.trim()) {
220+
return true;
221+
}
222+
}
223+
}
215224
};
216225
}
217226
};

test/ng/compileSpec.js

+54
Original file line numberDiff line numberDiff line change
@@ -8728,6 +8728,60 @@ describe('$compile', function() {
87288728
});
87298729
});
87308730

8731+
it('should compile and link the fallback content if only whitespace transcluded content is provided', function() {
8732+
var linkSpy = jasmine.createSpy('postlink');
8733+
8734+
module(function() {
8735+
directive('inner', function() {
8736+
return {
8737+
restrict: 'E',
8738+
template: 'old stuff! ',
8739+
link: linkSpy
8740+
};
8741+
});
8742+
8743+
directive('trans', function() {
8744+
return {
8745+
transclude: true,
8746+
template: '<div ng-transclude><inner></inner></div>'
8747+
};
8748+
});
8749+
});
8750+
inject(function(log, $rootScope, $compile) {
8751+
element = $compile('<div trans>\n \n</div>')($rootScope);
8752+
$rootScope.$apply();
8753+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude=""><inner>old stuff! </inner></div>');
8754+
expect(linkSpy).toHaveBeenCalled();
8755+
});
8756+
});
8757+
8758+
it('should not link the fallback content if only whitespace and comments are provided as transclude content', function() {
8759+
var linkSpy = jasmine.createSpy('postlink');
8760+
8761+
module(function() {
8762+
directive('inner', function() {
8763+
return {
8764+
restrict: 'E',
8765+
template: 'old stuff! ',
8766+
link: linkSpy
8767+
};
8768+
});
8769+
8770+
directive('trans', function() {
8771+
return {
8772+
transclude: true,
8773+
template: '<div ng-transclude><inner></inner></div>'
8774+
};
8775+
});
8776+
});
8777+
inject(function(log, $rootScope, $compile) {
8778+
element = $compile('<div trans>\n<!-- some comment --> \n</div>')($rootScope);
8779+
$rootScope.$apply();
8780+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude="">\n<!-- some comment --> \n</div>');
8781+
expect(linkSpy).not.toHaveBeenCalled();
8782+
});
8783+
});
8784+
87318785
it('should compile and link the fallback content if an optional transclusion slot is not provided', function() {
87328786
var linkSpy = jasmine.createSpy('postlink');
87338787

0 commit comments

Comments
 (0)