diff --git a/src/ng/compile.js b/src/ng/compile.js index 4baa53f7b79f..741eaeb90bbf 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -2761,18 +2761,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { // 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); - dst['style'] = (dst['style'] ? dst['style'] + ';' : '') + value; - // `dst` will never contain hasOwnProperty as DOM parser won't let it. - // You will get an "InvalidCharacterError: DOM Exception 5" error if you - // have an attribute like "has-own-property" or "data-has-own-property", etc. - } else if (key.charAt(0) !== '$' && !dst.hasOwnProperty(key)) { + // Check if we already set this attribute in the loop above. + // `dst` will never contain hasOwnProperty as DOM parser won't let it. + // You will get an "InvalidCharacterError: DOM Exception 5" error if you + // have an attribute like "has-own-property" or "data-has-own-property", etc. + if (!dst.hasOwnProperty(key) && key.charAt(0) !== '$') { dst[key] = value; - dstAttr[key] = srcAttr[key]; + + if (key !== 'class' && key !== 'style') { + dstAttr[key] = srcAttr[key]; + } } }); } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index c58fc1c8d2e0..668f6c687fa5 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -980,6 +980,29 @@ describe('$compile', function() { })); + it('should not set merged attributes twice in $attrs', function() { + var attrs; + + module(function() { + directive('logAttrs', function() { + return { + link: function($scope, $element, $attrs) { + attrs = $attrs; + } + }; + }); + }); + + inject(function($compile, $rootScope) { + element = $compile( + '