ember-scoped-css 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,648 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { rewriteCss } from './rewrite.js';
4
+
5
+ it('should rewrite css', function () {
6
+ const css = '.foo { color: red; }';
7
+ const postfix = 'postfix';
8
+ const fileName = 'foo.css';
9
+ const rewritten = rewriteCss(css, postfix, fileName, false);
10
+
11
+ expect(rewritten).to.equal(`/* foo.css */\n.foo_postfix { color: red; }\n`);
12
+ });
13
+
14
+ it('should use a custom layer', function () {
15
+ const css = '.foo { color: red; }';
16
+ const postfix = 'postfix';
17
+ const fileName = 'foo.css';
18
+ const rewritten = rewriteCss(css, postfix, fileName, 'utils');
19
+
20
+ expect(rewritten).to.equal(
21
+ `/* foo.css */\n@layer utils {\n\n.foo_postfix { color: red; }\n}\n`,
22
+ );
23
+ });
24
+
25
+ it('shouldnt rewrite global', function () {
26
+ const css = '.baz :global(.foo p) .bar { color: red; }';
27
+ const postfix = 'postfix';
28
+ const fileName = 'foo.css';
29
+ const rewritten = rewriteCss(css, postfix, fileName);
30
+
31
+ expect(rewritten).to.equal(
32
+ `/* foo.css */\n@layer components {\n\n.baz_postfix .foo p .bar_postfix { color: red; }\n}\n`,
33
+ );
34
+ });
35
+
36
+ it(`understands nth-of-type syntax`, function () {
37
+ const css = `
38
+ li:nth-of-type(odd) {}
39
+ li:nth-of-type(even) {}
40
+ `;
41
+
42
+ const postfix = 'postfix';
43
+ const fileName = 'foo.css';
44
+ const rewritten = rewriteCss(css, postfix, fileName);
45
+
46
+ expect(rewritten).toMatchInlineSnapshot(`
47
+ "/* foo.css */
48
+ @layer components {
49
+
50
+
51
+ li.postfix:nth-of-type(odd) {}
52
+ li.postfix:nth-of-type(even) {}
53
+
54
+ }
55
+ "
56
+ `);
57
+ });
58
+
59
+ describe('@container', () => {
60
+ it('works', () => {
61
+ const css = `
62
+ @container (width > 400px) {
63
+ h2 {
64
+ font-size: 1.5em;
65
+ }
66
+ }
67
+ `;
68
+ const postfix = 'postfix';
69
+ const fileName = 'foo.css';
70
+ const rewritten = rewriteCss(css, postfix, fileName);
71
+
72
+ expect(rewritten).toMatchInlineSnapshot(`
73
+ "/* foo.css */
74
+ @layer components {
75
+
76
+
77
+ @container (width > 400px) {
78
+ h2.postfix {
79
+ font-size: 1.5em;
80
+ }
81
+ }
82
+
83
+ }
84
+ "
85
+ `);
86
+ });
87
+
88
+ it('handles parameters', () => {
89
+ const css = `
90
+ /* With an optional <container-name> */
91
+ @container tall (height > 30rem) {
92
+ p {
93
+ line-height: 1.6;
94
+ }
95
+ }
96
+
97
+ /* With a <scroll-state> */
98
+ @container scroll-state(scrollable: top) {
99
+ .back-to-top-link {
100
+ visibility: visible;
101
+ }
102
+ }
103
+
104
+ /* With a <container-name> and a <scroll-state> */
105
+ @container sticky-heading scroll-state(stuck: top) {
106
+ h2 {
107
+ background: purple;
108
+ color: white;
109
+ }
110
+ }
111
+ `;
112
+
113
+ const postfix = 'postfix';
114
+ const fileName = 'foo.css';
115
+ const rewritten = rewriteCss(css, postfix, fileName);
116
+
117
+ expect(rewritten).toMatchInlineSnapshot(`
118
+ "/* foo.css */
119
+ @layer components {
120
+
121
+
122
+ /* With an optional <container-name> */
123
+ @container tall (height > 30rem) {
124
+ p.postfix {
125
+ line-height: 1.6;
126
+ }
127
+ }
128
+
129
+ /* With a <scroll-state> */
130
+ @container scroll-state(scrollable: top) {
131
+ .back-to-top-link_postfix {
132
+ visibility: visible;
133
+ }
134
+ }
135
+
136
+ /* With a <container-name> and a <scroll-state> */
137
+ @container sticky-heading scroll-state(stuck: top) {
138
+ h2.postfix {
139
+ background: purple;
140
+ color: white;
141
+ }
142
+ }
143
+
144
+ }
145
+ "
146
+ `);
147
+ });
148
+ });
149
+
150
+ describe('@media', () => {
151
+ it('works', () => {
152
+ const css = `
153
+ @media (height >= 680px), screen and (orientation: portrait) {
154
+ .foo { color: red; }
155
+ }
156
+ `;
157
+ const postfix = 'postfix';
158
+ const fileName = 'foo.css';
159
+ const rewritten = rewriteCss(css, postfix, fileName, 'utils');
160
+
161
+ expect(rewritten).toMatchInlineSnapshot(`
162
+ "/* foo.css */
163
+ @layer utils {
164
+
165
+
166
+ @media (height >= 680px), screen and (orientation: portrait) {
167
+ .foo_postfix { color: red; }
168
+ }
169
+
170
+ }
171
+ "
172
+ `);
173
+ });
174
+ });
175
+
176
+ describe('@keyframe', () => {
177
+ it(`rewrites`, function () {
178
+ const css = `
179
+ @keyframes luna-view-navigation {
180
+ 100% {
181
+ padding-top: 1rem;
182
+ }
183
+ }
184
+ `;
185
+
186
+ const postfix = 'postfix';
187
+ const fileName = 'foo.css';
188
+ const rewritten = rewriteCss(css, postfix, fileName);
189
+
190
+ expect(rewritten).toMatchInlineSnapshot(`
191
+ "/* foo.css */
192
+ @layer components {
193
+
194
+
195
+ @keyframes luna-view-navigation__postfix {
196
+ 100% {
197
+ padding-top: 1rem;
198
+ }
199
+ }
200
+
201
+ }
202
+ "
203
+ `);
204
+ });
205
+
206
+ it(`references are also scoped`, function () {
207
+ const css = `
208
+ p {
209
+ animation-duration: 3s;
210
+ animation-name: slide-in;
211
+ }
212
+
213
+ @keyframes slide-in {
214
+ from {
215
+ translate: 150vw 0;
216
+ scale: 200% 1;
217
+ }
218
+
219
+ to {
220
+ translate: 0 0;
221
+ scale: 100% 1;
222
+ }
223
+ }
224
+ `;
225
+
226
+ const postfix = 'postfix';
227
+ const fileName = 'foo.css';
228
+ const rewritten = rewriteCss(css, postfix, fileName);
229
+
230
+ expect(rewritten).toMatchInlineSnapshot(`
231
+ "/* foo.css */
232
+ @layer components {
233
+
234
+
235
+ p.postfix {
236
+ animation-duration: 3s;
237
+ animation-name: slide-in__postfix;
238
+ }
239
+
240
+ @keyframes slide-in__postfix {
241
+ from {
242
+ translate: 150vw 0;
243
+ scale: 200% 1;
244
+ }
245
+
246
+ to {
247
+ translate: 0 0;
248
+ scale: 100% 1;
249
+ }
250
+ }
251
+
252
+ }
253
+ "
254
+ `);
255
+ });
256
+
257
+ it('handles multiple references and keyframes', () => {
258
+ const css = `
259
+ p {
260
+ animation-duration: 3s;
261
+ animation-name: slide-in;
262
+ }
263
+ p.hello {
264
+ animation-duration: 5s;
265
+ animation-name: slide-in;
266
+ }
267
+ p span {
268
+ display: inline-block;
269
+ animation-duration: 3s;
270
+ animation-name: grow-shrink;
271
+ }
272
+
273
+ @keyframes slide-in {
274
+ from {
275
+ translate: 150vw 0;
276
+ scale: 200% 1;
277
+ }
278
+
279
+ to {
280
+ translate: 0 0;
281
+ scale: 100% 1;
282
+ }
283
+ }
284
+
285
+ @keyframes grow-shrink {
286
+ 25%,
287
+ 75% {
288
+ scale: 100%;
289
+ }
290
+
291
+ 50% {
292
+ scale: 200%;
293
+ color: magenta;
294
+ }
295
+ }
296
+ `;
297
+ const postfix = 'postfix';
298
+ const fileName = 'foo.css';
299
+ const rewritten = rewriteCss(css, postfix, fileName);
300
+
301
+ expect(rewritten).toMatchInlineSnapshot(`
302
+ "/* foo.css */
303
+ @layer components {
304
+
305
+
306
+ p.postfix {
307
+ animation-duration: 3s;
308
+ animation-name: slide-in__postfix;
309
+ }
310
+ p.postfix.hello_postfix {
311
+ animation-duration: 5s;
312
+ animation-name: slide-in__postfix;
313
+ }
314
+ p.postfix span.postfix {
315
+ display: inline-block;
316
+ animation-duration: 3s;
317
+ animation-name: grow-shrink__postfix;
318
+ }
319
+
320
+ @keyframes slide-in__postfix {
321
+ from {
322
+ translate: 150vw 0;
323
+ scale: 200% 1;
324
+ }
325
+
326
+ to {
327
+ translate: 0 0;
328
+ scale: 100% 1;
329
+ }
330
+ }
331
+
332
+ @keyframes grow-shrink__postfix {
333
+ 25%,
334
+ 75% {
335
+ scale: 100%;
336
+ }
337
+
338
+ 50% {
339
+ scale: 200%;
340
+ color: magenta;
341
+ }
342
+ }
343
+
344
+ }
345
+ "
346
+ `);
347
+ });
348
+
349
+ it('works in shorthand combo-declarations', () => {
350
+ const css = `
351
+ div {
352
+ width: 100px;
353
+ height: 100px;
354
+ background: red;
355
+ position: relative;
356
+ animation: mymove 5s infinite;
357
+ }
358
+
359
+ @keyframes mymove {
360
+ from {top: 0px;}
361
+ to {top: 200px;}
362
+ }
363
+ `;
364
+
365
+ const postfix = 'postfix';
366
+ const fileName = 'foo.css';
367
+ const rewritten = rewriteCss(css, postfix, fileName);
368
+
369
+ expect(rewritten).toMatchInlineSnapshot(`
370
+ "/* foo.css */
371
+ @layer components {
372
+
373
+
374
+ div.postfix {
375
+ width: 100px;
376
+ height: 100px;
377
+ background: red;
378
+ position: relative;
379
+ animation: mymove__postfix 5s infinite;
380
+ }
381
+
382
+ @keyframes mymove__postfix {
383
+ from {top: 0px;}
384
+ to {top: 200px;}
385
+ }
386
+
387
+ }
388
+ "
389
+ `);
390
+ });
391
+ });
392
+
393
+ describe('@counter-style', () => {
394
+ it('rewrites', () => {
395
+ const css = `
396
+ @counter-style circled-alpha {
397
+ system: fixed;
398
+ symbols: Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ;
399
+ suffix: " ";
400
+ }
401
+ `;
402
+
403
+ const postfix = 'postfix';
404
+ const fileName = 'foo.css';
405
+ const rewritten = rewriteCss(css, postfix, fileName);
406
+
407
+ expect(rewritten).toMatchInlineSnapshot(`
408
+ "/* foo.css */
409
+ @layer components {
410
+
411
+
412
+ @counter-style circled-alpha__postfix {
413
+ system: fixed;
414
+ symbols: Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ;
415
+ suffix: " ";
416
+ }
417
+
418
+ }
419
+ "
420
+ `);
421
+ });
422
+
423
+ it('updates references', () => {
424
+ const css = `
425
+ @counter-style circled-alpha {
426
+ system: fixed;
427
+ symbols: Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ;
428
+ suffix: " ";
429
+ }
430
+
431
+ .items {
432
+ list-style: circled-alpha;
433
+ }
434
+ `;
435
+
436
+ const postfix = 'postfix';
437
+ const fileName = 'foo.css';
438
+ const rewritten = rewriteCss(css, postfix, fileName);
439
+
440
+ expect(rewritten).toMatchInlineSnapshot(`
441
+ "/* foo.css */
442
+ @layer components {
443
+
444
+
445
+ @counter-style circled-alpha__postfix {
446
+ system: fixed;
447
+ symbols: Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ;
448
+ suffix: " ";
449
+ }
450
+
451
+ .items_postfix {
452
+ list-style: circled-alpha__postfix;
453
+ }
454
+
455
+ }
456
+ "
457
+ `);
458
+ });
459
+ });
460
+
461
+ describe('@position-try', () => {
462
+ it('rewrites', () => {
463
+ const css = `
464
+ @position-try --custom-left {
465
+ position-area: left;
466
+ width: 100px;
467
+ margin-right: 10px;
468
+ }
469
+ `;
470
+
471
+ const postfix = 'postfix';
472
+ const fileName = 'foo.css';
473
+ const rewritten = rewriteCss(css, postfix, fileName);
474
+
475
+ expect(rewritten).toMatchInlineSnapshot(`
476
+ "/* foo.css */
477
+ @layer components {
478
+
479
+
480
+ @position-try --custom-left__postfix {
481
+ position-area: left;
482
+ width: 100px;
483
+ margin-right: 10px;
484
+ }
485
+
486
+ }
487
+ "
488
+ `);
489
+ });
490
+
491
+ it('updates references', () => {
492
+ const css = `
493
+ @position-try --custom-left {
494
+ position-area: left;
495
+ width: 100px;
496
+ margin-right: 10px;
497
+ }
498
+
499
+ .infobox {
500
+ position-try-fallbacks:
501
+ --custom-left;
502
+ }
503
+ `;
504
+
505
+ const postfix = 'postfix';
506
+ const fileName = 'foo.css';
507
+ const rewritten = rewriteCss(css, postfix, fileName);
508
+
509
+ expect(rewritten).toMatchInlineSnapshot(`
510
+ "/* foo.css */
511
+ @layer components {
512
+
513
+
514
+ @position-try --custom-left__postfix {
515
+ position-area: left;
516
+ width: 100px;
517
+ margin-right: 10px;
518
+ }
519
+
520
+ .infobox_postfix {
521
+ position-try-fallbacks:
522
+ --custom-left__postfix;
523
+ }
524
+
525
+ }
526
+ "
527
+ `);
528
+ });
529
+ });
530
+
531
+ describe('@property', () => {
532
+ it('rewrites', () => {
533
+ const css = `
534
+ @property --item-size {
535
+ syntax: "<percentage>";
536
+ inherits: true;
537
+ initial-value: 40%;
538
+ }
539
+ `;
540
+
541
+ const postfix = 'postfix';
542
+ const fileName = 'foo.css';
543
+ const rewritten = rewriteCss(css, postfix, fileName);
544
+
545
+ expect(rewritten).toMatchInlineSnapshot(`
546
+ "/* foo.css */
547
+ @layer components {
548
+
549
+
550
+ @property --item-size__postfix {
551
+ syntax: "<percentage>";
552
+ inherits: true;
553
+ initial-value: 40%;
554
+ }
555
+
556
+ }
557
+ "
558
+ `);
559
+ });
560
+
561
+ it('updates references', () => {
562
+ const css = `
563
+ @property --item-size {
564
+ syntax: "<percentage>";
565
+ inherits: true;
566
+ initial-value: 40%;
567
+ }
568
+
569
+ .container {
570
+ display: flex;
571
+ height: 200px;
572
+ border: 1px dashed black;
573
+
574
+ /* set custom property values on parent */
575
+ --item-size: 20%;
576
+ --item-color: orange;
577
+ }
578
+
579
+ /* use custom properties to set item size and background color */
580
+ .item {
581
+ width: var(--item-size);
582
+ height: var(--item-size);
583
+ background-color: var(--item-color);
584
+ }
585
+ `;
586
+
587
+ const postfix = 'postfix';
588
+ const fileName = 'foo.css';
589
+ const rewritten = rewriteCss(css, postfix, fileName);
590
+
591
+ expect(rewritten).toMatchInlineSnapshot(`
592
+ "/* foo.css */
593
+ @layer components {
594
+
595
+
596
+ @property --item-size__postfix {
597
+ syntax: "<percentage>";
598
+ inherits: true;
599
+ initial-value: 40%;
600
+ }
601
+
602
+ .container_postfix {
603
+ display: flex;
604
+ height: 200px;
605
+ border: 1px dashed black;
606
+
607
+ /* set custom property values on parent */
608
+ --item-size: 20%;
609
+ --item-color: orange;
610
+ }
611
+
612
+ /* use custom properties to set item size and background color */
613
+ .item_postfix {
614
+ width: var(--item-size__postfix);
615
+ height: var(--item-size__postfix);
616
+ background-color: var(--item-color);
617
+ }
618
+
619
+ }
620
+ "
621
+ `);
622
+ });
623
+ });
624
+
625
+ describe('@supports', () => {
626
+ it('does nothing for feature checking', () => {
627
+ const css = `
628
+ @supports (transform-origin: 5% 5%) {}
629
+ @supports selector(h2 > p) {}
630
+ `;
631
+
632
+ const postfix = 'postfix';
633
+ const fileName = 'foo.css';
634
+ const rewritten = rewriteCss(css, postfix, fileName);
635
+
636
+ expect(rewritten).toMatchInlineSnapshot(`
637
+ "/* foo.css */
638
+ @layer components {
639
+
640
+
641
+ @supports (transform-origin: 5% 5%) {}
642
+ @supports selector(h2 > p) {}
643
+
644
+ }
645
+ "
646
+ `);
647
+ });
648
+ });
@@ -30,7 +30,7 @@ export function decodeScopedCSSRequest(request) {
30
30
 
31
31
  return {
32
32
  postfix: params.from,
33
- css: decodeURIComponent(params.css),
33
+ css: params.css,
34
34
  cssId: params.cssId,
35
35
  };
36
36
  }