Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c1fc857

Browse files
CaerusKaruThomasBurleson
authored andcommitted
fix(img-src): correctly initialize fallback value (#986)
1 parent 0eccec4 commit c1fc857

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/lib/extended/img-src/img-src.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,52 @@ describe('img-src directive', () => {
237237
}
238238
});
239239

240+
it('should work with responsive srcs, starting and ending with fallback', () => {
241+
const defaultSrc = 'https://www.gstatic.com/webp/gallery2/1.png';
242+
const xsSrc = 'https://www.gstatic.com/webp/gallery3/1.png';
243+
componentWithTemplate(`
244+
<img src="${defaultSrc}" src.xs="${xsSrc}">
245+
`);
246+
fixture.detectChanges();
247+
248+
let img = queryFor(fixture, 'img')[0];
249+
let imgEl = img.nativeElement;
250+
expect(imgEl).toBeDefined();
251+
if (isPlatformServer(platformId)) {
252+
expectEl(img).toHaveStyle({
253+
'content': `url(${defaultSrc})`
254+
}, styler);
255+
256+
matchMedia.activate('xs');
257+
fixture.detectChanges();
258+
expectEl(img).toHaveStyle({
259+
'content': `url(${xsSrc})`
260+
}, styler);
261+
262+
matchMedia.activate('lg');
263+
fixture.detectChanges();
264+
expectEl(img).toHaveStyle({
265+
'content': `url(${defaultSrc})`
266+
}, styler);
267+
} else {
268+
expect(imgEl).toHaveAttributes({
269+
src: defaultSrc
270+
});
271+
272+
matchMedia.activate('xs');
273+
fixture.detectChanges();
274+
expect(imgEl).toHaveAttributes({
275+
src: xsSrc
276+
});
277+
278+
matchMedia.activate('lg');
279+
fixture.detectChanges();
280+
expect(imgEl).toHaveAttributes({
281+
src: defaultSrc
282+
});
283+
}
284+
});
285+
240286
it('should work if default [src] is not defined', () => {
241287
componentWithTemplate(`
242288
<img [src.md]="mdSrc">

src/lib/extended/img-src/img-src.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ImgSrcDirective extends BaseDirective2 {
3030
@Input('src')
3131
set src(val: string) {
3232
this.defaultSrc = val;
33-
this.setValue('', this.defaultSrc);
33+
this.setValue(this.defaultSrc, '');
3434
}
3535

3636
constructor(protected elementRef: ElementRef,
@@ -41,7 +41,7 @@ export class ImgSrcDirective extends BaseDirective2 {
4141
@Inject(SERVER_TOKEN) protected serverModuleLoaded: boolean) {
4242
super(elementRef, styleBuilder, styler, marshal);
4343
this.init();
44-
this.setValue('', this.nativeElement.getAttribute('src') || '');
44+
this.setValue(this.nativeElement.getAttribute('src') || '', '');
4545
if (isPlatformServer(this.platformId) && this.serverModuleLoaded) {
4646
this.nativeElement.setAttribute('src', '');
4747
}
@@ -55,12 +55,12 @@ export class ImgSrcDirective extends BaseDirective2 {
5555
* Do nothing to standard `<img src="">` usages, only when responsive
5656
* keys are present do we actually call `setAttribute()`
5757
*/
58-
protected updateWithValue() {
59-
let url = this.activatedValue || this.defaultSrc;
58+
protected updateWithValue(value?: string) {
59+
const url = value || this.defaultSrc;
6060
if (isPlatformServer(this.platformId) && this.serverModuleLoaded) {
6161
this.addStyles(url);
6262
} else {
63-
this.nativeElement.setAttribute('src', String(url));
63+
this.nativeElement.setAttribute('src', url);
6464
}
6565
}
6666

0 commit comments

Comments
 (0)