Skip to content

Commit 939d181

Browse files
toufic-madampash
authored andcommitted
fix: support query strings in lazy-loaded srcsets (#387)
1 parent 0942c37 commit 939d181

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export const IS_LINK = new RegExp('https?://', 'i');
22
const IMAGE_RE = '.(png|gif|jpe?g)';
33
export const IS_IMAGE = new RegExp(`${IMAGE_RE}`, 'i');
4-
export const IS_SRCSET = new RegExp(`${IMAGE_RE}(\\s*[\\d.]+[wx])`, 'i');
4+
export const IS_SRCSET = new RegExp(
5+
`${IMAGE_RE}(\\?\\S+)?(\\s*[\\d.]+[wx])`,
6+
'i'
7+
);
58

69
export const TAGS_TO_REMOVE = ['script', 'style', 'form'].join(',');

src/resource/utils/dom/convert-lazy-loaded-images.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ describe('convertLazyLoadedImages($)', () => {
2828
);
2929
});
3030

31+
it('moves image source candidates containing query strings to srcset if placed in another attribute', () => {
32+
const html =
33+
'<img data-srcset="http://example.com/foo.jpg?w=400 2x, http://example.com/foo.jpg?w=600 3x">';
34+
const $ = cheerio.load(html);
35+
36+
const result = convertLazyLoadedImages($).html();
37+
38+
assert.equal(
39+
result,
40+
'<img data-srcset="http://example.com/foo.jpg?w=400 2x, http://example.com/foo.jpg?w=600 3x" srcset="http://example.com/foo.jpg?w=400 2x, http://example.com/foo.jpg?w=600 3x">'
41+
);
42+
});
43+
3144
it('properly handles src and srcset attributes', () => {
3245
const html =
3346
'<img data-src="http://example.com/foo.jpg" data-srcset="http://example.com/foo.jpg 2x">';

0 commit comments

Comments
 (0)