This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from Galooshi/fix-srcset-preload
Fix grabbing urls from srcset attributes
- Loading branch information
Showing
3 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
packages/happo-target-firefox/src/__tests__/getUrlsFromSrcset-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import getUrlsFromSrcset from '../getUrlsFromSrcset'; | ||
|
||
it('handles invalid values', () => { | ||
expect(getUrlsFromSrcset(' ')).toEqual([]); | ||
}); | ||
|
||
it('handles single urls without descriptor', () => { | ||
expect(getUrlsFromSrcset('/foo.png')).toEqual(['/foo.png']); | ||
}); | ||
|
||
it('handles single urls with commas', () => { | ||
expect(getUrlsFromSrcset('/foo,40,50.png')).toEqual(['/foo,40,50.png']); | ||
}); | ||
|
||
it('handles single urls with a width descriptor', () => { | ||
expect(getUrlsFromSrcset('http://foo.com/foo.png 200w')).toEqual(['http://foo.com/foo.png']); | ||
}); | ||
|
||
it('handles single urls with a pixel density descriptor', () => { | ||
expect(getUrlsFromSrcset('http://foo.com/foo.png 1.2x')).toEqual(['http://foo.com/foo.png']); | ||
}); | ||
|
||
it('strips whitespace', () => { | ||
expect(getUrlsFromSrcset(` | ||
/foo.png`)).toEqual(['/foo.png']); | ||
}); | ||
|
||
it('handles multiple urls', () => { | ||
expect(getUrlsFromSrcset('/foo.png 200w,/bar.png 400w')).toEqual(['/foo.png', '/bar.png']); | ||
}); | ||
|
||
it('handles multiple urls with commas', () => { | ||
expect(getUrlsFromSrcset('http://foo.com/foo,40,50 200w,/bar,20.png 400w')).toEqual(['http://foo.com/foo,40,50', '/bar,20.png']); | ||
}); | ||
|
||
it('handles multiple urls with whitespace', () => { | ||
expect(getUrlsFromSrcset(` | ||
http://foo.com/foo,40,50 200w, | ||
/bar,20.png 400w | ||
`)).toEqual(['http://foo.com/foo,40,50', '/bar,20.png']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const SRCSET_ITEM = /([^\s]+)(\s+[0-9.]+[wx])?(,?\s*)/g; | ||
|
||
export default function getUrlsFromSrcset(value) { | ||
const result = []; | ||
let match; | ||
while (match = SRCSET_ITEM.exec(value)) { // eslint-disable-line no-cond-assign | ||
result.push(match[1]); | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters