forked from sass/dart-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressed_test.dart
381 lines (333 loc) · 10.1 KB
/
compressed_test.dart
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
// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
@TestOn('vm')
library;
import 'package:test/test.dart';
import 'package:sass/sass.dart';
void main() {
group("in style rules", () {
test("removes unnecessary whitespace and semicolons", () {
expect(_compile("a {x: y}"), equals("a{x:y}"));
});
group("for selectors", () {
test("preserves whitespace where necessary", () {
expect(_compile("a b .c {x: y}"), equals("a b .c{x:y}"));
});
test("removes whitespace after commas", () {
expect(_compile("a, b, .c {x: y}"), equals("a,b,.c{x:y}"));
});
test("doesn't preserve newlines", () {
expect(_compile("a,\nb,\n.c {x: y}"), equals("a,b,.c{x:y}"));
});
test("removes whitespace around combinators", () {
expect(_compile("a > b {x: y}"), equals("a>b{x:y}"));
expect(_compile("a + b {x: y}"), equals("a+b{x:y}"));
expect(_compile("a ~ b {x: y}"), equals("a~b{x:y}"));
});
group("in prefixed pseudos", () {
test("preserves whitespace", () {
expect(
_compile("a:nth-child(2n of b) {x: y}"),
equals("a:nth-child(2n of b){x:y}"),
);
});
test("removes whitespace after commas", () {
expect(
_compile("a:nth-child(2n of b, c) {x: y}"),
equals("a:nth-child(2n of b,c){x:y}"),
);
});
});
group("in attribute selectors with modifiers", () {
test("removes whitespace when quotes are required", () {
expect(_compile('[a=" " b] {x: y}'), equals('[a=" "b]{x:y}'));
});
test("doesn't remove whitespace when quotes aren't required", () {
expect(_compile('[a="b"c] {x: y}'), equals('[a=b c]{x:y}'));
});
});
});
group("for declarations", () {
test("preserves semicolons when necessary", () {
expect(_compile("a {q: r; s: t}"), equals("a{q:r;s:t}"));
});
group("of custom properties", () {
test("folds whitespace for multiline properties", () {
expect(
_compile("""
a {
--foo: {
q: r;
b {
s: t;
}
}
}
"""),
equals("a{--foo: { q: r; b { s: t; } } }"),
);
});
test("folds whitespace for single-line properties", () {
expect(
_compile("""
a {
--foo: a b\t\tc;
}
"""),
equals("a{--foo: a b\tc}"),
);
});
test("preserves semicolons when necessary", () {
expect(
_compile("""
a {
--foo: {
a: b;
};
--bar: x y;
--baz: q r;
}
"""),
equals("a{--foo: { a: b; };--bar: x y;--baz: q r}"),
);
});
});
});
});
group("values:", () {
group("numbers", () {
test("omit the leading 0", () {
expect(_compile("a {b: 0.123}"), equals("a{b:.123}"));
expect(_compile("a {b: 0.123px}"), equals("a{b:.123px}"));
});
});
group("lists", () {
test("don't include spaces after commas", () {
expect(_compile("a {b: x, y, z}"), equals("a{b:x,y,z}"));
});
test("don't include spaces around slashes", () {
expect(
_compile("""
@use "sass:list";
a {b: list.slash(x, y, z)}
"""),
equals("a{b:x/y/z}"),
);
});
test("do include spaces when space-separated", () {
expect(_compile("a {b: x y z}"), equals("a{b:x y z}"));
});
});
group("colors", () {
test("use names when they're shortest", () {
expect(_compile("a {b: #f00}"), equals("a{b:red}"));
});
test("use terse hex when it's shortest", () {
expect(_compile("a {b: white}"), equals("a{b:#fff}"));
});
test("use verbose hex when it's shortest", () {
expect(_compile("a {b: darkgoldenrod}"), equals("a{b:#b8860b}"));
});
test("use rgba() when necessary", () {
expect(
_compile("a {b: rgba(255, 0, 0, 0.5)}"),
equals("a{b:rgba(255,0,0,.5)}"),
);
});
test("don't error when there's no name", () {
expect(_compile("a {b: #cc3232}"), equals("a{b:#cc3232}"));
});
});
group("strings", () {
group("emits private-use area characters as literal characters", () {
testCharacter(int character) {
var escape = "\\${character.toRadixString(16)}";
test(escape, () {
expect(
_compile("a {b: $escape}"),
equalsIgnoringWhitespace(
"a{b:${String.fromCharCode(character)}}",
),
);
});
}
group("in the basic multilingual plane", () {
testCharacter(0xe000);
testCharacter(0xf000);
testCharacter(0xf8ff);
});
group("in the supplementary planes", () {
testCharacter(0xf0000);
testCharacter(0xfabcd);
testCharacter(0xffffd);
testCharacter(0x100000);
testCharacter(0x10abcd);
testCharacter(0x10fffd);
});
});
});
});
group("the top level", () {
test("removes whitespace and semicolons between at-rules", () {
expect(_compile("@foo; @bar; @baz;"), equals("@foo;@bar;@baz"));
});
test("removes whitespace between style rules", () {
expect(_compile("a {b: c} x {y: z}"), equals("a{b:c}x{y:z}"));
});
});
group("@supports", () {
test("removes whitespace around the condition", () {
expect(
_compile("@supports (display: flex) {a {b: c}}"),
equals("@supports(display: flex){a{b:c}}"),
);
});
test("preserves whitespace before the condition if necessary", () {
expect(
_compile("@supports not (display: flex) {a {b: c}}"),
equals("@supports not (display: flex){a{b:c}}"),
);
});
});
group("@media", () {
test("removes whitespace around the query", () {
expect(
_compile("@media (min-width: 900px) {a {b: c}}"),
equals("@media(min-width: 900px){a{b:c}}"),
);
});
test("preserves whitespace before the query if necessary", () {
expect(
_compile("@media screen {a {b: c}}"),
equals("@media screen{a{b:c}}"),
);
});
// Removing whitespace after "and", "or", or "not" is forbidden because it
// would cause it to parse as a function token.
group('preserves whitespace when necessary', () {
test('around "and"', () {
expect(
_compile("""
@media screen and (min-width: 900px) and (max-width: 100px) {
a {b: c}
}
"""),
equals(
"@media screen and (min-width: 900px)and (max-width: 100px)"
"{a{b:c}}",
),
);
});
test('around "or"', () {
expect(
_compile("""
@media (min-width: 900px) or (max-width: 100px) or (print) {
a {b: c}
}
"""),
equals(
"@media(min-width: 900px)or (max-width: 100px)or (print)"
"{a{b:c}}",
),
);
});
test('after "not"', () {
expect(
_compile("""
@media not (min-width: 900px) {
a {b: c}
}
"""),
equals("@media not (min-width: 900px){a{b:c}}"),
);
});
});
test("preserves whitespace around the modifier", () {
expect(
_compile("@media only screen {a {b: c}}"),
equals("@media only screen{a{b:c}}"),
);
});
});
group("@keyframes", () {
test("removes whitespace after the selector", () {
expect(
_compile("@keyframes a {from {a: b}}"),
equals("@keyframes a{from{a:b}}"),
);
});
test("removes whitespace after commas", () {
expect(
_compile("@keyframes a {from, to {a: b}}"),
equals("@keyframes a{from,to{a:b}}"),
);
});
});
group("@import", () {
test("removes whitespace before the URL", () {
expect(_compile('@import "foo.css";'), equals('@import"foo.css"'));
});
test("converts a url() to a string", () {
expect(_compile('@import url(foo.css);'), equals('@import"foo.css"'));
expect(_compile('@import url("foo.css");'), equals('@import"foo.css"'));
});
test("removes whitespace before a media query", () {
expect(
_compile('@import "foo.css" screen;'),
equals('@import"foo.css"screen'),
);
});
test("removes whitespace before a supports condition", () {
expect(
_compile('@import "foo.css" supports(display: flex);'),
equals('@import"foo.css"supports(display: flex)'),
);
});
});
group("comments", () {
test("are removed", () {
expect(_compile("/* foo bar */"), isEmpty);
expect(
_compile("""
a {
b: c;
/* foo bar */
d: e;
}
"""),
equals("a{b:c;d:e}"),
);
});
test("remove their parents if they're the only contents", () {
expect(_compile("a {/* foo bar */}"), isEmpty);
expect(
_compile("""
a {
/* foo bar */
/* baz bang */
}
"""),
isEmpty,
);
});
test("are preserved with /*!", () {
expect(_compile("/*! foo bar */"), equals("/*! foo bar */"));
expect(
_compile("/*! foo */\n/*! bar */"),
equals("/*! foo *//*! bar */"),
);
expect(
_compile("""
a {
/*! foo bar */
}
"""),
equals("a{/*! foo bar */}"),
);
});
});
}
/// Like [compileString], but always produces compressed output.
String _compile(String source) =>
compileString(source, style: OutputStyle.compressed);