Skip to content

Commit a48e9b0

Browse files
authored
Updated placeholder styling (#288)
* removed styling from cl-img->img
1 parent ce128b1 commit a48e9b0

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

projects/angular-cld/src/lib/cloudinary-image.component.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,35 @@ describe('CloudinaryImage', () => {
678678
expect(img.getAttribute('style')).toEqual(jasmine.stringMatching('max-height: 100%; opacity: 0; position: absolute;'));
679679
});
680680
});
681+
describe('cl-image with placeholder and opacity', () => {
682+
@Component({
683+
template: `<div></div>
684+
<cl-image public-id="sample" style="opacity: 0.5">
685+
<cl-placeholder type="blur"></cl-placeholder>
686+
</cl-image>`
687+
})
688+
class TestComponent {}
689+
690+
let fixture: ComponentFixture<TestComponent>;
691+
let des: DebugElement[]; // the elements w/ the directive
692+
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
693+
{ cloud_name: '@@fake_angular2_sdk@@', client_hints: true } as CloudinaryConfiguration);
694+
beforeEach(() => {
695+
fixture = TestBed.configureTestingModule({
696+
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent, CloudinaryPlaceHolder],
697+
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
698+
}).createComponent(TestComponent);
699+
700+
fixture.detectChanges(); // initial binding
701+
// all elements with an attached CloudinaryImage
702+
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
703+
});
704+
705+
it('should not overwrite input opacity', () => {
706+
const img = des[0].nativeElement as HTMLImageElement;
707+
expect(img.getAttribute('style')).toEqual(jasmine.stringMatching('opacity: 0.5'));
708+
});
709+
});
681710
describe('lazy load image', async () => {
682711
@Component({
683712
template: `
@@ -799,6 +828,12 @@ describe('CloudinaryImage', () => {
799828
const img = des[0].children[0].nativeElement as HTMLImageElement;
800829
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('image/upload/e_blur:2000,f_auto,q_1/bear'));
801830
}));
831+
it('creates an img element without styling', fakeAsync(() => {
832+
tick();
833+
fixture.detectChanges();
834+
const img = des[0].children[0].nativeElement as HTMLImageElement;
835+
expect(img.getAttribute('style')).toEqual(null);
836+
}));
802837
});
803838
describe('placeholder type blur', () => {
804839
@Component({

projects/angular-cld/src/lib/cloudinary-image.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,17 @@ export class CloudinaryImage
9696
}
9797
}
9898

99+
/**
100+
* appends opacity and position to cl-img->img when placeholder is displayed
101+
* removes styling from cl-img->img when placeholder does not display
102+
*/
99103
setPlaceHolderStyle() {
100-
if (this.placeholderComponent) {
104+
if (this.shouldShowPlaceHolder) {
101105
this.renderer.setStyle(this.el.nativeElement.children[0], 'opacity', '0' );
102106
this.renderer.setStyle(this.el.nativeElement.children[0], 'position', 'absolute' );
107+
} else {
108+
// note this only removes styling from cl-img->img and not cl-img
109+
this.renderer.removeAttribute(this.el.nativeElement.children[0], 'style');
103110
}
104111
}
105112

@@ -156,7 +163,7 @@ export class CloudinaryImage
156163
});
157164

158165
// Enforcing placeholder style
159-
if (this.shouldShowPlaceHolder) {
166+
if (this.placeholderComponent) {
160167
this.setPlaceHolderStyle();
161168
}
162169
}

projects/angular-cld/src/lib/cloudinary-placeholder.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class CloudinaryPlaceHolder implements AfterContentChecked {
5353

5454
setElementAttributes(element, attributesLiteral) {
5555
Object.keys(attributesLiteral).forEach(attrName => {
56-
if (attrName !== 'src') {
56+
if (attrName !== 'src' && attrName !== 'style') {
5757
this.renderer.setAttribute(element, attrName, attributesLiteral[attrName]);
5858
}
5959
});

0 commit comments

Comments
 (0)