From 837b10b15ef3048e7cac9640d20d824e1d4c3e07 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Tue, 9 Aug 2022 15:17:42 +0200 Subject: [PATCH] chore: rename `DOMWorld` to `IsolatedWorld` (#8761) --- src/common/AriaQueryHandler.ts | 20 +++++++---- src/common/ElementHandle.ts | 2 +- src/common/ExecutionContext.ts | 6 ++-- src/common/FrameManager.ts | 12 +++---- src/common/{DOMWorld.ts => IsolatedWorld.ts} | 35 +++++++++++--------- src/common/Page.ts | 2 +- src/common/QueryHandler.ts | 8 ++--- src/types.ts | 2 +- 8 files changed, 48 insertions(+), 39 deletions(-) rename src/common/{DOMWorld.ts => IsolatedWorld.ts} (97%) diff --git a/src/common/AriaQueryHandler.ts b/src/common/AriaQueryHandler.ts index 97731ed5669d0..0e8677c360ee1 100644 --- a/src/common/AriaQueryHandler.ts +++ b/src/common/AriaQueryHandler.ts @@ -17,7 +17,11 @@ import {Protocol} from 'devtools-protocol'; import {assert} from './assert.js'; import {CDPSession} from './Connection.js'; -import {DOMWorld, PageBinding, WaitForSelectorOptions} from './DOMWorld.js'; +import { + IsolatedWorld, + PageBinding, + WaitForSelectorOptions, +} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; import {JSHandle} from './JSHandle.js'; import {InternalQueryHandler} from './QueryHandler.js'; @@ -101,19 +105,19 @@ const queryOne = async ( }; const waitFor = async ( - domWorld: DOMWorld, + isolatedWorld: IsolatedWorld, selector: string, options: WaitForSelectorOptions ): Promise | null> => { const binding: PageBinding = { name: 'ariaQuerySelector', pptrFunction: async (selector: string) => { - const root = options.root || (await domWorld._document()); + const root = options.root || (await isolatedWorld._document()); const element = await queryOne(root, selector); return element; }, }; - return (await domWorld._waitForSelectorInPage( + return (await isolatedWorld._waitForSelectorInPage( (_: Element, selector: string) => { return ( globalThis as unknown as { @@ -135,11 +139,13 @@ const queryAll = async ( const {name, role} = parseAriaSelector(selector); const res = await queryAXTree(exeCtx._client, element, name, role); const world = exeCtx._world!; - return (await Promise.all( + return Promise.all( res.map(axNode => { - return world.adoptBackendNode(axNode.backendDOMNodeId); + return world.adoptBackendNode(axNode.backendDOMNodeId) as Promise< + ElementHandle + >; }) - )) as Array>; + ); }; const queryAllArray = async ( diff --git a/src/common/ElementHandle.ts b/src/common/ElementHandle.ts index dea138d3c2644..feacee60094e6 100644 --- a/src/common/ElementHandle.ts +++ b/src/common/ElementHandle.ts @@ -1,7 +1,7 @@ import {Protocol} from 'devtools-protocol'; import {assert} from './assert.js'; import {CDPSession} from './Connection.js'; -import {WaitForSelectorOptions} from './DOMWorld.js'; +import {WaitForSelectorOptions} from './IsolatedWorld.js'; import {ExecutionContext} from './ExecutionContext.js'; import {Frame, FrameManager} from './FrameManager.js'; import { diff --git a/src/common/ExecutionContext.ts b/src/common/ExecutionContext.ts index 05d8941f3d273..ba19d71469917 100644 --- a/src/common/ExecutionContext.ts +++ b/src/common/ExecutionContext.ts @@ -17,7 +17,7 @@ import {Protocol} from 'devtools-protocol'; import {assert} from './assert.js'; import {CDPSession} from './Connection.js'; -import {DOMWorld} from './DOMWorld.js'; +import {IsolatedWorld} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; import {Frame} from './FrameManager.js'; import {JSHandle} from './JSHandle.js'; @@ -57,7 +57,7 @@ export class ExecutionContext { /** * @internal */ - _world?: DOMWorld; + _world?: IsolatedWorld; /** * @internal */ @@ -73,7 +73,7 @@ export class ExecutionContext { constructor( client: CDPSession, contextPayload: Protocol.Runtime.ExecutionContextDescription, - world?: DOMWorld + world?: IsolatedWorld ) { this._client = client; this._world = world; diff --git a/src/common/FrameManager.ts b/src/common/FrameManager.ts index 6d1efd82029df..d79c8e3025267 100644 --- a/src/common/FrameManager.ts +++ b/src/common/FrameManager.ts @@ -17,7 +17,7 @@ import {Protocol} from 'devtools-protocol'; import {assert} from './assert.js'; import {CDPSession} from './Connection.js'; -import {DOMWorld, WaitForSelectorOptions} from './DOMWorld.js'; +import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; import {EventEmitter} from './EventEmitter.js'; import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js'; @@ -530,7 +530,7 @@ export class FrameManager extends EventEmitter { const frameId = auxData && auxData.frameId; const frame = typeof frameId === 'string' ? this.#frames.get(frameId) : undefined; - let world: DOMWorld | undefined; + let world: IsolatedWorld | undefined; if (frame) { // Only care about execution contexts created for the current session. if (frame._client() !== session) { @@ -763,11 +763,11 @@ export class Frame { /** * @internal */ - _mainWorld!: DOMWorld; + _mainWorld!: IsolatedWorld; /** * @internal */ - _secondaryWorld!: DOMWorld; + _secondaryWorld!: IsolatedWorld; /** * @internal */ @@ -803,13 +803,13 @@ export class Frame { */ _updateClient(client: CDPSession): void { this.#client = client; - this._mainWorld = new DOMWorld( + this._mainWorld = new IsolatedWorld( this.#client, this._frameManager, this, this._frameManager._timeoutSettings ); - this._secondaryWorld = new DOMWorld( + this._secondaryWorld = new IsolatedWorld( this.#client, this._frameManager, this, diff --git a/src/common/DOMWorld.ts b/src/common/IsolatedWorld.ts similarity index 97% rename from src/common/DOMWorld.ts rename to src/common/IsolatedWorld.ts index 59b88058cfcf8..dc996af9f6da3 100644 --- a/src/common/DOMWorld.ts +++ b/src/common/IsolatedWorld.ts @@ -72,7 +72,7 @@ export interface PageBinding { /** * @internal */ -export class DOMWorld { +export class IsolatedWorld { #frameManager: FrameManager; #client: CDPSession; #frame: Frame; @@ -564,7 +564,7 @@ export class DOMWorld { // Previous operation added the binding so we are done. if ( this.#ctxBindings.has( - DOMWorld.#bindingIdentifier(name, context._contextId) + IsolatedWorld.#bindingIdentifier(name, context._contextId) ) ) { return; @@ -604,7 +604,7 @@ export class DOMWorld { } } this.#ctxBindings.add( - DOMWorld.#bindingIdentifier(name, context._contextId) + IsolatedWorld.#bindingIdentifier(name, context._contextId) ); }; @@ -632,7 +632,7 @@ export class DOMWorld { if ( type !== 'internal' || !this.#ctxBindings.has( - DOMWorld.#bindingIdentifier(name, context._contextId) + IsolatedWorld.#bindingIdentifier(name, context._contextId) ) ) { return; @@ -695,7 +695,7 @@ export class DOMWorld { return checkWaitForOptions(node, waitForVisible, waitForHidden); } const waitTaskOptions: WaitTaskOptions = { - domWorld: this, + isolatedWorld: this, predicateBody: makePredicateString(predicate, queryOne), predicateAcceptsContextElement: true, title, @@ -723,7 +723,7 @@ export class DOMWorld { const {polling = 'raf', timeout = this.#timeoutSettings.timeout()} = options; const waitTaskOptions: WaitTaskOptions = { - domWorld: this, + isolatedWorld: this, predicateBody: pageFunction, predicateAcceptsContextElement: false, title: 'function', @@ -769,7 +769,7 @@ export class DOMWorld { * @internal */ export interface WaitTaskOptions { - domWorld: DOMWorld; + isolatedWorld: IsolatedWorld; predicateBody: Function | string; predicateAcceptsContextElement: boolean; title: string; @@ -786,7 +786,7 @@ const noop = (): void => {}; * @internal */ export class WaitTask { - #domWorld: DOMWorld; + #isolatedWorld: IsolatedWorld; #polling: 'raf' | 'mutation' | number; #timeout: number; #predicateBody: string; @@ -824,7 +824,7 @@ export class WaitTask { return `return (${predicateBody})(...args);`; } - this.#domWorld = options.domWorld; + this.#isolatedWorld = options.isolatedWorld; this.#polling = options.polling; this.#timeout = options.timeout; this.#root = options.root || null; @@ -834,9 +834,9 @@ export class WaitTask { this.#args = options.args; this.#binding = options.binding; this.#runCount = 0; - this.#domWorld._waitTasks.add(this); + this.#isolatedWorld._waitTasks.add(this); if (this.#binding) { - this.#domWorld._boundFunctions.set( + this.#isolatedWorld._boundFunctions.set( this.#binding.name, this.#binding.pptrFunction ); @@ -868,12 +868,15 @@ export class WaitTask { const runCount = ++this.#runCount; let success: JSHandle | null = null; let error: Error | null = null; - const context = await this.#domWorld.executionContext(); + const context = await this.#isolatedWorld.executionContext(); if (this.#terminated || runCount !== this.#runCount) { return; } if (this.#binding) { - await this.#domWorld._addBindingToContext(context, this.#binding.name); + await this.#isolatedWorld._addBindingToContext( + context, + this.#binding.name + ); } if (this.#terminated || runCount !== this.#runCount) { return; @@ -904,7 +907,7 @@ export class WaitTask { // throw an error - ignore this predicate run altogether. if ( !error && - (await this.#domWorld + (await this.#isolatedWorld .evaluate(s => { return !s; }, success) @@ -922,7 +925,7 @@ export class WaitTask { if (error.message.includes('TypeError: binding is not a function')) { return this.rerun(); } - // When frame is detached the task should have been terminated by the DOMWorld. + // When frame is detached the task should have been terminated by the IsolatedWorld. // This can fail if we were adding this task while the frame was detached, // so we terminate here instead. if ( @@ -960,7 +963,7 @@ export class WaitTask { #cleanup(): void { this.#timeoutTimer !== undefined && clearTimeout(this.#timeoutTimer); - this.#domWorld._waitTasks.delete(this); + this.#isolatedWorld._waitTasks.delete(this); } } diff --git a/src/common/Page.ts b/src/common/Page.ts index 4983178d4fe05..e21c905fa1f06 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -23,7 +23,7 @@ import {CDPSession, CDPSessionEmittedEvents} from './Connection.js'; import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js'; import {Coverage} from './Coverage.js'; import {Dialog} from './Dialog.js'; -import {WaitForSelectorOptions} from './DOMWorld.js'; +import {WaitForSelectorOptions} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; import {EmulationManager} from './EmulationManager.js'; import {EventEmitter, Handler} from './EventEmitter.js'; diff --git a/src/common/QueryHandler.ts b/src/common/QueryHandler.ts index dcdadc37783d4..7f952c7d33850 100644 --- a/src/common/QueryHandler.ts +++ b/src/common/QueryHandler.ts @@ -15,7 +15,7 @@ */ import {ariaHandler} from './AriaQueryHandler.js'; -import {DOMWorld, WaitForSelectorOptions} from './DOMWorld.js'; +import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js'; import {ElementHandle} from './ElementHandle.js'; import {JSHandle} from './JSHandle.js'; @@ -72,7 +72,7 @@ export interface InternalQueryHandler { * Akin to {@link Window.prototype.querySelectorAll}. */ waitFor?: ( - domWorld: DOMWorld, + isolatedWorld: IsolatedWorld, selector: string, options: WaitForSelectorOptions ) => Promise | null>; @@ -95,11 +95,11 @@ function internalizeCustomQueryHandler( return null; }; internalHandler.waitFor = ( - domWorld: DOMWorld, + isolatedWorld: IsolatedWorld, selector: string, options: WaitForSelectorOptions ) => { - return domWorld._waitForSelectorInPage(queryOne, selector, options); + return isolatedWorld._waitForSelectorInPage(queryOne, selector, options); }; } diff --git a/src/types.ts b/src/types.ts index df7eead4de671..33c405d68ffcc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,7 +18,6 @@ export * from './common/Connection.js'; export * from './common/ConnectionTransport.js'; export * from './common/ConsoleMessage.js'; export * from './common/Coverage.js'; -export * from './common/DOMWorld.js'; export * from './common/Debug.js'; export * from './common/DeviceDescriptors.js'; export * from './common/Dialog.js'; @@ -33,6 +32,7 @@ export * from './common/FrameManager.js'; export * from './common/HTTPRequest.js'; export * from './common/HTTPResponse.js'; export * from './common/Input.js'; +export * from './common/IsolatedWorld.js'; export * from './common/JSHandle.js'; export * from './common/LifecycleWatcher.js'; export * from './common/NetworkConditions.js';