Skip to content

Commit

Permalink
fix style attr passed as object not rendered properly for image field
Browse files Browse the repository at this point in the history
  • Loading branch information
yavorsk committed Oct 8, 2024
1 parent 50eb0f6 commit 14a1102
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ describe('<img *scImage />', () => {
alt: 'my image',
height: '650',
width: '300',
style: { background: 'white', color: 'black' },
},
};
const eeMedia = {
Expand Down Expand Up @@ -224,6 +225,15 @@ describe('<img *scImage />', () => {
expect(img.width).toBe(150);
});

it('should render img with style prop', () => {
comp2.field = media;
fixture2.detectChanges();

const img = de.nativeElement.getElementsByTagName('img')[0];
expect(img.style.getPropertyValue('background')).toBe('white');
expect(img.style.getPropertyValue('color')).toBe('black');
});

it('should render img with addtional props in EE mode', () => {
comp2.field = eeMedia;
fixture2.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class ImageDirective extends BaseFieldDirective implements OnChanges {
...parsedAttrs,
};
// eslint-disable-next-line prefer-const
let { src, srcSet, ...otherAttrs } = combinedAttrs;
let { src, srcSet, style, ...otherAttrs } = combinedAttrs;
if (!src) {
return null;
}
Expand All @@ -139,6 +139,13 @@ export class ImageDirective extends BaseFieldDirective implements OnChanges {
} else {
newAttrs.src = src;
}

if (style) {
newAttrs.style = Object.entries(style)
.map(([propName, value]) => `${propName}:${value}`)
.join(';');
}

return newAttrs;
}

Expand Down

0 comments on commit 14a1102

Please sign in to comment.