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

fix($compile): retain CSS classes added in clone fn on async directive #5617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
linkNode = $compileNode[0];

if (beforeTemplateLinkNode !== beforeTemplateCompileNode) {
var oldClasses = beforeTemplateLinkNode.className;
// it was cloned therefore we have to clone as well.
linkNode = jqLiteClone(compileNode);
replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode);

// Copy in CSS classes from original node
safeAddClass(jqLite(linkNode), oldClasses);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to make a change to safeAddClass so that I can make sure it works correctly for SVG elements and SVGAnimatedString class names.

That can wait for a different PR, though.

}
if (afterTemplateNodeLinkFn.transclude) {
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude);
Expand Down
20 changes: 20 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,26 @@ describe('$compile', function() {
});


it('should copy classes from pre-template node into linked element', function() {
module(function() {
directive('test', valueFn({
templateUrl: 'test.html',
replace: true
}));
});
inject(function($compile, $templateCache, $rootScope) {
var child;
$templateCache.put('test.html', '<p class="template-class">Hello</p>');
element = $compile('<div test></div>')($rootScope, function(node) {
node.addClass('clonefn-class');
});
$rootScope.$digest();
expect(element).toHaveClass('template-class');
expect(element).toHaveClass('clonefn-class');
});
});


describe('delay compile / linking functions until after template is resolved', function(){
var template;
beforeEach(module(function() {
Expand Down