-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use
composedPath
to find anchor elements (#2769)
* feat: Use `composedPath` to find anchor elements The client-side router registers event handlers on mouse movements and clicks that search anchor elements in an event's target's ancestors to perform prefetching or routing. The current implementation uses `parentNode` which "skips" anchor elements in shadow doms. This change searches instead the `event.composedPath()`, which includes nodes in open shadow trees. * refactor: `find_anchor` to receive a full event * test: Add integration tests * docs: Add changeset
- Loading branch information
1 parent
f075f05
commit c90c8aa
Showing
4 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
[feat] Use `event.composedPath` to find anchors for prefetching and routing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/kit/test/apps/basics/src/routes/routing/shadow-dom/_tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as assert from 'uvu/assert'; | ||
|
||
/** @type {import('test').TestMaker} */ | ||
export default function (test) { | ||
test( | ||
'client router captures anchors in shadow dom', | ||
'/routing/shadow-dom', | ||
async ({ app, capture_requests, page, clicknav, js }) => { | ||
if (js) { | ||
await app.prefetchRoutes(['/routing/a']).catch((e) => { | ||
// from error handler tests; ignore | ||
if (!e.message.includes('Crashing now')) throw e; | ||
}); | ||
|
||
const requests = await capture_requests(async () => { | ||
await clicknav('div[id="clickme"]'); | ||
assert.equal(await page.textContent('h1'), 'a'); | ||
}); | ||
|
||
assert.equal(requests, []); | ||
} | ||
} | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/kit/test/apps/basics/src/routes/routing/shadow-dom/index.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script> | ||
import { onMount } from 'svelte'; | ||
/** @type {HTMLDivElement} */ | ||
let elem; | ||
onMount(() => { | ||
const shadow = elem.attachShadow({ mode: 'open' }); | ||
const anchor = document.createElement('a'); | ||
anchor.href = '/routing/a'; | ||
anchor.innerHTML = '<slot>'; | ||
shadow.appendChild(anchor); | ||
}); | ||
</script> | ||
|
||
<div bind:this={elem}> | ||
<div id="clickme">Hello world</div> | ||
</div> |