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

fix($compile): fix duplicate style attributes #6242

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
4 changes: 3 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
safeAddClass($element, value);
dst['class'] = (dst['class'] ? dst['class'] + ' ' : '') + value;
} else if (key == 'style') {
$element.attr('style', $element.attr('style') + ';' + value);
if($element.attr("style") !== value) {
$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
Expand Down
15 changes: 15 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ describe('$compile', function() {
replace: true,
template: '<tfoot><tr><td>TD</td></tr></tfoot>'
}));
directive('replaceWithNonInterpolatedStyle', valueFn({
replace: true,
template: '<div style="color: red;">Replace with non-interpolated style!</div>',
compile: function(element, attr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need this compile fn

attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
}));


Expand Down Expand Up @@ -635,6 +643,13 @@ describe('$compile', function() {
}));
}

it('should handle non-interpolated css style from replacing directive', inject(
function($compile, $rootScope) {
element = $compile('<div replace-with-non-interpolated-style></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('style')).toMatch(/^color\: red;?$/ig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this test fail without the change to the compiler? it doesn't look like it should

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I answered my own question, okay.

}));

it('should merge interpolated css class', inject(function($compile, $rootScope) {
element = $compile('<div class="one {{cls}} three" replace></div>')($rootScope);

Expand Down