mirror of https://github.com/sveltejs/svelte
fix: append_styles resolving to `document.head` in WC (#18614)
Fixes #18288 Check the surrounding branch effect's start node to retrieve the correct root node instead of just the anchor, since the latter could come from each.js/branch.js and be a text node that is never going to get connected --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>pull/18537/merge
parent
ee1249b43c
commit
a4c60ccdbb
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: reliably resolve append_style to its correct root
|
||||
@ -0,0 +1,7 @@
|
||||
<p>child</p>
|
||||
|
||||
<style>
|
||||
p {
|
||||
color: rgb(255, 0, 0);
|
||||
}
|
||||
</style>
|
||||
@ -0,0 +1,24 @@
|
||||
import { assert_ok, test } from '../../assert';
|
||||
|
||||
const tick = () => Promise.resolve();
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
target.innerHTML = '<my-app></my-app>';
|
||||
|
||||
// wait for the initial mount, the `onMount` reveal and the deferred re-render
|
||||
await tick();
|
||||
await tick();
|
||||
await tick();
|
||||
await tick();
|
||||
|
||||
/** @type {any} */
|
||||
const el = target.querySelector('my-app');
|
||||
const p = el.shadowRoot.querySelector('p');
|
||||
assert_ok(p);
|
||||
|
||||
// The child's scoped styles must be injected into the shadow root, not `document.head`
|
||||
assert_ok(el.shadowRoot.querySelector('style'));
|
||||
assert.equal(getComputedStyle(p).color, 'rgb(255, 0, 0)');
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,18 @@
|
||||
<svelte:options customElement="my-app" />
|
||||
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import Child from './Child.svelte';
|
||||
|
||||
let items = $state([]);
|
||||
|
||||
// Add the item _after_ the initial mount, so that the each block renders the
|
||||
// new item into an offscreen anchor that is discarded once it's committed to
|
||||
// the DOM. This reproduces styles being injected into `document.head` instead
|
||||
// of the shadow root (https://github.com/sveltejs/svelte/issues/18288)
|
||||
onMount(() => {
|
||||
items = [1];
|
||||
});
|
||||
</script>
|
||||
|
||||
{#each items as item (item)}<Child />{/each}
|
||||
Loading…
Reference in new issue