diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts
index e9976a7fa6d8..f563f6d099da 100644
--- a/src/runtime/internal/dom.ts
+++ b/src/runtime/internal/dom.ts
@@ -134,9 +134,9 @@ export function append_styles(
style_sheet_id: string,
styles: string
) {
- const append_styles_to = get_root_for_styles(target);
+ const append_styles_to = get_root_for_style(target);
- if (!append_styles_to?.getElementById(style_sheet_id)) {
+ if (!append_styles_to.getElementById(style_sheet_id)) {
const style = element('style');
style.id = style_sheet_id;
style.textContent = styles;
@@ -144,20 +144,19 @@ export function append_styles(
}
}
-export function get_root_for_node(node: Node) {
+export function get_root_for_style(node: Node): ShadowRoot | Document {
if (!node) return document;
- return (node.getRootNode ? node.getRootNode() : node.ownerDocument); // check for getRootNode because IE is still supported
-}
-
-function get_root_for_styles(node: Node) {
- const root = get_root_for_node(node);
- return (root as ShadowRoot).host ? root as ShadowRoot : root as Document;
+ const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
+ if ((root as ShadowRoot).host) {
+ return root as ShadowRoot;
+ }
+ return document;
}
export function append_empty_stylesheet(node: Node) {
const style_element = element('style') as HTMLStyleElement;
- append_stylesheet(get_root_for_styles(node), style_element);
+ append_stylesheet(get_root_for_style(node), style_element);
return style_element;
}
diff --git a/src/runtime/internal/style_manager.ts b/src/runtime/internal/style_manager.ts
index a646c9b916f7..0993b3bf1833 100644
--- a/src/runtime/internal/style_manager.ts
+++ b/src/runtime/internal/style_manager.ts
@@ -1,4 +1,4 @@
-import { append_empty_stylesheet, get_root_for_node } from './dom';
+import { append_empty_stylesheet, get_root_for_style } from './dom';
import { raf } from './environment';
interface ExtendedDoc extends Document {
@@ -29,7 +29,7 @@ export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b:
const rule = keyframes + `100% {${fn(b, 1 - b)}}\n}`;
const name = `__svelte_${hash(rule)}_${uid}`;
- const doc = get_root_for_node(node) as unknown as ExtendedDoc;
+ const doc = get_root_for_style(node) as ExtendedDoc;
active_docs.add(doc);
const stylesheet = doc.__svelte_stylesheet || (doc.__svelte_stylesheet = append_empty_stylesheet(node).sheet as CSSStyleSheet);
const current_rules = doc.__svelte_rules || (doc.__svelte_rules = {});
diff --git a/test/runtime/samples/target-dom-detached/App.svelte b/test/runtime/samples/target-dom-detached/App.svelte
new file mode 100644
index 000000000000..251e4803077e
--- /dev/null
+++ b/test/runtime/samples/target-dom-detached/App.svelte
@@ -0,0 +1,11 @@
+
+
+
Hello {name}
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/target-dom-detached/_config.js b/test/runtime/samples/target-dom-detached/_config.js
new file mode 100644
index 000000000000..b63656530c73
--- /dev/null
+++ b/test/runtime/samples/target-dom-detached/_config.js
@@ -0,0 +1,16 @@
+export default {
+ skip_if_ssr: true,
+ compileOptions: {
+ cssHash: () => 'svelte-xyz'
+ },
+ async test({ assert, component, target, window }) {
+ assert.htmlEqual(
+ window.document.head.innerHTML,
+ ''
+ );
+ assert.htmlEqual(
+ component.div.innerHTML,
+ 'Hello World
'
+ );
+ }
+};
diff --git a/test/runtime/samples/target-dom-detached/main.svelte b/test/runtime/samples/target-dom-detached/main.svelte
new file mode 100644
index 000000000000..42e7dffee904
--- /dev/null
+++ b/test/runtime/samples/target-dom-detached/main.svelte
@@ -0,0 +1,18 @@
+
diff --git a/test/runtime/samples/target-dom/App.svelte b/test/runtime/samples/target-dom/App.svelte
new file mode 100644
index 000000000000..251e4803077e
--- /dev/null
+++ b/test/runtime/samples/target-dom/App.svelte
@@ -0,0 +1,11 @@
+
+
+Hello {name}
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/target-dom/_config.js b/test/runtime/samples/target-dom/_config.js
new file mode 100644
index 000000000000..b63656530c73
--- /dev/null
+++ b/test/runtime/samples/target-dom/_config.js
@@ -0,0 +1,16 @@
+export default {
+ skip_if_ssr: true,
+ compileOptions: {
+ cssHash: () => 'svelte-xyz'
+ },
+ async test({ assert, component, target, window }) {
+ assert.htmlEqual(
+ window.document.head.innerHTML,
+ ''
+ );
+ assert.htmlEqual(
+ component.div.innerHTML,
+ 'Hello World
'
+ );
+ }
+};
diff --git a/test/runtime/samples/target-dom/main.svelte b/test/runtime/samples/target-dom/main.svelte
new file mode 100644
index 000000000000..68d299055283
--- /dev/null
+++ b/test/runtime/samples/target-dom/main.svelte
@@ -0,0 +1,18 @@
+
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/target-shadow-dom/App.svelte b/test/runtime/samples/target-shadow-dom/App.svelte
new file mode 100644
index 000000000000..251e4803077e
--- /dev/null
+++ b/test/runtime/samples/target-shadow-dom/App.svelte
@@ -0,0 +1,11 @@
+
+
+Hello {name}
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/target-shadow-dom/_config.js b/test/runtime/samples/target-shadow-dom/_config.js
new file mode 100644
index 000000000000..cec383afd086
--- /dev/null
+++ b/test/runtime/samples/target-shadow-dom/_config.js
@@ -0,0 +1,13 @@
+export default {
+ skip_if_ssr: true,
+ compileOptions: {
+ cssHash: () => 'svelte-xyz'
+ },
+ async test({ assert, component, target, window }) {
+ assert.htmlEqual(window.document.head.innerHTML, '');
+ assert.htmlEqual(component.div.shadowRoot.innerHTML, `
+
+ Hello World
+ `);
+ }
+};
diff --git a/test/runtime/samples/target-shadow-dom/main.svelte b/test/runtime/samples/target-shadow-dom/main.svelte
new file mode 100644
index 000000000000..cb47ad01f0e3
--- /dev/null
+++ b/test/runtime/samples/target-shadow-dom/main.svelte
@@ -0,0 +1,19 @@
+
+
+
\ No newline at end of file