Skip to content

Commit 26d1455

Browse files
tanhauhauMathiasWP
andauthored
fixed removal of empty stylesheets created from transitions (#7662)
Co-authored-by: Mathias Picker <[email protected]>
1 parent 05b5be0 commit 26d1455

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/runtime/internal/dom.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,9 @@ export function get_root_for_style(node: Node): ShadowRoot | Document {
154154
return node.ownerDocument;
155155
}
156156

157-
export function append_empty_stylesheet(node: Node) {
158-
const style_element = element('style') as HTMLStyleElement;
159-
append_stylesheet(get_root_for_style(node), style_element);
160-
return style_element.sheet as CSSStyleSheet;
161-
}
162-
163-
function append_stylesheet(node: ShadowRoot | Document, style: HTMLStyleElement) {
157+
export function append_stylesheet(node: ShadowRoot | Document, style: HTMLStyleElement) {
164158
append((node as Document).head || node, style);
159+
return style.sheet as CSSStyleSheet;
165160
}
166161

167162
export function append_hydration(target: NodeEx, node: NodeEx) {

src/runtime/internal/style_manager.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { append_empty_stylesheet, get_root_for_style } from './dom';
1+
import { append_stylesheet, detach, element, get_root_for_style } from './dom';
22
import { raf } from './environment';
33

44
interface StyleInformation {
5-
stylesheet: CSSStyleSheet;
5+
style_element: HTMLStyleElement;
66
rules: Record<string, true>;
77
}
88

@@ -20,8 +20,8 @@ function hash(str: string) {
2020
return hash >>> 0;
2121
}
2222

23-
function create_style_information(doc: Document | ShadowRoot, node: Element & ElementCSSInlineStyle) {
24-
const info = { stylesheet: append_empty_stylesheet(node), rules: {} };
23+
function create_style_information(doc: Document | ShadowRoot) {
24+
const info = { style_element: element('style'), rules: {} };
2525
managed_styles.set(doc, info);
2626
return info;
2727
}
@@ -39,9 +39,10 @@ export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b:
3939
const name = `__svelte_${hash(rule)}_${uid}`;
4040
const doc = get_root_for_style(node);
4141

42-
const { stylesheet, rules } = managed_styles.get(doc) || create_style_information(doc, node);
42+
const { style_element, rules } = managed_styles.get(doc) || create_style_information(doc);
4343

4444
if (!rules[name]) {
45+
const stylesheet = append_stylesheet(doc, style_element);
4546
rules[name] = true;
4647
stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length);
4748
}
@@ -71,10 +72,8 @@ export function clear_rules() {
7172
raf(() => {
7273
if (active) return;
7374
managed_styles.forEach(info => {
74-
const { stylesheet } = info;
75-
let i = stylesheet.cssRules.length;
76-
while (i--) stylesheet.deleteRule(i);
77-
info.rules = {};
75+
const { style_element } = info;
76+
detach(style_element);
7877
});
7978
managed_styles.clear();
8079
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
skip_if_ssr: true,
3+
skip_if_hydrate: true,
4+
skip_if_hydrate_from_ssr: true,
5+
test({ raf, assert, component, window }) {
6+
component.visible = true;
7+
raf.tick(100);
8+
component.visible = false;
9+
raf.tick(200);
10+
raf.tick(0);
11+
12+
assert.htmlEqual(window.document.head.innerHTML, '');
13+
}
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
export let visible = false;
3+
4+
function foo() {
5+
return {
6+
duration: 100,
7+
css: () => ''
8+
};
9+
}
10+
</script>
11+
12+
{#if visible}
13+
<div transition:foo></div>
14+
{/if}

0 commit comments

Comments
 (0)