OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library var_test; | 5 library var_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:csslib/parser.dart'; | |
9 import 'package:csslib/visitor.dart'; | |
10 import 'testing.dart'; | 8 import 'testing.dart'; |
11 | 9 |
| 10 List options = ['--no-colors', '--warnings_as_errors', 'memory']; |
| 11 |
| 12 compileAndValidate(String input, String generated) { |
| 13 var errors = []; |
| 14 var stylesheet = compileCss(input, errors: errors, opts: options); |
| 15 expect(stylesheet != null, true); |
| 16 expect(errors.isEmpty, true, reason: errors.toString()); |
| 17 expect(prettyPrint(stylesheet), generated); |
| 18 } |
| 19 |
| 20 compilePolyfillAndValidate(String input, String generated) { |
| 21 var errors = []; |
| 22 var stylesheet = polyFillCompileCss(input, errors: errors, opts: options); |
| 23 expect(stylesheet != null, true); |
| 24 expect(errors.isEmpty, true, reason: errors.toString()); |
| 25 expect(prettyPrint(stylesheet), generated); |
| 26 } |
| 27 |
12 void simpleVar() { | 28 void simpleVar() { |
13 final errors = []; | |
14 final input = ''':root { | 29 final input = ''':root { |
15 var-color-background: red; | 30 var-color-background: red; |
16 var-color-foreground: blue; | 31 var-color-foreground: blue; |
17 | 32 |
| 33 var-c: #00ff00; |
| 34 var-b: var(c); |
18 var-a: var(b); | 35 var-a: var(b); |
19 var-b: var(c); | |
20 var-c: #00ff00; | |
21 } | 36 } |
22 .testIt { | 37 .testIt { |
23 color: var(color-foreground); | 38 color: var(color-foreground); |
24 background: var(color-background); | 39 background: var(color-background); |
25 } | 40 } |
26 '''; | 41 '''; |
27 | 42 |
28 final generated = ''':root { | 43 final generated = ''':root { |
29 var-color-background: #f00; | 44 var-color-background: #f00; |
30 var-color-foreground: #00f; | 45 var-color-foreground: #00f; |
| 46 var-c: #0f0; |
| 47 var-b: var(c); |
31 var-a: var(b); | 48 var-a: var(b); |
32 var-b: var(c); | |
33 var-c: #0f0; | |
34 } | 49 } |
35 .testIt { | 50 .testIt { |
36 color: var(color-foreground); | 51 color: var(color-foreground); |
37 background: var(color-background); | 52 background: var(color-background); |
38 }'''; | 53 }'''; |
39 | 54 |
40 var stylesheet = compileCss(input, errors: errors, | 55 final generatedPolyfill = ''':root { |
41 opts: ['--no-colors', 'memory']); | 56 } |
| 57 .testIt { |
| 58 color: #00f; |
| 59 background: #f00; |
| 60 }'''; |
42 | 61 |
43 expect(stylesheet != null, true); | 62 compileAndValidate(input, generated); |
44 expect(errors.isEmpty, true, reason: errors.toString()); | 63 compilePolyfillAndValidate(input, generatedPolyfill); |
45 expect(prettyPrint(stylesheet), generated); | |
46 } | 64 } |
47 | 65 |
48 void expressionsVar() { | 66 void expressionsVar() { |
49 final errors = []; | 67 final errors = []; |
50 final input = ''':root { | 68 final input = ''':root { |
51 var-color-background: red; | 69 var-color-background: red; |
52 var-color-foreground: blue; | 70 var-color-foreground: blue; |
53 | 71 |
| 72 var-c: #00ff00; |
| 73 var-b: var(c); |
54 var-a: var(b); | 74 var-a: var(b); |
55 var-b: var(c); | |
56 var-c: #00ff00; | |
57 | 75 |
58 var-image: url(test.png); | 76 var-image: url(test.png); |
59 | 77 |
60 var-b-width: 20cm; | 78 var-b-width: 20cm; |
61 var-m-width: 33%; | 79 var-m-width: 33%; |
62 var-b-height: 30EM; | 80 var-b-height: 30EM; |
63 var-width: .6in; | 81 var-width: .6in; |
64 var-length: 1.2in; | 82 var-length: 1.2in; |
65 var-web-stuff: -10Px; | 83 var-web-stuff: -10Px; |
66 var-rgba: rgba(10,20,255); | 84 var-rgba: rgba(10,20,255); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 126 } |
109 | 127 |
110 .foobar { | 128 .foobar { |
111 grid-columns: var(grid-columns); | 129 grid-columns: var(grid-columns); |
112 } | 130 } |
113 '''; | 131 '''; |
114 | 132 |
115 final generated = ''':root { | 133 final generated = ''':root { |
116 var-color-background: #f00; | 134 var-color-background: #f00; |
117 var-color-foreground: #00f; | 135 var-color-foreground: #00f; |
| 136 var-c: #0f0; |
| 137 var-b: var(c); |
118 var-a: var(b); | 138 var-a: var(b); |
119 var-b: var(c); | |
120 var-c: #0f0; | |
121 var-image: url("test.png"); | 139 var-image: url("test.png"); |
122 var-b-width: 20cm; | 140 var-b-width: 20cm; |
123 var-m-width: 33%; | 141 var-m-width: 33%; |
124 var-b-height: 30em; | 142 var-b-height: 30em; |
125 var-width: .6in; | 143 var-width: .6in; |
126 var-length: 1.2in; | 144 var-length: 1.2in; |
127 var-web-stuff: -10px; | 145 var-web-stuff: -10px; |
128 var-rgba: rgba(10, 20, 255); | 146 var-rgba: rgba(10, 20, 255); |
129 var-transition: color 0.4s; | 147 var-transition: color 0.4s; |
130 var-transform: rotate(20deg); | 148 var-transform: rotate(20deg); |
(...skipping 29 matching lines...) Expand all Loading... |
160 } | 178 } |
161 @font-face { | 179 @font-face { |
162 font-family: var(font-family); | 180 font-family: var(font-family); |
163 src: var(src-1); | 181 src: var(src-1); |
164 unicode-range: var(unicode-range-1); | 182 unicode-range: var(unicode-range-1); |
165 } | 183 } |
166 .foobar { | 184 .foobar { |
167 grid-columns: var(grid-columns); | 185 grid-columns: var(grid-columns); |
168 }'''; | 186 }'''; |
169 | 187 |
170 var stylesheet = compileCss(input, errors: errors, | 188 compileAndValidate(input, generated); |
171 opts: ['--no-colors', 'memory']); | |
172 | 189 |
173 expect(stylesheet != null, true); | 190 var generatedPolyfill = r''':root { |
174 expect(errors.isEmpty, true, reason: errors.toString()); | 191 } |
175 expect(prettyPrint(stylesheet), generated); | 192 .testIt { |
| 193 color: #00f; |
| 194 background: #0f0; |
| 195 background-image: url("test.png"); |
| 196 border-width: 20cm; |
| 197 margin-width: 33%; |
| 198 border-height: 30em; |
| 199 width: .6in; |
| 200 length: 1.2in; |
| 201 -web-stuff: -10px; |
| 202 background-color: rgba(10, 20, 255); |
| 203 transition: color 0.4s; |
| 204 transform: rotate(20deg); |
| 205 content: "✔"; |
| 206 text-shadow: 0 -1px 0 #bfbfbf; |
| 207 } |
| 208 @font-face { |
| 209 font-family: Gentium; |
| 210 src: url("http://example.com/fonts/Gentium.ttf"); |
| 211 unicode-range: U+000-49F, U+2000-27FF, U+2900-2BFF, U+1D400-1D7FF; |
| 212 } |
| 213 @font-face { |
| 214 font-family: Gentium; |
| 215 src: local(Gentium Bold), local(Gentium-Bold), url("GentiumBold.ttf"); |
| 216 unicode-range: U+0A-FF, U+980-9FF, U+????, U+3???; |
| 217 } |
| 218 .foobar { |
| 219 grid-columns: 10px ("content" 1fr 10px) [4]; |
| 220 }'''; |
| 221 |
| 222 compilePolyfillAndValidate(input, generatedPolyfill); |
176 } | 223 } |
177 | 224 |
178 void defaultVar() { | 225 void defaultVar() { |
179 final errors = []; | 226 final errors = []; |
180 final input = ''' | 227 final input = ''' |
181 :root { | 228 :root { |
182 var-color-background: red; | 229 var-color-background: red; |
183 var-color-foreground: blue; | 230 var-color-foreground: blue; |
184 | 231 |
185 var-a: var(b); | 232 var-a: var(b, #0a0); |
186 var-b: var(c); | 233 var-b: var(c, #0b0); |
187 var-c: #00ff00; | 234 var-c: #00ff00; |
188 | 235 |
189 var-image: url(test.png); | 236 var-image: url(test.png); |
190 | 237 |
191 var-b-width: 20cm; | 238 var-b-width: 20cm; |
192 var-m-width: 33%; | 239 var-m-width: 33%; |
193 var-b-height: 30EM; | 240 var-b-height: 30EM; |
194 } | 241 } |
195 | 242 |
196 .test { | 243 .test { |
197 background-color: var(test, orange); | 244 background-color: var(test, orange); |
198 } | 245 } |
199 | 246 |
200 body { | 247 body { |
201 background: var(a) var(image) no-repeat right top; | 248 background: var(a) var(image) no-repeat right top; |
202 } | 249 } |
203 | 250 |
204 div { | 251 div { |
205 background: var(color-background) url('img_tree.png') no-repeat right top; | 252 background: var(color-background) url('img_tree.png') no-repeat right top; |
206 } | 253 } |
207 | 254 |
208 .test-2 { | 255 .test-2 { |
209 background: var(color-background) var(image-2, url('img_1.png')) | 256 background: var(color-background) var(image-2, url('img_1.png')) |
210 no-repeat right top; | 257 no-repeat right top; |
211 } | 258 } |
212 | 259 |
213 .test-3 { | 260 .test-3 { |
214 background: var(color-background) var(image-2) no-repeat right top; | 261 background: var(color-background) var(image) no-repeat right top; |
215 } | 262 } |
216 | 263 |
217 .test-4 { | 264 .test-4 { |
218 background: #ffff00 var(image) no-repeat right top; | 265 background: #ffff00 var(image) no-repeat right top; |
219 } | 266 } |
220 | 267 |
221 .test-5 { | 268 .test-5 { |
222 background: var(test-color, var(a)) var(image) no-repeat right top; | 269 background: var(test-color, var(a)) var(image) no-repeat right top; |
223 } | 270 } |
224 | 271 |
225 .test-6 { | 272 .test-6 { |
226 border: red var(a-1, solid 20px); | 273 border: red var(a-1, solid 20px); |
227 } | 274 } |
228 '''; | 275 '''; |
229 | 276 |
230 final generated = ''':root { | 277 final generated = ''':root { |
231 var-color-background: #f00; | 278 var-color-background: #f00; |
232 var-color-foreground: #00f; | 279 var-color-foreground: #00f; |
233 var-a: var(b); | 280 var-a: var(b, #0a0); |
234 var-b: var(c); | 281 var-b: var(c, #0b0); |
235 var-c: #0f0; | 282 var-c: #0f0; |
236 var-image: url("test.png"); | 283 var-image: url("test.png"); |
237 var-b-width: 20cm; | 284 var-b-width: 20cm; |
238 var-m-width: 33%; | 285 var-m-width: 33%; |
239 var-b-height: 30em; | 286 var-b-height: 30em; |
240 } | 287 } |
241 .test { | 288 .test { |
242 background-color: var(test, #ffa500); | 289 background-color: var(test, #ffa500); |
243 } | 290 } |
244 body { | 291 body { |
245 background: var(a) var(image) no-repeat right top; | 292 background: var(a) var(image) no-repeat right top; |
246 } | 293 } |
247 div { | 294 div { |
248 background: var(color-background) url("img_tree.png") no-repeat right top; | 295 background: var(color-background) url("img_tree.png") no-repeat right top; |
249 } | 296 } |
250 .test-2 { | 297 .test-2 { |
251 background: var(color-background) var(image-2, url("img_1.png")) no-repeat rig
ht top; | 298 background: var(color-background) var(image-2, url("img_1.png")) no-repeat rig
ht top; |
252 } | 299 } |
253 .test-3 { | 300 .test-3 { |
254 background: var(color-background) var(image-2) no-repeat right top; | 301 background: var(color-background) var(image) no-repeat right top; |
255 } | 302 } |
256 .test-4 { | 303 .test-4 { |
257 background: #ff0 var(image) no-repeat right top; | 304 background: #ff0 var(image) no-repeat right top; |
258 } | 305 } |
259 .test-5 { | 306 .test-5 { |
260 background: var(test-color, var(a)) var(image) no-repeat right top; | 307 background: var(test-color, var(a)) var(image) no-repeat right top; |
261 } | 308 } |
262 .test-6 { | 309 .test-6 { |
263 border: #f00 var(a-1, solid 20px); | 310 border: #f00 var(a-1, solid 20px); |
264 }'''; | 311 }'''; |
265 | 312 |
266 var stylesheet = compileCss(input, errors: errors, | 313 compileAndValidate(input, generated); |
267 opts: ['--no-colors', 'memory']); | |
268 | 314 |
269 expect(stylesheet != null, true); | 315 var generatedPolyfill = r''':root { |
270 expect(errors.isEmpty, true, reason: errors.toString()); | 316 } |
271 expect(prettyPrint(stylesheet), generated); | 317 .test { |
| 318 background-color: #ffa500; |
| 319 } |
| 320 body { |
| 321 background: #0a0 url("test.png") no-repeat right top; |
| 322 } |
| 323 div { |
| 324 background: #f00 url("img_tree.png") no-repeat right top; |
| 325 } |
| 326 .test-2 { |
| 327 background: #f00 url("img_1.png") no-repeat right top; |
| 328 } |
| 329 .test-3 { |
| 330 background: #f00 url("test.png") no-repeat right top; |
| 331 } |
| 332 .test-4 { |
| 333 background: #ff0 url("test.png") no-repeat right top; |
| 334 } |
| 335 .test-5 { |
| 336 background: #0a0 url("test.png") no-repeat right top; |
| 337 } |
| 338 .test-6 { |
| 339 border: #f00 solid 20px; |
| 340 }'''; |
| 341 |
| 342 compilePolyfillAndValidate(input, generatedPolyfill); |
272 } | 343 } |
273 | 344 |
274 void cyclesVar() { | 345 void undefinedVars() { |
275 final errors = []; | 346 final errors = []; |
276 final input = ''':root { | 347 final input = ''':root { |
277 var-color-background: red; | 348 var-color-background: red; |
278 var-color-foreground: blue; | 349 var-color-foreground: blue; |
279 | 350 |
280 var-a: var(b); | 351 var-a: var(b); |
281 var-b: var(c); | 352 var-b: var(c); |
282 var-c: #00ff00; | 353 var-c: #00ff00; |
283 | 354 |
284 var-one: var(two); | 355 var-one: var(two); |
285 var-two: var(one); | 356 var-two: var(one); |
286 | 357 |
287 var-four: var(five); | 358 var-four: var(five); |
288 var-five: var(six); | 359 var-five: var(six); |
289 var-six: var(four); | 360 var-six: var(four); |
290 | 361 |
291 var-def-1: var(def-2); | 362 var-def-1: var(def-2); |
292 var-def-2: var(def-3); | 363 var-def-2: var(def-3); |
293 var-def-3: var(def-2); | 364 var-def-3: var(def-2); |
294 } | 365 } |
| 366 |
| 367 .testIt { |
| 368 color: var(color-foreground); |
| 369 background: var(color-background); |
| 370 } |
| 371 .test-1 { |
| 372 color: var(c); |
| 373 } |
| 374 .test-2 { |
| 375 color: var(one); |
| 376 background: var(six); |
| 377 } |
| 378 '''; |
| 379 |
| 380 final generatedPolyfill = ''':root { |
| 381 } |
| 382 .testIt { |
| 383 color: #00f; |
| 384 background: #f00; |
| 385 } |
| 386 .test-1 { |
| 387 color: #0f0; |
| 388 } |
| 389 .test-2 { |
| 390 color: ; |
| 391 background: ; |
| 392 }'''; |
| 393 |
| 394 var errorStrings = [ |
| 395 'error :5:14: Variable is not defined.\n' |
| 396 ' var-a: var(b);\n' |
| 397 ' ^^', |
| 398 'error :6:14: Variable is not defined.\n' |
| 399 ' var-b: var(c);\n' |
| 400 ' ^^', |
| 401 'error :9:16: Variable is not defined.\n' |
| 402 ' var-one: var(two);\n' |
| 403 ' ^^^^', |
| 404 'error :12:17: Variable is not defined.\n' |
| 405 ' var-four: var(five);\n' |
| 406 ' ^^^^^', |
| 407 'error :13:17: Variable is not defined.\n' |
| 408 ' var-five: var(six);\n' |
| 409 ' ^^^^', |
| 410 'error :16:18: Variable is not defined.\n' |
| 411 ' var-def-1: var(def-2);\n' |
| 412 ' ^^^^^^', |
| 413 'error :17:18: Variable is not defined.\n' |
| 414 ' var-def-2: var(def-3);\n' |
| 415 ' ^^^^^^', |
| 416 ]; |
| 417 |
| 418 var generated = r''':root { |
| 419 var-color-background: #f00; |
| 420 var-color-foreground: #00f; |
| 421 var-a: var(b); |
| 422 var-b: var(c); |
| 423 var-c: #0f0; |
| 424 var-one: var(two); |
| 425 var-two: var(one); |
| 426 var-four: var(five); |
| 427 var-five: var(six); |
| 428 var-six: var(four); |
| 429 var-def-1: var(def-2); |
| 430 var-def-2: var(def-3); |
| 431 var-def-3: var(def-2); |
| 432 } |
295 .testIt { | 433 .testIt { |
296 color: var(color-foreground); | 434 color: var(color-foreground); |
297 background: var(color-background); | 435 background: var(color-background); |
298 } | 436 } |
299 .test-2 { | 437 .test-1 { |
300 color: var(one); | 438 color: var(c); |
301 } | |
302 '''; | |
303 | |
304 final generated = ''':root { | |
305 var-color-background: #f00; | |
306 var-color-foreground: #00f; | |
307 var-a: var(b); | |
308 var-b: var(c); | |
309 var-c: #0f0; | |
310 } | |
311 .testIt { | |
312 color: var(color-foreground); | |
313 background: var(color-background); | |
314 } | 439 } |
315 .test-2 { | 440 .test-2 { |
316 color: var(one); | 441 color: var(one); |
| 442 background: var(six); |
317 }'''; | 443 }'''; |
| 444 int testBitMap = 0; |
318 | 445 |
319 var stylesheet = compileCss(input, errors: errors, | 446 compileAndValidate(input, generated); |
320 opts: ['--no-colors', '--warnings_as_errors', 'memory']); | 447 |
| 448 var stylesheet = polyFillCompileCss(input, errors: errors..clear(), |
| 449 opts: options); |
321 | 450 |
322 expect(stylesheet != null, true); | 451 expect(stylesheet != null, true); |
323 expect(errors.length, 8, reason: errors.toString()); | 452 |
324 int testBitMap = 0; | 453 expect(errors.length, errorStrings.length, reason: errors.toString()); |
325 var errorStrings = [ | 454 testBitMap = 0; |
326 'error :14:3: var cycle detected var-six\n' | 455 |
327 ' var-six: var(four);\n' | |
328 ' ^^^^^^^^^^^^^^^^^^', | |
329 'error :18:3: var cycle detected var-def-3\n' | |
330 ' var-def-3: var(def-2);\n' | |
331 ' ^^^^^^^^^^^^^^^^^^^^^', | |
332 'error :10:3: var cycle detected var-two\n' | |
333 ' var-two: var(one);\n' | |
334 ' ^^^^^^^^^^^^^^^^^', | |
335 'error :17:3: var cycle detected var-def-2\n' | |
336 ' var-def-2: var(def-3);\n' | |
337 ' ^^^^^^^^^^^^^^^^^^^^^', | |
338 'error :16:3: var cycle detected var-def-1\n' | |
339 ' var-def-1: var(def-2);\n' | |
340 ' ^^^^^^^^^^^^^^^^^^^^^', | |
341 'error :13:3: var cycle detected var-five\n' | |
342 ' var-five: var(six);\n' | |
343 ' ^^^^^^^^^^^^^^^^^^', | |
344 'error :9:3: var cycle detected var-one\n' | |
345 ' var-one: var(two);\n' | |
346 ' ^^^^^^^^^^^^^^^^^', | |
347 'error :12:3: var cycle detected var-four\n' | |
348 ' var-four: var(five);\n' | |
349 ' ^^^^^^^^^^^^^^^^^^^' | |
350 ]; | |
351 outer: for (var error in errors) { | 456 outer: for (var error in errors) { |
352 var errorString = error.toString(); | 457 var errorString = error.toString(); |
353 for (int i = 0; i < 8; i++) { | 458 for (int i = 0; i < errorStrings.length; i++) { |
354 if (errorString == errorStrings[i]) { | 459 if (errorString == errorStrings[i]) { |
355 testBitMap |= 1 << i; | 460 testBitMap |= 1 << i; |
356 continue outer; | 461 continue outer; |
357 } | 462 } |
358 } | 463 } |
359 fail("Unexpected error string: $errorString"); | 464 fail("Unexpected error string: $errorString"); |
360 } | 465 } |
361 expect(testBitMap, equals((1 << 8) - 1)); | 466 expect(testBitMap, equals((1 << errorStrings.length) - 1)); |
362 expect(prettyPrint(stylesheet), generated); | 467 expect(prettyPrint(stylesheet), generatedPolyfill); |
363 } | 468 } |
364 | 469 |
365 parserVar() { | 470 parserVar() { |
366 final errors = []; | |
367 final input = ''':root { | 471 final input = ''':root { |
368 var-color-background: red; | 472 var-color-background: red; |
369 var-color-foreground: blue; | 473 var-color-foreground: blue; |
370 | 474 |
| 475 var-c: #00ff00; |
| 476 var-b: var(c); |
371 var-a: var(b); | 477 var-a: var(b); |
372 var-b: var(c); | |
373 var-c: #00ff00; | |
374 | 478 |
375 var-image: url(test.png); | 479 var-image: url(test.png); |
376 | 480 |
377 var-b-width: 20cm; | 481 var-b-width: 20cm; |
378 var-m-width: 33%; | 482 var-m-width: 33%; |
379 var-b-height: 30EM; | 483 var-b-height: 30EM; |
380 var-width: .6in; | 484 var-width: .6in; |
381 var-length: 1.2in; | 485 var-length: 1.2in; |
382 var-web-stuff: -10Px; | 486 var-web-stuff: -10Px; |
383 var-rgba: rgba(10,20,255); | 487 var-rgba: rgba(10,20,255); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 } | 529 } |
426 | 530 |
427 .foobar { | 531 .foobar { |
428 grid-columns: var(grid-columns); | 532 grid-columns: var(grid-columns); |
429 } | 533 } |
430 '''; | 534 '''; |
431 | 535 |
432 final generated = ''':root { | 536 final generated = ''':root { |
433 var-color-background: #f00; | 537 var-color-background: #f00; |
434 var-color-foreground: #00f; | 538 var-color-foreground: #00f; |
| 539 var-c: #0f0; |
| 540 var-b: var(c); |
435 var-a: var(b); | 541 var-a: var(b); |
436 var-b: var(c); | |
437 var-c: #0f0; | |
438 var-image: url("test.png"); | 542 var-image: url("test.png"); |
439 var-b-width: 20cm; | 543 var-b-width: 20cm; |
440 var-m-width: 33%; | 544 var-m-width: 33%; |
441 var-b-height: 30em; | 545 var-b-height: 30em; |
442 var-width: .6in; | 546 var-width: .6in; |
443 var-length: 1.2in; | 547 var-length: 1.2in; |
444 var-web-stuff: -10px; | 548 var-web-stuff: -10px; |
445 var-rgba: rgba(10, 20, 255); | 549 var-rgba: rgba(10, 20, 255); |
446 var-transition: color 0.4s; | 550 var-transition: color 0.4s; |
447 var-transform: rotate(20deg); | 551 var-transform: rotate(20deg); |
(...skipping 29 matching lines...) Expand all Loading... |
477 } | 581 } |
478 @font-face { | 582 @font-face { |
479 font-family: var(font-family); | 583 font-family: var(font-family); |
480 src: var(src-1); | 584 src: var(src-1); |
481 unicode-range: var(unicode-range-1); | 585 unicode-range: var(unicode-range-1); |
482 } | 586 } |
483 .foobar { | 587 .foobar { |
484 grid-columns: var(grid-columns); | 588 grid-columns: var(grid-columns); |
485 }'''; | 589 }'''; |
486 | 590 |
487 var stylesheet = parseCss(input, errors: errors, | 591 compileAndValidate(input, generated); |
488 opts: ['--no-colors', 'memory']); | |
489 | 592 |
490 expect(stylesheet != null, true); | 593 var generatedPolyfill = r''':root { |
491 expect(errors.isEmpty, true, reason: errors.toString()); | 594 } |
492 expect(prettyPrint(stylesheet), generated); | 595 .testIt { |
| 596 color: #00f; |
| 597 background: #0f0; |
| 598 background-image: url("test.png"); |
| 599 border-width: 20cm; |
| 600 margin-width: 33%; |
| 601 border-height: 30em; |
| 602 width: .6in; |
| 603 length: 1.2in; |
| 604 -web-stuff: -10px; |
| 605 background-color: rgba(10, 20, 255); |
| 606 transition: color 0.4s; |
| 607 transform: rotate(20deg); |
| 608 content: "✔"; |
| 609 text-shadow: 0 -1px 0 #bfbfbf; |
| 610 } |
| 611 @font-face { |
| 612 font-family: Gentium; |
| 613 src: url("http://example.com/fonts/Gentium.ttf"); |
| 614 unicode-range: U+000-49F, U+2000-27FF, U+2900-2BFF, U+1D400-1D7FF; |
| 615 } |
| 616 @font-face { |
| 617 font-family: Gentium; |
| 618 src: local(Gentium Bold), local(Gentium-Bold), url("GentiumBold.ttf"); |
| 619 unicode-range: U+0A-FF, U+980-9FF, U+????, U+3???; |
| 620 } |
| 621 .foobar { |
| 622 grid-columns: 10px ("content" 1fr 10px) [4]; |
| 623 }'''; |
| 624 compilePolyfillAndValidate(input, generatedPolyfill); |
493 } | 625 } |
494 | 626 |
495 testVar() { | 627 testVar() { |
496 final errors = []; | 628 final errors = []; |
497 final input = ''' | 629 final input = ''' |
498 @color-background: red; | 630 @color-background: red; |
499 @color-foreground: blue; | 631 @color-foreground: blue; |
500 | 632 |
501 .test { | 633 .test { |
502 background-color: var(color-background); | 634 background-color: var(color-background); |
503 color: var(color-foreground); | 635 color: var(color-foreground); |
504 } | 636 } |
505 '''; | 637 '''; |
506 final generated = ''' | 638 final generated = ''' |
507 var-color-background: #f00; | 639 var-color-background: #f00; |
508 var-color-foreground: #00f; | 640 var-color-foreground: #00f; |
509 | 641 |
510 .test { | 642 .test { |
511 background-color: var(color-background); | 643 background-color: var(color-background); |
512 color: var(color-foreground); | 644 color: var(color-foreground); |
513 }'''; | 645 }'''; |
514 | 646 |
515 var stylesheet = parseCss(input, errors: errors, | 647 var stylesheet = parseCss(input, errors: errors, |
516 opts: ['--no-colors', 'memory']); | 648 opts: ['--no-colors', 'memory']); |
517 | 649 |
518 expect(stylesheet != null, true); | 650 expect(stylesheet != null, true); |
519 expect(errors.isEmpty, true, reason: errors.toString()); | 651 expect(errors.isEmpty, true, reason: errors.toString()); |
520 expect(prettyPrint(stylesheet), generated); | 652 expect(prettyPrint(stylesheet), generated); |
521 | 653 |
522 stylesheet = compileCss(input, errors: errors..clear(), | 654 compileAndValidate(input, generated); |
523 opts: ['--no-colors', 'memory']); | |
524 | |
525 expect(stylesheet != null, true); | |
526 expect(errors.isEmpty, true, reason: errors.toString()); | |
527 expect(prettyPrint(stylesheet), generated); | |
528 | 655 |
529 final input2 = ''' | 656 final input2 = ''' |
530 @color-background: red; | 657 @color-background: red; |
531 @color-foreground: blue; | 658 @color-foreground: blue; |
532 | 659 |
533 .test { | 660 .test { |
534 background-color: @color-background; | 661 background-color: @color-background; |
535 color: @color-foreground; | 662 color: @color-foreground; |
536 } | 663 } |
537 '''; | 664 '''; |
538 final generated2 = '''var-color-background: #f00; | 665 final generated2 = '''var-color-background: #f00; |
539 var-color-foreground: #00f; | 666 var-color-foreground: #00f; |
540 | 667 |
541 .test { | 668 .test { |
542 background-color: var(color-background); | 669 background-color: var(color-background); |
543 color: var(color-foreground); | 670 color: var(color-foreground); |
544 }'''; | 671 }'''; |
545 | 672 |
546 stylesheet = parseCss(input, errors: errors..clear(), | 673 stylesheet = parseCss(input, errors: errors..clear(), |
547 opts: ['--no-colors', 'memory']); | 674 opts: ['--no-colors', 'memory']); |
548 | 675 |
549 expect(stylesheet != null, true); | 676 expect(stylesheet != null, true); |
550 expect(errors.isEmpty, true, reason: errors.toString()); | 677 expect(errors.isEmpty, true, reason: errors.toString()); |
551 expect(prettyPrint(stylesheet), generated2); | 678 expect(prettyPrint(stylesheet), generated2); |
552 | 679 |
553 stylesheet = compileCss(input2, errors: errors..clear(), | 680 compileAndValidate(input2, generated2); |
554 opts: ['--no-colors', 'memory', '--no-less']); | |
555 | |
556 expect(stylesheet != null, true); | |
557 expect(errors.isEmpty, true, reason: errors.toString()); | |
558 expect(prettyPrint(stylesheet), generated2); | |
559 } | 681 } |
560 | 682 |
561 testLess() { | 683 testLess() { |
562 final errors = []; | 684 final errors = []; |
563 final input = ''' | 685 final input = ''' |
564 @color-background: red; | 686 @color-background: red; |
565 @color-foreground: blue; | 687 @color-foreground: blue; |
566 | 688 |
567 .test { | 689 .test { |
568 background-color: var(color-background); | 690 background-color: var(color-background); |
569 color: var(color-foreground); | 691 color: var(color-foreground); |
570 } | 692 } |
571 '''; | 693 '''; |
572 final generated = '''var-color-background: #f00; | 694 final generated = '''var-color-background: #f00; |
573 var-color-foreground: #00f; | 695 var-color-foreground: #00f; |
574 | 696 |
575 .test { | 697 .test { |
576 background-color: var(color-background); | 698 background-color: var(color-background); |
577 color: var(color-foreground); | 699 color: var(color-foreground); |
578 }'''; | 700 }'''; |
579 | 701 |
580 var stylesheet = parseCss(input, errors: errors, | 702 var stylesheet = parseCss(input, errors: errors, |
581 opts: ['--no-colors', 'memory']); | 703 opts: ['--no-colors', 'memory']); |
582 | 704 |
583 expect(stylesheet != null, true); | 705 expect(stylesheet != null, true); |
584 expect(errors.isEmpty, true, reason: errors.toString()); | 706 expect(errors.isEmpty, true, reason: errors.toString()); |
585 expect(prettyPrint(stylesheet), generated); | 707 expect(prettyPrint(stylesheet), generated); |
586 | 708 |
587 stylesheet = compileCss(input, errors: errors..clear(), | 709 compileAndValidate(input, generated); |
588 opts: ['--no-colors', 'memory']); | |
589 | |
590 expect(stylesheet != null, true); | |
591 expect(errors.isEmpty, true, reason: errors.toString()); | |
592 expect(prettyPrint(stylesheet), generated); | |
593 | 710 |
594 final input2 = ''' | 711 final input2 = ''' |
595 @color-background: red; | 712 @color-background: red; |
596 @color-foreground: blue; | 713 @color-foreground: blue; |
597 | 714 |
598 .test { | 715 .test { |
599 background-color: @color-background; | 716 background-color: @color-background; |
600 color: @color-foreground; | 717 color: @color-foreground; |
601 } | 718 } |
602 '''; | 719 '''; |
603 final generated2 = '''var-color-background: #f00; | 720 final generated2 = '''var-color-background: #f00; |
604 var-color-foreground: #00f; | 721 var-color-foreground: #00f; |
605 | 722 |
606 .test { | 723 .test { |
607 background-color: var(color-background); | 724 background-color: var(color-background); |
608 color: var(color-foreground); | 725 color: var(color-foreground); |
609 }'''; | 726 }'''; |
610 | 727 |
611 stylesheet = parseCss(input, errors: errors..clear(), | 728 stylesheet = parseCss(input, errors: errors..clear(), |
612 opts: ['--no-colors', 'memory']); | 729 opts: ['--no-colors', 'memory']); |
613 | 730 |
614 expect(stylesheet != null, true); | 731 expect(stylesheet != null, true); |
615 expect(errors.isEmpty, true, reason: errors.toString()); | 732 expect(errors.isEmpty, true, reason: errors.toString()); |
616 expect(prettyPrint(stylesheet), generated2); | 733 expect(prettyPrint(stylesheet), generated2); |
617 | 734 |
618 stylesheet = compileCss(input2, errors: errors..clear(), | 735 compileAndValidate(input2, generated2); |
619 opts: ['--no-colors', 'memory', '--no-less']); | |
620 | |
621 expect(stylesheet != null, true); | |
622 expect(errors.isEmpty, true, reason: errors.toString()); | |
623 expect(prettyPrint(stylesheet), generated2); | |
624 } | 736 } |
625 | 737 |
626 void polyfill() { | 738 void polyfill() { |
627 var errors = []; | 739 compilePolyfillAndValidate(r''' |
628 var input = r''' | |
629 @color-background: red; | 740 @color-background: red; |
630 @color-foreground: blue; | 741 @color-foreground: blue; |
631 .test { | 742 .test { |
632 background-color: @color-background; | 743 background-color: @color-background; |
633 color: @color-foreground; | 744 color: @color-foreground; |
| 745 }''', r'''.test { |
| 746 background-color: #f00; |
| 747 color: #00f; |
| 748 }'''); |
| 749 } |
| 750 |
| 751 void testIndirects() { |
| 752 compilePolyfillAndValidate(''' |
| 753 :root { |
| 754 var-redef: #0f0; |
| 755 |
| 756 var-a1: #fff; |
| 757 var-a2: var(a1); |
| 758 var-a3: var(a2); |
| 759 |
| 760 var-redef: #000; |
| 761 } |
| 762 .test { |
| 763 background-color: @a1; |
| 764 color: @a2; |
| 765 border-color: @a3; |
| 766 } |
| 767 .test-1 { |
| 768 color: @redef; |
| 769 }''', r''':root { |
| 770 } |
| 771 .test { |
| 772 background-color: #fff; |
| 773 color: #fff; |
| 774 border-color: #fff; |
| 775 } |
| 776 .test-1 { |
| 777 color: #000; |
| 778 }'''); |
| 779 } |
| 780 |
| 781 void includes() { |
| 782 var errors = []; |
| 783 var file1Input = r''' |
| 784 :root { |
| 785 var-redef: #0f0; |
| 786 |
| 787 var-a1: #fff; |
| 788 var-a2: var(a1); |
| 789 var-a3: var(a2); |
| 790 |
| 791 var-redef: #000; |
| 792 } |
| 793 .test-1 { |
| 794 background-color: @a1; |
| 795 color: @a2; |
| 796 border-color: @a3; |
| 797 } |
| 798 .test-1a { |
| 799 color: @redef; |
| 800 } |
| 801 '''; |
| 802 |
| 803 var file2Input = r''' |
| 804 :root { |
| 805 var-redef: #0b0; |
| 806 var-b3: var(a3); |
| 807 } |
| 808 .test-2 { |
| 809 color: var(b3); |
| 810 background-color: var(redef); |
| 811 border-color: var(a3); |
| 812 } |
| 813 '''; |
| 814 |
| 815 var input = r''' |
| 816 :root { |
| 817 var-redef: #0c0; |
| 818 } |
| 819 .test-main { |
| 820 color: var(b3); |
| 821 background-color: var(redef); |
| 822 border-color: var(a3); |
| 823 } |
| 824 '''; |
| 825 |
| 826 var generated1 = r''':root { |
| 827 var-redef: #0f0; |
| 828 var-a1: #fff; |
| 829 var-a2: var(a1); |
| 830 var-a3: var(a2); |
| 831 var-redef: #000; |
| 832 } |
| 833 .test-1 { |
| 834 background-color: var(a1); |
| 835 color: var(a2); |
| 836 border-color: var(a3); |
| 837 } |
| 838 .test-1a { |
| 839 color: var(redef); |
634 }'''; | 840 }'''; |
635 | 841 |
636 var generated = r'''.test { | 842 var stylesheet1 = compileCss(file1Input, errors: errors, opts: options); |
637 background-color: #f00; | 843 expect(stylesheet1 != null, true); |
638 color: #00f; | 844 expect(errors.isEmpty, true, reason: errors.toString()); |
| 845 expect(prettyPrint(stylesheet1), generated1); |
| 846 |
| 847 var generated2 = r''':root { |
| 848 var-redef: #0b0; |
| 849 var-b3: var(a3); |
| 850 } |
| 851 .test-2 { |
| 852 color: var(b3); |
| 853 background-color: var(redef); |
| 854 border-color: var(a3); |
639 }'''; | 855 }'''; |
640 | 856 |
641 var stylesheet = compileCss(input, errors: errors, | 857 var stylesheet2 = compileCss(file2Input, includes: [stylesheet1], |
642 opts: ['--no-colors', 'memory'], polyfill: true); | 858 errors: errors..clear(), opts: options); |
| 859 expect(stylesheet2 != null, true); |
| 860 expect(errors.isEmpty, true, reason: errors.toString()); |
| 861 expect(prettyPrint(stylesheet2), generated2); |
643 | 862 |
644 expect(stylesheet != null, true); | 863 var generatedPolyfill1 = r''':root { |
| 864 } |
| 865 .test-1 { |
| 866 background-color: #fff; |
| 867 color: #fff; |
| 868 border-color: #fff; |
| 869 } |
| 870 .test-1a { |
| 871 color: #000; |
| 872 }'''; |
| 873 var styleSheet1Polyfill = compileCss(file1Input, errors: errors..clear(), |
| 874 polyfill: true, opts: options); |
| 875 expect(styleSheet1Polyfill != null, true); |
645 expect(errors.isEmpty, true, reason: errors.toString()); | 876 expect(errors.isEmpty, true, reason: errors.toString()); |
646 expect(prettyPrint(stylesheet), generated); | 877 expect(prettyPrint(styleSheet1Polyfill), generatedPolyfill1); |
| 878 |
| 879 var generatedPolyfill2 = r''':root { |
647 } | 880 } |
| 881 .test-2 { |
| 882 color: #fff; |
| 883 background-color: #0b0; |
| 884 border-color: #fff; |
| 885 }'''; |
| 886 var styleSheet2Polyfill = compileCss(file2Input, includes: [stylesheet1], |
| 887 errors: errors..clear(), polyfill: true, opts: options); |
| 888 expect(styleSheet2Polyfill != null, true); |
| 889 expect(errors.isEmpty, true, reason: errors.toString()); |
| 890 expect(prettyPrint(styleSheet2Polyfill), generatedPolyfill2); |
648 | 891 |
| 892 // Make sure includes didn't change. |
| 893 expect(prettyPrint(stylesheet1), generated1); |
649 | 894 |
| 895 var generatedPolyfill = r''':root { |
| 896 } |
| 897 .test-main { |
| 898 color: #fff; |
| 899 background-color: #0c0; |
| 900 border-color: #fff; |
| 901 }'''; |
| 902 var stylesheetPolyfill = compileCss(input, |
| 903 includes: [stylesheet1, stylesheet2], errors: errors..clear(), |
| 904 polyfill: true, opts: options); |
| 905 |
| 906 expect(stylesheetPolyfill != null, true); |
| 907 expect(errors.isEmpty, true, reason: errors.toString()); |
| 908 expect(prettyPrint(stylesheetPolyfill), generatedPolyfill); |
| 909 |
| 910 // Make sure includes didn't change. |
| 911 expect(prettyPrint(stylesheet1), generated1); |
| 912 expect(prettyPrint(stylesheet2), generated2); |
| 913 } |
650 | 914 |
651 main() { | 915 main() { |
652 test('Simple var', simpleVar); | 916 test('Simple var', simpleVar); |
653 test('Expressions var', expressionsVar); | 917 test('Expressions var', expressionsVar); |
654 test('Default value in var()', defaultVar); | 918 test('Default value in var()', defaultVar); |
655 test('CSS Parser only var', parserVar); | 919 test('CSS Parser only var', parserVar); |
656 test('Var syntax', testVar); | 920 test('Var syntax', testVar); |
657 test('Cycles var', cyclesVar); | 921 test('Indirects', testIndirects); |
| 922 test('Forward Refs', undefinedVars); |
658 test('Less syntax', testLess); | 923 test('Less syntax', testLess); |
659 test('Polyfill', polyfill); | 924 test('Polyfill', polyfill); |
| 925 test('Multi-file', includes); |
660 } | 926 } |
OLD | NEW |