// Copyright (c) Microsoft Corporation. // Licensed under the MIT license. import * as js from './javascript'; import { helper } from './helper'; import * as dom from './dom'; type Boxed = { [Index in keyof Args]: Args[Index] | js.JSHandle }; type PageFunction = string | ((...args: Args) => R | Promise); type PageFunctionOn = string | ((on: On, ...args: Args) => R | Promise); type Handle = T extends Node ? dom.ElementHandle : js.JSHandle; type ElementForSelector = T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : Element; export type Evaluate = (pageFunction: PageFunction, ...args: Boxed) => Promise; export type EvaluateHandle = (pageFunction: PageFunction, ...args: Boxed) => Promise>; export type $Eval = (selector: S, pageFunction: PageFunctionOn, Args, R>, ...args: Boxed) => Promise; export type $$Eval = (selector: S, pageFunction: PageFunctionOn[], Args, R>, ...args: Boxed) => Promise; export type EvaluateOn = (pageFunction: PageFunctionOn, ...args: Boxed) => Promise; export type EvaluateHandleOn = (pageFunction: PageFunctionOn, ...args: Boxed) => Promise>; export type Size = { width: number, height: number }; export type Point = { x: number, y: number }; export type Rect = Size & Point; export type Quad = [ Point, Point, Point, Point ]; export type TimeoutOptions = { timeout?: number }; export type Visibility = 'visible' | 'hidden' | 'any'; export type Selector = { selector: string, visibility?: Visibility }; export type Polling = 'raf' | 'mutation' | number; export type WaitForFunctionOptions = TimeoutOptions & { polling?: Polling }; export function selectorToString(selector: string | Selector): string { if (typeof selector === 'string') return selector; let label; switch (selector.visibility) { case 'visible': label = '[visible] '; break; case 'hidden': label = '[hidden] '; break; case 'any': case undefined: label = ''; break; } return `${label}${selector.selector}`; } // Ensures that we don't use accidental properties in selector, e.g. scope. export function clearSelector(selector: string | Selector): string | Selector { if (helper.isString(selector)) return selector; return { selector: selector.selector, visibility: selector.visibility }; } export type ElementScreenshotOptions = { type?: 'png' | 'jpeg', path?: string, quality?: number, omitBackground?: boolean, }; export type ScreenshotOptions = ElementScreenshotOptions & { fullPage?: boolean, clip?: Rect, }; export type Viewport = { width: number; height: number; deviceScaleFactor?: number; isMobile?: boolean; isLandscape?: boolean; hasTouch?: boolean; };