-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
has-fixup.js
41 lines (34 loc) · 983 Bytes
/
has-fixup.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
// This is a very temporary fix to split :has() selectors
// into their own rules for `optimizeUniversalDefaults`
module.exports = function () {
return {
postcssPlugin: 'has-fixup',
RuleExit(rule, { Rule }) {
if (rule.selectors.length < 2) {
return
}
if (!rule.selectors.some(s => s.includes(':has'))) {
return
}
let withHas = []
let withoutHas = []
// Find every selector with the :has() pseudo-class
for (let sel of rule.selectors) {
if (sel.includes(':has')) {
withHas.push(sel)
} else {
withoutHas.push(sel)
}
}
// Remove the :has() selectors from the original rule
rule.selectors = withoutHas
// Add the :has() selectors to a new rule
const newRule = new Rule({
selectors: withHas,
nodes: rule.clone().nodes,
})
rule.parent.insertAfter(rule, newRule)
}
}
};
module.exports.postcss = true;