-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.test.js
1965 lines (1915 loc) · 57 KB
/
index.test.js
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
const postcss = require("postcss");
const plugin = require("../src");
const name = require("../package.json").name;
const tests = [
{
name: "scope selectors",
input: ".foobar { a_value: some-value; }",
expected: ":local(.foobar) { a_value: some-value; }",
},
{
name: "scope escaped selectors",
input: ".\\3A \\) {}",
expected: ":local(.\\3A \\)) {}",
},
{
name: "scope ids",
input: "#foobar { a_value: some-value; }",
expected: ":local(#foobar) { a_value: some-value; }",
},
{
name: "scope escaped ids",
input: "#\\#test {}",
expected: ":local(#\\#test) {}",
},
{
name: "scope escaped ids (2)",
input: "#u-m\\00002b {}",
expected: ":local(#u-m\\00002b) {}",
},
{
name: "scope multiple selectors",
input: ".foo, .baz { a_value: some-value; }",
expected: ":local(.foo), :local(.baz) { a_value: some-value; }",
},
{
name: "scope sibling selectors",
input: ".foo ~ .baz { a_value: some-value; }",
expected: ":local(.foo) ~ :local(.baz) { a_value: some-value; }",
},
{
name: "scope psuedo elements",
input: ".foo:after { a_value: some-value; }",
expected: ":local(.foo):after { a_value: some-value; }",
},
{
name: "scope media queries",
input: "@media only screen { .foo { a_value: some-value; } }",
expected: "@media only screen { :local(.foo) { a_value: some-value; } }",
},
{
name: "allow narrow global selectors",
input: ":global(.foo .bar) { a_value: some-value; }",
expected: ".foo .bar { a_value: some-value; }",
},
{
name: "allow narrow local selectors",
input: ":local(.foo .bar) { a_value: some-value; }",
expected: ":local(.foo) :local(.bar) { a_value: some-value; }",
},
{
name: "allow broad global selectors",
input: ":global .foo .bar { a_value: some-value; }",
expected: ".foo .bar { a_value: some-value; }",
},
{
name: "allow broad local selectors",
input: ":local .foo .bar { a_value: some-value; }",
expected: ":local(.foo) :local(.bar) { a_value: some-value; }",
},
{
name: "allow multiple narrow global selectors",
input: ":global(.foo), :global(.bar) { a_value: some-value; }",
expected: ".foo, .bar { a_value: some-value; }",
},
{
name: "allow multiple broad global selectors",
input: ":global .foo, :global .bar { a_value: some-value; }",
expected: ".foo, .bar { a_value: some-value; }",
},
{
name: "allow multiple broad local selectors",
input: ":local .foo, :local .bar { a_value: some-value; }",
expected: ":local(.foo), :local(.bar) { a_value: some-value; }",
},
{
name: "allow narrow global selectors nested inside local styles",
input: ".foo :global(.foo .bar) { a_value: some-value; }",
expected: ":local(.foo) .foo .bar { a_value: some-value; }",
},
{
name: "allow broad global selectors nested inside local styles",
input: ".foo :global .foo .bar { a_value: some-value; }",
expected: ":local(.foo) .foo .bar { a_value: some-value; }",
},
{
name: "allow parentheses inside narrow global selectors",
input: ".foo :global(.foo:not(.bar)) { a_value: some-value; }",
expected: ":local(.foo) .foo:not(.bar) { a_value: some-value; }",
},
{
name: "allow parentheses inside narrow local selectors",
input: ".foo :local(.foo:not(.bar)) { a_value: some-value; }",
expected:
":local(.foo) :local(.foo):not(:local(.bar)) { a_value: some-value; }",
},
{
name: "allow narrow global selectors appended to local styles",
input: ".foo:global(.foo.bar) { a_value: some-value; }",
expected: ":local(.foo).foo.bar { a_value: some-value; }",
},
{
name: "ignore selectors that are already local",
input: ":local(.foobar) { a_value: some-value; }",
expected: ":local(.foobar) { a_value: some-value; }",
},
{
name: "ignore nested selectors that are already local",
input: ":local(.foo) :local(.bar) { a_value: some-value; }",
expected: ":local(.foo) :local(.bar) { a_value: some-value; }",
},
{
name: "ignore multiple selectors that are already local",
input: ":local(.foo), :local(.bar) { a_value: some-value; }",
expected: ":local(.foo), :local(.bar) { a_value: some-value; }",
},
{
name: "ignore sibling selectors that are already local",
input: ":local(.foo) ~ :local(.bar) { a_value: some-value; }",
expected: ":local(.foo) ~ :local(.bar) { a_value: some-value; }",
},
{
name: "ignore psuedo elements that are already local",
input: ":local(.foo):after { a_value: some-value; }",
expected: ":local(.foo):after { a_value: some-value; }",
},
{
name: "trim whitespace after empty broad selector",
input: ".bar :global :global { a_value: some-value; }",
expected: ":local(.bar) { a_value: some-value; }",
},
{
name: "broad global should be limited to selector",
input:
":global .foo, .bar :global, .foobar :global { a_value: some-value; }",
expected: ".foo, :local(.bar), :local(.foobar) { a_value: some-value; }",
},
{
name: "broad global should be limited to nested selector",
input: ".foo:not(:global .bar).foobar { a_value: some-value; }",
expected: ":local(.foo):not(.bar):local(.foobar) { a_value: some-value; }",
},
{
name: "broad global and local should allow switching",
input:
".foo :global .bar :local .foobar :local .barfoo { a_value: some-value; }",
expected:
":local(.foo) .bar :local(.foobar) :local(.barfoo) { a_value: some-value; }",
},
{
name: "localize a single animation-name",
input: ".foo { animation-name: bar; }",
expected: ":local(.foo) { animation-name: :local(bar); }",
},
{
name: "localize a single animation-name #2",
input: ".foo { animation-name: local(bar); }",
expected: ":local(.foo) { animation-name: :local(bar); }",
},
{
name: "not localize animation-name in a var function",
input: ".foo { animation-name: var(--bar); }",
expected: ":local(.foo) { animation-name: var(--bar); }",
},
{
name: "not localize animation-name in a var function #2",
input: ".foo { animation-name: vAr(--bar); }",
expected: ":local(.foo) { animation-name: vAr(--bar); }",
},
{
name: "not localize animation-name in an env function",
input: ".foo { animation-name: env(bar); }",
expected: ":local(.foo) { animation-name: env(bar); }",
},
{
name: "not localize animation-name in an global function",
input: ".foo { animation-name: global(bar); }",
expected: ":local(.foo) { animation-name: bar; }",
},
{
name: "localize and not localize animation-name in mixed case",
input:
".foo { animation-name: fadeInOut, global(moveLeft300px), local(bounce); }",
expected:
":local(.foo) { animation-name: :local(fadeInOut), moveLeft300px, :local(bounce); }",
},
{
name: "localize and not localize animation-name in mixed case #2",
options: { mode: "global" },
input:
".foo { animation-name: fadeInOut, global(moveLeft300px), local(bounce); }",
expected:
".foo { animation-name: fadeInOut, moveLeft300px, :local(bounce); }",
},
{
name: "localize and not localize animation-name in mixed case #3",
options: { mode: "pure" },
input:
".foo { animation-name: fadeInOut, global(moveLeft300px), local(bounce); }",
expected:
":local(.foo) { animation-name: :local(fadeInOut), moveLeft300px, :local(bounce); }",
},
{
name: "not localize animation in an global function",
input: ".foo { animation: global(bar); }",
expected: ":local(.foo) { animation: bar; }",
},
{
name: "not localize a certain animation in an global function",
input: ".foo { animation: global(bar), foo; }",
expected: ":local(.foo) { animation: bar, :local(foo); }",
},
{
name: "localize and not localize a certain animation in mixed case",
input: ".foo { animation: rotate 1s, global(spin) 3s, local(fly) 6s; }",
expected:
":local(.foo) { animation: :local(rotate) 1s, spin 3s, :local(fly) 6s; }",
},
{
name: "localize and not localize a certain animation in mixed case #2",
options: { mode: "global" },
input: ".foo { animation: rotate 1s, global(spin) 3s, local(fly) 6s; }",
expected: ".foo { animation: rotate 1s, spin 3s, :local(fly) 6s; }",
},
{
name: "localize and not localize a certain animation in mixed case #2",
options: { mode: "pure" },
input: ".foo { animation: rotate 1s, global(spin) 3s, local(fly) 6s; }",
expected:
":local(.foo) { animation: :local(rotate) 1s, spin 3s, :local(fly) 6s; }",
},
{
name: "not localize animation-name in an env function #2",
input: ".foo { animation-name: eNv(bar); }",
expected: ":local(.foo) { animation-name: eNv(bar); }",
},
{
name: "not localize a single animation-delay",
input: ".foo { animation-delay: 1s; }",
expected: ":local(.foo) { animation-delay: 1s; }",
},
{
name: "localize multiple animation-names",
input: ".foo { animation-name: bar, foobar; }",
expected: ":local(.foo) { animation-name: :local(bar), :local(foobar); }",
},
{
name: "not localize revert",
input: ".foo { animation: revert; }",
expected: ":local(.foo) { animation: revert; }",
},
{
name: "not localize revert #2",
input: ".foo { animation-name: revert; }",
expected: ":local(.foo) { animation-name: revert; }",
},
{
name: "not localize revert #3",
input: ".foo { animation-name: revert, foo, none; }",
expected: ":local(.foo) { animation-name: revert, :local(foo), none; }",
},
{
name: "not localize revert-layer",
input: ".foo { animation: revert-layer; }",
expected: ":local(.foo) { animation: revert-layer; }",
},
{
name: "not localize revert",
input: ".foo { animation-name: revert-layer; }",
expected: ":local(.foo) { animation-name: revert-layer; }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: \\@bounce; }",
expected: ":local(.foo) { animation: :local(\\@bounce); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: bou\\@nce; }",
expected: ":local(.foo) { animation: :local(bou\\@nce); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: \\ as; }",
expected: ":local(.foo) { animation: :local(\\ as); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: t\\ t; }",
expected: ":local(.foo) { animation: :local(t\\ t); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: -\\a; }",
expected: ":local(.foo) { animation: :local(-\\a); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: --\\a; }",
expected: ":local(.foo) { animation: :local(--\\a); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: \\a; }",
expected: ":local(.foo) { animation: :local(\\a); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: -\\a; }",
expected: ":local(.foo) { animation: :local(-\\a); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: --; }",
expected: ":local(.foo) { animation: :local(--); }",
},
{
name: "localize animation using special characters",
input: ".foo { animation: 😃bounce😃; }",
expected: ":local(.foo) { animation: :local(😃bounce😃); }",
},
{
name: "not localize revert",
input: ".foo { animation: --foo; }",
expected: ":local(.foo) { animation: :local(--foo); }",
},
{
name: "not localize name in nested function",
input: ".foo { animation: fade .2s var(--easeOutQuart) .1s forwards }",
expected:
":local(.foo) { animation: :local(fade) .2s var(--easeOutQuart) .1s forwards }",
},
{
name: "not localize name in nested function #2",
input: ".foo { animation: fade .2s env(FOO_BAR) .1s forwards, name }",
expected:
":local(.foo) { animation: :local(fade) .2s env(FOO_BAR) .1s forwards, :local(name) }",
},
{
name: "not localize name in nested function #3",
input: ".foo { animation: var(--foo-bar) .1s forwards, name }",
expected:
":local(.foo) { animation: var(--foo-bar) .1s forwards, :local(name) }",
},
{
name: "not localize name in nested function #3",
input: ".foo { animation: var(--foo-bar) .1s forwards name, name }",
expected:
":local(.foo) { animation: var(--foo-bar) .1s forwards :local(name), :local(name) }",
},
{
name: "localize animation",
input: ".foo { animation: a; }",
expected: ":local(.foo) { animation: :local(a); }",
},
{
name: "localize animation #2",
input: ".foo { animation: bar 5s, foobar; }",
expected: ":local(.foo) { animation: :local(bar) 5s, :local(foobar); }",
},
{
name: "localize animation #3",
input: ".foo { animation: ease ease; }",
expected: ":local(.foo) { animation: ease :local(ease); }",
},
{
name: "localize animation #4",
input: ".foo { animation: 0s ease 0s 1 normal none test running; }",
expected:
":local(.foo) { animation: 0s ease 0s 1 normal none :local(test) running; }",
},
{
name: "localize animation with vendor prefix",
input: ".foo { -webkit-animation: bar; animation: bar; }",
expected:
":local(.foo) { -webkit-animation: :local(bar); animation: :local(bar); }",
},
{
name: "not localize other rules",
input: '.foo { content: "animation: bar;" }',
expected: ':local(.foo) { content: "animation: bar;" }',
},
{
name: "not localize global rules",
input: ":global .foo { animation: foo; animation-name: bar; }",
expected: ".foo { animation: foo; animation-name: bar; }",
},
{
name: "handle nested global",
input: ":global .a:not(:global .b) { a_value: some-value; }",
expected: ".a:not(.b) { a_value: some-value; }",
},
{
name: "handle nested global #1",
input:
":global .a:not(:global .b:not(:global .c)) { a_value: some-value; }",
expected: ".a:not(.b:not(.c)) { a_value: some-value; }",
},
{
name: "handle nested global #2",
input: ":local .a:not(:not(:not(:global .c))) { a_value: some-value; }",
expected: ":local(.a):not(:not(:not(.c))) { a_value: some-value; }",
},
{
name: "handle nested global #3",
input: ":global .a:not(:global .b, :global .c) { a_value: some-value; }",
expected: ".a:not(.b, .c) { a_value: some-value; }",
},
{
name: "handle nested global #4",
input: ":local .a:not(:global .b, :local .c) { a_value: some-value; }",
expected: ":local(.a):not(.b, :local(.c)) { a_value: some-value; }",
},
{
name: "handle nested global #5",
input: ":global .a:not(:local .b, :global .c) { a_value: some-value; }",
expected: ".a:not(:local(.b), .c) { a_value: some-value; }",
},
{
name: "handle nested global #6",
input: ":global .a:not(.b, .c) { a_value: some-value; }",
expected: ".a:not(.b, .c) { a_value: some-value; }",
},
{
name: "handle nested global #7",
input: ":local .a:not(.b, .c) { a_value: some-value; }",
expected: ":local(.a):not(:local(.b), :local(.c)) { a_value: some-value; }",
},
{
name: "handle nested global #8",
input: ":global .a:not(:local .b, .c) { a_value: some-value; }",
expected: ".a:not(:local(.b), :local(.c)) { a_value: some-value; }",
},
{
name: "handle a complex animation rule",
input:
".foo { animation: foo, bar 5s linear 2s infinite alternate, barfoo 1s; }",
expected:
":local(.foo) { animation: :local(foo), :local(bar) 5s linear 2s infinite alternate, :local(barfoo) 1s; }",
},
{
name: "handle animations where the first value is not the animation name",
input: ".foo { animation: 1s foo; }",
expected: ":local(.foo) { animation: 1s :local(foo); }",
},
{
name: "handle animations where the first value is not the animation name whilst also using keywords",
input: ".foo { animation: 1s normal ease-out infinite foo; }",
expected:
":local(.foo) { animation: 1s normal ease-out infinite :local(foo); }",
},
{
name: "not treat animation curve as identifier of animation name even if it separated by comma",
input:
".foo { animation: slide-right 300ms forwards ease-out, fade-in 300ms forwards ease-out; }",
expected:
":local(.foo) { animation: :local(slide-right) 300ms forwards ease-out, :local(fade-in) 300ms forwards ease-out; }",
},
{
name: 'not treat "start" and "end" keywords in steps() function as identifiers',
input: [
".foo { animation: spin 1s steps(12, end) infinite; }",
".foo { animation: spin 1s STEPS(12, start) infinite; }",
".foo { animation: spin 1s steps(12, END) infinite; }",
".foo { animation: spin 1s steps(12, START) infinite; }",
].join("\n"),
expected: [
":local(.foo) { animation: :local(spin) 1s steps(12, end) infinite; }",
":local(.foo) { animation: :local(spin) 1s STEPS(12, start) infinite; }",
":local(.foo) { animation: :local(spin) 1s steps(12, END) infinite; }",
":local(.foo) { animation: :local(spin) 1s steps(12, START) infinite; }",
].join("\n"),
},
{
name: "handle animations with custom timing functions",
input:
".foo { animation: 1s normal cubic-bezier(0.25, 0.5, 0.5. 0.75) foo; }",
expected:
":local(.foo) { animation: 1s normal cubic-bezier(0.25, 0.5, 0.5. 0.75) :local(foo); }",
},
{
name: "handle animations whose names are keywords",
input: ".foo { animation: 1s infinite infinite; }",
expected: ":local(.foo) { animation: 1s infinite :local(infinite); }",
},
{
name: 'handle not localize an animation shorthand value of "inherit"',
input: ".foo { animation: inherit; }",
expected: ":local(.foo) { animation: inherit; }",
},
{
name: 'handle "constructor" as animation name',
input: ".foo { animation: constructor constructor; }",
expected:
":local(.foo) { animation: :local(constructor) :local(constructor); }",
},
{
name: "default to global when mode provided",
input: ".foo { a_value: some-value; }",
options: { mode: "global" },
expected: ".foo { a_value: some-value; }",
},
{
name: "default to local when mode provided",
input: ".foo { a_value: some-value; }",
options: { mode: "local" },
expected: ":local(.foo) { a_value: some-value; }",
},
{
name: "use correct spacing",
input: [
".a :local .b {}",
".a:local.b {}",
".a:local(.b) {}",
".a:local( .b ) {}",
".a :local(.b) {}",
".a :local( .b ) {}",
":local(.a).b {}",
":local( .a ).b {}",
":local(.a) .b {}",
":local( .a ) .b {}",
].join("\n"),
options: { mode: "global" },
expected: [
".a :local(.b) {}",
".a:local(.b) {}",
".a:local(.b) {}",
".a:local(.b) {}",
".a :local(.b) {}",
".a :local(.b) {}",
":local(.a).b {}",
":local(.a).b {}",
":local(.a) .b {}",
":local(.a) .b {}",
].join("\n"),
},
{
name: "localize keyframes",
input:
"@keyframes foo { from: { a_value: some-value; } to { a_value: some-value; } }",
expected:
"@keyframes :local(foo) { from: { a_value: some-value; } to { a_value: some-value; } }",
},
{
name: "localize keyframes starting with special characters",
input: "@keyframes \\@foo { from { color: red; } to { color: blue; } }",
expected:
"@keyframes :local(\\@foo) { from { color: red; } to { color: blue; } }",
},
{
name: "localize keyframes containing special characters",
input: "@keyframes f\\@oo { from { color: red; } to { color: blue; } }",
expected:
"@keyframes :local(f\\@oo) { from { color: red; } to { color: blue; } }",
},
{
name: "localize keyframes in global default mode",
input: "@keyframes foo { a_value: some-value; }",
options: { mode: "global" },
expected: "@keyframes foo { a_value: some-value; }",
},
{
name: "localize explicit keyframes",
input:
"@keyframes :local(foo) { 0% { color: red; } 33.3% { color: yellow; } 100% { color: blue; } } @-webkit-keyframes :global(bar) { from { color: red; } to { color: blue; } }",
expected:
"@keyframes :local(foo) { 0% { color: red; } 33.3% { color: yellow; } 100% { color: blue; } } @-webkit-keyframes bar { from { color: red; } to { color: blue; } }",
},
{
name: "ignore :export statements",
input: ":export { foo: __foo; }",
expected: ":export { foo: __foo; }",
},
{
name: "ignore :import statemtents",
input: ':import("~/lol.css") { foo: __foo; }',
expected: ':import("~/lol.css") { foo: __foo; }',
},
{
name: "incorrectly handle nested selectors",
input: ".bar:not(:global .foo, .baz) { a_value: some-value; }",
expected: ":local(.bar):not(.foo, .baz) { a_value: some-value; }",
},
{
name: "compile in pure mode",
input:
':global(.foo).bar, [type="radio"] ~ .label, :not(.foo), #bar { a_value: some-value; }',
options: { mode: "pure" },
expected:
'.foo:local(.bar), [type="radio"] ~ :local(.label), :not(:local(.foo)), :local(#bar) { a_value: some-value; }',
},
{
name: "compile explict global element",
input: ":global(input) { a_value: some-value; }",
expected: "input { a_value: some-value; }",
},
{
name: "compile explict global attribute",
input:
':global([type="radio"]), :not(:global [type="radio"]) { a_value: some-value; }',
expected: '[type="radio"], :not([type="radio"]) { a_value: some-value; }',
},
{
name: "throw on invalid mode",
input: "",
options: { mode: "???" },
error: /"global", "local" or "pure"/,
},
{
name: "throw on inconsistent selector result",
input: ":global .foo, .bar { a_value: some-value; }",
error: /Inconsistent/,
},
{
name: "throw on nested :locals",
input: ":local(:local(.foo)) { a_value: some-value; }",
error: /is not allowed inside/,
},
{
name: "throw on nested :globals",
input: ":global(:global(.foo)) { a_value: some-value; }",
error: /is not allowed inside/,
},
{
name: "throw on nested mixed",
input: ":local(:global(.foo)) { a_value: some-value; }",
error: /is not allowed inside/,
},
{
name: "throw on nested broad :local",
input: ":global(:local .foo) { a_value: some-value; }",
error: /is not allowed inside/,
},
{
name: "throw on incorrect spacing with broad :global",
input: ".foo :global.bar { a_value: some-value; }",
error: /Missing whitespace after :global/,
},
{
name: "throw on incorrect spacing with broad :local",
input: ".foo:local .bar { a_value: some-value; }",
error: /Missing whitespace before :local/,
},
{
name: "throw on not pure selector (global class)",
input: ":global(.foo) { a_value: some-value; }",
options: { mode: "pure" },
error: /":global\(\.foo\)" is not pure/,
},
{
name: "throw on not pure selector (with multiple 1)",
input: ".foo, :global(.bar) { a_value: some-value; }",
options: { mode: "pure" },
error: /".foo, :global\(\.bar\)" is not pure/,
},
{
name: "throw on not pure selector (with multiple 2)",
input: ":global(.bar), .foo { a_value: some-value; }",
options: { mode: "pure" },
error: /":global\(\.bar\), .foo" is not pure/,
},
{
name: "throw on not pure selector (element)",
input: "input { a_value: some-value; }",
options: { mode: "pure" },
error: /"input" is not pure/,
},
{
name: "throw on not pure selector (attribute)",
input: '[type="radio"] { a_value: some-value; }',
options: { mode: "pure" },
error: /"\[type="radio"\]" is not pure/,
},
{
name: "throw on not pure keyframes",
input: "@keyframes :global(foo) { a_value: some-value; }",
options: { mode: "pure" },
error: /@keyframes :global\(\.\.\.\) is not allowed in pure mode/,
},
{
name: "pass through global element",
input: "input { a_value: some-value; }",
expected: "input { a_value: some-value; }",
},
{
name: "localise class and pass through element",
input: ".foo input { a_value: some-value; }",
expected: ":local(.foo) input { a_value: some-value; }",
},
{
name: "pass through attribute selector",
input: '[type="radio"] { a_value: some-value; }',
expected: '[type="radio"] { a_value: some-value; }',
},
{
name: "not modify urls without option",
input:
".a { background: url(./image.png); }\n" +
":global .b { background: url(image.png); }\n" +
'.c { background: url("./image.png"); }',
expected:
":local(.a) { background: url(./image.png); }\n" +
".b { background: url(image.png); }\n" +
':local(.c) { background: url("./image.png"); }',
},
{
name: "rewrite url in local block",
input:
".a { background: url(./image.png); }\n" +
":global .b { background: url(image.png); }\n" +
'.c { background: url("./image.png"); }\n' +
".c { background: url('./image.png'); }\n" +
'.d { background: -webkit-image-set(url("./image.png") 1x, url("./image2x.png") 2x); }\n' +
'@font-face { src: url("./font.woff"); }\n' +
'@-webkit-font-face { src: url("./font.woff"); }\n' +
'@media screen { .a { src: url("./image.png"); } }\n' +
'@keyframes :global(ani1) { 0% { src: url("image.png"); } }\n' +
'@keyframes ani2 { 0% { src: url("./image.png"); } }\n' +
"foo { background: end-with-url(something); }",
options: {
rewriteUrl: function (global, url) {
const mode = global ? "global" : "local";
return "(" + mode + ")" + url + '"' + mode + '"';
},
},
expected:
':local(.a) { background: url((local\\)./image.png\\"local\\"); }\n' +
'.b { background: url((global\\)image.png\\"global\\"); }\n' +
':local(.c) { background: url("(local)./image.png\\"local\\""); }\n' +
":local(.c) { background: url('(local)./image.png\"local\"'); }\n" +
':local(.d) { background: -webkit-image-set(url("(local)./image.png\\"local\\"") 1x, url("(local)./image2x.png\\"local\\"") 2x); }\n' +
'@font-face { src: url("(local)./font.woff\\"local\\""); }\n' +
'@-webkit-font-face { src: url("(local)./font.woff\\"local\\""); }\n' +
'@media screen { :local(.a) { src: url("(local)./image.png\\"local\\""); } }\n' +
'@keyframes ani1 { 0% { src: url("(global)image.png\\"global\\""); } }\n' +
'@keyframes :local(ani2) { 0% { src: url("(local)./image.png\\"local\\""); } }\n' +
"foo { background: end-with-url(something); }",
},
{
name: "not crash on atrule without nodes",
input: '@charset "utf-8";',
expected: '@charset "utf-8";',
},
{
name: "not crash on a rule without nodes",
input: (function () {
const inner = postcss.rule({ selector: ".b", ruleWithoutBody: true });
const outer = postcss.rule({ selector: ".a" }).push(inner);
const root = postcss.root().push(outer);
inner.nodes = undefined;
return root;
})(),
// postcss-less's stringify would honor `ruleWithoutBody` and omit the trailing `{}`
expected: ":local(.a) {\n :local(.b) {}\n}",
},
{
name: "not break unicode characters",
input: '.a { content: "\\2193" }',
expected: ':local(.a) { content: "\\2193" }',
},
{
name: "not break unicode characters",
input: '.a { content: "\\2193\\2193" }',
expected: ':local(.a) { content: "\\2193\\2193" }',
},
{
name: "not break unicode characters",
input: '.a { content: "\\2193 \\2193" }',
expected: ':local(.a) { content: "\\2193 \\2193" }',
},
{
name: "not break unicode characters",
input: '.a { content: "\\2193\\2193\\2193" }',
expected: ':local(.a) { content: "\\2193\\2193\\2193" }',
},
{
name: "not break unicode characters",
input: '.a { content: "\\2193 \\2193 \\2193" }',
expected: ':local(.a) { content: "\\2193 \\2193 \\2193" }',
},
{
name: "not ignore custom property set",
input:
":root { --title-align: center; --sr-only: { position: absolute; } }",
expected:
":root { --title-align: center; --sr-only: { position: absolute; } }",
},
/**
* Imported aliases
*/
{
name: "not localize imported alias",
input: `
:import(foo) { a_value: some-value; }
.foo > .a_value { a_value: some-value; }
`,
expected: `
:import(foo) { a_value: some-value; }
:local(.foo) > .a_value { a_value: some-value; }
`,
},
{
name: "not localize nested imported alias",
input: `
:import(foo) { a_value: some-value; }
.foo > .a_value > .bar { a_value: some-value; }
`,
expected: `
:import(foo) { a_value: some-value; }
:local(.foo) > .a_value > :local(.bar) { a_value: some-value; }
`,
},
{
name: "ignore imported in explicit local",
input: `
:import(foo) { a_value: some-value; }
:local(.a_value) { a_value: some-value; }
`,
expected: `
:import(foo) { a_value: some-value; }
:local(.a_value) { a_value: some-value; }
`,
},
{
name: "escape local context with explict global",
input: `
:import(foo) { a_value: some-value; }
:local .foo :global(.a_value) .bar { a_value: some-value; }
`,
expected: `
:import(foo) { a_value: some-value; }
:local(.foo) .a_value :local(.bar) { a_value: some-value; }
`,
},
{
name: "respect explicit local",
input: `
:import(foo) { a_value: some-value; }
.a_value :local .a_value .foo :global .a_value { a_value: some-value; }
`,
expected: `
:import(foo) { a_value: some-value; }
.a_value :local(.a_value) :local(.foo) .a_value { a_value: some-value; }
`,
},
{
name: "not localize imported animation-name",
input: `
:import(file) { a_value: some-value; }
.foo { animation-name: a_value; }
`,
expected: `
:import(file) { a_value: some-value; }
:local(.foo) { animation-name: a_value; }
`,
},
{
name: "throw on invalid syntax id usage",
input: ". { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid syntax class usage",
input: "# { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid syntax local class usage",
input: ":local(.) { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid syntax local id usage",
input: ":local(#) { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid global class usage",
input: ":global(.) { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid global class usage",
input: ":global(#) { a_value: some-value; }",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid global class usage",
input: ":global(.a:not(:global .b, :global .c)) { a_value: some-value; }",
error: /A :global is not allowed inside of a :global/,
},
{
name: "consider & statements as pure",
input: ".foo { &:hover { a_value: some-value; } }",
options: { mode: "pure" },
expected: ":local(.foo) { &:hover { a_value: some-value; } }",
},
{
name: "consider & statements as pure #2",
input:
".foo { @media screen and (min-width: 900px) { &:hover { a_value: some-value; } } }",
options: { mode: "pure" },
expected:
":local(.foo) { @media screen and (min-width: 900px) { &:hover { a_value: some-value; } } }",
},
{
name: "consider global inside local as pure",
input: ".foo button { a_value: some-value; }",
options: { mode: "pure" },
expected: ":local(.foo) button { a_value: some-value; }",
},
{
name: "consider selector & statements as pure",
input: ".foo { html &:hover { a_value: some-value; } }",
options: { mode: "pure" },
expected: ":local(.foo) { html &:hover { a_value: some-value; } }",
},
{
name: "consider selector & statements as pure",
input: ".foo { &:global(.bar) { a_value: some-value; } }",
options: { mode: "pure" },
expected: ":local(.foo) { &.bar { a_value: some-value; } }",
},
{
name: "throw on nested & selectors without a local selector",
input: ":global(.foo) { &:hover { a_value: some-value; } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "should suppress errors for global selectors after ignore comment",
options: { mode: "pure" },
input: `/* cssmodules-pure-ignore */
:global(.foo) { color: blue; }`,
expected: `.foo { color: blue; }`,
},
{
name: "should suppress errors for global selectors after ignore comment #2",
options: { mode: "pure" },
input: `/* cssmodules-pure-ignore */
/* another comment */
:global(.foo) { color: blue; }`,
expected: `/* another comment */
.foo { color: blue; }`,
},
{
name: "should suppress errors for global selectors after ignore comment #3",
options: { mode: "pure" },
input: `/* another comment */
/* cssmodules-pure-ignore */
:global(.foo) { color: blue; }`,
expected: `/* another comment */
.foo { color: blue; }`,
},
{
name: "should suppress errors for global selectors after ignore comment #4",
options: { mode: "pure" },
input: `/* cssmodules-pure-ignore */ /* another comment */
:global(.foo) { color: blue; }`,
expected: `/* another comment */
.foo { color: blue; }`,
},
{
name: "should suppress errors for global selectors after ignore comment #5",
options: { mode: "pure" },
input: `/* another comment */ /* cssmodules-pure-ignore */
:global(.foo) { color: blue; }`,
expected: `/* another comment */
.foo { color: blue; }`,
},
{
name: "should suppress errors for global selectors after ignore comment #6",
options: { mode: "pure" },
input: `.foo { /* cssmodules-pure-ignore */ :global(.bar) { color: blue }; }`,
expected: `:local(.foo) { .bar { color: blue }; }`,