-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcssVariables.test.ts
More file actions
181 lines (160 loc) · 4.79 KB
/
Copy pathcssVariables.test.ts
File metadata and controls
181 lines (160 loc) · 4.79 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import fs from "node:fs";
import path from "node:path";
import { baseModeFor, CONCRETE_THEMES, type ConcreteThemeName } from ".";
const REQUIRED_VARIABLES = [
"--content-primary",
"--content-success",
"--content-destructive",
"--content-warning",
"--surface-primary",
"--border-default",
"--border-success",
"--border-destructive",
"--git-added",
"--git-deleted",
"--git-modified",
"--git-merged",
"--surface-git-added",
"--surface-git-deleted",
// Extended palette surface. These carry semantic color meaning in
// alerts, badges, chips, and syntax highlighting. A concrete theme
// must resolve every token after base mode and variant overrides are
// applied.
"--content-link",
"--surface-destructive",
"--surface-green",
"--surface-orange",
"--surface-sky",
"--surface-red",
"--surface-purple",
"--surface-magenta",
"--surface-git-merged",
"--border-warning",
"--border-sky",
"--border-green",
"--border-magenta",
"--border-purple",
"--highlight-purple",
"--highlight-green",
"--highlight-orange",
"--highlight-sky",
"--highlight-red",
"--highlight-magenta",
"--syntax-key",
"--syntax-string",
"--syntax-number",
"--syntax-boolean",
"--git-added-bright",
"--git-deleted-bright",
"--git-merged-bright",
];
const THEME_CLASSES = [
":root",
...CONCRETE_THEMES.map((themeName) => `.${themeName}`),
];
const COLORBLIND_THEME_CLASSES = [
".dark-protan-deuter",
".light-protan-deuter",
".dark-tritan",
".light-tritan",
];
const TRITAN_THEME_CLASSES = [".dark-tritan", ".light-tritan"];
function stripCssComments(css: string): string {
return css.replace(/\/\*[\s\S]*?\*\//g, "");
}
function extractBlock(css: string, selector: string): string | null {
const cssWithoutComments = stripCssComments(css);
for (const match of cssWithoutComments.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
const selectorList = match[1];
const block = match[2];
if (selectorList === undefined || block === undefined) {
continue;
}
const selectors = selectorList.split(",").map((value) => value.trim());
if (selectors.includes(selector)) {
return block;
}
}
return null;
}
function extractVariable(block: string, variable: string): string | null {
return extractVariables(block).get(variable) ?? null;
}
function extractVariables(block: string): Map<string, string> {
const variables = new Map<string, string>();
for (const match of block.matchAll(/(--[\w-]+)\s*:\s*([^;]+);/g)) {
const variable = match[1];
const value = match[2];
if (variable === undefined || value === undefined) {
continue;
}
variables.set(variable, value.trim());
}
return variables;
}
function extractEffectiveBlock(css: string, selector: string): string | null {
const block = extractBlock(css, selector);
if (block === null) {
return null;
}
if (!selector.startsWith(".") || !selector.includes("-")) {
return block;
}
const themeName = selector.slice(1) as ConcreteThemeName;
const baseBlock = extractBlock(css, `.${baseModeFor(themeName)}`);
if (baseBlock === null) {
return null;
}
return `${baseBlock}\n${block}`;
}
describe("theme CSS variables", () => {
const cssPath = path.resolve(__dirname, "../index.css");
const css = fs.readFileSync(cssPath, "utf8");
for (const selector of THEME_CLASSES) {
describe(selector, () => {
const block = extractBlock(css, selector);
const effectiveBlock = extractEffectiveBlock(css, selector);
it("has a rule block in index.css", () => {
expect(block).not.toBeNull();
});
if (effectiveBlock !== null) {
for (const variable of REQUIRED_VARIABLES) {
it(`resolves ${variable}`, () => {
expect(extractVariable(effectiveBlock, variable)).not.toBeNull();
});
}
}
});
}
for (const selector of COLORBLIND_THEME_CLASSES) {
describe(`${selector} semantic separation`, () => {
const block = extractEffectiveBlock(css, selector);
it("keeps warning distinct from destructive colors", () => {
expect(block).not.toBeNull();
expect(extractVariable(block ?? "", "--content-warning")).not.toBe(
extractVariable(block ?? "", "--content-destructive"),
);
expect(extractVariable(block ?? "", "--surface-orange")).not.toBe(
extractVariable(block ?? "", "--surface-red"),
);
});
it("keeps links distinct from success colors", () => {
expect(block).not.toBeNull();
expect(extractVariable(block ?? "", "--content-link")).not.toBe(
extractVariable(block ?? "", "--content-success"),
);
});
});
}
for (const selector of TRITAN_THEME_CLASSES) {
describe(`${selector} warning surface`, () => {
const block = extractEffectiveBlock(css, selector);
it("keeps warning surfaces on the fuchsia surface token", () => {
expect(block).not.toBeNull();
expect(extractVariable(block ?? "", "--surface-orange")).toBe(
extractVariable(block ?? "", "--surface-magenta"),
);
});
});
}
});