Skip to content

Commit

Permalink
feat: add rootFrame helper / convenience method
Browse files Browse the repository at this point in the history
Bug: #11072
  • Loading branch information
thiagowfx committed Oct 13, 2023
1 parent eae37d9 commit be87afd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/puppeteer-core/src/api/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ export abstract class Frame extends EventEmitter<FrameEvents> {
*/
abstract parentFrame(): Frame | null;

/**
* The root of this frame, if any.
*/
abstract rootFrame(): Frame | null;

/**
* An array of child frames.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/puppeteer-core/src/bidi/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ export class BidiFrame extends Frame {
return this.#page.frame(this._parentId ?? '');
}

override rootFrame(): BidiFrame | null {
if (this.parentFrame() === null) {
return this;
} else {
return this.parentFrame()!.rootFrame();
}
}

override childFrames(): BidiFrame[] {
return this.#page.childFrames(this.#context.id);
}
Expand Down
14 changes: 11 additions & 3 deletions packages/puppeteer-core/src/cdp/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ export class CdpFrame extends Frame {
return this._frameManager._frameTree.parentFrame(this._id) || null;
}

override rootFrame(): CdpFrame | null {
if (this.parentFrame() === null) {
return this;
} else {
return this.parentFrame()!.rootFrame();
}
}

override childFrames(): CdpFrame[] {
return this._frameManager._frameTree.childFrames(this._id);
}
Expand All @@ -295,9 +303,9 @@ export class CdpFrame extends Frame {
if (this.isOOPFrame()) {
return this._frameManager._deviceRequestPromptManager(this.#client);
}
const parentFrame = this.parentFrame();
assert(parentFrame !== null);
return parentFrame.#deviceRequestPromptManager();
const rootFrame = this.rootFrame();
assert(rootFrame !== null);
return rootFrame._frameManager._deviceRequestPromptManager(this.#client);
}

@throwIfDetached
Expand Down

0 comments on commit be87afd

Please sign in to comment.