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

Commit 1ab6e90

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 40b8721 commit 1ab6e90

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
@@ -1648,7 +1648,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16481648
// reapply the old attributes to the new element
16491649
forEach(dst, function(value, key) {
16501650
if (key.charAt(0) != '$') {
1651-
if (src[key]) {
1651+
if (src[key] && src[key] !== value) {
16521652
value += (key === 'style' ? ';' : ' ') + src[key];
16531653
}
16541654
dst.$set(key, value, true, srcAttr[key]);

test/ng/compileSpec.js

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

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

0 commit comments

Comments
 (0)