Skip to content

Commit 3a5f7b4

Browse files
committed
fix($compile): don't add replaced attributes twice to $attrs
Fixes angular#8159 Closes angular#5597 Closes angular#5597 Closes angular#5597 Closes angular#5597 Closes angular#5597 Closes angular#5597
1 parent ca1e39c commit 3a5f7b4

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/ng/compile.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -2761,18 +2761,22 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
27612761

27622762
// copy the new attributes on the old attrs object
27632763
forEach(src, function(value, key) {
2764-
if (key === 'class') {
2765-
safeAddClass($element, value);
2766-
dst['class'] = (dst['class'] ? dst['class'] + ' ' : '') + value;
2767-
} else if (key === 'style') {
2768-
$element.attr('style', $element.attr('style') + ';' + value);
2769-
dst['style'] = (dst['style'] ? dst['style'] + ';' : '') + value;
2770-
// `dst` will never contain hasOwnProperty as DOM parser won't let it.
2771-
// You will get an "InvalidCharacterError: DOM Exception 5" error if you
2772-
// have an attribute like "has-own-property" or "data-has-own-property", etc.
2773-
} else if (key.charAt(0) !== '$' && !dst.hasOwnProperty(key)) {
2774-
dst[key] = value;
2775-
dstAttr[key] = srcAttr[key];
2764+
// Check if we already set this attribute in the loop above
2765+
if (!dst[key]) {
2766+
2767+
if (key === 'class') {
2768+
safeAddClass($element, value);
2769+
dst['class'] = (dst['class'] ? dst['class'] + ' ' : '') + value;
2770+
} else if (key === 'style') {
2771+
$element.attr('style', $element.attr('style') + ';' + value);
2772+
dst['style'] = (dst['style'] ? dst['style'] + ';' : '') + value;
2773+
// `dst` will never contain hasOwnProperty as DOM parser won't let it.
2774+
// You will get an "InvalidCharacterError: DOM Exception 5" error if you
2775+
// have an attribute like "has-own-property" or "data-has-own-property", etc.
2776+
} else if (key.charAt(0) !== '$' && !dst.hasOwnProperty(key)) {
2777+
dst[key] = value;
2778+
dstAttr[key] = srcAttr[key];
2779+
}
27762780
}
27772781
});
27782782
}

test/ng/compileSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,32 @@ describe('$compile', function() {
980980
}));
981981

982982

983+
984+
985+
it('should not set merged attributes twice in $attrs', function() {
986+
var attrs;
987+
988+
module(function() {
989+
directive('logAttrs', function() {
990+
return {
991+
link: function($scope, $element, $attrs) {
992+
attrs = $attrs;
993+
}
994+
}
995+
})
996+
});
997+
998+
inject(function($compile, $rootScope) {
999+
element = $compile(
1000+
'<div><div log-attrs replace class="myLog"></div><div>')($rootScope);
1001+
var div = element.find('div');
1002+
expect(div.attr('class')).toBe('myLog log');
1003+
console.log('attrs', attrs.class);
1004+
expect(attrs.class).toBe('myLog log');
1005+
});
1006+
});
1007+
1008+
9831009
it('should prevent multiple templates per element', inject(function($compile) {
9841010
try {
9851011
$compile('<div><span replace class="replace"></span></div>');

0 commit comments

Comments
 (0)