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

Commit

Permalink
fix(ngTransclude): use fallback content if only whitespace is provided
Browse files Browse the repository at this point in the history
If the transcluded content is only whitespace then we should use the
fallback content instead. This allows more flexibility in formatting
your HTML.

Closes #15077
Closes #15140

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.
  • Loading branch information
petebacondarwin committed Sep 15, 2016
1 parent 1547c75 commit 32aa7e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/ng/directive/ngTransclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*
* If the transcluded content is not empty (i.e. contains one or more DOM nodes, including whitespace text nodes), any existing
* content of this element will be removed before the transcluded content is inserted.
* If the transcluded content is empty, the existing content is left intact. This lets you provide fallback content in the case
* that no transcluded content is provided.
* If the transcluded content is empty (or only whitespace), the existing content is left intact. This lets you provide fallback
* content in the case that no transcluded content is provided.
*
* @element ANY
*
Expand Down Expand Up @@ -195,7 +195,7 @@ var ngTranscludeDirective = ['$compile', function($compile) {
}

function ngTranscludeCloneAttachFn(clone, transcludedScope) {
if (clone.length) {
if (clone.length && notWhitespace(clone)) {
$element.append(clone);
} else {
useFallbackContent();
Expand All @@ -212,6 +212,15 @@ var ngTranscludeDirective = ['$compile', function($compile) {
$element.append(clone);
});
}

function notWhitespace(nodes) {
for (var i = 0, ii = nodes.length; i < ii; i++) {
var node = nodes[i];
if (node.nodeType !== NODE_TYPE_TEXT || node.nodeValue.trim()) {
return true;
}
}
}
};
}
};
Expand Down
54 changes: 54 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8728,6 +8728,60 @@ describe('$compile', function() {
});
});

it('should compile and link the fallback content if only whitespace transcluded content is provided', function() {
var linkSpy = jasmine.createSpy('postlink');

module(function() {
directive('inner', function() {
return {
restrict: 'E',
template: 'old stuff! ',
link: linkSpy
};
});

directive('trans', function() {
return {
transclude: true,
template: '<div ng-transclude><inner></inner></div>'
};
});
});
inject(function(log, $rootScope, $compile) {
element = $compile('<div trans>\n \n</div>')($rootScope);
$rootScope.$apply();
expect(sortedHtml(element.html())).toEqual('<div ng-transclude=""><inner>old stuff! </inner></div>');
expect(linkSpy).toHaveBeenCalled();
});
});

it('should not link the fallback content if only whitespace and comments are provided as transclude content', function() {
var linkSpy = jasmine.createSpy('postlink');

module(function() {
directive('inner', function() {
return {
restrict: 'E',
template: 'old stuff! ',
link: linkSpy
};
});

directive('trans', function() {
return {
transclude: true,
template: '<div ng-transclude><inner></inner></div>'
};
});
});
inject(function(log, $rootScope, $compile) {
element = $compile('<div trans>\n<!-- some comment --> \n</div>')($rootScope);
$rootScope.$apply();
expect(sortedHtml(element.html())).toEqual('<div ng-transclude="">\n<!-- some comment --> \n</div>');
expect(linkSpy).not.toHaveBeenCalled();
});
});

it('should compile and link the fallback content if an optional transclusion slot is not provided', function() {
var linkSpy = jasmine.createSpy('postlink');

Expand Down

0 comments on commit 32aa7e7

Please sign in to comment.