From 5210ae2610bd7284c3f6e37f3a4a47245706db68 Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Tue, 17 Sep 2024 17:50:27 +0200 Subject: [PATCH] fix: allow component to be mounted in iframe (#13225) * fix: allow component to be mounted in iframe * prettier * prettier * fix: allow HMR cleanup styles in iframe and custom elements * add test * avoid creating unnecessary microtasks * simplify * rename stuff * typo * lint * changeset * changeset --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Rich Harris Co-authored-by: Rich Harris --- .changeset/silly-fishes-deny.md | 5 +++ .changeset/wild-mice-look.md | 5 +++ .../3-transform/client/transform-client.js | 13 +------- .../svelte/src/internal/client/dev/css.js | 31 +++++++++++++++++++ .../svelte/src/internal/client/dom/css.js | 14 +++++++-- packages/svelte/src/internal/client/index.js | 1 + .../samples/mount-in-iframe/App.svelte | 19 ++++++++++++ .../samples/mount-in-iframe/Child.svelte | 13 ++++++++ .../samples/mount-in-iframe/_config.js | 22 +++++++++++++ .../samples/mount-in-iframe/main.svelte | 11 +++++++ 10 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 .changeset/silly-fishes-deny.md create mode 100644 .changeset/wild-mice-look.md create mode 100644 packages/svelte/src/internal/client/dev/css.js create mode 100644 packages/svelte/tests/runtime-browser/samples/mount-in-iframe/App.svelte create mode 100644 packages/svelte/tests/runtime-browser/samples/mount-in-iframe/Child.svelte create mode 100644 packages/svelte/tests/runtime-browser/samples/mount-in-iframe/_config.js create mode 100644 packages/svelte/tests/runtime-browser/samples/mount-in-iframe/main.svelte diff --git a/.changeset/silly-fishes-deny.md b/.changeset/silly-fishes-deny.md new file mode 100644 index 0000000000..a77098c597 --- /dev/null +++ b/.changeset/silly-fishes-deny.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: allow custom element styles to be updated in HMR mode diff --git a/.changeset/wild-mice-look.md b/.changeset/wild-mice-look.md new file mode 100644 index 0000000000..535ace2fa5 --- /dev/null +++ b/.changeset/wild-mice-look.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: inject styles correctly when mounting inside an iframe diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index 690cdb4345..45e48e565e 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -494,18 +494,7 @@ export function client_component(analysis, options) { if (analysis.css.hash) { // remove existing ` diff --git a/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/_config.js b/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/_config.js new file mode 100644 index 0000000000..0f04e9659a --- /dev/null +++ b/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/_config.js @@ -0,0 +1,22 @@ +import { flushSync } from 'svelte'; +import { test } from '../../assert'; + +export default test({ + async test({ target, assert }) { + const button = target.querySelector('button'); + const h1 = () => + /** @type {HTMLHeadingElement} */ ( + /** @type {Window} */ ( + target.querySelector('iframe')?.contentWindow + ).document.querySelector('h1') + ); + + assert.equal(h1().textContent, 'count: 0'); + assert.equal(getComputedStyle(h1()).color, 'rgb(255, 0, 0)'); + + flushSync(() => button?.click()); + + assert.equal(h1().textContent, 'count: 1'); + assert.equal(getComputedStyle(h1()).color, 'rgb(255, 0, 0)'); + } +}); diff --git a/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/main.svelte b/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/main.svelte new file mode 100644 index 0000000000..358698f8ec --- /dev/null +++ b/packages/svelte/tests/runtime-browser/samples/mount-in-iframe/main.svelte @@ -0,0 +1,11 @@ + + + + +{#key count} + +{/key}