Skip to content

Commit

Permalink
avoid href lookups for non-anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Sep 14, 2021
1 parent c70504b commit 8680834
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/lib/web-worker/worker-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export class WorkerNode {
get ownerDocument(): WorkerDocument {
return self.document as any;
}

get href() {
return undefined;
}
set href(_: any) {}
}

export class WorkerElement extends WorkerNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test';

test('element method', async ({ page }) => {
await page.goto('/element-method/');
test('element', async ({ page }) => {
await page.goto('/element/');

await page.waitForSelector('.completed');

Expand Down Expand Up @@ -36,4 +36,7 @@ test('element method', async ({ page }) => {
const y = await testGetBoundingClientRectY.textContent();
const yInt = parseFloat(y);
expect(yInt).toBeGreaterThan(4);

const testAnchor = page.locator('#testAnchor');
await expect(testAnchor).toHaveText('/element/some/other/path');
});
20 changes: 17 additions & 3 deletions tests/element-method/index.html → tests/element/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Partytown Test Page" />
<title>Element Method</title>
<title>Element</title>
<link
rel="icon"
id="favicon"
Expand Down Expand Up @@ -42,15 +42,17 @@
}
li strong,
li code,
li button {
li button,
li a {
white-space: nowrap;
flex: 1;
margin: 0 5px;
padding: 0;
}
</style>
</head>
<body mph="88">
<h1>Element Method</h1>
<h1>Element</h1>
<ul>
<li>
<strong>getBoundingClientRect()</strong>
Expand Down Expand Up @@ -179,6 +181,18 @@ <h1>Element Method</h1>
</script>
</li>

<li>
<strong>set/get a.href</strong>
<a id="testAnchor"></a>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testAnchor');
elm.href = './some/other/path';
elm.textContent = elm.pathname;
})();
</script>
</li>

<script type="text/partytown">
(function () {
document.body.classList.add('completed');
Expand Down
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ <h1>Partytown Tests 🎉</h1>
<ul>
<li><a href="/document/">Document</a></li>
<li><a href="/document-prod/">Document (Prod Build)</a></li>
<li><a href="/element/">Element</a></li>
<li><a href="/element-class/">Element Class</a></li>
<li><a href="/element-method/">Element Method</a></li>
<li><a href="/element-style/">Element Style</a></li>
<li><a href="/event-forwarding/">Event Forwarding</a></li>
<li><a href="/events/">Events</a></li>
Expand Down
26 changes: 19 additions & 7 deletions tests/node/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,53 @@ <h1 class="title">Node</h1>
<ul>
<li>
<strong>createElement() / appendChild()</strong>
<span id="test-input"></span>
<code id="testInput"></code>
<script type="text/partytown">
(function () {
const input = document.createElement('input');
input.type = 'checkbox';
input.checked = true;
input.id = 'test-checkbox';
document.getElementById('test-input').appendChild(input);
input.id = 'testCheckbox';
document.getElementById('testInput').appendChild(input);
})();
</script>
</li>

<li>
<strong>createTextNode() / insertBefore()</strong>
<span id="test-text"> World</span>
<code id="testText"> World</code>
<script type="text/partytown">
(function () {
const text = document.createTextNode('Hello');
const i = document.getElementById('test-text');
const i = document.getElementById('testText');
i.insertBefore(text, i.firstChild);
})();
</script>
</li>

<li>
<strong>removeChild()</strong>
<span id="test-remove">This is<span id="test-not"> not</span> awesome</span>
<code id="testRemove">This is<span id="testNot"> not</span> awesome</code>
<script type="text/partytown">
(function () {
const i = document.getElementById('test-not');
const i = document.getElementById('testNot');
i.parentNode.removeChild(i);
})();
</script>
</li>

<li>
<strong>set/get node.href undefined</strong>
<code id="testHrefProp"></code>
<script type="text/partytown">
(function () {
const elm = document.getElementById('testHrefProp');
elm.href = './some/other/path';
elm.textContent = elm.href === undefined ? 'undefined' : elm.href;
})();
</script>
</li>

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

await page.waitForSelector('.completed');

const testCheckbox = page.locator('#test-checkbox');
const testCheckbox = page.locator('#testCheckbox');
await expect(testCheckbox).toBeChecked();

const testText = page.locator('#test-text');
const testText = page.locator('#testText');
await expect(testText).toHaveText('Hello World');

const testRemove = page.locator('#test-remove');
const testRemove = page.locator('#testRemove');
await expect(testRemove).toHaveText('This is awesome');

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

1 comment on commit 8680834

@vercel
Copy link

@vercel vercel bot commented on 8680834 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.