Skip to content

[fix] Removal of empty stylesheets created from transitions #7662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,9 @@ export function get_root_for_style(node: Node): ShadowRoot | Document {
return node.ownerDocument;
}

export function append_empty_stylesheet(node: Node) {
const style_element = element('style') as HTMLStyleElement;
append_stylesheet(get_root_for_style(node), style_element);
return style_element.sheet as CSSStyleSheet;
}

function append_stylesheet(node: ShadowRoot | Document, style: HTMLStyleElement) {
export function append_stylesheet(node: ShadowRoot | Document, style: HTMLStyleElement) {
append((node as Document).head || node, style);
return style.sheet as CSSStyleSheet;
}

export function append_hydration(target: NodeEx, node: NodeEx) {
Expand Down
17 changes: 8 additions & 9 deletions src/runtime/internal/style_manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { append_empty_stylesheet, get_root_for_style } from './dom';
import { append_stylesheet, detach, element, get_root_for_style } from './dom';
import { raf } from './environment';

interface StyleInformation {
stylesheet: CSSStyleSheet;
style_element: HTMLStyleElement;
rules: Record<string, true>;
}

Expand All @@ -20,8 +20,8 @@ function hash(str: string) {
return hash >>> 0;
}

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

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

if (!rules[name]) {
const stylesheet = append_stylesheet(doc, style_element);
rules[name] = true;
stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length);
}
Expand Down Expand Up @@ -71,10 +72,8 @@ export function clear_rules() {
raf(() => {
if (active) return;
managed_styles.forEach(info => {
const { stylesheet } = info;
let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i);
info.rules = {};
const { style_element } = info;
detach(style_element);
});
managed_styles.clear();
});
Expand Down
14 changes: 14 additions & 0 deletions test/runtime/samples/style_manager-cleanup/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
skip_if_ssr: true,
skip_if_hydrate: true,
skip_if_hydrate_from_ssr: true,
test({ raf, assert, component, window }) {
component.visible = true;
raf.tick(100);
component.visible = false;
raf.tick(200);
raf.tick(0);

assert.htmlEqual(window.document.head.innerHTML, '');
}
};
14 changes: 14 additions & 0 deletions test/runtime/samples/style_manager-cleanup/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
export let visible = false;

function foo() {
return {
duration: 100,
css: () => ''
};
}
</script>

{#if visible}
<div transition:foo></div>
{/if}