Skip to content

Commit

Permalink
reduce parent lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Sep 14, 2021
1 parent 8680834 commit e201cf6
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/lib/sandbox/main-instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getInstanceId = (
if (instance) {
const nodeName = (instance as any as Node).nodeName;
if (nodeName === NodeName.Document) {
return PlatformApiId.documentElement;
return PlatformApiId.document;
}
if (nodeName === NodeName.DocumentElement) {
return PlatformApiId.documentElement;
Expand Down
27 changes: 15 additions & 12 deletions src/lib/web-worker/worker-document.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { callMethod, getter, setter } from './worker-proxy';
import { createElement, WorkerElement, WorkerScriptElement } from './worker-node';
import { createElement, WorkerElement, WorkerNode, WorkerScriptElement } from './worker-node';
import { InterfaceType, PlatformApiId } from '../types';
import { logWorkerGetter, logWorkerSetter, toLower } from '../utils';
import { webWorkerCtx, WinIdKey } from './worker-constants';

export class WorkerDocument extends WorkerElement {
get body() {
return getElementProp(this, PlatformApiId.body, 'BODY');
return getNodeProp(this, PlatformApiId.body, 'BODY');
}

get compatMode() {
Expand Down Expand Up @@ -47,7 +47,7 @@ export class WorkerDocument extends WorkerElement {
}

get documentElement() {
return getElementProp(this, PlatformApiId.documentElement, 'HTML');
return getNodeProp(this, PlatformApiId.documentElement, 'HTML');
}

getElementsByTagName(tagName: string) {
Expand All @@ -72,7 +72,7 @@ export class WorkerDocument extends WorkerElement {
}

get head() {
return getElementProp(this, PlatformApiId.head, 'HEAD');
return getNodeProp(this, PlatformApiId.head, 'HEAD');
}

get implementation() {
Expand All @@ -90,6 +90,14 @@ export class WorkerDocument extends WorkerElement {
webWorkerCtx.$location$!.href = url + '';
}

get parentNode() {
return null;
}

get parentElement() {
return null;
}

get readyState() {
if (webWorkerCtx.$documentReadyState$ !== 'complete') {
webWorkerCtx.$documentReadyState$ = getter(this, ['readyState']);
Expand All @@ -113,15 +121,10 @@ export class WorkerDocument extends WorkerElement {
}
}

const getElementProp = (
doc: WorkerDocument,
instanceId: number,
tagName: 'BODY' | 'HEAD' | 'HTML'
) => {
return new WorkerElement({
$winId$: doc[WinIdKey],
const getNodeProp = (node: WorkerNode, instanceId: number, tagName: string) =>
new WorkerElement({
$winId$: node[WinIdKey],
$interfaceType$: InterfaceType.Element,
$instanceId$: instanceId,
$nodeName$: tagName,
});
};
22 changes: 20 additions & 2 deletions src/lib/web-worker/worker-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { callMethod, getter, proxy, setter } from './worker-proxy';
import { ExtraInstruction, SerializedInstance } from '../types';
import { ExtraInstruction, InterfaceType, PlatformApiId, SerializedInstance } from '../types';
import { imageRequest, scriptElementSetSrc } from './worker-exec';
import {
InstanceIdKey,
Expand Down Expand Up @@ -32,7 +32,7 @@ export class WorkerNode {
}

get ownerDocument(): WorkerDocument {
return self.document as any;
return document as any;
}

get href() {
Expand Down Expand Up @@ -83,6 +83,24 @@ export class WorkerAnchorElement extends WorkerElement {
}
}

export class WorkerDocumentElementChild extends WorkerElement {
get parentElement() {
return document.documentElement;
}
get parentNode() {
return document.documentElement;
}
}

export class WorkerDocumentElement extends WorkerElement {
get parentElement() {
return null;
}
get parentNode() {
return document;
}
}

export class WorkerSrcElement extends WorkerElement {
[PrivateValues]: {
/** completed */
Expand Down
10 changes: 6 additions & 4 deletions src/lib/web-worker/worker-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './worker-constants';
import {
WorkerAnchorElement,
WorkerDocumentElementChild,
WorkerElement,
WorkerImageElement,
WorkerNode,
Expand Down Expand Up @@ -132,19 +133,20 @@ const constructInstance = (serializedInstance: SerializedInstance): any => {
if (instanceId === PlatformApiId.window) {
return self;
}
if (interfaceType === InterfaceType.Document) {
return new WorkerDocument(serializedNode);
}
if (interfaceType === InterfaceType.Element) {
const ElementConstructors: { [tagname: string]: any } = {
A: WorkerAnchorElement,
BODY: WorkerDocumentElementChild,
HEAD: WorkerDocumentElementChild,
IFRAME: WorkerIFrameElement,
IMG: WorkerImageElement,
SCRIPT: WorkerScriptElement,
};
return new (ElementConstructors[serializedNode.$nodeName$!] || WorkerElement)(serializedNode);
}
if (interfaceType === InterfaceType.Document) {
// this scenario would be for an iframe's document, not the main document
return new WorkerDocument(serializedNode);
}
if (interfaceType === InterfaceType.TextNode) {
return new WorkerNode(serializedInstance);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/document/document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ test('document', async ({ page }) => {
const testCurrentScriptSrcLocation = page.locator('#testCurrentScriptSrcLocation');
await expect(testCurrentScriptSrc).toHaveText('src');
await expect(testCurrentScriptSrcLocation).toHaveText('/document/current-script-src.js');

const testHeadParentNode = page.locator('#testHeadParentNode');
await expect(testHeadParentNode).toHaveText('HTML HTML');

const testBodyParentNode = page.locator('#testBodyParentNode');
await expect(testBodyParentNode).toHaveText('HTML HTML');

const testDocumentElementParentNode = page.locator('#testDocumentElementParentNode');
await expect(testDocumentElementParentNode).toHaveText('#document null');

const testDocumentParentNode = page.locator('#testDocumentParentNode');
await expect(testDocumentParentNode).toHaveText('null null');
});
44 changes: 44 additions & 0 deletions tests/document/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,50 @@ <h1 class="title">Document</h1>
></script>
</li>

<li>
<strong>head.parentNode/Element</strong>
<code id="testHeadParentNode"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testHeadParentNode');
elm.textContent = document.head.parentNode.nodeName + ' ' + document.head.parentElement.nodeName;
})();
</script>
</li>

<li>
<strong>body.parentNode/Element</strong>
<code id="testBodyParentNode"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testBodyParentNode');
elm.textContent = document.body.parentNode.nodeName + ' ' + document.body.parentElement.nodeName;
})();
</script>
</li>

<li>
<strong>documentElement.parentNode/Element</strong>
<code id="testDocumentElementParentNode"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testDocumentElementParentNode');
elm.textContent = document.documentElement.parentNode.nodeName + ' ' + String(document.documentElement.parentElement);
})();
</script>
</li>

<li>
<strong>document.parentNode/Element</strong>
<code id="testDocumentParentNode"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testDocumentParentNode');
elm.textContent = String(document.parentNode) + ' ' + String(document.parentElement);
})();
</script>
</li>

<script type="text/partytown">
(function () {
document.body.classList.add('completed');
Expand Down
3 changes: 3 additions & 0 deletions tests/element/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ test('element', async ({ page }) => {

const testAnchor = page.locator('#testAnchor');
await expect(testAnchor).toHaveText('/element/some/other/path');

const testParentElement = page.locator('#testParentElement');
await expect(testParentElement).toHaveText('hasParentElement');
});
12 changes: 12 additions & 0 deletions tests/element/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ <h1>Element</h1>
</script>
</li>

<li class="hasParentElement">
<strong>parentElement</strong>
<code id="testParentElement"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testParentElement');
const parentElement = elm.parentElement;
elm.textContent = parentElement.className;
})();
</script>
</li>

<script type="text/partytown">
(function () {
document.body.classList.add('completed');
Expand Down
14 changes: 13 additions & 1 deletion tests/node/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,19 @@ <h1 class="title">Node</h1>
(function () {
const elm = document.getElementById('testHrefProp');
elm.href = './some/other/path';
elm.textContent = elm.href === undefined ? 'undefined' : elm.href;
elm.textContent = String(elm.href);
})();
</script>
</li>

<li class="hasParentNode">
<strong>parentNode</strong>
<code id="testParentNode"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testParentNode');
const parentNode = elm.parentNode;
elm.textContent = parentNode.className;
})();
</script>
</li>
Expand Down
3 changes: 3 additions & 0 deletions tests/node/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ test('node', async ({ page }) => {

const testHrefProp = page.locator('#testHrefProp');
await expect(testHrefProp).toHaveText('undefined');

const testParentNode = page.locator('#testParentNode');
await expect(testParentNode).toHaveText('hasParentNode');
});

1 comment on commit e201cf6

@vercel
Copy link

@vercel vercel bot commented on e201cf6 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.