1
1
import { append_empty_stylesheet , get_root_for_style } from './dom' ;
2
2
import { raf } from './environment' ;
3
3
4
- interface ExtendedDoc extends Document {
5
- __svelte_stylesheet : CSSStyleSheet ;
6
- __svelte_rules : Record < string , true > ;
4
+ interface StyleInformation {
5
+ stylesheet : CSSStyleSheet ;
6
+ rules : Record < string , true > ;
7
7
}
8
8
9
- const active_docs = new Set < ExtendedDoc > ( ) ;
9
+ // we need to store the information for multiple documents because a Svelte application could also contain iframes
10
+ // https://github.com/sveltejs/svelte/issues/3624
11
+ const managed_styles = new Map < Document | ShadowRoot , StyleInformation > ( ) ;
10
12
let active = 0 ;
11
13
12
14
// https://github.com/darkskyapp/string-hash/blob/master/index.js
@@ -18,6 +20,12 @@ function hash(str: string) {
18
20
return hash >>> 0 ;
19
21
}
20
22
23
+ function create_style_information ( doc : Document | ShadowRoot , node : Element & ElementCSSInlineStyle ) {
24
+ const info = { stylesheet : append_empty_stylesheet ( node ) , rules : { } } ;
25
+ managed_styles . set ( doc , info ) ;
26
+ return info ;
27
+ }
28
+
21
29
export function create_rule ( node : Element & ElementCSSInlineStyle , a : number , b : number , duration : number , delay : number , ease : ( t : number ) => number , fn : ( t : number , u : number ) => string , uid : number = 0 ) {
22
30
const step = 16.666 / duration ;
23
31
let keyframes = '{\n' ;
@@ -29,13 +37,12 @@ export function create_rule(node: Element & ElementCSSInlineStyle, a: number, b:
29
37
30
38
const rule = keyframes + `100% {${ fn ( b , 1 - b ) } }\n}` ;
31
39
const name = `__svelte_${ hash ( rule ) } _${ uid } ` ;
32
- const doc = get_root_for_style ( node ) as ExtendedDoc ;
33
- active_docs . add ( doc ) ;
34
- const stylesheet = doc . __svelte_stylesheet || ( doc . __svelte_stylesheet = append_empty_stylesheet ( node ) . sheet as CSSStyleSheet ) ;
35
- const current_rules = doc . __svelte_rules || ( doc . __svelte_rules = { } ) ;
40
+ const doc = get_root_for_style ( node ) ;
41
+
42
+ const { stylesheet, rules } = managed_styles . get ( doc ) || create_style_information ( doc , node ) ;
36
43
37
- if ( ! current_rules [ name ] ) {
38
- current_rules [ name ] = true ;
44
+ if ( ! rules [ name ] ) {
45
+ rules [ name ] = true ;
39
46
stylesheet . insertRule ( `@keyframes ${ name } ${ rule } ` , stylesheet . cssRules . length ) ;
40
47
}
41
48
@@ -63,12 +70,12 @@ export function delete_rule(node: Element & ElementCSSInlineStyle, name?: string
63
70
export function clear_rules ( ) {
64
71
raf ( ( ) => {
65
72
if ( active ) return ;
66
- active_docs . forEach ( doc => {
67
- const stylesheet = doc . __svelte_stylesheet ;
73
+ managed_styles . forEach ( info => {
74
+ const { stylesheet } = info ;
68
75
let i = stylesheet . cssRules . length ;
69
76
while ( i -- ) stylesheet . deleteRule ( i ) ;
70
- doc . __svelte_rules = { } ;
77
+ info . rules = { } ;
71
78
} ) ;
72
- active_docs . clear ( ) ;
79
+ managed_styles . clear ( ) ;
73
80
} ) ;
74
81
}
0 commit comments