-
Notifications
You must be signed in to change notification settings - Fork 6.1k
feat(screenshot): add webp output format #41152
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
Open
yury-s
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
yury-s:feat-screenshot-webp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f889a7d
feat(screenshot): add webp output format
yury-s a4a5135
feat(webp): add self-built libwebp WASM codec (@utils/webp)
yury-s 655e218
feat(webp): use the webp codec in screenshots, comparisons and scaling
yury-s 8f82000
fix(webp): address review comments
yury-s bcede3a
feat(webp): default webp screenshots to lossless
yury-s e700ec3
docs(webp): document lossless default; fix two test failures
yury-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ declare global { | |
| } | ||
|
|
||
| export type ScreenshotOptions = { | ||
| type?: 'png' | 'jpeg'; | ||
| type?: 'png' | 'jpeg' | 'webp'; | ||
| quality?: number; | ||
| omitBackground?: boolean; | ||
| animations?: 'disabled' | 'allow'; | ||
|
|
@@ -297,7 +297,7 @@ export class Screenshotter { | |
| } | ||
| } | ||
|
|
||
| private async _screenshot(progress: Progress, format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, fitsViewport: boolean, options: ScreenshotOptions): Promise<Buffer> { | ||
| private async _screenshot(progress: Progress, format: 'png' | 'jpeg' | 'webp', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, fitsViewport: boolean, options: ScreenshotOptions): Promise<Buffer> { | ||
| if ((options as any).__testHookBeforeScreenshot) | ||
| await progress.race((options as any).__testHookBeforeScreenshot()); | ||
|
|
||
|
|
@@ -307,7 +307,8 @@ export class Screenshotter { | |
| const cleanupHighlight = await this._maskElements(progress, options); | ||
|
|
||
| try { | ||
| const quality = format === 'jpeg' ? options.quality ?? 80 : undefined; | ||
| // webp treats quality 100 (or omitted) as lossless, so default it to that. | ||
|
Collaborator
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. Useless comment! |
||
| const quality = format === 'jpeg' ? options.quality ?? 80 : format === 'webp' ? options.quality ?? 100 : undefined; | ||
| const buffer = await this._page.delegate.takeScreenshot(progress, format, documentRect, viewportRect, quality, fitsViewport, options.scale || 'device'); | ||
| await progress.race(cleanupHighlight()); | ||
| if (shouldSetDefaultBackground) | ||
|
|
@@ -353,20 +354,20 @@ function trimClipToSize(clip: types.Rect, size: types.Size): types.Rect { | |
| return result; | ||
| } | ||
|
|
||
| export function validateScreenshotOptions(options: ScreenshotOptions): 'png' | 'jpeg' { | ||
| let format: 'png' | 'jpeg' | null = null; | ||
| export function validateScreenshotOptions(options: ScreenshotOptions): 'png' | 'jpeg' | 'webp' { | ||
| let format: 'png' | 'jpeg' | 'webp' | null = null; | ||
| // options.type takes precedence over inferring the type from options.path | ||
| // because it may be a 0-length file with no extension created beforehand (i.e. as a temp file). | ||
| if (options.type) { | ||
| assert(options.type === 'png' || options.type === 'jpeg', 'Unknown options.type value: ' + options.type); | ||
| assert(options.type === 'png' || options.type === 'jpeg' || options.type === 'webp', 'Unknown options.type value: ' + options.type); | ||
| format = options.type; | ||
| } | ||
|
|
||
| if (!format) | ||
| format = 'png'; | ||
|
|
||
| if (options.quality !== undefined) { | ||
| assert(format === 'jpeg', 'options.quality is unsupported for the ' + format + ' screenshots'); | ||
| assert(format !== 'png', 'options.quality is unsupported for the ' + format + ' screenshots'); | ||
| assert(typeof options.quality === 'number', 'Expected options.quality to be a number but found ' + (typeof options.quality)); | ||
| assert(Number.isInteger(options.quality), 'Expected options.quality to be an integer'); | ||
| assert(options.quality >= 0 && options.quality <= 100, 'Expected options.quality to be between 0 and 100 (inclusive), got ' + options.quality); | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why not move both
comparatorandareEqualScreenshotsto the line 745 new code, instead of callinggetComparator()twice and writing a lengthy comment?