Skip to content

Commit

Permalink
Fix strict mode with fallback
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
sindresorhus committed Jan 29, 2024
1 parent 5db551e commit 0518f4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const fallbackDescriptorDuplicateCheck = allDescriptors => {
throw new Error('Only one fallback image candidate is allowed');
}

if (allDescriptors.x['1']) {
// TODO: Use `?`.
if (allDescriptors.x && allDescriptors.x['1']) {
throw new Error('A fallback image is equivalent to a 1x descriptor, providing both is invalid.');
}

Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ test('parseSrcset() should parse srcset separated without whitespaces', t => {
]);
});

test('parseSrcset() - strict mode', t => {
t.deepEqual(
parseSrcset('images/x.jpg, images/x-retina.jpg 2x', {strict: true}),
[
{
url: 'images/x.jpg',
},
{
url: 'images/x-retina.jpg',
density: 2,
},
],
);
});

test('stringifySrcset() should stringify srcset', t => {
const fixture = [
{url: 'banner-HD.jpeg', density: 2},
Expand Down

0 comments on commit 0518f4f

Please sign in to comment.