-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtailwindcss.test.ts
More file actions
356 lines (307 loc) · 13 KB
/
tailwindcss.test.ts
File metadata and controls
356 lines (307 loc) · 13 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { describe, expect, it } from "./test-deps.ts";
let tailwindExcerpt =
`/*! modern-normalize v1.0.0 | MIT License | https://github.com/sindresorhus/modern-normalize */*,::after,::before{box-sizing:border-box}:root{-moz-tab-size:4;tab-size:4}html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}body{font-family:system-ui,-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji'}hr{height:0;color:inherit}abbr[title]{-webkit-text-decoration:underline dotted;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}::-moz-focus-inner{border-style:none;padding:0}:-moz-focusring{outline:1px dotted ButtonText}:-moz-ui-invalid{box-shadow:none}legend{padding:0}progress{vertical-align:baseline}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}button{background-color:transparent;background-image:none}button:focus{outline:1px dotted;outline:5px auto -webkit-focus-ring-color}fieldset{margin:0;padding:0}ol,ul{list-style:none;margin:0;padding:0}html{font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";line-height:1.5}body{font-family:inherit;line-height:inherit}*,::after,::before{box-sizing:border-box;border-width:0;border-style:solid;border-color:#e5e7eb}hr{border-top-width:1px}img{border-style:solid}textarea{resize:vertical}input::placeholder,textarea::placeholder{color:#9ca3af}[role=button],button{cursor:pointer}table{border-collapse:collapse}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}button,input,optgroup,select,textarea{padding:0;line-height:inherit;color:inherit}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}.space-y-0>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(0px * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(0px * var(--tw-space-y-reverse))}.space-x-0>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(0px * var(--tw-space-x-reverse));margin-left:calc(0px * calc(1 - var(--tw-space-x-reverse)))}.space-y-1>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.25rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.25rem * var(--tw-space-y-reverse))}.space-x-1>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.25rem * var(--tw-space-x-reverse));margin-left:calc(.25rem * calc(1 - var(--tw-space-x-reverse)))}.space-y-2>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.5rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.5rem * var(--tw-space-y-reverse))}.space-x-2>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.5rem * var(--tw-space-x-reverse));margin-left:calc(.5rem * calc(1 - var(--tw-space-x-reverse)))}.space-y-3>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.75rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.75rem * var(--tw-space-y-reverse))}.space-x-3>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.75rem * var(--tw-space-x-reverse));margin-left:calc(.75rem * calc(1 - var(--tw-space-x-reverse)))}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem * var(--tw-space-y-reverse))}.space-x-4>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1rem * var(--tw-space-x-reverse));margin-left:calc(1rem * calc(1 - var(--tw-space-x-reverse)))}.space-y-5>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1.25rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1.25rem * var(--tw-space-y-reverse))}`;
// tailwindExcerpt = `/*! modern-normalize v1.0.0 | MIT License | https://github.com/sindresorhus/modern-normalize */*,::after,::before{box-sizing:border-box}:root{-moz-tab-size:4;tab-size:4}html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}body{font-family:system-ui,-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji'}hr{height:0;color:inherit}`;
import { has, hasMore, lookAhead, parse, ParseGenerator } from "./index.ts";
interface CSSComment {
type: "comment";
content: string;
}
interface CSSDeclaration {
name: string;
rawValue: string;
}
interface CSSRule {
type: "rule";
selectors: Array<string>;
declarations: Array<CSSDeclaration>;
}
interface CSSMedia {
type: "media";
rawFeatures: string;
rules: Array<CSSRule>;
}
const whitespaceMay = /^\s*/;
function* PropertyParser() {
const [name]: [string] = yield /^[-a-z]+/;
return name;
}
function* ValueParser() {
const [rawValue]: [string] = yield /^[^;}]+/;
// const [rawValue]: [
// string
// ] = yield /^(0|-?(\d+[.]\d+|[.]\d+|\d+[.]|\d+)(rem|em|%|px|pt|ch|)(\s[-\w,\s'"]+)?|var\(--[\w-]+\)|[-\w,\s'"]+)|#[\da-fA-F]+/;
return rawValue;
}
function* DeclarationParser() {
const name = yield PropertyParser;
yield whitespaceMay;
yield ":";
yield whitespaceMay;
const rawValue = yield ValueParser;
yield whitespaceMay;
yield has(";");
return { name, rawValue };
}
function* SelectorComponentParser() {
// const [selector]: [
// string
// ] = yield /^(:root|[*]|::after|::before|html|[a-z][\w]*)/;
const [selector]: [string] = yield /^([.#]?[-:=~*>.#\(\)\w\[\]]+)/;
return selector;
}
function* RuleParser(): ParseGenerator<CSSRule> {
const declarations: Array<CSSDeclaration> = [];
// const [selector] = yield /(:root|[*]|[a-z][\w]*)/;
const selectors: Array<string> = [];
yield whitespaceMay;
while (true) {
selectors.push(yield SelectorComponentParser);
yield whitespaceMay;
if (yield has(",")) {
yield whitespaceMay;
continue;
}
if (yield has("{")) break;
}
// yield whitespaceMay;
// yield "{";
yield whitespaceMay;
while ((yield has("}")) === false) {
declarations.push(yield DeclarationParser);
yield whitespaceMay;
}
return { type: "rule", selectors: selectors, declarations };
}
type MediaQueryParser = {
type: "media";
rawFeatures: string;
rules: ReadonlyArray<CSSRule>;
};
function* MediaQueryParser():
| Generator<RegExp, MediaQueryParser, [string] & ReadonlyArray<string>>
| Generator<unknown, MediaQueryParser, unknown> {
yield "@media";
yield whitespaceMay;
yield "(";
const [rawFeatures]: [string] = yield /^[^)]+/;
yield ")";
yield whitespaceMay;
yield "{";
const rules: ReadonlyArray<CSSRule> = yield RulesParser;
yield "}";
return { type: "media", rawFeatures, rules };
}
function* CommentParser(): Generator<
RegExp | string,
CSSComment,
[string, string]
> {
yield "/*";
const [, content] = yield /^(.*?)\*\//;
return { type: "comment", content };
}
function* RulesParser(): ParseGenerator<Array<CSSRule>> {
const rules: Array<CSSRule> = [];
const hasClosingParent = has(lookAhead(/}/));
yield whitespaceMay;
while (yield hasMore) {
if (yield hasClosingParent) break;
rules.push(yield [RuleParser, CommentParser]);
yield whitespaceMay;
// if (yield closingParent) break;
}
return rules;
}
function* StylesheetParser() {
const elements: Array<any> = [];
yield whitespaceMay;
while (yield hasMore) {
elements.push(yield [RuleParser, CommentParser, MediaQueryParser]);
yield whitespaceMay;
}
return elements;
}
function parseCSS(cssSource: string) {
return parse(cssSource, StylesheetParser());
}
/////
function* generateComment(item: CSSComment) {
yield "/*";
yield item.content;
yield "*/";
}
function* generateDeclaration(declaration: CSSDeclaration) {
yield declaration.name;
yield ":";
yield declaration.rawValue;
}
function* generateRule(item: CSSRule) {
yield item.selectors.join(",");
yield "{";
for (const [index, declaration] of item.declarations.entries()) {
yield* generateDeclaration(declaration);
if (index < item.declarations.length - 1) {
yield ";";
}
}
yield "}";
}
function* generateMediaSource(item: CSSMedia) {
yield "@media ";
yield "(";
yield item.rawFeatures;
yield ")";
yield "{";
for (const rule of item.rules) {
yield* generateRule(rule);
}
yield "}";
}
function* generateCSSSource(items: Array<CSSRule | CSSMedia | CSSComment>) {
for (const item of items) {
if (item.type === "media") {
yield* generateMediaSource(item);
} else if (item.type === "rule") {
yield* generateRule(item);
} else if (item.type === "comment") {
yield* generateComment(item);
}
}
}
function stringifyCSS(items: Array<CSSRule | CSSMedia | CSSComment>) {
return Array.from(generateCSSSource(items)).join("");
}
describe("CSS values", () => {
it("parses 42", () => {
expect(parse("42", ValueParser())).toMatchObject({
remaining: "",
result: "42",
success: true,
});
});
it("parses 1.15", () => {
expect(parse("1.15", ValueParser())).toMatchObject({
remaining: "",
result: "1.15",
success: true,
});
});
it("parses 1.", () => {
expect(parse("1.", ValueParser())).toMatchObject({
remaining: "",
result: "1.",
success: true,
});
});
it("parses .1", () => {
expect(parse(".1", ValueParser())).toMatchObject({
remaining: "",
result: ".1",
success: true,
});
});
it("parses hex color", () => {
expect(parse("#e5e7eb", ValueParser())).toMatchObject({
remaining: "",
result: "#e5e7eb",
success: true,
});
});
it("parses 100%", () => {
expect(parse("100%", ValueParser())).toMatchObject({
remaining: "",
result: "100%",
success: true,
});
});
it("parses font stack", () => {
expect(
parse(
`system-ui,-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji'`,
ValueParser(),
),
).toMatchObject({
remaining: "",
result:
`system-ui,-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji'`,
success: true,
});
});
it("parses border long-form", () => {
expect(parse(`1px solid black`, ValueParser())).toMatchObject({
remaining: "",
result: `1px solid black`,
success: true,
});
});
it("parses var", () => {
expect(parse(`var(--primary)`, ValueParser())).toMatchObject({
remaining: "",
result: `var(--primary)`,
success: true,
});
});
});
describe("selectors", () => {
it("parses .container", () => {
expect(parse(".container", SelectorComponentParser())).toEqual({
remaining: "",
result: ".container",
success: true,
});
});
});
describe("media queries", () => {
it("parses empty", () => {
expect(
parse(`@media (min-width:640px){}`, MediaQueryParser() as any),
).toEqual({
remaining: "",
result: {
rawFeatures: "min-width:640px",
rules: [],
type: "media",
},
success: true,
});
});
it("parses with class", () => {
expect(
parse(
`@media (min-width:640px){.container{max-width:640px}}`,
MediaQueryParser() as any,
),
).toEqual({
remaining: "",
result: {
rawFeatures: "min-width:640px",
rules: [
{
declarations: [
{
name: "max-width",
rawValue: "640px",
},
],
selectors: [".container"],
type: "rule",
},
],
type: "media",
},
success: true,
});
});
});
it("parses Tailwind excerpt", () => {
const result = parseCSS(tailwindExcerpt);
expect(result.success).toBe(true);
});
it("parses and stringifies Tailwind excerpt", () => {
const result = parseCSS(tailwindExcerpt);
if (result.success !== true) {
fail("Parsing failed");
}
expect(stringifyCSS(result.result)).toEqual(tailwindExcerpt);
});