From efd736b26527ca1309115b23d682487aa1e13658 Mon Sep 17 00:00:00 2001 From: Max Martinsson Date: Wed, 6 Jun 2012 16:23:07 +0200 Subject: [PATCH] fix($compile): correctly merge class attr for replace directives Merging of interpolated class attribute from directive template with replace:true works Closes #1006 --- src/ng/compile.js | 3 +++ test/ng/compileSpec.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/ng/compile.js b/src/ng/compile.js index 5c7f14198409..8190afb1cebc 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -844,6 +844,7 @@ function $CompileProvider($provide) { var srcAttr = src.$attr, dstAttr = dst.$attr, $element = dst.$$element; + // reapply the old attributes to the new element forEach(dst, function(value, key) { if (key.charAt(0) != '$') { @@ -853,10 +854,12 @@ function $CompileProvider($provide) { dst.$set(key, value, true, srcAttr[key]); } }); + // copy the new attributes on the old attrs object forEach(src, function(value, key) { if (key == 'class') { safeAddClass($element, value); + dst.class = (dst.class ? dst.class + ' ' : '') + value; } else if (key == 'style') { $element.attr('style', $element.attr('style') + ';' + value); } else if (key.charAt(0) != '$' && !dst.hasOwnProperty(key)) { diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 86ba3ade81ab..8867fe4616de 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -378,6 +378,14 @@ describe('$compile', function() { expect(element).toBe(attr.$$element); } })); + $compileProvider.directive('replaceWithInterpolatedClass', valueFn({ + replace: true, + template: '
Replace with interpolated class!
', + compile: function(element, attr) { + attr.$set('compiled', 'COMPILED'); + expect(element).toBe(attr.$$element); + } + })); })); @@ -456,6 +464,14 @@ describe('$compile', function() { })); + it('should handle interpolated css from replacing directive', inject( + function($compile, $rootScope) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + expect(element).toHaveClass('class_2'); + })); + + it('should merge interpolated css class', inject(function($compile, $rootScope) { element = $compile('
')($rootScope);