Skip to content

Commit 3e9c3d1

Browse files
authored
fix: solve error with anchor without href (#523)
1 parent eac85b8 commit 3e9c3d1

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/lib/web-worker/worker-anchor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export const patchHTMLAnchorElement = (WorkerHTMLAnchorElement: any, env: WebWor
1616

1717
if (typeof value !== 'string') {
1818
href = getter(this, ['href']);
19+
if (href === '') {
20+
if (anchorProp === 'protocol') {
21+
return ':';
22+
}
23+
return '';
24+
}
25+
1926
setInstanceStateValue(this, StateProp.url, href);
2027
value = (new URL(href) as any)[anchorProp];
2128
}

tests/platform/anchor/anchor.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ test('anchor', async ({ page }) => {
77
const testAnchor = page.locator('#testAnchor');
88
await expect(testAnchor).toHaveText('/tests/platform/anchor/some/other/path');
99

10+
await page.waitForSelector('.emptyAnchorContainer');
11+
const emptyAnchorContainer = page.locator('#emptyAnchorContainer');
12+
await expect(emptyAnchorContainer).toHaveText(
13+
'Anchor properties: {"hash":"","host":"","hostname":"","href":"","origin":"","pathname":"","port":"","protocol":":","search":""}'
14+
);
15+
1016
await page.waitForSelector('.testAnchorConstructor');
1117
const testAnchorConstructor = page.locator('#testAnchorConstructor');
1218
await expect(testAnchorConstructor).toHaveText('HTMLAnchorElement HTMLAnchorElement');
@@ -34,7 +40,7 @@ test('anchor', async ({ page }) => {
3440
await page.waitForSelector('.testSetHref2');
3541
const testSetHref2 = page.locator('#testSetHref2');
3642
const desiredLocalUrl = new URL(page.url());
37-
desiredLocalUrl.pathname = '/local-pathname'
43+
desiredLocalUrl.pathname = '/local-pathname';
3844
await expect(testSetHref2).toHaveText(desiredLocalUrl.toString());
3945

4046
await page.waitForSelector('.testGetSearch');

tests/platform/anchor/index.html

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ <h1>Anchor</h1>
7676
</script>
7777
</li>
7878

79+
<li>
80+
<strong>get empty a.href properties</strong>
81+
<div id="emptyAnchorContainer"></div>
82+
<script type="text/partytown">
83+
(function () {
84+
try {
85+
const container = document.getElementById('emptyAnchorContainer');
86+
container.className = 'emptyAnchorContainer';
87+
const elm = document.createElement('a');
88+
const { hash, host, hostname, href, origin, pathname, port, protocol, search } = elm;
89+
elm.href = pathname;
90+
container.textContent = 'Anchor properties: ' + JSON.stringify({ hash, host, hostname, href, origin, pathname, port, protocol, search });
91+
} catch (err) {
92+
container.textContent = 'An error ocurred: ' + err.message;
93+
console.error(err);
94+
}
95+
})();
96+
</script>
97+
</li>
98+
7999
<li>
80100
<strong>constructor.name</strong>
81101
<div><a id="testAnchorConstructor"></a></div>
@@ -183,8 +203,8 @@ <h1>Anchor</h1>
183203
<li>
184204
<strong>get <a id="getSearch" href="https://builder.io/?a=42&b=23">search</a></strong>
185205
<div>
186-
<strong>search:</strong><span id="testGetSearch"></span>
187-
<strong>href:</strong><span id="testGetSearchHref"></span>
206+
<strong>search:</strong><span id="testGetSearch"></span> <strong>href:</strong
207+
><span id="testGetSearchHref"></span>
188208
</div>
189209
<script type="text/partytown">
190210
(function () {
@@ -203,8 +223,8 @@ <h1>Anchor</h1>
203223
<li>
204224
<strong>set <a id="setSearch" href="https://builder.io/?a=42&b=23">search</a></strong>
205225
<div>
206-
<strong>search:</strong><span id="testSetSearch"></span>
207-
<strong>href:</strong><span id="testSetSearchHref"></span>
226+
<strong>search:</strong><span id="testSetSearch"></span> <strong>href:</strong
227+
><span id="testSetSearchHref"></span>
208228
</div>
209229
<script type="text/partytown">
210230
(function () {

0 commit comments

Comments
 (0)