Skip to content

Commit 77fafe2

Browse files
fix: synchronize angular image empty attributes on updates
1 parent c48ddf6 commit 77fafe2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,16 @@ export class CloudinaryImageComponent implements OnInit, OnChanges, OnDestroy {
103103
}
104104

105105
/**
106-
* Add attributes to img element.
106+
* Sync attributes to img element.
107107
*/
108108
syncAttributes() {
109109
['alt', 'width', 'height', 'loading'].forEach(attr => {
110-
if (this[attr]) {
110+
const isAttributeSet = this[attr] !== undefined && this[attr] !== null;
111+
if (isAttributeSet) {
111112
this.el.nativeElement.children[0].setAttribute(attr, this[attr]);
112113
this.el.nativeElement.removeAttribute(attr);
114+
} else {
115+
this.el.nativeElement.children[0].removeAttribute(attr);
113116
}
114117
});
115118
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,12 @@ describe('CloudinaryImageComponent render', () => {
6161
component.ngOnChanges();
6262
expect(img.outerHTML).toBe('<img _ngcontent-a-c11="" alt="updated alt text" width="800px" height="1000px"' +
6363
' loading="lazy" src="https://res.cloudinary.com/demo/image/upload/sample">');
64+
component.width = undefined;
65+
component.height = undefined;
66+
component.alt = "";
67+
component.loading = "lazy";
68+
component.ngOnChanges();
69+
expect(img.outerHTML).toBe('<img _ngcontent-a-c11="" alt=""' +
70+
' loading="lazy" src="https://res.cloudinary.com/demo/image/upload/sample">');
6471
}));
6572
});

0 commit comments

Comments
 (0)