-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathutils.js
More file actions
76 lines (63 loc) · 2.07 KB
/
utils.js
File metadata and controls
76 lines (63 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {sheets as defaultSheets} from '../packages/jss/src'
import * as moduleIdExports from '../packages/jss/src/utils/moduleId'
export function resetModuleId() {
moduleIdExports.default = 0
}
export function resetSheets(sheets = defaultSheets) {
return () => {
sheets.reset()
const styles = document.head.querySelectorAll('[data-jss]')
for (let i = 0; i < styles.length; i++) {
document.head.removeChild(styles[i])
}
}
}
export function removeWhitespace(str) {
return str.replace(/\s/g, '')
}
export function getRules(style) {
const rulesArr = []
const {cssRules} = style.sheet
for (const key in cssRules) {
if ([].hasOwnProperty.call(cssRules, key)) {
rulesArr.push(cssRules[key])
}
}
return rulesArr
}
export function getStyle() {
return document.getElementsByTagName('style')[0]
}
export function getCss(style) {
// IE doesn't provide correct `sheet.cssRules` when at-rules have been added
// by using `.addRule()` API.
// Others do not update .textContent result when `.addRule()` was used.
const textContent = removeWhitespace(style.textContent)
const cssText = getRules(style)
.map(rule => removeWhitespace(rule.cssText))
.join('')
// We try to use the most complete version.
// Potentially this is fragile too.
return textContent.length > cssText.length ? textContent : cssText
}
/**
* We need to remove vendor prefixes for some tests,
* because some browsers automatically add them (like Mobile Safari 9.0.0)
*/
export function removeVendorPrefixes(str) {
return str.replace(/-webkit-|-moz-|-o-|-ms-/g, '')
}
export function computeStyle(className) {
const el = document.createElement('div')
el.className = className
document.body.appendChild(el)
const style = window.getComputedStyle ? getComputedStyle(el) : el.currentStyle
// This will work also for CSS2Properties from Firefox.
const styleCopy = {}
for (const key in style) styleCopy[key] = style[key]
setTimeout(() => {
document.body.removeChild(el)
})
return styleCopy
}
export const createGenerateId = () => rule => `${rule.key}-id`