Skip to content

Commit 9a8a83c

Browse files
fix: properly parse srcset attribute (#510)
1 parent 2cab633 commit 9a8a83c

File tree

8 files changed

+5328
-2992
lines changed

8 files changed

+5328
-2992
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
build/
33
coverage
44
.vscode
5+
.DS_Store

package-lock.json

Lines changed: 5308 additions & 2989 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"meow": "^10.1.1",
3333
"mime": "^3.0.0",
3434
"server-destroy": "^1.0.1",
35+
"srcset": "^5.0.0",
3536
"update-notifier": "^6.0.0"
3637
},
3738
"devDependencies": {

src/links.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {WritableStream} from 'htmlparser2/lib/WritableStream';
22
import {Readable} from 'stream';
33
import {URL} from 'url';
4+
import {parseSrcset} from 'srcset';
45

56
const linksAttr = {
67
background: ['body'],
@@ -115,9 +116,7 @@ function isAbsoluteUrl(url: string): boolean {
115116
function parseAttr(name: string, value: string): string[] {
116117
switch (name) {
117118
case 'srcset':
118-
return value
119-
.split(',')
120-
.map((pair: string) => pair.trim().split(/\s+/)[0]);
119+
return parseSrcset(value).map(p => p.url);
121120
default:
122121
return [value];
123122
}

test/fixtures/picture/image.jpeg

43.8 KB
Loading

test/fixtures/picture/image.webp

29.6 KB
Binary file not shown.

test/fixtures/picture/index.html

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

test/test.index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ describe('linkinator', () => {
228228
);
229229
});
230230

231+
it('should work with picture elements', async () => {
232+
const results = await check({path: 'test/fixtures/picture'});
233+
assert.ok(results.passed);
234+
assert.strictEqual(results.links.length, 4);
235+
});
236+
231237
it('should not recurse by default', async () => {
232238
const results = await check({path: 'test/fixtures/recurse'});
233239
assert.strictEqual(results.links.length, 2);

0 commit comments

Comments
 (0)