-
Notifications
You must be signed in to change notification settings - Fork 27.3k
fix($compile): don't add replaced attributes twice to $attrs #14737
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2761,18 +2761,22 @@ 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)) { | ||
| dst[key] = value; | ||
| dstAttr[key] = srcAttr[key]; | ||
| // Check if we already set this attribute in the loop above | ||
| if (!dst[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)) { | ||
|
||
| dst[key] = value; | ||
| dstAttr[key] = srcAttr[key]; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried removing it - injection seems necessary |
||
| element = $compile( | ||
| '<div><div log-attrs replace class="myLog"></div><div>')($rootScope); | ||
| var div = element.find('div'); | ||
| expect(div.attr('class')).toBe('myLog log'); | ||
| expect(attrs.class).toBe('myLog log'); | ||
| }); | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a test for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really? Both are handled by the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are different |
||
|
|
||
|
|
||
| it('should prevent multiple templates per element', inject(function($compile) { | ||
| try { | ||
| $compile('<div><span replace class="replace"></span></div>'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without thinking much about it, using
!dst.hasOwnProperty(key)feels safer (and should be quicker).