-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathatrules.test.js
591 lines (528 loc) · 17 KB
/
atrules.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
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { analyze } from '../index.js'
const AtRules = suite('at-rules')
AtRules('finds @layer', () => {
// Fixture is pretty much a straight copy from all code examples from
// https://css-tricks.com/css-cascade-layers/
const fixture = `
/* establish a layer order up-front, from lowest to highest priority */
@layer reset, defaults, patterns, components, utilities, overrides;
/* add styles to layers */
@layer utilities {
/* high layer priority, despite low specificity */
[data-color='brand'] {
color: var(--brand, rebeccapurple);
}
}
@layer defaults {
/* higher specificity, but lower layer priority */
a:any-link { color: maroon; }
}
/* un-layered styles have the highest priority */
a {
color: mediumvioletred;
}
@layer layer-1 { a { color: red; } }
@layer layer-2 { a { color: orange; } }
@layer layer-3 {
@layer sub-layer-1 { a { color: yellow; } }
@layer sub-layer-2 { a { color: green; } }
/* un-nested */ a { color: blue; }
}
/* un-layered */ a { color: indigo; }
@layer reset, defaults, framework;
@layer components, defaults, framework, reset, utilities;
@layer one {
/* sorting the sub-layers */
@layer two, three;
/* styles ... */
@layer three { /* styles ... */ }
@layer two { /* styles ... */ }
}
/* sorting nested layers directly */
@layer one.two, one.three;
/* adding to nested layers directly */
@layer one.three { /* ... */ }
@layer one.two { /* ... */ }
@layer reset.type, default.type, reset.media, default.media;
@layer
reset,
default,
themes,
patterns,
layouts,
components,
utilities;
@layer components {
@layer defaults, structures, themes, utilities;
}
`
const actual = analyze(fixture).atrules.layer
const expected = {
total: 46,
totalUnique: 25,
unique: {
"defaults": 5,
"layer-1": 1,
"layer-2": 1,
"layer-3": 1,
"sub-layer-1": 1,
"sub-layer-2": 1,
"reset": 4,
"framework": 2,
"components": 4,
"utilities": 5,
"one": 1,
"two": 2,
"three": 2,
"one.two": 2,
"one.three": 2,
"reset.type": 1,
"default.type": 1,
"reset.media": 1,
"default.media": 1,
"default": 1,
"themes": 2,
"patterns": 2,
"layouts": 1,
"structures": 1,
"overrides": 1,
},
uniquenessRatio: 25 / 46
}
assert.equal(actual, expected)
})
AtRules('finds @font-face', () => {
const fixture = `
@font-face {
font-family: Arial;
src: url("https://url-to-arial.woff");
}
@font-face {
font-display: swap;
font-family: Test;
font-stretch: condensed;
font-style: italic;
font-weight: 700;
font-variant: no-common-ligatures proportional-nums;
font-feature-settings: "liga" 0;
font-variation-settings: "xhgt" 0.7;
src: local("Input Mono");
unicode-range: U+0025-00FF;
}
@font-face {
font-family: 'Input Mono';
src: local('Input Mono') url("https://url-to-input-mono.woff");
}
@font-face {
font-family: MyHelvetica;
src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf);
font-weight: bold;
}
/* Duplicate @font-face in Media Query */
@media (min-width: 1000px) {
@font-face {
font-family: 'Input Mono';
src: local('Input Mono') url("https://url-to-input-mono.woff");
}
}`
const actual = analyze(fixture).atrules.fontface
const expected = {
total: 5,
totalUnique: 5,
unique: [
{
"font-family": "Arial",
"src": "url(\"https://url-to-arial.woff\")"
},
{
"font-display": `swap`,
"font-family": `Test`,
"font-stretch": `condensed`,
"font-style": `italic`,
"font-weight": `700`,
"font-variant": `no-common-ligatures proportional-nums`,
"font-feature-settings": `"liga" 0`,
"font-variation-settings": `"xhgt" 0.7`,
"src": `local("Input Mono")`,
"unicode-range": `U+0025-00FF`,
},
{
"font-family": "'Input Mono'",
"src": "local('Input Mono') url(\"https://url-to-input-mono.woff\")"
},
{
'font-family': 'MyHelvetica',
'src': 'local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf)',
'font-weight': 'bold',
},
{
"font-family": "'Input Mono'",
"src": "local('Input Mono') url(\"https://url-to-input-mono.woff\")"
}
],
uniquenessRatio: 1
}
assert.equal(actual, expected)
})
AtRules('handles @font-face encoding issues (GH-307)', () => {
// Actual CSS once found in a <style> tag on vistaprint.nl
// CSSTree parses it without errors, but analyzer failed on it;
let css = `
@font-face {
font-family: 'Graphik';
font-stretch: normal;
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('https://www.vistaprint.nl/swan/v1/fonts/graphic_regular2.7c96db81b23a97fd67cbeb7e7efad583.woff2') format('woff2'),
url('https://www.vistaprint.nl/swan/v1/fonts/graphic_regular.e694f50e9c3a4ee999da756a90b0e872.woff') format('woff');
}
@font-face {
font-family: 'Graphik';
font-stretch: normal;
font-style: normal;
font-weight: 700;
font-display: swap;
src: url('https://www.vistaprint.nl/swan/v1/fonts/graphic_medium2.3829398551b96ac319a48122465462c2.woff2') format('woff2'),
url('https://www.vistaprint.nl/swan/v1/fonts/graphic_medium.1e5761591bb4bdd7e1d6ef96bb7cef90.woff') format('woff');
}
`
assert.not.throws(() => analyze(css))
let actual = analyze(css).atrules.fontface
assert.is(actual.total, 2)
assert.equal(actual.unique[0], {
'font-family': `'`,
'font-stretch': `normal`,
'font-style': `normal`,
'font-weight': `400`,
'font-display': `swap`,
'src': `url('https://www.vistaprint.nl/swan/v1/fonts/graphic_regular2.7c96db81b23a97fd67cbeb7e7efad583.woff2') format('woff2'),
url('https://www.vistaprint.nl/swan/v1/fonts/graphic_regular.e694f50e9c3a4ee999da756a90b0e872.woff') format('woff')`
})
})
AtRules('finds @imports', () => {
const fixture = `
@import "https://example.com/without-url";
@import url("https://example.com/with-url");
@import url("https://example.com/with-media-query") screen and (min-width: 33em);
@import url("https://example.com/with-multiple-media-queries") screen, projection;
/* styles imported into to the <layer-name> layer */
@import url('example.css') layer(named-layer);
/* styles imported into to a new anonymous layer */
@import url('../example.css') layer;
@import url('remedy.css') layer(reset.remedy);
`
const actual = analyze(fixture).atrules.import
const expected = {
total: 7,
totalUnique: 7,
unique: {
'"https://example.com/without-url"': 1,
'url("https://example.com/with-url")': 1,
'url("https://example.com/with-media-query") screen and (min-width: 33em)': 1,
'url("https://example.com/with-multiple-media-queries") screen, projection': 1,
'url(\'example.css\') layer(named-layer)': 1,
'url(\'../example.css\') layer': 1,
'url(\'remedy.css\') layer(reset.remedy)': 1,
},
uniquenessRatio: 1,
}
assert.equal(actual, expected)
})
AtRules.only('finds layer names across `@layer` and `@import layer()`', () => {
const fixture = `
@import url("https://example.com/with-media-query") (min-width: 33em);
@import "https://unpkg.com/open-props" layer(base);
@import url("https://unpkg.com/open-props") layer(base);
/* styles imported into to the <layer-name> layer */
@import url('example.css') layer(named-layer);
/* styles imported into to a new anonymous layer */
@import url('../example.css') layer;
@import url('remedy.css') layer(reset.remedy);
/* establish a layer order up-front, from lowest to highest priority */
@layer reset, defaults, utilities;
/* add styles to layers */
@layer utilities {
/* high layer priority, despite low specificity */
[data-color='brand'] {
color: var(--brand, rebeccapurple);
}
}
`
const actual = analyze(fixture).stylesheet.layers
const expected = {
total: 9,
totalUnique: 7,
unique: {
'base': 2,
'named-layer': 1,
'<anonymous>': 1,
'reset.remedy': 1,
'reset': 1,
'defaults': 1,
'utilities': 2,
},
uniquenessRatio: 7 / 9,
}
assert.equal(actual, expected)
})
AtRules('finds @charsets', () => {
const fixture = `
@charset "UTF-8";
@charset "UTF-16";
`
const actual = analyze(fixture).atrules.charset
const expected = {
total: 2,
totalUnique: 2,
unique: {
'"UTF-8"': 1,
'"UTF-16"': 1,
},
uniquenessRatio: 2 / 2
}
assert.equal(actual, expected)
})
AtRules('finds @supports', () => {
const fixture = `
@supports (filter: blur(5px)) {}
@supports (display: table-cell) and (display: list-item) {}
@supports (-webkit-appearance: none) {}
@media (min-width: 0) {
@supports (-webkit-appearance: none) {}
}
`
const actual = analyze(fixture).atrules.supports
assert.equal(actual.total, 4)
assert.equal(actual.totalUnique, 3)
assert.equal(actual.uniquenessRatio, 3 / 4)
assert.equal(actual.unique, {
'(filter: blur(5px))': 1,
'(display: table-cell) and (display: list-item)': 1,
'(-webkit-appearance: none)': 2,
})
})
AtRules('finds @supports browserhacks', () => {
const fixture = `
@supports (-webkit-appearance:none) {}
@supports (-webkit-appearance: none) {}
@supports (-moz-appearance:meterbar) {}
@supports (-moz-appearance:meterbar) and (display:flex) {}
@supports (-moz-appearance:meterbar) and (cursor:zoom-in) {}
@supports (-moz-appearance:meterbar) and (background-attachment:local) {}
@supports (-moz-appearance:meterbar) and (image-orientation:90deg) {}
@supports (-moz-appearance:meterbar) and (all:initial) {}
@supports (-moz-appearance:meterbar) and (list-style-type:japanese-formal) {}
@supports (-moz-appearance:meterbar) and (background-blend-mode:difference,normal) {}
/* Not a hack */
@supports (color: red) {}
`
const result = analyze(fixture)
const actual = result.atrules.supports.browserhacks
const expected = {
total: 10,
totalUnique: 10,
uniquenessRatio: 10 / 10,
unique: {
'(-webkit-appearance:none)': 1,
'(-webkit-appearance: none)': 1,
'(-moz-appearance:meterbar)': 1,
'(-moz-appearance:meterbar) and (display:flex)': 1,
'(-moz-appearance:meterbar) and (cursor:zoom-in)': 1,
'(-moz-appearance:meterbar) and (background-attachment:local)': 1,
'(-moz-appearance:meterbar) and (image-orientation:90deg)': 1,
'(-moz-appearance:meterbar) and (all:initial)': 1,
'(-moz-appearance:meterbar) and (list-style-type:japanese-formal)': 1,
'(-moz-appearance:meterbar) and (background-blend-mode:difference,normal)': 1,
}
}
assert.equal(actual, expected)
})
AtRules('finds @media', () => {
const fixture = `
@media screen {}
@media screen and (min-width: 33em) {}
@media (min-width: 20px) {}
@media (max-width: 200px) {}
@media screen or print {}
@media all and (transform-3d), (-webkit-transform-3d) {}
@supports (-webkit-appearance: none) {
@media (min-width: 0) {}
}
`
const actual = analyze(fixture).atrules.media
assert.is(actual.total, 7)
assert.is(actual.totalUnique, 7)
assert.equal(actual.unique, {
'screen': 1,
'screen and (min-width: 33em)': 1,
'(min-width: 20px)': 1,
'(max-width: 200px)': 1,
'screen or print': 1,
'all and (transform-3d), (-webkit-transform-3d)': 1,
'(min-width: 0)': 1,
})
assert.is(actual.uniquenessRatio, 1)
})
AtRules('finds @media browserhacks', () => {
const fixture = `
@media \\0 all {}
@media \\0 screen {}
@media \\0screen {}
@media screen\\9 {}
@media \\0screen\\,screen\\9 {}
@media screen and (min-width:0\\0) {}
@media all and (-moz-images-in-menus:0) and (min-resolution: .001dpcm) {}
@media all and (-moz-images-in-menus:0) { @media (min-width: 0px) {} }
@media screen and (-moz-images-in-menus:0) {}
@media screen and (min--moz-device-pixel-ratio:0) {}
@media all and (min--moz-device-pixel-ratio:0) and (min-resolution: .001dpcm) {}
@media all and (min--moz-device-pixel-ratio:0) { @media (min-width: 0px) {} }
@media all and (min--moz-device-pixel-ratio:0) and (min-resolution: 3e1dpcm) {}
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {}
@media (min-resolution: .001dpcm) { _:-o-prefocus, .selector {} }
@media all and (-webkit-min-device-pixel-ratio:0) and (min-resolution: .001dpcm) { .selector {} }
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {}
`
const result = analyze(fixture)
const actual = result.atrules.media.browserhacks
const expected = {
total: 17,
totalUnique: 17,
uniquenessRatio: 1,
unique: {
"\\0 all": 1,
"\\0 screen": 1,
"\\0screen": 1,
"screen\\9": 1,
"\\0screen\\,screen\\9": 1,
"screen and (min-width:0\\0)": 1,
"all and (-moz-images-in-menus:0) and (min-resolution: .001dpcm)": 1,
"all and (-moz-images-in-menus:0)": 1,
"screen and (-moz-images-in-menus:0)": 1,
"screen and (min--moz-device-pixel-ratio:0)": 1,
"all and (min--moz-device-pixel-ratio:0)": 1,
"all and (min--moz-device-pixel-ratio:0) and (min-resolution: .001dpcm)": 1,
"all and (min--moz-device-pixel-ratio:0) and (min-resolution: 3e1dpcm)": 1,
"screen and (-ms-high-contrast: active), (-ms-high-contrast: none)": 1,
"(min-resolution: .001dpcm)": 1,
"all and (-webkit-min-device-pixel-ratio:0) and (min-resolution: .001dpcm)": 1,
"all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0)": 1,
}
}
assert.equal(actual, expected)
})
AtRules('analyzes @keyframes', () => {
const fixture = `
@keyframes one {}
@keyframes one {}
@keyframes TWO {}
/* Prefixes */
@-webkit-keyframes animation {}
@-moz-keyframes animation {}
@-o-keyframes animation {}
`
const actual = analyze(fixture).atrules.keyframes
const expected = {
total: 6,
totalUnique: 5,
unique: {
'@keyframes one': 2,
'@keyframes TWO': 1,
'@-webkit-keyframes animation': 1,
'@-moz-keyframes animation': 1,
'@-o-keyframes animation': 1,
},
uniquenessRatio: 5 / 6,
prefixed: {
total: 3,
totalUnique: 3,
unique: {
'@-webkit-keyframes animation': 1,
'@-moz-keyframes animation': 1,
'@-o-keyframes animation': 1,
},
uniquenessRatio: 3 / 3,
ratio: 3 / 6
}
}
assert.equal(actual, expected)
})
AtRules('counts ratio correctly when no @keyframes present', () => {
const fixture = `@media (min-width: 0px) {}`
const actual = analyze(fixture)
const expected = {
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
prefixed: {
total: 0,
totalUnique: 0,
unique: {},
uniquenessRatio: 0,
ratio: 0,
},
}
assert.equal(actual.atrules.keyframes, expected)
})
AtRules('analyzes container queries', () => {
// Fixture contains examples from the spec.
// https://drafts.csswg.org/css-contain-3/
const fixture = `
/* Example 2 */
@container (inline-size > 45em) {
.media-object {
grid-template: 'img content' auto / auto 1fr;
}
}
/* Example 3 */
@container (width > 40em) {
h2 { font-size: 1.5em; }
}
/* Example 4 */
@container (--cards) {
article {
border: thin solid silver;
border-radius: 0.5em;
padding: 1em;
}
}
/* Example 5 */
@container page-layout (block-size > 12em) {
.card { margin-block: 2em; }
}
@container component-library (inline-size > 30em) {
.card { margin-inline: 2em; }
}
/* Example 8 */
@container card (inline-size > 30em) and (--responsive = true) {
/* styles */
}
/* Example 11 */
@container type(inline-size) {
/* only applies when an inline-size container is available */
h2 { font-size: calc(1.2em + 1cqi); }
}
`
const result = analyze(fixture)
const actual = result.atrules.container
const expected = {
total: 7,
totalUnique: 7,
unique: {
'(inline-size > 45em)': 1,
'(width > 40em)': 1,
'(--cards)': 1,
'page-layout (block-size > 12em)': 1,
'component-library (inline-size > 30em)': 1,
'card (inline-size > 30em) and (--responsive = true)': 1,
'type(inline-size)': 1,
},
uniquenessRatio: 7 / 7
}
assert.equal(actual, expected)
})
AtRules.run()