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

Commit

Permalink
fix($compile): Merge interpolated css class when replacing an element
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Mar 20, 2012
1 parent f701ce0 commit f49eaf8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/service/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ function $CompileProvider($provide) {
// reapply the old attributes to the new element
forEach(dst, function(value, key) {
if (key.charAt(0) != '$') {
if (src[key]) {
value += (key === 'style' ? ';' : ' ') + src[key];
}
dst.$set(key, value, srcAttr[key]);
}
});
Expand Down Expand Up @@ -873,6 +876,12 @@ function $CompileProvider($provide) {
compile: function(element, attr) {
if (interpolateFn) {
return function(scope, element, attr) {
if (name === 'class') {
// we need to interpolate classes again, in the case the element was replaced
// and therefore the two class attrs got merged - we want to interpolate the result
interpolateFn = $interpolate(attr[name], true);
}

// we define observers array only for interpolated attrs
// and ignore observers for non interpolated attrs to save some memory
attr.$observers[name] = [];
Expand Down
35 changes: 34 additions & 1 deletion test/service/compilerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ describe('$compile', function() {
}));


it('should merge attributes', inject(function($compile, $rootScope) {
it('should merge attributes including style attr', inject(function($compile, $rootScope) {
element = $compile(
'<div><div replace class="medium-log" style="height: 20px" ></div><div>')
($rootScope);
Expand Down Expand Up @@ -431,6 +431,39 @@ describe('$compile', function() {
$rootScope.$digest();
expect(element.text()).toEqual('Hello: 1; Hello: 2; ');
}));


it('should merge interpolated css class', inject(function($compile, $rootScope) {
element = $compile('<div class="one {{cls}} three" replace></div>')($rootScope);

$rootScope.$apply(function() {
$rootScope.cls = 'two';
});

expect(element).toHaveClass('one');
expect(element).toHaveClass('two'); // interpolated
expect(element).toHaveClass('three');
expect(element).toHaveClass('log'); // merged from replace directive template
}));


it('should merge interpolated css class with ng-repeat',
inject(function($compile, $rootScope) {
element = $compile(
'<div>' +
'<div ng-repeat="i in [1]" class="one {{cls}} three" replace></div>' +
'</div>')($rootScope);

$rootScope.$apply(function() {
$rootScope.cls = 'two';
});

var child = element.find('div').eq(0);
expect(child).toHaveClass('one');
expect(child).toHaveClass('two'); // interpolated
expect(child).toHaveClass('three');
expect(child).toHaveClass('log'); // merged from replace directive template
}));
});


Expand Down

0 comments on commit f49eaf8

Please sign in to comment.