-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for capturing asset in multiple dpr and width combination #1536
Changes from 7 commits
eb9847d
a7e10e4
61113a9
b62f174
9877cc3
91e1b17
b90df23
dade996
2c31be6
11d8aff
3bad053
5e46151
45e1625
2eeef07
901d936
c9f1b43
7fe9bdb
957bc8a
fe4980f
9897bad
d808e07
240b409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,8 @@ function getSnapshotOptions(options, { config, meta }) { | |
authorization: config.discovery.authorization, | ||
disableCache: config.discovery.disableCache, | ||
captureMockedServiceWorker: config.discovery.captureMockedServiceWorker, | ||
captureSrcset: config.discovery.captureSrcset, | ||
captureResponsiveAssets: config.discovery.captureResponsiveAssets, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be needed, we should call this explicitly again if there is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we should do this automatically. |
||
userAgent: config.discovery.userAgent | ||
} | ||
}, options], (path, prev, next) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) { | ||
srcSet = srcSet.split(/,\s+/); | ||
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 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' | ||
]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not something we want to call per snapshot, move it to the snapshot config validation and cache the response in percy.js so that it can be applied to all snapshots in current build