-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrewriteStyleSheet.js
81 lines (72 loc) · 2.56 KB
/
rewriteStyleSheet.js
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
77
78
79
80
81
import { PSEUDO_STATES } from "./constants"
import { splitSelectors } from "./splitSelectors"
const pseudoStates = Object.values(PSEUDO_STATES)
const matchOne = new RegExp(`:(${pseudoStates.join("|")})`)
const matchAll = new RegExp(`:(${pseudoStates.join("|")})`, "g")
const warnings = new Set()
const warnOnce = (message) => {
if (warnings.has(message)) return
// eslint-disable-next-line no-console
console.warn(message)
warnings.add(message)
}
const rewriteRule = (cssText, selectorText, shadowRoot) => {
return cssText.replace(
selectorText,
splitSelectors(selectorText)
.flatMap((selector) => {
if (selector.includes(".pseudo-")) {
return []
}
if (!matchOne.test(selector)) {
return [selector]
}
const states = []
const plainSelector = selector.replace(matchAll, (_, state) => {
states.push(state)
return ""
})
const classSelector = states.reduce(
(acc, state) => acc.replace(new RegExp(`:${state}`, "g"), `.pseudo-${state}`),
selector
)
if (selector.startsWith(":host(") || selector.startsWith("::slotted(")) {
return [selector, classSelector]
}
const ancestorSelector = shadowRoot
? `:host(${states.map((s) => `.pseudo-${s}`).join("")}) ${plainSelector}`
: `${states.map((s) => `.pseudo-${s}`).join("")} ${plainSelector}`
return [selector, classSelector, ancestorSelector]
})
.join(", ")
)
}
// Rewrites the style sheet to add alternative selectors for any rule that targets a pseudo state.
// A sheet can only be rewritten once, and may carry over between stories.
export const rewriteStyleSheet = (sheet, shadowRoot, shadowHosts) => {
if (sheet.__pseudoStatesRewritten) return
sheet.__pseudoStatesRewritten = true
try {
let index = 0
for (const { cssText, selectorText } of sheet.cssRules) {
if (matchOne.test(selectorText)) {
const newRule = rewriteRule(cssText, selectorText, shadowRoot)
sheet.deleteRule(index)
sheet.insertRule(newRule, index)
if (shadowRoot) shadowHosts.add(shadowRoot.host)
}
index++
if (index > 1000) {
warnOnce("Reached maximum of 1000 pseudo selectors per sheet, skipping the rest.")
break
}
}
} catch (e) {
if (e.toString().includes("cssRules")) {
warnOnce(`Can't access cssRules, likely due to CORS restrictions: ${sheet.href}`)
} else {
// eslint-disable-next-line no-console
console.error(e, sheet.href)
}
}
}