From 945f1c72629dbfa2c01da83c5632d2e6480ad005 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Fri, 1 Mar 2024 17:11:10 -0500 Subject: [PATCH] fix: webContents.print options should be optional (#41478) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- lib/browser/api/web-contents.ts | 14 ++++++-------- spec/api-web-contents-spec.ts | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index 0205081aa4f5b..71de819911a45 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -262,13 +262,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 = { ...options }; - const pageSize = options.pageSize ?? 'A4'; if (typeof pageSize === 'object') { if (!pageSize.height || !pageSize.width) { @@ -282,7 +280,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, @@ -294,7 +292,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, @@ -307,9 +305,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.'); diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index ea31ce50e349c..4ea4be8033fe6 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -188,11 +188,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', () => {