Skip to content

Commit 4fdbd85

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
fix(common): consider density descriptors with multiple digits as valid (#47230)
Valid density descriptors used in the NgOptimizedImage can contain multiple digits (ex. 1.25x, 25x). This change fixes the issue where density descriptors with multiple digits (ex. 25x) where considered invalid. Please note that a valid density descriptor might still be rejected by the directive's validation logic if the supplied value is too big. PR Close #47230
1 parent d34ae84 commit 4fdbd85

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/common/src/directives/ng_optimized_image/ng_optimized_image.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ const VALID_WIDTH_DESCRIPTOR_SRCSET = /^((\s*\d+w\s*(,|$)){1,})$/;
3232

3333
/**
3434
* RegExpr to determine whether a src in a srcset is using density descriptors.
35-
* Should match something like: "1x, 2x". Also supports decimals like "1.5x".
35+
* Should match something like: "1x, 2x, 50x". Also supports decimals like "1.5x, 1.50x".
3636
*/
37-
const VALID_DENSITY_DESCRIPTOR_SRCSET = /^((\s*\d(\.\d+)?x\s*(,|$)){1,})$/;
37+
const VALID_DENSITY_DESCRIPTOR_SRCSET = /^((\s*\d+(\.\d+)?x\s*(,|$)){1,})$/;
3838

3939
/**
4040
* Srcset values with a density descriptor higher than this value will actively
@@ -506,7 +506,7 @@ function assertUnderDensityCap(dir: NgOptimizedImage, value: string) {
506506
`${RECOMMENDED_SRCSET_DENSITY_CAP}x but supports image densities up to ` +
507507
`${ABSOLUTE_SRCSET_DENSITY_CAP}x. The human eye cannot distinguish between image densities ` +
508508
`greater than ${RECOMMENDED_SRCSET_DENSITY_CAP}x - which makes them unnecessary for ` +
509-
`most use cases. Images that will be pinch-zoomed are typically the primary use case for` +
509+
`most use cases. Images that will be pinch-zoomed are typically the primary use case for ` +
510510
`${ABSOLUTE_SRCSET_DENSITY_CAP}x images. Please remove the high density descriptor and try again.`);
511511
}
512512
}

packages/common/test/directives/ng_optimized_image_spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,36 @@ describe('Image directive', () => {
367367
`${ABSOLUTE_SRCSET_DENSITY_CAP}x. The human eye cannot distinguish between image densities ` +
368368
`greater than ${
369369
RECOMMENDED_SRCSET_DENSITY_CAP}x - which makes them unnecessary for ` +
370-
`most use cases. Images that will be pinch-zoomed are typically the primary use case for` +
370+
`most use cases. Images that will be pinch-zoomed are typically the primary use case for ` +
371+
`${ABSOLUTE_SRCSET_DENSITY_CAP}x images. Please remove the high density descriptor and try again.`);
372+
});
373+
374+
375+
it('should throw if rawSrcset exceeds the density cap with multiple digits', () => {
376+
const imageLoader = (config: ImageLoaderConfig) => {
377+
const width = config.width ? `-${config.width}` : ``;
378+
return window.location.origin + `/path/${config.src}${width}.png`;
379+
};
380+
setupTestingModule({imageLoader});
381+
382+
const template = `
383+
<img rawSrc="img" rawSrcset="1x, 200x" width="100" height="50">
384+
`;
385+
expect(() => {
386+
const fixture = createTestComponent(template);
387+
fixture.detectChanges();
388+
})
389+
.toThrowError(
390+
`NG0${
391+
RuntimeErrorCode
392+
.INVALID_INPUT}: The NgOptimizedImage directive (activated on an <img> element with the \`rawSrc="img"\`) ` +
393+
`has detected that the \`rawSrcset\` contains an unsupported image density:` +
394+
`\`1x, 200x\`. NgOptimizedImage generally recommends a max image density of ` +
395+
`${RECOMMENDED_SRCSET_DENSITY_CAP}x but supports image densities up to ` +
396+
`${ABSOLUTE_SRCSET_DENSITY_CAP}x. The human eye cannot distinguish between image densities ` +
397+
`greater than ${
398+
RECOMMENDED_SRCSET_DENSITY_CAP}x - which makes them unnecessary for ` +
399+
`most use cases. Images that will be pinch-zoomed are typically the primary use case for ` +
371400
`${ABSOLUTE_SRCSET_DENSITY_CAP}x images. Please remove the high density descriptor and try again.`);
372401
});
373402

0 commit comments

Comments
 (0)