Skip to content

Commit

Permalink
fix: remove import cycle in query handlers (#11234)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Oct 23, 2023
1 parent edec7d5 commit 954c75f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ module.exports = {
},
],

'import/no-cycle': [
'error',
{maxDepth: Infinity, allowUnsafeDynamicCyclicDependency: true},
],
'import/no-cycle': ['error', {maxDepth: Infinity}],

'no-restricted-syntax': [
'error',
Expand Down
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/api/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {assert} from '../util/assert.js';
import {AsyncIterableUtil} from '../util/AsyncIterableUtil.js';
import {throwIfDisposed} from '../util/decorators.js';

import {_isElementHandle} from './ElementHandleSymbol.js';
import type {
KeyboardTypeOptions,
KeyPressOptions,
Expand Down Expand Up @@ -149,6 +150,11 @@ export interface ElementScreenshotOptions extends ScreenshotOptions {
export abstract class ElementHandle<
ElementType extends Node = Element,
> extends JSHandle<ElementType> {
/**
* @internal
*/
declare [_isElementHandle]: boolean;

/**
* A given method will have it's `this` replaced with an isolated version of
* `this` when decorated with this decorator.
Expand Down Expand Up @@ -212,6 +218,7 @@ export abstract class ElementHandle<
constructor(handle: JSHandle<ElementType>) {
super();
this.handle = handle;
this[_isElementHandle] = true;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/puppeteer-core/src/api/ElementHandleSymbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @internal
*/
export const _isElementHandle = Symbol('_isElementHandle');
9 changes: 4 additions & 5 deletions packages/puppeteer-core/src/common/QueryHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type {ElementHandle} from '../api/ElementHandle.js';
import {_isElementHandle} from '../api/ElementHandleSymbol.js';
import type {Frame} from '../api/Frame.js';
import type {WaitForSelectorOptions} from '../api/Page.js';
import type PuppeteerUtil from '../injected/injected.js';
Expand Down Expand Up @@ -132,8 +133,7 @@ export class QueryHandler {
return context.puppeteerUtil;
})
);
const {ElementHandle} = await import('../api/ElementHandle.js');
if (!(result instanceof ElementHandle)) {
if (!(_isElementHandle in result)) {
return null;
}
return result.move();
Expand All @@ -151,10 +151,9 @@ export class QueryHandler {
selector: string,
options: WaitForSelectorOptions
): Promise<ElementHandle<Node> | null> {
const {ElementHandle} = await import('../api/ElementHandle.js');
let frame!: Frame;
using element = await (async () => {
if (!(elementOrFrame instanceof ElementHandle)) {
if (!(_isElementHandle in elementOrFrame)) {
frame = elementOrFrame;
return;
}
Expand Down Expand Up @@ -198,7 +197,7 @@ export class QueryHandler {
throw signal.reason;
}

if (!(handle instanceof ElementHandle)) {
if (!(_isElementHandle in handle)) {
return null;
}
return await frame.mainRealm().transferHandle(handle);
Expand Down

0 comments on commit 954c75f

Please sign in to comment.