-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathsame-preceding-element.ts
More file actions
97 lines (78 loc) · 2.35 KB
/
same-preceding-element.ts
File metadata and controls
97 lines (78 loc) · 2.35 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import parser from 'postcss-selector-parser';
// .a > :-csstools-matches(.b > .c)
// equivalent to
// .a.b > .c
//
// and
//
// .a + :-csstools-matches(.b + .c)
// equivalent to
// .a.b + .c
//
// because adjacent elements have the same parent element.
export function samePrecedingElement(selector: parser.Container): boolean {
if (!selector || !selector.nodes) {
return false;
}
if (selector.type !== 'selector') {
return false;
}
let combinatorIndex = -1;
for (let i = 0; i < selector.nodes.length; i++) {
const node = selector.nodes[i];
if (parser.isCombinator(node)) {
combinatorIndex = i;
break;
}
if (parser.isPseudoElement(node)) {
return false;
}
}
if (combinatorIndex === -1) {
return false;
}
const isPseudoIndex = combinatorIndex + 1;
// immediate sibling/child combinator
if (!selector.nodes[combinatorIndex] || selector.nodes[combinatorIndex].type !== 'combinator' || (selector.nodes[combinatorIndex].value !== '>' && selector.nodes[combinatorIndex].value !== '+')) {
return false;
}
const combinator = selector.nodes[combinatorIndex].value;
if (!selector.nodes[isPseudoIndex] || selector.nodes[isPseudoIndex].type !== 'pseudo' || selector.nodes[isPseudoIndex].value !== ':-csstools-matches') {
return false;
}
// second `:-csstools-matches`
{
if (!selector.nodes[isPseudoIndex].nodes || selector.nodes[isPseudoIndex].nodes.length !== 1) {
return false;
}
if (selector.nodes[isPseudoIndex].nodes[0].type !== 'selector') {
return false;
}
if (!selector.nodes[isPseudoIndex].nodes[0].nodes || selector.nodes[isPseudoIndex].nodes[0].nodes.length !== 3) {
return false;
}
// same combinator
if (!selector.nodes[isPseudoIndex].nodes[0].nodes || selector.nodes[isPseudoIndex].nodes[0].nodes[1].type !== 'combinator' || selector.nodes[isPseudoIndex].nodes[0].nodes[1].value !== combinator) {
return false;
}
}
const isPseudo = selector.nodes[isPseudoIndex];
if (!isPseudo || !parser.isPseudoClass(isPseudo)) {
return false;
}
const before = selector.nodes.slice(0, combinatorIndex);
const after = selector.nodes.slice(isPseudoIndex + 1);
selector.each((node) => {
node.remove();
});
before.forEach((node) => {
selector.append(node);
});
isPseudo.nodes[0].nodes.forEach((node) => {
selector.append(node);
});
after.forEach((node) => {
selector.append(node);
});
return true;
}