fix: webContents.print options should be optional (#41480)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
parent
56dfcc5468
commit
17856cf91a
2 changed files with 27 additions and 8 deletions
|
@ -263,13 +263,11 @@ WebContents.prototype.printToPDF = async function (options) {
|
||||||
|
|
||||||
// TODO(codebytere): deduplicate argument sanitization by moving rest of
|
// TODO(codebytere): deduplicate argument sanitization by moving rest of
|
||||||
// print param logic into new file shared between printToPDF and print
|
// print param logic into new file shared between printToPDF and print
|
||||||
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
|
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions = {}, callback) {
|
||||||
if (typeof options !== 'object') {
|
if (typeof options !== 'object' || options == null) {
|
||||||
throw new TypeError('webContents.print(): Invalid print settings specified.');
|
throw new TypeError('webContents.print(): Invalid print settings specified.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const printSettings: Record<string, any> = { ...options };
|
|
||||||
|
|
||||||
const pageSize = options.pageSize ?? 'A4';
|
const pageSize = options.pageSize ?? 'A4';
|
||||||
if (typeof pageSize === 'object') {
|
if (typeof pageSize === 'object') {
|
||||||
if (!pageSize.height || !pageSize.width) {
|
if (!pageSize.height || !pageSize.width) {
|
||||||
|
@ -283,7 +281,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
throw new RangeError('height and width properties must be minimum 352 microns.');
|
throw new RangeError('height and width properties must be minimum 352 microns.');
|
||||||
}
|
}
|
||||||
|
|
||||||
printSettings.mediaSize = {
|
options.mediaSize = {
|
||||||
name: 'CUSTOM',
|
name: 'CUSTOM',
|
||||||
custom_display_name: 'Custom',
|
custom_display_name: 'Custom',
|
||||||
height_microns: height,
|
height_microns: height,
|
||||||
|
@ -295,7 +293,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
};
|
};
|
||||||
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
|
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
|
||||||
const mediaSize = PDFPageSizes[pageSize];
|
const mediaSize = PDFPageSizes[pageSize];
|
||||||
printSettings.mediaSize = {
|
options.mediaSize = {
|
||||||
...mediaSize,
|
...mediaSize,
|
||||||
imageable_area_left_microns: 0,
|
imageable_area_left_microns: 0,
|
||||||
imageable_area_bottom_microns: 0,
|
imageable_area_bottom_microns: 0,
|
||||||
|
@ -308,9 +306,9 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
|
|
||||||
if (this._print) {
|
if (this._print) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
this._print(printSettings, callback);
|
this._print(options, callback);
|
||||||
} else {
|
} else {
|
||||||
this._print(printSettings);
|
this._print(options);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.error('Error: Printing feature is disabled.');
|
console.error('Error: Printing feature is disabled.');
|
||||||
|
|
|
@ -187,11 +187,32 @@ describe('webContents module', () => {
|
||||||
|
|
||||||
afterEach(closeAllWindows);
|
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', () => {
|
it('throws when invalid settings are passed', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
// @ts-ignore this line is intentionally incorrect
|
// @ts-ignore this line is intentionally incorrect
|
||||||
w.webContents.print(true);
|
w.webContents.print(true);
|
||||||
}).to.throw('webContents.print(): Invalid print settings specified.');
|
}).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', () => {
|
it('throws when an invalid pageSize is passed', () => {
|
||||||
|
|
Loading…
Reference in a new issue