Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {

this.setHostAttribute('loading', this.getLoadingBehavior());
this.setHostAttribute('fetchpriority', this.getFetchPriority());

// The `data-ng-img` attribute flags an image as using the directive, to allow
// for analysis of the directive's performance.
this.setHostAttribute('ng-img', 'true');

// The `src` and `srcset` attributes should be set last since other attributes
// could affect the image's loading behavior.
const rewrittenSrc = this.getRewrittenSrc();
Expand Down
23 changes: 23 additions & 0 deletions packages/common/test/directives/ng_optimized_image_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,29 @@ describe('Image directive', () => {
});
});

describe('meta data', () => {
it('should add a data attribute to the element for identification', () => {
setupTestingModule();
const template = '<img ngSrc="a.png" width="100" height="50">';
Comment thread
AndrewKushnir marked this conversation as resolved.
Resolved

const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.getAttribute('ng-img')).not.toBeNull();
});
it('should add a data attribute to the element for identification, when ngSrc bound', () => {
setupTestingModule();
const template = `<img [ngSrc]="'a.png'" width="100" height="50">`;

const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.getAttribute('ng-img')).not.toBeNull();
});
});

describe('fill mode', () => {
it('should allow unsized images in fill mode', () => {
setupTestingModule();
Expand Down