7
7
8
8
const getCssCustomProperties = ( ) => {
9
9
const cssCustomProperties = { }
10
- let root = Object . entries ( document . styleSheets ) . filter ( ( value ) => value [ 1 ] . cssText . substring ( 0 , ':root' . length ) === ':root' )
11
- if ( root . length === 0 ) {
12
- root = Object . entries ( document . styleSheets )
10
+ const sheets = document . styleSheets
11
+ let cssText = ''
12
+ for ( let i = sheets . length - 1 ; i > - 1 ; i -- ) {
13
+ const rules = sheets [ i ] . cssRules
14
+ for ( let j = rules . length - 1 ; j > - 1 ; j -- ) {
15
+ if ( rules [ j ] . selectorText === '.ie-custom-properties' ) {
16
+ cssText = rules [ j ] . cssText
17
+ break
18
+ }
19
+ }
20
+ if ( cssText ) {
21
+ break
22
+ }
13
23
}
14
- const rule = Object . entries ( root [ 0 ] [ 1 ] . cssRules ) . filter ( ( value ) => value [ 1 ] . selectorText === '.ie-custom-properties' )
15
- const cssText = rule [ 0 ] [ 1 ] . style . cssText
24
+
25
+ cssText = cssText . substring (
26
+ cssText . lastIndexOf ( '{' ) + 1 ,
27
+ cssText . lastIndexOf ( '}' )
28
+ )
29
+
16
30
cssText . split ( ';' ) . forEach ( ( property ) => {
17
31
if ( property ) {
18
32
const name = property . split ( ': ' ) [ 0 ]
19
33
const value = property . split ( ': ' ) [ 1 ]
20
- cssCustomProperties [ `--${ name . trim ( ) } ` ] = value . trim ( )
34
+ if ( name && value ) {
35
+ cssCustomProperties [ `--${ name . trim ( ) } ` ] = value . trim ( )
36
+ }
21
37
}
22
38
} )
23
39
return cssCustomProperties
@@ -38,16 +54,4 @@ const getStyle = (property, element = document.body) => {
38
54
return style
39
55
}
40
56
41
- if ( ! Object . entries ) {
42
- Object . entries = function ( obj ) {
43
- const ownProps = Object . keys ( obj )
44
- let i = ownProps . length
45
- const resArray = new Array ( i )
46
- while ( i -- ) {
47
- resArray [ i ] = [ ownProps [ i ] , obj [ ownProps [ i ] ] ]
48
- }
49
- return resArray
50
- }
51
- }
52
-
53
57
export default getStyle
0 commit comments