-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathis-in-compound.ts
More file actions
58 lines (48 loc) · 1.48 KB
/
is-in-compound.ts
File metadata and controls
58 lines (48 loc) · 1.48 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
import parser from 'postcss-selector-parser';
// .a:-csstools-matches(.b > .c)
// equivalent to
// .b > .c.a
//
// :-csstools-matches(.b > .c).a
// equivalent to
// .b > .c.a
//
// because `:-csstools-matches()` matches the final element of the selector,
export function isInCompoundWithOneOtherElement(selector: parser.Container): boolean {
if (!selector || !selector.nodes) {
return false;
}
if (!parser.isSelector(selector)) {
return false;
}
if (selector.nodes.length !== 2) {
return false;
}
let isPseudoIndex: number = -1;
let simpleSelectorIndex: number = -1;
if (selector.nodes[0] && parser.isPseudoClass(selector.nodes[0]) && selector.nodes[0].value === ':-csstools-matches') {
isPseudoIndex = 0;
simpleSelectorIndex = 1;
} else if (selector.nodes[1] && parser.isPseudoClass(selector.nodes[1]) && selector.nodes[1].value === ':-csstools-matches') {
isPseudoIndex = 1;
simpleSelectorIndex = 0;
}
const isPseudo: parser.Node | undefined = selector.nodes[isPseudoIndex];
if (!isPseudo || !parser.isPseudoClass(isPseudo) || isPseudo.nodes.length !== 1) {
return false;
}
const simpleSelector: parser.Node | undefined = selector.nodes[simpleSelectorIndex];
if (!simpleSelector) {
return false;
}
if (parser.isCombinator(simpleSelector)) {
return false;
}
if (parser.isPseudoElement(simpleSelector)) {
return false;
}
isPseudo.nodes[0].append(simpleSelector.clone());
isPseudo.replaceWith(...isPseudo.nodes[0].nodes);
simpleSelector.remove();
return true;
}