chore: align page.pdf options to the rpc protocol (#3521)
Drive-by: remove unused devices from playwright impl.
This commit is contained in:
parent
e7e8524e14
commit
e54195ccfb
|
|
@ -15,7 +15,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { assert, helper } from '../helper';
|
import { assert } from '../helper';
|
||||||
import * as types from '../types';
|
import * as types from '../types';
|
||||||
import { CRSession } from './crConnection';
|
import { CRSession } from './crConnection';
|
||||||
import { readProtocolStream } from './crProtocolHelper';
|
import { readProtocolStream } from './crProtocolHelper';
|
||||||
|
|
@ -41,31 +41,22 @@ const unitToPixels: { [key: string]: number } = {
|
||||||
'mm': 3.78
|
'mm': 3.78
|
||||||
};
|
};
|
||||||
|
|
||||||
function convertPrintParameterToInches(parameter: (string | number | undefined)): (number | undefined) {
|
function convertPrintParameterToInches(text: string | undefined): number | undefined {
|
||||||
if (typeof parameter === 'undefined')
|
if (text === undefined)
|
||||||
return undefined;
|
return undefined;
|
||||||
let pixels: number;
|
let unit = text.substring(text.length - 2).toLowerCase();
|
||||||
if (helper.isNumber(parameter)) {
|
let valueText = '';
|
||||||
// Treat numbers as pixel values to be aligned with phantom's paperSize.
|
if (unitToPixels.hasOwnProperty(unit)) {
|
||||||
pixels = parameter;
|
valueText = text.substring(0, text.length - 2);
|
||||||
} 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];
|
|
||||||
} else {
|
} 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;
|
return pixels / 96;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,7 +78,6 @@ export class CRPDF {
|
||||||
pageRanges = '',
|
pageRanges = '',
|
||||||
preferCSSPageSize = false,
|
preferCSSPageSize = false,
|
||||||
margin = {},
|
margin = {},
|
||||||
path = null
|
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let paperWidth = 8.5;
|
let paperWidth = 8.5;
|
||||||
|
|
@ -124,6 +114,6 @@ export class CRPDF {
|
||||||
pageRanges,
|
pageRanges,
|
||||||
preferCSSPageSize
|
preferCSSPageSize
|
||||||
});
|
});
|
||||||
return await readProtocolStream(this._client, result.stream!, path);
|
return await readProtocolStream(this._client, result.stream!, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,12 @@ import { Dispatcher, DispatcherScope } from './dispatcher';
|
||||||
import { SelectorsDispatcher } from './selectorsDispatcher';
|
import { SelectorsDispatcher } from './selectorsDispatcher';
|
||||||
import { Electron } from '../../server/electron';
|
import { Electron } from '../../server/electron';
|
||||||
import { ElectronDispatcher } from './electronDispatcher';
|
import { ElectronDispatcher } from './electronDispatcher';
|
||||||
|
import { DeviceDescriptors } from '../../deviceDescriptors';
|
||||||
|
|
||||||
export class PlaywrightDispatcher extends Dispatcher<Playwright, PlaywrightInitializer> implements PlaywrightChannel {
|
export class PlaywrightDispatcher extends Dispatcher<Playwright, PlaywrightInitializer> implements PlaywrightChannel {
|
||||||
constructor(scope: DispatcherScope, playwright: Playwright) {
|
constructor(scope: DispatcherScope, playwright: Playwright) {
|
||||||
const electron = (playwright as any).electron as (Electron | undefined);
|
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 }));
|
.map(([name, descriptor]) => ({ name, descriptor }));
|
||||||
super(scope, playwright, 'Playwright', {
|
super(scope, playwright, 'Playwright', {
|
||||||
chromium: new BrowserTypeDispatcher(scope, playwright.chromium),
|
chromium: new BrowserTypeDispatcher(scope, playwright.chromium),
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,6 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as types from '../types';
|
|
||||||
import { DeviceDescriptors } from '../deviceDescriptors';
|
|
||||||
import { Chromium } from './chromium';
|
import { Chromium } from './chromium';
|
||||||
import { WebKit } from './webkit';
|
import { WebKit } from './webkit';
|
||||||
import { Firefox } from './firefox';
|
import { Firefox } from './firefox';
|
||||||
|
|
@ -24,14 +22,11 @@ import * as browserPaths from '../install/browserPaths';
|
||||||
|
|
||||||
export class Playwright {
|
export class Playwright {
|
||||||
readonly selectors = selectors;
|
readonly selectors = selectors;
|
||||||
readonly devices: types.Devices;
|
|
||||||
readonly chromium: Chromium;
|
readonly chromium: Chromium;
|
||||||
readonly firefox: Firefox;
|
readonly firefox: Firefox;
|
||||||
readonly webkit: WebKit;
|
readonly webkit: WebKit;
|
||||||
|
|
||||||
constructor(packagePath: string, browsers: browserPaths.BrowserDescriptor[]) {
|
constructor(packagePath: string, browsers: browserPaths.BrowserDescriptor[]) {
|
||||||
this.devices = DeviceDescriptors;
|
|
||||||
|
|
||||||
const chromium = browsers.find(browser => browser.name === 'chromium');
|
const chromium = browsers.find(browser => browser.name === 'chromium');
|
||||||
this.chromium = new Chromium(packagePath, chromium!);
|
this.chromium = new Chromium(packagePath, chromium!);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -116,11 +116,10 @@ export type PDFOptions = {
|
||||||
landscape?: boolean,
|
landscape?: boolean,
|
||||||
pageRanges?: string,
|
pageRanges?: string,
|
||||||
format?: string,
|
format?: string,
|
||||||
width?: string|number,
|
width?: string,
|
||||||
height?: string|number,
|
height?: string,
|
||||||
preferCSSPageSize?: boolean,
|
preferCSSPageSize?: boolean,
|
||||||
margin?: {top?: string|number, bottom?: string|number, left?: string|number, right?: string|number},
|
margin?: {top?: string, bottom?: string, left?: string, right?: string},
|
||||||
path?: string,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CSSCoverageOptions = {
|
export type CSSCoverageOptions = {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue