From 3e9c3d106b5cc5e1ecbe82b737b73c3113e59190 Mon Sep 17 00:00:00 2001 From: franpeza <85033117+franpeza@users.noreply.github.com> Date: Fri, 29 Dec 2023 19:59:28 +0100 Subject: [PATCH] fix: solve error with anchor without href (#523) --- src/lib/web-worker/worker-anchor.ts | 7 +++++++ tests/platform/anchor/anchor.spec.ts | 8 +++++++- tests/platform/anchor/index.html | 28 ++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/lib/web-worker/worker-anchor.ts b/src/lib/web-worker/worker-anchor.ts index b09cfdc9..83c2e8e3 100644 --- a/src/lib/web-worker/worker-anchor.ts +++ b/src/lib/web-worker/worker-anchor.ts @@ -16,6 +16,13 @@ export const patchHTMLAnchorElement = (WorkerHTMLAnchorElement: any, env: WebWor if (typeof value !== 'string') { href = getter(this, ['href']); + if (href === '') { + if (anchorProp === 'protocol') { + return ':'; + } + return ''; + } + setInstanceStateValue(this, StateProp.url, href); value = (new URL(href) as any)[anchorProp]; } diff --git a/tests/platform/anchor/anchor.spec.ts b/tests/platform/anchor/anchor.spec.ts index 901c3964..3a0112da 100644 --- a/tests/platform/anchor/anchor.spec.ts +++ b/tests/platform/anchor/anchor.spec.ts @@ -7,6 +7,12 @@ test('anchor', async ({ page }) => { const testAnchor = page.locator('#testAnchor'); await expect(testAnchor).toHaveText('/tests/platform/anchor/some/other/path'); + await page.waitForSelector('.emptyAnchorContainer'); + const emptyAnchorContainer = page.locator('#emptyAnchorContainer'); + await expect(emptyAnchorContainer).toHaveText( + 'Anchor properties: {"hash":"","host":"","hostname":"","href":"","origin":"","pathname":"","port":"","protocol":":","search":""}' + ); + await page.waitForSelector('.testAnchorConstructor'); const testAnchorConstructor = page.locator('#testAnchorConstructor'); await expect(testAnchorConstructor).toHaveText('HTMLAnchorElement HTMLAnchorElement'); @@ -34,7 +40,7 @@ test('anchor', async ({ page }) => { await page.waitForSelector('.testSetHref2'); const testSetHref2 = page.locator('#testSetHref2'); const desiredLocalUrl = new URL(page.url()); - desiredLocalUrl.pathname = '/local-pathname' + desiredLocalUrl.pathname = '/local-pathname'; await expect(testSetHref2).toHaveText(desiredLocalUrl.toString()); await page.waitForSelector('.testGetSearch'); diff --git a/tests/platform/anchor/index.html b/tests/platform/anchor/index.html index d804d7d5..fe213293 100644 --- a/tests/platform/anchor/index.html +++ b/tests/platform/anchor/index.html @@ -76,6 +76,26 @@

Anchor

+
  • + get empty a.href properties +
    + +
  • +
  • constructor.name
    @@ -183,8 +203,8 @@

    Anchor

  • get search
    - search: - href: + search: href: