forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss-parser.ts
More file actions
426 lines (367 loc) · 11.3 KB
/
css-parser.ts
File metadata and controls
426 lines (367 loc) · 11.3 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { comment, rule, type AstNode, type Comment, type Declaration, type Rule } from './ast'
export function parse(input: string) {
let ast: AstNode[] = []
let licenseComments: Comment[] = []
let stack: (Rule | null)[] = []
let parent = null as Rule | null
let node = null as AstNode | null
let current = ''
let closingBracketStack = ''
for (let i = 0; i < input.length; i++) {
let char = input[i]
// Current character is a `\` therefore the next character is escaped,
// consume it together with the next character and continue.
//
// E.g.:
//
// ```css
// .hover\:foo:hover {}
// ^
// ```
//
if (char === '\\') {
current += input.slice(i, i + 2)
i += 1
}
// Start of a comment.
//
// E.g.:
//
// ```css
// /* Example */
// ^^^^^^^^^^^^^
// .foo {
// color: red; /* Example */
// ^^^^^^^^^^^^^
// }
// .bar {
// color: /* Example */ red;
// ^^^^^^^^^^^^^
// }
// ```
else if (char === '/' && input[i + 1] === '*') {
let start = i
for (let j = i + 2; j < input.length; j++) {
// Current character is a `\` therefore the next character is escaped.
if (input[j] === '\\') {
j += 1
}
// End of the comment
else if (input[j] === '*' && input[j + 1] === '/') {
i = j + 1
break
}
}
let commentString = input.slice(start, i + 1)
// Collect all license comments so that we can hoist them to the top of
// the AST.
if (commentString[2] === '!') {
licenseComments.push(comment(commentString.slice(2, -2)))
}
}
// Start of a string.
else if (char === '"' || char === "'") {
let start = i
// We need to ensure that the closing quote is the same as the opening
// quote.
//
// E.g.:
//
// ```css
// .foo {
// content: "This is a string with a 'quote' in it";
// ^ ^ -> These are not the end of the string.
// }
// ```
for (let j = i + 1; j < input.length; j++) {
// Current character is a `\` therefore the next character is escaped.
if (input[j] === '\\') {
j += 1
}
// End of the string.
else if (input[j] === char) {
i = j
break
}
// End of the line without ending the string but with a `;` at the end.
//
// E.g.:
//
// ```css
// .foo {
// content: "This is a string with a;
// ^ Missing "
// }
// ```
else if (input[j] === ';' && input[j + 1] === '\n') {
throw new Error(`Unterminated string: ${input.slice(start, j + 1) + char}`)
}
// End of the line without ending the string.
//
// E.g.:
//
// ```css
// .foo {
// content: "This is a string with a
// ^ Missing "
// }
// ```
else if (input[j] === '\n') {
throw new Error(`Unterminated string: ${input.slice(start, j) + char}`)
}
}
// Adjust `current` to include the string.
current += input.slice(start, i + 1)
}
// Skip whitespace if the next character is also whitespace. This allows us
// to reduce the amount of whitespace in the AST.
else if (
(char === ' ' || char === '\n' || char === '\t') &&
(input[i + 1] === ' ' || input[i + 1] === '\n' || input[i + 1] === '\t')
) {
continue
}
// Start of a custom property.
//
// Custom properties are very permissive and can contain almost any
// character, even `;` and `}`. Therefore we have to make sure that we are
// at the correct "end" of the custom property by making sure everything is
// balanced.
else if (char === '-' && input[i + 1] === '-' && current.length === 0) {
let closingBracketStack = ''
let start = i
let colonIdx = -1
for (let j = i + 2; j < input.length; j++) {
// Current character is a `\` therefore the next character is escaped.
if (input[j] === '\\') {
j += 1
}
// Start of a comment.
else if (input[j] === '/' && input[j + 1] === '*') {
for (let k = j + 2; k < input.length; k++) {
// Current character is a `\` therefore the next character is escaped.
if (input[k] === '\\') {
k += 1
}
// End of the comment
else if (input[k] === '*' && input[k + 1] === '/') {
j = k + 1
break
}
}
}
// End of the "property" of the property-value pair.
else if (colonIdx === -1 && input[j] === ':') {
colonIdx = current.length + j - start
}
// End of the custom property.
else if (input[j] === ';' && closingBracketStack.length === 0) {
current += input.slice(start, j)
i = j
break
}
// Start of a block.
else if (input[j] === '(') {
closingBracketStack += ')'
} else if (input[j] === '[') {
closingBracketStack += ']'
} else if (input[j] === '{') {
closingBracketStack += '}'
}
// End of the custom property if didn't use a `;` to end the custom
// property.
//
// E.g.:
//
// ```css
// .foo {
// --custom: value
// ^
// }
// ```
else if ((input[j] === '}' || input.length - 1 === j) && closingBracketStack.length === 0) {
i = j - 1
current += input.slice(start, j)
break
}
// End of a block.
else if (input[j] === ')' || input[j] === ']' || input[j] === '}') {
if (
closingBracketStack.length > 0 &&
input[j] === closingBracketStack[closingBracketStack.length - 1]
) {
closingBracketStack = closingBracketStack.slice(0, -1)
}
}
}
let declaration = parseDeclaration(current, colonIdx)
if (parent) {
parent.nodes.push(declaration)
} else {
ast.push(declaration)
}
current = ''
}
// End of a body-less at-rule.
//
// E.g.:
//
// ```css
// @charset "UTF-8";
// ^
// ```
else if (char === ';' && current[0] === '@') {
node = rule(current, [])
// At-rule is nested inside of a rule, attach it to the parent.
if (parent) {
parent.nodes.push(node)
}
// We are the root node which means we are done with the current node.
else {
ast.push(node)
}
// Reset the state for the next node.
current = ''
node = null
}
// End of a declaration.
//
// E.g.:
//
// ```css
// .foo {
// color: red;
// ^
// }
// ```
//
else if (char === ';') {
let declaration = parseDeclaration(current)
if (parent) {
parent.nodes.push(declaration)
} else {
ast.push(declaration)
}
current = ''
}
// Start of a block.
else if (char === '{') {
closingBracketStack += '}'
// At this point `current` should resemble a selector or an at-rule.
node = rule(current.trim(), [])
// Attach the rule to the parent in case it's nested.
if (parent) {
parent.nodes.push(node)
}
// Push the current node to the stack and make it the new parent.
stack.push(parent)
parent = node
// Reset the state for the next node.
current = ''
node = null
}
// End of a block.
else if (char === '}') {
if (closingBracketStack === '') {
throw new Error('Missing opening {')
}
closingBracketStack = closingBracketStack.slice(0, -1)
// When we hit a `}` and `current` is filled in, then it means that we did
// not complete the previous node yet. This means that we hit a
// declaration without a `;` at the end.
if (current.length > 0) {
// This can happen for nested at-rules.
//
// E.g.:
//
// ```css
// @layer foo {
// @tailwind utilities
// ^
// }
// ```
if (current[0] === '@') {
node = rule(current.trim(), [])
// At-rule is nested inside of a rule, attach it to the parent.
if (parent) {
parent.nodes.push(node)
}
// We are the root node which means we are done with the current node.
else {
ast.push(node)
}
// Reset the state for the next node.
current = ''
node = null
}
// But it can also happen for declarations.
//
// E.g.:
//
// ```css
// .foo {
// color: red
// ^
// }
// ```
else {
// Split `current` into a `property` and a `value`. At this point the
// comments are already removed which means that we don't have to worry
// about `:` inside of comments.
let colonIdx = current.indexOf(':')
// Attach the declaration to the parent.
if (parent) {
let importantIdx = current.indexOf('!important', colonIdx + 1)
parent.nodes.push({
kind: 'declaration',
property: current.slice(0, colonIdx).trim(),
value: current
.slice(colonIdx + 1, importantIdx === -1 ? current.length : importantIdx)
.trim(),
important: importantIdx !== -1,
} satisfies Declaration)
}
}
}
// We are done with the current node, which means we can go up one level
// in the stack.
let grandParent = stack.pop() ?? null
// We are the root node which means we are done and contine with the next
// node.
if (grandParent === null && parent) {
ast.push(parent)
}
// Go up one level in the stack.
parent = grandParent
// Reset the state for the next node.
current = ''
node = null
}
// Any other character is part of the current node.
else {
// Skip whitespace at the start of a new node.
if (current.length === 0 && (char === ' ' || char === '\n' || char === '\t')) {
continue
}
current += char
}
}
// When we are done parsing then everything should be balanced. If we still
// have a leftover `parent`, then it means that we have an unterminated block.
if (closingBracketStack.length > 0 && parent) {
throw new Error(`Missing closing } at ${parent.selector}`)
}
if (licenseComments.length > 0) {
return (licenseComments as AstNode[]).concat(ast)
}
return ast
}
function parseDeclaration(current: string, colonIdx: number = current.indexOf(':')): Declaration {
let importantIdx = current.indexOf('!important', colonIdx + 1)
return {
kind: 'declaration',
property: current.slice(0, colonIdx).trim(),
value: current.slice(colonIdx + 1, importantIdx === -1 ? current.length : importantIdx).trim(),
important: importantIdx !== -1,
}
}