Skip to content

Commit 541fa95

Browse files
dgozmanyury-s
authored andcommitted
fix(ownerFrame): correctly handle adopted node usecase (#677)
1 parent b3cd7a4 commit 541fa95

File tree

11 files changed

+59
-15
lines changed

11 files changed

+59
-15
lines changed

src/browserContext.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { helper } from './helper';
2222

2323
export interface BrowserContextDelegate {
2424
pages(): Promise<Page[]>;
25+
existingPages(): Page[];
2526
newPage(): Promise<Page>;
2627
close(): Promise<void>;
2728

@@ -69,6 +70,10 @@ export class BrowserContext {
6970
await this.setGeolocation(this._options.geolocation);
7071
}
7172

73+
_existingPages(): Page[] {
74+
return this._delegate.existingPages();
75+
}
76+
7277
async pages(): Promise<Page[]> {
7378
return this._delegate.pages();
7479
}

src/chromium/crBrowser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
7474
return pages.filter(page => !!page) as Page[];
7575
},
7676

77+
existingPages: (): Page[] => {
78+
const pages: Page[] = [];
79+
for (const target of this._allTargets()) {
80+
if (target.browserContext() === context && target._crPage)
81+
pages.push(target._crPage.page());
82+
}
83+
return pages;
84+
},
85+
7786
newPage: async (): Promise<Page> => {
7887
const { targetId } = await this._client.send('Target.createTarget', { url: 'about:blank', browserContextId: contextId || undefined });
7988
const target = this._targets.get(targetId)!;

src/chromium/crPage.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ export class CRPage implements PageDelegate {
424424
return this._page._frameManager.frame(nodeInfo.node.frameId);
425425
}
426426

427-
async getOwnerFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
427+
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
428428
// document.documentElement has frameId of the owner frame.
429429
const documentElement = await handle.evaluateHandle(node => {
430430
const doc = node as Document;
@@ -440,10 +440,10 @@ export class CRPage implements PageDelegate {
440440
const nodeInfo = await this._client.send('DOM.describeNode', {
441441
objectId: remoteObject.objectId
442442
});
443-
const frame = nodeInfo && typeof nodeInfo.node.frameId === 'string' ?
444-
this._page._frameManager.frame(nodeInfo.node.frameId) : null;
443+
const frameId = nodeInfo && typeof nodeInfo.node.frameId === 'string' ?
444+
nodeInfo.node.frameId : null;
445445
await documentElement.dispose();
446-
return frame;
446+
return frameId;
447447
}
448448

449449
isElementHandle(remoteObject: any): boolean {

src/dom.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,16 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
135135
}
136136

137137
async ownerFrame(): Promise<frames.Frame | null> {
138-
return this._page._delegate.getOwnerFrame(this);
138+
const frameId = await this._page._delegate.getOwnerFrame(this);
139+
if (!frameId)
140+
return null;
141+
const pages = this._page.browserContext()._existingPages();
142+
for (const page of pages) {
143+
const frame = page._frameManager.frame(frameId);
144+
if (frame)
145+
return frame;
146+
}
147+
return null;
139148
}
140149

141150
async contentFrame(): Promise<frames.Frame | null> {

src/firefox/ffBrowser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
164164
return pages.filter(page => !!page);
165165
},
166166

167+
existingPages: (): Page[] => {
168+
const pages: Page[] = [];
169+
for (const target of this._allTargets()) {
170+
if (target.browserContext() === context && target._ffPage)
171+
pages.push(target._ffPage._page);
172+
}
173+
return pages;
174+
},
175+
167176
newPage: async (): Promise<Page> => {
168177
const {targetId} = await this._connection.send('Target.newPage', {
169178
browserContextId: browserContextId || undefined

src/firefox/ffPage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,12 @@ export class FFPage implements PageDelegate {
373373
return this._page._frameManager.frame(contentFrameId);
374374
}
375375

376-
async getOwnerFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
376+
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
377377
const { ownerFrameId } = await this._session.send('Page.describeNode', {
378378
frameId: handle._context.frame._id,
379379
objectId: toRemoteObject(handle).objectId!,
380380
});
381-
if (!ownerFrameId)
382-
return null;
383-
return this._page._frameManager.frame(ownerFrameId);
381+
return ownerFrameId || null;
384382
}
385383

386384
isElementHandle(remoteObject: any): boolean {

src/page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface PageDelegate {
6262
isElementHandle(remoteObject: any): boolean;
6363
adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>>;
6464
getContentFrame(handle: dom.ElementHandle): Promise<frames.Frame | null>; // Only called for frame owner elements.
65-
getOwnerFrame(handle: dom.ElementHandle): Promise<frames.Frame | null>;
65+
getOwnerFrame(handle: dom.ElementHandle): Promise<string | null>; // Returns frameId.
6666
getContentQuads(handle: dom.ElementHandle): Promise<types.Quad[] | null>;
6767
layoutViewport(): Promise<{ width: number, height: number }>;
6868
setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void>;

src/webkit/wkBrowser.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ export class WKBrowser extends platform.EventEmitter implements Browser {
168168
return await Promise.all(pageProxies.map(proxy => proxy.page()));
169169
},
170170

171+
existingPages: (): Page[] => {
172+
const pages: Page[] = [];
173+
for (const pageProxy of this._pageProxies.values()) {
174+
if (pageProxy._browserContext !== context)
175+
continue;
176+
const page = pageProxy.existingPage();
177+
if (page)
178+
pages.push(page);
179+
}
180+
return pages;
181+
},
182+
171183
newPage: async (): Promise<Page> => {
172184
const { pageProxyId } = await this._browserSession.send('Browser.createPage', { browserContextId });
173185
const pageProxy = this._pageProxies.get(pageProxyId)!;

src/webkit/wkPage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,16 +484,14 @@ export class WKPage implements PageDelegate {
484484
return this._page._frameManager.frame(nodeInfo.contentFrameId);
485485
}
486486

487-
async getOwnerFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
487+
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
488488
const remoteObject = toRemoteObject(handle);
489489
if (!remoteObject.objectId)
490490
return null;
491491
const nodeInfo = await this._session.send('DOM.describeNode', {
492492
objectId: remoteObject.objectId
493493
});
494-
if (!nodeInfo.ownerFrameId)
495-
return null;
496-
return this._page._frameManager.frame(nodeInfo.ownerFrameId);
494+
return nodeInfo.ownerFrameId || null;
497495
}
498496

499497
isElementHandle(remoteObject: any): boolean {

src/webkit/wkPageProxy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export class WKPageProxy {
9999
return this._pagePromise;
100100
}
101101

102+
existingPage(): Page | undefined {
103+
return this._wkPage ? this._wkPage._page : undefined;
104+
}
105+
102106
onPopupCreated(popupPageProxy: WKPageProxy) {
103107
const wkPage = this._wkPage;
104108
if (!wkPage || !wkPage._page.listenerCount(Events.Page.Popup))

0 commit comments

Comments
 (0)