/
css-declaration-sorter.js
151 lines (128 loc) · 3.69 KB
/
css-declaration-sorter.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
'use strict';
const { test } = require('node:test');
const {
processCSSWithPresetFactory,
} = require('../../../util/integrationTestHelpers.js');
const preset = require('..');
const { processCSS, passthroughCSS } = processCSSWithPresetFactory(preset);
test(
'keep order of short- and longhand properties, left to right',
passthroughCSS('a{animation-name:a;animation:b}')
);
test(
'keep order of short- and longhand properties, right to left',
passthroughCSS('a{animation:b;animation-name:a}')
);
test(
'keep order of short- and longhand properties, in at-rule',
passthroughCSS('@supports(display:grid){a{animation:a;animation-name:b}}')
);
test(
'keep order of unknown properties, left to right',
passthroughCSS('a{z-unknown:0;a-unknown:0}')
);
test(
'keep order of unknown properties, right to left',
passthroughCSS('a{a-unknown:0;z-unknown:0}')
);
test(
'keep order of vendor prefixed properties',
passthroughCSS('a{animation-name:a;-webkit-animation:b}')
);
test(
'keep order of border properties, short- and longhand, left to right',
passthroughCSS('a{border-color:purple;border-top:1px solid}')
);
test(
'keep all property at the top',
processCSS(
'a{all:unset;display:flex;align-items:center}',
'a{all:unset;align-items:center;display:flex}'
)
);
test(
'move all property to the top',
processCSS(
'a{display:flex;all:unset;align-items:center}',
'a{all:unset;align-items:center;display:flex}'
)
);
test(
'keep order of CSS variables',
processCSS(
`:root{--a-var:'value';--c-var:calc(10px + 20px);--b-var: 12px;}`,
':root{--a-var:"value";--c-var:30px;--b-var:12px}'
)
);
test(
'sort values between short- and longhand properties',
processCSS(
'a{animation-name:a;z-index:0;animation:b;color:0}',
'a{animation-name:a;animation:b;color:0;z-index:0}'
)
);
test(
'do not sort values between unknown properties',
passthroughCSS('a{z-unknown:0;z-index:0;a-unknown:0;color:0}')
);
test(
'sort mixed short- and longhand properties',
processCSS(
'a{animation-name:a;flex-wrap:0;animation:b;flex-flow:1}',
'a{animation-name:a;animation:b;flex-wrap:0;flex-flow:1}'
)
);
test(
'sort mixed properties in combination with short- and longhand properties',
processCSS(
'a{z-index:0;animation-name:a;flex-wrap:0;animation:b;flex-flow:1}',
'a{animation-name:a;animation:b;flex-wrap:0;flex-flow:1;z-index:0}'
)
);
test(
'sort properties inside at-rules, @media',
processCSS(
'@media(min-width:0){flex:1;animation:a}',
'@media(min-width:0){animation:a;flex:1}'
)
);
test(
'sort properties inside at-rules, @supports',
processCSS(
'@supports(display:grid){flex:1;animation:a}',
'@supports(display:grid){animation:a;flex:1}'
)
);
test(
'sort properties inside at-rules, @font-face',
processCSS(
'@font-face{font-family:a;src:url()}',
'@font-face{font-family:a;src:url()}'
)
);
test(
'works with postcss-merge-longhand, margin',
processCSS('a{z-index:1;margin-top:0;margin:0}', 'a{margin:0;z-index:1}')
);
test(
'works with postcss-merge-longhand, padding',
processCSS('a{z-index:1;padding-top:0;padding:0}', 'a{padding:0;z-index:1}')
);
test(
'works with postcss-merge-longhand, border',
processCSS('a{z-index:1;border-top:0;border:0}', 'a{border:0;z-index:1}')
);
test(
'works with postcss-merge-longhand, border keep short- and longhand order',
processCSS(
'a{border-color:purple;border-top-width:2px;border-top-style:solid}',
'a{border-top:2px solid;border-color:purple}'
)
);
test(
'works with postcss-merge-longhand, border keep mixed short- and longhand order',
processCSS(
'a{border-color:purple;border:0;border-top:3px solid}',
'a{border:0;border-top:3px solid}'
)
);