Skip to content

Commit 5c867ec

Browse files
authored
Merge pull request #1 from coreui/dev-sync
refactor(get-style): fix IE issues, optimize, cleanup
2 parents a558683 + e5e4ea6 commit 5c867ec

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

js/src/utilities/get-style.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,33 @@
77

88
const getCssCustomProperties = () => {
99
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+
}
1323
}
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+
1630
cssText.split(';').forEach((property) => {
1731
if (property) {
1832
const name = property.split(': ')[0]
1933
const value = property.split(': ')[1]
20-
cssCustomProperties[`--${name.trim()}`] = value.trim()
34+
if (name && value) {
35+
cssCustomProperties[`--${name.trim()}`] = value.trim()
36+
}
2137
}
2238
})
2339
return cssCustomProperties
@@ -38,16 +54,4 @@ const getStyle = (property, element = document.body) => {
3854
return style
3955
}
4056

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-
5357
export default getStyle

0 commit comments

Comments
 (0)