@@ -21,6 +21,20 @@ export function cssom(element?: CSSStyleSheet | Element | null | false): Sheet<C
21
21
return {
22
22
target,
23
23
24
+ snapshot ( ) {
25
+ // collect current rules
26
+ const rules = Array . from ( target . cssRules , ( rule ) => rule . cssText )
27
+
28
+ return ( ) => {
29
+ // remove all existing rules
30
+ this . clear ( )
31
+
32
+ // add all snapshot rules back
33
+ // eslint-disable-next-line @typescript-eslint/unbound-method
34
+ rules . forEach ( this . insert as ( cssText : string , index : number ) => void )
35
+ }
36
+ } ,
37
+
24
38
clear ( ) {
25
39
// remove all added rules
26
40
for ( let index = target . cssRules . length ; index -- ; ) {
@@ -32,10 +46,10 @@ export function cssom(element?: CSSStyleSheet | Element | null | false): Sheet<C
32
46
target . ownerNode ?. remove ( )
33
47
} ,
34
48
35
- insert ( css , index ) {
49
+ insert ( cssText , index ) {
36
50
try {
37
51
// Insert
38
- target . insertRule ( css , index )
52
+ target . insertRule ( cssText , index )
39
53
} catch ( error ) {
40
54
// Empty rule to keep index valid — not using `*{}` as that would show up in all rules (DX)
41
55
target . insertRule ( ':root{}' , index )
@@ -44,8 +58,8 @@ export function cssom(element?: CSSStyleSheet | Element | null | false): Sheet<C
44
58
// lets filter them to prevent unnecessary warnings
45
59
// ::-moz-focus-inner
46
60
// :-moz-focusring
47
- if ( ! / : - [ m w o ] / . test ( css ) ) {
48
- console . warn ( error , css )
61
+ if ( ! / : - [ m w o ] / . test ( cssText ) ) {
62
+ console . warn ( error , cssText )
49
63
}
50
64
}
51
65
} ,
@@ -60,6 +74,20 @@ export function dom(element?: Element | null | false): Sheet<HTMLStyleElement> {
60
74
return {
61
75
target,
62
76
77
+ snapshot ( ) {
78
+ // collect current rules
79
+ const rules = Array . from ( target . childNodes , ( node ) => node . textContent as string )
80
+
81
+ return ( ) => {
82
+ // remove all existing rules
83
+ this . clear ( )
84
+
85
+ // add all snapshot rules back
86
+ // eslint-disable-next-line @typescript-eslint/unbound-method
87
+ rules . forEach ( this . insert as ( cssText : string , index : number ) => void )
88
+ }
89
+ } ,
90
+
63
91
clear ( ) {
64
92
target . textContent = ''
65
93
} ,
@@ -68,8 +96,8 @@ export function dom(element?: Element | null | false): Sheet<HTMLStyleElement> {
68
96
target . remove ( )
69
97
} ,
70
98
71
- insert ( css , index ) {
72
- target . insertBefore ( document . createTextNode ( css ) , target . childNodes [ index ] || null )
99
+ insert ( cssText , index ) {
100
+ target . insertBefore ( document . createTextNode ( cssText ) , target . childNodes [ index ] || null )
73
101
} ,
74
102
75
103
resume : noop ,
@@ -78,9 +106,20 @@ export function dom(element?: Element | null | false): Sheet<HTMLStyleElement> {
78
106
79
107
export function virtual ( includeResumeData ?: boolean ) : Sheet < string [ ] > {
80
108
const target : string [ ] = [ ]
109
+
81
110
return {
82
111
target,
83
112
113
+ snapshot ( ) {
114
+ // collect current rules
115
+ const rules = [ ...target ]
116
+
117
+ return ( ) => {
118
+ // remove all existing rules and add all snapshot rules back
119
+ target . splice ( 0 , target . length , ...rules )
120
+ }
121
+ } ,
122
+
84
123
clear ( ) {
85
124
target . length = 0
86
125
} ,
@@ -128,7 +167,7 @@ export function stringify(target: unknown): string {
128
167
// string[] | CSSStyleSheet | HTMLStyleElement
129
168
return (
130
169
// prefer the raw text content of a CSSStyleSheet as it may include the resume data
131
- ( ( target as CSSStyleSheet ) . ownerNode || ( target as HTMLStyleElement ) ) ? .textContent ||
170
+ ( ( target as CSSStyleSheet ) . ownerNode || ( target as HTMLStyleElement ) ) . textContent ||
132
171
( ( target as CSSStyleSheet ) . cssRules
133
172
? Array . from ( ( target as CSSStyleSheet ) . cssRules , ( rule ) => rule . cssText )
134
173
: asArray ( target )
0 commit comments