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

fix($compile): merge attributes safely to avoid upsetting IE8 #4207

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ function $CompileProvider($provide) {
if (writeAttr !== false) {
if (value === null || value === undefined) {
this.$$element.removeAttr(attrName);
} else {
} else if (value !== this.$$element.attr(attrName)) {
// Set new value only if it is not already there, avoiding
// issues with IE, where certain attributes cannot be set
// on in-document elements, e.g. 'type' on <input>
this.$$element.attr(attrName, value);
}
}
Expand Down Expand Up @@ -1130,8 +1133,9 @@ function $CompileProvider($provide) {
// reapply the old attributes to the new element
forEach(dst, function(value, key) {
if (key.charAt(0) != '$') {
if (src[key]) {
value += (key === 'style' ? ';' : ' ') + src[key];
var srcVal = src[key];
if (srcVal && srcVal !== value) {
value += (key === 'style' ? ';' : ' ') + srcVal;
}
dst.$set(key, value, true, srcAttr[key]);
}
Expand Down
16 changes: 16 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ describe('$compile', function() {
expect(element).toBe(attr.$$element);
}
}));
directive('replaceWithArbitraryAttribute', valueFn({
replace: true,
template: '<div data-foo="bar">With data-foo</div>',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
}));


Expand Down Expand Up @@ -538,6 +546,14 @@ describe('$compile', function() {
expect(div.attr('high-log')).toEqual('');
}));

it('should preserve identical attributes when merging', inject(function($compile, $rootScope) {
element = $compile(
'<div><div replace-with-arbitrary-attribute data-foo="bar"/><div>')
($rootScope);
var div = element.find('div');
expect(div.attr('data-foo')).toBe('bar');
}));

it('should prevent multiple templates per element', inject(function($compile) {
try {
$compile('<div><span replace class="replace"></span></div>');
Expand Down