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

fix(img-src): correctly initialize fallback value #986

Merged
merged 2 commits into from
Jan 13, 2019
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
46 changes: 46 additions & 0 deletions src/lib/extended/img-src/img-src.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,52 @@ describe('img-src directive', () => {
}
});

it('should work with responsive srcs, starting and ending with fallback', () => {
const defaultSrc = 'https://www.gstatic.com/webp/gallery2/1.png';
const xsSrc = 'https://www.gstatic.com/webp/gallery3/1.png';
componentWithTemplate(`
<img src="${defaultSrc}" src.xs="${xsSrc}">
`);
fixture.detectChanges();

let img = queryFor(fixture, 'img')[0];
let imgEl = img.nativeElement;
expect(imgEl).toBeDefined();
if (isPlatformServer(platformId)) {
expectEl(img).toHaveStyle({
'content': `url(${defaultSrc})`
}, styler);

matchMedia.activate('xs');
fixture.detectChanges();
expectEl(img).toHaveStyle({
'content': `url(${xsSrc})`
}, styler);

matchMedia.activate('lg');
fixture.detectChanges();
expectEl(img).toHaveStyle({
'content': `url(${defaultSrc})`
}, styler);
} else {
expect(imgEl).toHaveAttributes({
src: defaultSrc
});

matchMedia.activate('xs');
fixture.detectChanges();
expect(imgEl).toHaveAttributes({
src: xsSrc
});

matchMedia.activate('lg');
fixture.detectChanges();
expect(imgEl).toHaveAttributes({
src: defaultSrc
});
}
});

it('should work if default [src] is not defined', () => {
componentWithTemplate(`
<img [src.md]="mdSrc">
Expand Down
10 changes: 5 additions & 5 deletions src/lib/extended/img-src/img-src.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ImgSrcDirective extends BaseDirective2 {
@Input('src')
set src(val: string) {
this.defaultSrc = val;
this.setValue('', this.defaultSrc);
this.setValue(this.defaultSrc, '');
}

constructor(protected elementRef: ElementRef,
Expand All @@ -41,7 +41,7 @@ export class ImgSrcDirective extends BaseDirective2 {
@Inject(SERVER_TOKEN) protected serverModuleLoaded: boolean) {
super(elementRef, styleBuilder, styler, marshal);
this.init();
this.setValue('', this.nativeElement.getAttribute('src') || '');
this.setValue(this.nativeElement.getAttribute('src') || '', '');
if (isPlatformServer(this.platformId) && this.serverModuleLoaded) {
this.nativeElement.setAttribute('src', '');
}
Expand All @@ -55,12 +55,12 @@ export class ImgSrcDirective extends BaseDirective2 {
* Do nothing to standard `<img src="">` usages, only when responsive
* keys are present do we actually call `setAttribute()`
*/
protected updateWithValue() {
let url = this.activatedValue || this.defaultSrc;
protected updateWithValue(value?: string) {
const url = value || this.defaultSrc;
if (isPlatformServer(this.platformId) && this.serverModuleLoaded) {
this.addStyles(url);
} else {
this.nativeElement.setAttribute('src', String(url));
this.nativeElement.setAttribute('src', url);
}
}

Expand Down