Skip to content

Commit

Permalink
Fix handling of components being on separate lines
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
sindresorhus committed Jan 29, 2024
1 parent 0518f4f commit 08f8154
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ const validDescriptorCheck = (value, postfix, descriptor) => {
export function parseSrcset(string, {strict = false} = {}) {
const allDescriptors = strict ? {} : undefined;

return string.split(imageCandidateRegex)
return string
.replace(/\r?\n/, '')
.replace(/,\s+/, ', ')
.split(imageCandidateRegex)
.filter((part, index) => index % 2 === 1)
.map(part => {
const [url, ...descriptors] = part.trim().split(/\s+/);
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ test('parseSrcset() - strict mode', t => {
);
});

test('parseSrcset() - should correctly parse srcset with varied spacing and newlines', t => {
const fixture = `image.png,
image@2x.png 2x`;

t.deepEqual(parseSrcset(fixture), [
{url: 'image.png'},
{url: 'image@2x.png', density: 2},
]);
});

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

0 comments on commit 08f8154

Please sign in to comment.