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

Commit

Permalink
fix($compile): correctly merge class attr for replace directives
Browse files Browse the repository at this point in the history
Merging of interpolated class attribute from directive template with replace:true works

Closes #1006
  • Loading branch information
maxmart authored and IgorMinar committed Jun 8, 2012
1 parent 10f80d7 commit fb99b53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,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) != '$') {
Expand All @@ -874,10 +875,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)) {
Expand Down
16 changes: 16 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ describe('$compile', function() {
expect(element).toBe(attr.$$element);
}
}));
directive('replaceWithInterpolatedClass', valueFn({
replace: true,
template: '<div class="class_{{1+1}}">Replace with interpolated class!</div>',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
}));


Expand Down Expand Up @@ -466,6 +474,14 @@ describe('$compile', function() {
}));


it('should handle interpolated css from replacing directive', inject(
function($compile, $rootScope) {
element = $compile('<div replace-with-interpolated-class></div>')($rootScope);
$rootScope.$digest();
expect(element).toHaveClass('class_2');
}));


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

Expand Down

0 comments on commit fb99b53

Please sign in to comment.