Skip to content

Commit

Permalink
fix: properly parse srcset attribute (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jul 9, 2022
1 parent 2cab633 commit 9a8a83c
Show file tree
Hide file tree
Showing 8 changed files with 5,328 additions and 2,992 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
build/
coverage
.vscode
.DS_Store
8,297 changes: 5,308 additions & 2,989 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"meow": "^10.1.1",
"mime": "^3.0.0",
"server-destroy": "^1.0.1",
"srcset": "^5.0.0",
"update-notifier": "^6.0.0"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions src/links.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {WritableStream} from 'htmlparser2/lib/WritableStream';
import {Readable} from 'stream';
import {URL} from 'url';
import {parseSrcset} from 'srcset';

const linksAttr = {
background: ['body'],
Expand Down Expand Up @@ -115,9 +116,7 @@ function isAbsoluteUrl(url: string): boolean {
function parseAttr(name: string, value: string): string[] {
switch (name) {
case 'srcset':
return value
.split(',')
.map((pair: string) => pair.trim().split(/\s+/)[0]);
return parseSrcset(value).map(p => p.url);
default:
return [value];
}
Expand Down
Binary file added test/fixtures/picture/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/picture/image.webp
Binary file not shown.
10 changes: 10 additions & 0 deletions test/fixtures/picture/index.html

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/test.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ describe('linkinator', () => {
);
});

it('should work with picture elements', async () => {
const results = await check({path: 'test/fixtures/picture'});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 4);
});

it('should not recurse by default', async () => {
const results = await check({path: 'test/fixtures/recurse'});
assert.strictEqual(results.links.length, 2);
Expand Down

0 comments on commit 9a8a83c

Please sign in to comment.