From bbaf380e07e7baa017c95349613e7c1b3effc77f Mon Sep 17 00:00:00 2001 From: Slawek Kolodziej Date: Tue, 24 May 2022 16:16:42 +0200 Subject: [PATCH 1/2] fix: anchor property setter --- src/lib/utils.ts | 9 ++++ src/lib/web-worker/worker-anchor.ts | 18 +++++--- tests/platform/anchor/anchor.spec.ts | 18 ++++++++ tests/platform/anchor/index.html | 62 ++++++++++++++++++++++++++-- 4 files changed, 99 insertions(+), 8 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 4091c474..1515c7f2 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -128,3 +128,12 @@ export const createElementFromConstructor = ( : doc.createElement(htmlConstructorTags[tag] || tag); } }; + +export const isValidUrl = (url: any): boolean => { + try { + new URL(url); + return true; + } catch (_) { + return false; + } +} diff --git a/src/lib/web-worker/worker-anchor.ts b/src/lib/web-worker/worker-anchor.ts index 548c174c..9ca8e382 100644 --- a/src/lib/web-worker/worker-anchor.ts +++ b/src/lib/web-worker/worker-anchor.ts @@ -1,5 +1,5 @@ import { commaSplit } from './worker-constants'; -import { definePrototypePropertyDescriptor } from '../utils'; +import { definePrototypePropertyDescriptor, isValidUrl } from '../utils'; import { getInstanceStateValue, setInstanceStateValue } from './worker-state'; import { getter, setter } from './worker-proxy'; import { resolveToUrl } from './worker-exec'; @@ -24,10 +24,18 @@ export const patchHTMLAnchorElement = (WorkerHTMLAnchorElement: any, env: WebWor }, set(this: any, value) { - let href = getInstanceStateValue(this, StateProp.url); - let url: any = resolveToUrl(env, href); - - url[anchorProp] = new URL(value + '', url.href); + let url; + + if (anchorProp === 'href') { + const baseHref = isValidUrl(value) + ? new URL(value).href + : env.$location$.href + url = resolveToUrl(env, baseHref); + url.href = new URL(value + '', url.href); + } else { + url = resolveToUrl(env, this.href); + url[anchorProp] = value; + } setInstanceStateValue(this, StateProp.url, url.href); diff --git a/tests/platform/anchor/anchor.spec.ts b/tests/platform/anchor/anchor.spec.ts index 3d61f18d..901c3964 100644 --- a/tests/platform/anchor/anchor.spec.ts +++ b/tests/platform/anchor/anchor.spec.ts @@ -30,4 +30,22 @@ test('anchor', async ({ page }) => { await page.waitForSelector('.testSetHref'); const testSetHref = page.locator('#testSetHref'); await expect(testSetHref).toHaveText('/pathname'); + + await page.waitForSelector('.testSetHref2'); + const testSetHref2 = page.locator('#testSetHref2'); + const desiredLocalUrl = new URL(page.url()); + desiredLocalUrl.pathname = '/local-pathname' + await expect(testSetHref2).toHaveText(desiredLocalUrl.toString()); + + await page.waitForSelector('.testGetSearch'); + const testGetSearch = page.locator('#testGetSearch'); + await expect(testGetSearch).toHaveText('?a=42&b=23'); + const testGetSearchHref = page.locator('#testGetSearchHref'); + await expect(testGetSearchHref).toHaveText('https://builder.io/?a=42&b=23'); + + await page.waitForSelector('.testSetSearch'); + const testSetSearch = page.locator('#testSetSearch'); + await expect(testSetSearch).toHaveText('?x=1&y=2'); + const testSetSearchHref = page.locator('#testSetSearchHref'); + await expect(testSetSearchHref).toHaveText('https://builder.io/?x=1&y=2'); }); diff --git a/tests/platform/anchor/index.html b/tests/platform/anchor/index.html index 94adeb8d..d804d7d5 100644 --- a/tests/platform/anchor/index.html +++ b/tests/platform/anchor/index.html @@ -75,7 +75,7 @@

Anchor

})(); - +
  • constructor.name
    @@ -90,7 +90,7 @@

    Anchor

  • createElement('a'), no appendChild -
    +
    -
  • +
  • get href @@ -164,6 +164,62 @@

    Anchor

    })();
  • + +
  • + set href 2 +
    + +
  • + +
  • + get search +
    + search: + href: +
    + +
  • + +
  • + set search +
    + search: + href: +
    + +

  • From 4bb863ac66d83caeb5c47c22bd861a8afdfbe95b Mon Sep 17 00:00:00 2001 From: Slawek Kolodziej Date: Tue, 24 May 2022 16:28:04 +0200 Subject: [PATCH 2/2] refactor: clean up anchor setter --- src/lib/web-worker/worker-anchor.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/web-worker/worker-anchor.ts b/src/lib/web-worker/worker-anchor.ts index 9ca8e382..337ff510 100644 --- a/src/lib/web-worker/worker-anchor.ts +++ b/src/lib/web-worker/worker-anchor.ts @@ -27,11 +27,13 @@ export const patchHTMLAnchorElement = (WorkerHTMLAnchorElement: any, env: WebWor let url; if (anchorProp === 'href') { - const baseHref = isValidUrl(value) - ? new URL(value).href - : env.$location$.href - url = resolveToUrl(env, baseHref); - url.href = new URL(value + '', url.href); + if (isValidUrl(value)) { + url = new URL(value); + } else { + const baseHref = env.$location$.href + url = resolveToUrl(env, baseHref); + url.href = new URL(value + '', url.href); + } } else { url = resolveToUrl(env, this.href); url[anchorProp] = value;