diff --git a/src/runtime/client/partials.ts b/src/runtime/client/partials.ts index f4add4c9..41a2c1e9 100644 --- a/src/runtime/client/partials.ts +++ b/src/runtime/client/partials.ts @@ -75,13 +75,16 @@ function maybeUpdateHistory(nextUrl: URL) { document.addEventListener("click", async (e) => { let el = e.target; - if (el && el instanceof HTMLElement) { + if (el && (el instanceof HTMLElement || el instanceof SVGElement)) { const originalEl = el; // Check if we clicked inside an anchor link if (el.nodeName !== "A") { el = el.closest("a"); } + if (el === null) { + el = originalEl.closest("button"); + } if ( // Check that we're still dealing with an anchor tag @@ -132,33 +135,22 @@ document.addEventListener("click", async (e) => { indicator.value = false; } } - } else { - let button: HTMLButtonElement | HTMLElement | null = originalEl; - // Check if we clicked on a button - if (button.nodeName !== "A") { - button = button.closest("button"); + } else if ( + el && el instanceof HTMLButtonElement && + (el.type !== "submit" || el.form === null) + ) { + const partial = el.getAttribute(PARTIAL_ATTR); + + // Check if the user opted out of client side navigation. + if (partial === null || !checkClientNavEnabled(el)) { + return; } - if ( - button !== null && button instanceof HTMLButtonElement && - (button.type !== "submit" || button.form === null) - ) { - const partial = button.getAttribute(PARTIAL_ATTR); - - // Check if the user opted out of client side navigation. - if ( - partial === null || - !checkClientNavEnabled(button) - ) { - return; - } - - const partialUrl = new URL( - partial, - location.href, - ); - await fetchPartials(partialUrl, partialUrl); - } + const partialUrl = new URL( + partial, + location.href, + ); + await fetchPartials(partialUrl, partialUrl); } } }); diff --git a/tests/partials_test.tsx b/tests/partials_test.tsx index 2dedcd80..ead9409e 100644 --- a/tests/partials_test.tsx +++ b/tests/partials_test.tsx @@ -1263,6 +1263,178 @@ Deno.test({ sanitizeOps: false, }); +Deno.test({ + name: "partials - with SVG in link", + fn: async () => { + const app = testApp() + .get("/partial", (ctx) => { + return ctx.render( + + +

done

+ +
+
, + ); + }) + .get("/", (ctx) => { + return ctx.render( + +
+ + + + + + + + update + + +

init

+ +
+
+
, + ); + }); + + await withBrowserApp(app, async (page, address) => { + await page.goto(address, { waitUntil: "load" }); + await page.locator(".ready").wait(); + + await page.locator(".increment").click(); + await waitForText(page, ".output", "1"); + + await page.locator(".update").click(); + + await page.waitForFunction(() => { + const url = new URL(window.location.href); + return url.pathname === "/foo"; + }); + await page.locator(".done").wait(); + await waitForText(page, ".output", "1"); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: "partials - with SVG in button", + fn: async () => { + const app = testApp() + .get("/partial", (ctx) => { + return ctx.render( + + +

done

+ +
+
, + ); + }) + .get("/", (ctx) => { + return ctx.render( + +
+ + +

init

+ +
+
+
, + ); + }); + + await withBrowserApp(app, async (page, address) => { + await page.goto(address, { waitUntil: "load" }); + await page.locator(".ready").wait(); + + await page.locator(".increment").click(); + await waitForText(page, ".output", "1"); + + await page.locator(".update").click(); + + await page.waitForFunction(() => { + const url = new URL(window.location.href); + return url.pathname === "/partial"; + }); + await page.locator(".done").wait(); + await waitForText(page, ".output", "1"); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + Deno.test({ name: "partials - opt out of partial navigation", fn: async () => {