forked from brikou/CSS-in-JS-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertScopedCssForEmotion.ts
More file actions
57 lines (49 loc) · 1.7 KB
/
convertScopedCssForEmotion.ts
File metadata and controls
57 lines (49 loc) · 1.7 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
import {parse} from "postcss";
import {stringify} from "postcss";
import type {AnyNode} from "postcss";
import { convertSelectorForEmotion } from "./convertSelectorForEmotion";
import { escapeScopedCss } from "./escapeScopedCss";
export function convertScopedCssForEmotion(
scopedCss: string,
scope: string,
knownScopes: Set<string>,
): string {
let scopedCssForEmotion = "";
function builder(
output: string,
node?: AnyNode,
flag?: "start" | "end",
) {
if (
(flag === "start" || flag === "end") &&
node &&
node.type === "rule"
) {
if (node.selector === scope) {
if (node.parent.type === "root") {
return;
} else if (flag === "start") {
output = "& {";
}
} else {
if (flag === "start") {
const convertedSelectors: Set<string> = new Set();
(node.selectors || []).forEach((selector) => {
convertedSelectors.add(
convertSelectorForEmotion(
selector,
scope,
knownScopes,
),
);
});
// TODO remove join usage once https://github.com/prettier/prettier/issues/2883 is resolved
output = `${[...convertedSelectors].join(", ")} {`;
}
}
}
scopedCssForEmotion += output;
}
stringify(parse(scopedCss), builder)
return escapeScopedCss(scopedCssForEmotion);
}