Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-angular] Fix style attribute from field not rendered properly in image field #1944

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Our versioning strategy is as follows:
* `[templates/nextjs]` Fix embedded personalization not rendering correctly after navigation through router links. ([#1911](https://github.com/Sitecore/jss/pull/1911))
* `[template/angular]` Prevent client-side dictionary API call when SSR data is available ([#1930](https://github.com/Sitecore/jss/pull/1930)) ([#1932](https://github.com/Sitecore/jss/pull/1932))
* `[sitecore-jss-angular]` Fix default empty field components to not render the unwanted wrapping tags ([#1937](https://github.com/Sitecore/jss/pull/1937)) ([#1940](https://github.com/Sitecore/jss/pull/1940))
* `[sitecore-jss-angular]` Fix image field style property not rendered properly ([#1944](https://github.com/Sitecore/jss/pull/1944))

### 🎉 New Features & Improvements

Expand Down
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