Skip to content

Commit 4e07844

Browse files
Merge pull request #235 from cloudinary/angular-sync-image-attributes
fix: angular sync image attributes
2 parents 02031d3 + 11b9374 commit 4e07844

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

packages/angular/projects/cloudinary-library/src/lib/cloudinary-image.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class CloudinaryImageComponent implements OnInit, OnChanges, OnDestroy {
7979
*/
8080
ngOnInit() {
8181
this.htmlLayerInstance = new HtmlImageLayer(this.el.nativeElement.children[0], this.cldImg, this.plugins, SDKAnalyticsConstants);
82-
this.addAttributes();
82+
this.syncAttributes();
8383
}
8484

8585
/**
@@ -90,6 +90,7 @@ export class CloudinaryImageComponent implements OnInit, OnChanges, OnDestroy {
9090
if (this.htmlLayerInstance) {
9191
cancelCurrentlyRunningPlugins(this.htmlLayerInstance.htmlPluginState);
9292
this.htmlLayerInstance.update(this.cldImg, this.plugins, SDKAnalyticsConstants);
93+
this.syncAttributes();
9394
}
9495
}
9596

@@ -104,7 +105,7 @@ export class CloudinaryImageComponent implements OnInit, OnChanges, OnDestroy {
104105
/**
105106
* Add attributes to img element.
106107
*/
107-
addAttributes() {
108+
syncAttributes() {
108109
['alt', 'width', 'height', 'loading'].forEach(attr => {
109110
if (this[attr]) {
110111
this.el.nativeElement.children[0].setAttribute(attr, this[attr]);

packages/angular/projects/cloudinary-library/src/tests/cloudinary-image.component.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('CloudinaryImageComponent render', () => {
4242
expect(mockPlugin).toHaveBeenCalledTimes(2);
4343
}));
4444

45-
it('should add attributes to image', fakeAsync(() => {
45+
it('should sync attributes to image', fakeAsync(() => {
4646
component.cldImg = cloudinaryImage;
4747
component.width = '400px';
4848
component.alt = 'text text text';
@@ -54,7 +54,12 @@ describe('CloudinaryImageComponent render', () => {
5454
const img = imgElement.querySelector('img');
5555
expect(img.outerHTML).toBe('<img _ngcontent-a-c11="" alt="text text text" width="400px" height="500px"' +
5656
' loading="eager" src="https://res.cloudinary.com/demo/image/upload/sample">');
57+
component.width = "800px";
58+
component.alt = "updated alt text";
59+
component.height = "1000px";
60+
component.loading = "lazy";
61+
component.ngOnChanges();
62+
expect(img.outerHTML).toBe('<img _ngcontent-a-c11="" alt="updated alt text" width="800px" height="1000px"' +
63+
' loading="lazy" src="https://res.cloudinary.com/demo/image/upload/sample">');
5764
}));
5865
});
59-
60-

0 commit comments

Comments
 (0)