Skip to content

Commit

Permalink
chore: align page.pdf options to the rpc protocol (#3521)
Browse files Browse the repository at this point in the history
Drive-by: remove unused devices from playwright impl.
  • Loading branch information
dgozman authored Aug 19, 2020
1 parent e7e8524 commit e54195c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
40 changes: 15 additions & 25 deletions src/chromium/crPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { assert, helper } from '../helper';
import { assert } from '../helper';
import * as types from '../types';
import { CRSession } from './crConnection';
import { readProtocolStream } from './crProtocolHelper';
Expand All @@ -41,31 +41,22 @@ const unitToPixels: { [key: string]: number } = {
'mm': 3.78
};

function convertPrintParameterToInches(parameter: (string | number | undefined)): (number | undefined) {
if (typeof parameter === 'undefined')
function convertPrintParameterToInches(text: string | undefined): number | undefined {
if (text === undefined)
return undefined;
let pixels: number;
if (helper.isNumber(parameter)) {
// Treat numbers as pixel values to be aligned with phantom's paperSize.
pixels = parameter;
} else if (helper.isString(parameter)) {
const text: string = parameter;
let unit = text.substring(text.length - 2).toLowerCase();
let valueText = '';
if (unitToPixels.hasOwnProperty(unit)) {
valueText = text.substring(0, text.length - 2);
} else {
// In case of unknown unit try to parse the whole parameter as number of pixels.
// This is consistent with phantom's paperSize behavior.
unit = 'px';
valueText = text;
}
const value = Number(valueText);
assert(!isNaN(value), 'Failed to parse parameter value: ' + text);
pixels = value * unitToPixels[unit];
let unit = text.substring(text.length - 2).toLowerCase();
let valueText = '';
if (unitToPixels.hasOwnProperty(unit)) {
valueText = text.substring(0, text.length - 2);
} else {
throw new Error('page.pdf() Cannot handle parameter type: ' + (typeof parameter));
// In case of unknown unit try to parse the whole parameter as number of pixels.
// This is consistent with phantom's paperSize behavior.
unit = 'px';
valueText = text;
}
const value = Number(valueText);
assert(!isNaN(value), 'Failed to parse parameter value: ' + text);
const pixels = value * unitToPixels[unit];
return pixels / 96;
}

Expand All @@ -87,7 +78,6 @@ export class CRPDF {
pageRanges = '',
preferCSSPageSize = false,
margin = {},
path = null
} = options;

let paperWidth = 8.5;
Expand Down Expand Up @@ -124,6 +114,6 @@ export class CRPDF {
pageRanges,
preferCSSPageSize
});
return await readProtocolStream(this._client, result.stream!, path);
return await readProtocolStream(this._client, result.stream!, null);
}
}
3 changes: 2 additions & 1 deletion src/rpc/server/playwrightDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import { Dispatcher, DispatcherScope } from './dispatcher';
import { SelectorsDispatcher } from './selectorsDispatcher';
import { Electron } from '../../server/electron';
import { ElectronDispatcher } from './electronDispatcher';
import { DeviceDescriptors } from '../../deviceDescriptors';

export class PlaywrightDispatcher extends Dispatcher<Playwright, PlaywrightInitializer> implements PlaywrightChannel {
constructor(scope: DispatcherScope, playwright: Playwright) {
const electron = (playwright as any).electron as (Electron | undefined);
const deviceDescriptors = Object.entries(playwright.devices)
const deviceDescriptors = Object.entries(DeviceDescriptors)
.map(([name, descriptor]) => ({ name, descriptor }));
super(scope, playwright, 'Playwright', {
chromium: new BrowserTypeDispatcher(scope, playwright.chromium),
Expand Down
5 changes: 0 additions & 5 deletions src/server/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import * as types from '../types';
import { DeviceDescriptors } from '../deviceDescriptors';
import { Chromium } from './chromium';
import { WebKit } from './webkit';
import { Firefox } from './firefox';
Expand All @@ -24,14 +22,11 @@ import * as browserPaths from '../install/browserPaths';

export class Playwright {
readonly selectors = selectors;
readonly devices: types.Devices;
readonly chromium: Chromium;
readonly firefox: Firefox;
readonly webkit: WebKit;

constructor(packagePath: string, browsers: browserPaths.BrowserDescriptor[]) {
this.devices = DeviceDescriptors;

const chromium = browsers.find(browser => browser.name === 'chromium');
this.chromium = new Chromium(packagePath, chromium!);

Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ export type PDFOptions = {
landscape?: boolean,
pageRanges?: string,
format?: string,
width?: string|number,
height?: string|number,
width?: string,
height?: string,
preferCSSPageSize?: boolean,
margin?: {top?: string|number, bottom?: string|number, left?: string|number, right?: string|number},
path?: string,
margin?: {top?: string, bottom?: string, left?: string, right?: string},
}

export type CSSCoverageOptions = {
Expand Down

0 comments on commit e54195c

Please sign in to comment.