-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for image srcset capturing (#1534)
* Added support for image srcset capturing * Added tests + addressed comments * Fix breaking tests * Remove default value captureSrcset * Fix edge case
- Loading branch information
1 parent
f3b0fad
commit 76df992
Showing
9 changed files
with
243 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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,50 @@ | ||
function getSrcsets(dom) { | ||
const links = new Set(); | ||
|
||
for (let img of dom.querySelectorAll('img[srcset]')) { | ||
handleSrcSet(img.srcset, links); | ||
} | ||
|
||
for (let picture of dom.querySelectorAll('picture')) { | ||
for (let source of picture.querySelectorAll('source')) { | ||
handleSrcSet(source.srcset, links); | ||
} | ||
} | ||
return Array.from(links); | ||
} | ||
|
||
function handleSrcSet(srcSet, links) { | ||
let pattern = /,\s+/; | ||
|
||
// We found couple of combination of srcset which needs different regex. | ||
// example - https://url.com?param=a,b <--- here only separeting with , will cause incorrect capture. | ||
// srcset = https://abc.com 320w,https://abc.com/a 400 <--- here srcset doesnot have space after comm. | ||
if (!srcSet.match(pattern)) { | ||
pattern = /,/; | ||
} | ||
srcSet = srcSet.split(pattern); | ||
for (let src of srcSet) { | ||
src = src.trim(); | ||
src = src.split(' ')[0]; | ||
links.add(getFormattedLink(src)); | ||
} | ||
} | ||
function getFormattedLink(src) { | ||
const anchor = document.createElement('a'); | ||
anchor.href = src; | ||
return anchor.href; | ||
} | ||
|
||
export function loadAllSrcsetLinks() { | ||
const allImgTags = []; | ||
const links = getSrcsets(document); | ||
for (const link of links) { | ||
const img = document.createElement('img'); | ||
img.src = link; | ||
allImgTags.push(img); | ||
} | ||
// Adding to window so GC won't abort request | ||
window.allImgTags = allImgTags; | ||
return allImgTags; | ||
} | ||
export default loadAllSrcsetLinks; |
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,61 @@ | ||
import { withExample, platforms } from './helpers'; | ||
|
||
import { loadAllSrcsetLinks } from '@percy/dom'; | ||
|
||
describe('loadAllSrcsetLinks', () => { | ||
let imgTags; | ||
|
||
platforms.forEach((platform) => { | ||
it(`${platform}: capture url from img srcset`, async () => { | ||
withExample(` | ||
<img srcset="base/test/assets/example.webp, base/test/assets/example.png 100px" /> | ||
`); | ||
|
||
imgTags = loadAllSrcsetLinks(); | ||
|
||
expect(imgTags.map(s => s.src)).toEqual(['http://localhost:9876/base/test/assets/example.webp', 'http://localhost:9876/base/test/assets/example.png']); | ||
}); | ||
|
||
it(`${platform}: capture url from img srcset where there is no space after ,`, async () => { | ||
withExample(` | ||
<img srcset="base/test/assets/example.webp 200px,base/test/assets/example.png 100px" /> | ||
`); | ||
|
||
imgTags = loadAllSrcsetLinks(); | ||
|
||
expect(imgTags.map(s => s.src)).toEqual(['http://localhost:9876/base/test/assets/example.webp', 'http://localhost:9876/base/test/assets/example.png']); | ||
}); | ||
|
||
it(`${platform}: capture url from source of picture`, async () => { | ||
withExample(` | ||
<picture> | ||
<source srcset='//locahost:9876/base/test/assets/example.webp, //localhost:9876/base/test/assets/example.png 2x' /> | ||
<source srcset='//locahost:9876/base/test/assets/example.jpeg 100px, //locahost:9876/base/test/assets/example1.jpeg 200px' /> | ||
</picture> | ||
`); | ||
|
||
imgTags = loadAllSrcsetLinks(); | ||
|
||
expect(imgTags.map(s => s.src)).toEqual([ | ||
'http://locahost:9876/base/test/assets/example.webp', | ||
'http://localhost:9876/base/test/assets/example.png', | ||
'http://locahost:9876/base/test/assets/example.jpeg', | ||
'http://locahost:9876/base/test/assets/example1.jpeg' | ||
]); | ||
}); | ||
|
||
it(`${platform}: srcset inside shadowroot`, () => { | ||
withExample(` | ||
<img srcset="/base/test/assets/example.webp, /base/test/assets/example.png 100px, /base/test/assets/example1.png 2x" /> | ||
`, { withShadow: true }); | ||
|
||
imgTags = loadAllSrcsetLinks(); | ||
|
||
expect(imgTags.map(s => s.src)).toEqual([ | ||
'http://localhost:9876/base/test/assets/example.webp', | ||
'http://localhost:9876/base/test/assets/example.png', | ||
'http://localhost:9876/base/test/assets/example1.png' | ||
]); | ||
}); | ||
}); | ||
}); |