From 5ed721b9b5e95ae08450e1ae9d5202e7f3f79295 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 cloneAttachFn on asynchronous directives Previously, classes added to asynchronous directive elements during the clone attach function would not persist after the node is merged with the template, prior to linking. This change corrects this behaviour and brings it in line with synchronous directives. Closes #5439 Closes #5617 --- 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() {