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

Commit b635903

Browse files
jeffwhelpleytbosch
authored andcommitted
fix($compile): do not merge attrs that are the same for replace directives
If a directives specifies `replace:true` and the template of the directive contains a root element with an attribute which already exists at the place where the directive is used with the same value, don't duplicate the value. Closes #7463
1 parent c9ee20b commit b635903

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16311631
// reapply the old attributes to the new element
16321632
forEach(dst, function(value, key) {
16331633
if (key.charAt(0) != '$') {
1634-
if (src[key]) {
1634+
if (src[key] && src[key] !== value) {
16351635
value += (key === 'style' ? ';' : ' ') + src[key];
16361636
}
16371637
dst.$set(key, value, true, srcAttr[key]);

test/ng/compileSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ describe('$compile', function() {
493493
expect(element).toBe(attr.$$element);
494494
}
495495
}));
496+
directive('nomerge', valueFn({
497+
restrict: 'CAM',
498+
replace: true,
499+
template: '<div class="log" id="myid" high-log>No Merge!</div>',
500+
compile: function(element, attr) {
501+
attr.$set('compiled', 'COMPILED');
502+
expect(element).toBe(attr.$$element);
503+
}
504+
}));
496505
directive('append', valueFn({
497506
restrict: 'CAM',
498507
template: '<div class="log" style="width: 10px" high-log>Append!</div>',
@@ -597,6 +606,16 @@ describe('$compile', function() {
597606
expect(div.attr('high-log')).toEqual('');
598607
}));
599608

609+
it('should not merge attributes if they are the same', inject(function($compile, $rootScope) {
610+
element = $compile(
611+
'<div><div nomerge class="medium-log" id="myid"></div><div>')
612+
($rootScope);
613+
var div = element.find('div');
614+
expect(div.hasClass('medium-log')).toBe(true);
615+
expect(div.hasClass('log')).toBe(true);
616+
expect(div.attr('id')).toEqual('myid');
617+
}));
618+
600619
it('should prevent multiple templates per element', inject(function($compile) {
601620
try {
602621
$compile('<div><span replace class="replace"></span></div>');

0 commit comments

Comments
 (0)