Browse Source

fix: `webContents.print` parameter validation error (#38614)

Shelley Vohr 1 year ago
parent
commit
c0d9764de9
3 changed files with 29 additions and 7 deletions
  1. 17 7
      lib/browser/api/web-contents.ts
  2. 8 0
      spec/api-web-contents-spec.ts
  3. 4 0
      typings/internal-electron.d.ts

+ 17 - 7
lib/browser/api/web-contents.ts

@@ -337,10 +337,9 @@ 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 (printOptions: ElectronInternal.WebContentsPrintOptions, callback) {
-  const options = printOptions ?? {};
-  if (options.pageSize) {
-    const pageSize = options.pageSize;
+WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
+  if (typeof options === 'object') {
+    const pageSize = options.pageSize ?? 'A4';
     if (typeof pageSize === 'object') {
       if (!pageSize.height || !pageSize.width) {
         throw new Error('height and width properties are required for pageSize');
@@ -357,10 +356,21 @@ WebContents.prototype.print = function (printOptions: ElectronInternal.WebConten
         name: 'CUSTOM',
         custom_display_name: 'Custom',
         height_microns: height,
-        width_microns: width
+        width_microns: width,
+        imageable_area_left_microns: 0,
+        imageable_area_bottom_microns: 0,
+        imageable_area_right_microns: width,
+        imageable_area_top_microns: height
+      };
+    } else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
+      const mediaSize = PDFPageSizes[pageSize];
+      options.mediaSize = {
+        ...mediaSize,
+        imageable_area_left_microns: 0,
+        imageable_area_bottom_microns: 0,
+        imageable_area_right_microns: mediaSize.width_microns,
+        imageable_area_top_microns: mediaSize.height_microns
       };
-    } else if (PDFPageSizes[pageSize]) {
-      options.mediaSize = PDFPageSizes[pageSize];
     } else {
       throw new Error(`Unsupported pageSize: ${pageSize}`);
     }

+ 8 - 0
spec/api-web-contents-spec.ts

@@ -194,6 +194,14 @@ describe('webContents module', () => {
       }).to.throw('webContents.print(): Invalid print settings specified.');
     });
 
+    it('throws when an invalid pageSize is passed', () => {
+      const badSize = 5;
+      expect(() => {
+        // @ts-ignore this line is intentionally incorrect
+        w.webContents.print({ pageSize: badSize });
+      }).to.throw(`Unsupported pageSize: ${badSize}`);
+    });
+
     it('throws when an invalid callback is passed', () => {
       expect(() => {
         // @ts-ignore this line is intentionally incorrect

+ 4 - 0
typings/internal-electron.d.ts

@@ -242,6 +242,10 @@ declare namespace ElectronInternal {
     custom_display_name: string,
     height_microns: number,
     width_microns: number,
+    imageable_area_left_microns?: number,
+    imageable_area_bottom_microns?: number,
+    imageable_area_right_microns?: number,
+    imageable_area_top_microns?: number,
     is_default?: 'true',
   }