Skip to content
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

fix: webContents.print options should be optional #41467

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,11 @@ WebContents.prototype.printToPDF = async function (options) {

// TODO(codebytere): deduplicate argument sanitization by moving rest of
// print param logic into new file shared between printToPDF and print
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
if (typeof options !== 'object') {
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions = {}, callback) {
if (typeof options !== 'object' || options == null) {
throw new TypeError('webContents.print(): Invalid print settings specified.');
}

const printSettings: Record<string, any> = { ...options };

const pageSize = options.pageSize ?? 'A4';
if (typeof pageSize === 'object') {
if (!pageSize.height || !pageSize.width) {
Expand All @@ -283,7 +281,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
throw new RangeError('height and width properties must be minimum 352 microns.');
}

printSettings.mediaSize = {
options.mediaSize = {
name: 'CUSTOM',
custom_display_name: 'Custom',
height_microns: height,
Expand All @@ -295,7 +293,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
};
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
const mediaSize = PDFPageSizes[pageSize];
printSettings.mediaSize = {
options.mediaSize = {
...mediaSize,
imageable_area_left_microns: 0,
imageable_area_bottom_microns: 0,
Expand All @@ -308,9 +306,9 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri

if (this._print) {
if (callback) {
this._print(printSettings, callback);
this._print(options, callback);
} else {
this._print(printSettings);
this._print(options);
}
} else {
console.error('Error: Printing feature is disabled.');
Expand Down
21 changes: 21 additions & 0 deletions spec/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,32 @@ describe('webContents module', () => {

afterEach(closeAllWindows);

it('does not throw when options are not passed', () => {
expect(() => {
w.webContents.print();
}).not.to.throw();

expect(() => {
w.webContents.print(undefined);
}).not.to.throw();
});

it('does not throw when options object is empty', () => {
expect(() => {
w.webContents.print({});
}).not.to.throw();
});

it('throws when invalid settings are passed', () => {
expect(() => {
// @ts-ignore this line is intentionally incorrect
w.webContents.print(true);
}).to.throw('webContents.print(): Invalid print settings specified.');

expect(() => {
// @ts-ignore this line is intentionally incorrect
w.webContents.print(null);
}).to.throw('webContents.print(): Invalid print settings specified.');
});

it('throws when an invalid pageSize is passed', () => {
Expand Down