Skip to content

Commit

Permalink
fix(common): Add data attribtue to NgOptimizedImage (#48497)
Browse files Browse the repository at this point in the history
Add a tracking attribute in oder to be able to distinguish usages of NgOptimizedImage from standard images

PR Close #48497
  • Loading branch information
atcastle authored and alxhub committed Jan 4, 2023
1 parent aa51e9b commit 2f4f063
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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">';

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

0 comments on commit 2f4f063

Please sign in to comment.