From 92a57b2020cff7cf8857e19c5a9a91ef66a9c47d Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Fri, 3 Jan 2014 11:50:53 -0500 Subject: [PATCH] fix($compile): retain CSS classes added in clone fn on async directive In synchronous directives (without a templateUrl), classes added to the node during the clone attach function persist in the post-link function. Before this patch, classes addedd to asynchronous templates during the clone attach function would not persist after the node is replaced with the template prior to linking. Fixes #5439 --- src/ng/compile.js | 4 ++++ test/ng/compileSpec.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/ng/compile.js b/src/ng/compile.js index 193dff7ad19d..36c23086b1ee 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -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); } if (afterTemplateNodeLinkFn.transclude) { childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index c37461fcc007..557fb85c8a56 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -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', '

Hello

'); + element = $compile('
')($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() {